From b1216374d8861b990a37117d12c7fca839a07550 Mon Sep 17 00:00:00 2001 From: jpmeijers Date: Tue, 13 Jun 2017 17:58:30 +0200 Subject: [PATCH] Better handling of environment variable strings. --- CONFIGURATION.md | 25 ++++++++++++----- run.py | 70 +++++++++++++++++++++++++++--------------------- 2 files changed, 58 insertions(+), 37 deletions(-) diff --git a/CONFIGURATION.md b/CONFIGURATION.md index 86d738a..6c459c5 100644 --- a/CONFIGURATION.md +++ b/CONFIGURATION.md @@ -2,15 +2,18 @@ ##Required global variables * GW_ID required * GW_KEY required -* GW_CONTACT_EMAIL required - default an empty string - The gateway owner's contact information. + This gateway ID and gateway Key for TTN will be used to fetch the gateway's information form the TTN console. When SERVER_TTN is true, this will also be used to conenct and forward packets to TTN. ##Optional global variables +* GW_CONTACT_EMAIL optional - default an empty string + The gateway owner's contact information. Will be overridden by the value from the TTN console. +* GW_DESCRIPTION optional - default an empty string + A description of this gateway. Will be overridden by the value from the TTN console. * GW_RESET_PIN - default 22 - The physical pin number on the Raspberry Pi to which the concentrator's reset is connected. If you followed the [TTN-ZH instruction](https://github.com/ttn-zh/ic880a-gateway/wiki), or used [Gonzalo Casas' backplane board](https://www.tindie.com/stores/gnz/), this is most likely pin number 22. As pin 22 is the default value, you do not need to define it in this case. + The physical pin number on the Raspberry Pi to which the concentrator's reset is connected. See the [README](README.md) file for a description and a list of common values. * GW_GPS optional - default False - * If True, use the hardware GPS. - * If False, + * If true, use the hardware GPS. + * If false, use either fake gps if a location was configured in the TTN console, otherwise try using fake gps with the reference location as set via environment variables, otherwise don't send coordinates. @@ -22,10 +25,16 @@ The longitude to use for fake gps if the coordinates are not set in the TTN console. * GW_REF_ALTITUDE optional - default 0 The altitude to use for fake gps if the coordinates are not set in the TTN console. +* GW_FWD_CRC_ERR optional - default false + Forward packets with an invalid CRC. +* GW_FWD_CRC_VAL optional - default true. + Forward packets with a valid CRC. +* GW_ANTENNA_GAIN optional - default 0. + Set this to the dBd gain of your antenna. The dBd value is the dBi value minus 2.15dB, ie. dBd = dBi-2.15. This is used to reduce the TX power of the concentrator to stay within the legal limits. ##Server variables All server variables are optional, but when a server is enabled, it is recommended to set all variables to configure it completely. -* SERVER_TTN optional - default True +* SERVER_TTN optional - default true Should the gateway connect to the TTN backend * SERVER_1_ENABLED optional - default false @@ -55,6 +64,10 @@ All server variables are optional, but when a server is enabled, it is recommend * SERVER_3_GWKEY * SERVER_3_DOWNLINK - default false +## Note about boolean values + +Use `true` and `false` as lower case words to enable or disable features via environment variables. Any other format will not be interpreted correctly. + #Logal debugging ``` docker run --device /dev/ttyAMA0:/dev/ttyAMA0 --device /dev/mem:/dev/mem --privileged -e GW_TYPE="imst-ic880a-spi" -e GW_DESCRIPTION="test gateway" -e GW_CONTACT_EMAIL="" -e GW_ID="" -e GW_KEY="" newforwarder diff --git a/run.py b/run.py index 6bb319f..64f4b0e 100755 --- a/run.py +++ b/run.py @@ -76,7 +76,7 @@ print ("*** Fetching config from TTN account server") print ("*******************") # Define default configs -description = "" +description = os.getenv('GW_DESCRIPTION', "") placement = "" latitude = os.getenv('GW_REF_LATITUDE', 0) longitude = os.getenv('GW_REF_LONGITUDE', 0) @@ -164,23 +164,37 @@ gateway_conf = {} gateway_conf['gateway_ID'] = my_eui gateway_conf['contact_email'] = os.getenv('GW_CONTACT_EMAIL', "") gateway_conf['description'] = description -gateway_conf['forward_crc_error'] = True -gateway_conf['forward_crc_valid'] = True +gateway_conf['antenna_gain'] = float(os.getenv('GW_ANTENNA_GAIN', 0)) + +if(os.getenv('GW_FWD_CRC_ERR', False)=="true"): + #default is False + gateway_conf['forward_crc_error'] = True + +if(os.getenv('GW_FWD_CRC_VAL', True)=="false"): + #default is True + gateway_conf['forward_crc_valid'] = False + +# Parse GW_GPS env var. It is a string, we need a boolean. +gw_gps = os.getenv('GW_GPS', False) +if(gw_gps=="true"): + gw_gps = True +else: + gw_gps = False # Use hardware GPS -if(os.getenv('GW_GPS', False)==True): +if(gw_gps): print ("Using real GPS") gateway_conf['gps'] = True gateway_conf['fake_gps'] = False gateway_conf['gps_tty_path'] = os.getenv('GW_GPS_PORT', "/dev/ttyAMA0") # Use fake GPS with coordinates from TTN -elif(os.getenv('GW_GPS', False)==False and latitude!=0 and longitude!=0): +elif(gw_gps==False and latitude!=0 and longitude!=0): print ("Using fake GPS") gateway_conf['gps'] = True gateway_conf['fake_gps'] = True - gateway_conf['ref_latitude'] = latitude - gateway_conf['ref_longitude'] = longitude - gateway_conf['ref_altitude'] = altitude + gateway_conf['ref_latitude'] = float(latitude) + gateway_conf['ref_longitude'] = float(longitude) + gateway_conf['ref_altitude'] = float(altitude) # No GPS coordinates else: print ("Not sending coordinates") @@ -192,7 +206,7 @@ else: gateway_conf['servers'] = [] # Add TTN server -if(os.getenv('SERVER_TTN', True)): +if(os.getenv('SERVER_TTN', True)!="false"): server = {} server['serv_type'] = "ttn" server['server_address'] = router @@ -203,7 +217,7 @@ if(os.getenv('SERVER_TTN', True)): gateway_conf['servers'].append(server) # Add up to 3 additional servers -if(os.getenv('SERVER_1_ENABLED', False)): +if(os.getenv('SERVER_1_ENABLED', "false")=="true"): server = {} if(os.getenv('SERVER_1_TYPE', "semtech")=="ttn"): server['serv_type'] = "ttn" @@ -212,10 +226,7 @@ if(os.getenv('SERVER_1_ENABLED', False)): server['server_address'] = os.environ.get("SERVER_1_ADDRESS") server['serv_port_up'] = int(os.environ.get("SERVER_1_PORTUP")) server['serv_port_down'] = int(os.environ.get("SERVER_1_PORTDOWN")) - if(os.getenv('SERVER_1_ENABLED', "false")=="true"): - server['serv_enabled'] = True - else: - server['serv_enabled'] = False + server['serv_enabled'] = True if(os.getenv('SERVER_1_DOWNLINK', "false")=="true"): server['serv_down_enabled'] = True else: @@ -226,15 +237,12 @@ if(os.getenv('SERVER_2_ENABLED', False)): server = {} if(os.getenv('SERVER_2_TYPE', "semtech")=="ttn"): server['serv_type'] = "ttn" + server['serv_gw_id'] = os.environ.get("SERVER_2_GWID") + server['serv_gw_key'] = os.environ.get("SERVER_2_GWKEY") server['server_address'] = os.environ.get("SERVER_2_ADDRESS") - server['serv_port_up'] = os.environ.get("SERVER_2_PORTUP") - server['serv_port_down'] = os.environ.get("SERVER_2_PORTDOWN") - server['serv_gw_id'] = os.environ.get("SERVER_2_GWID") - server['serv_gw_key'] = os.environ.get("SERVER_2_GWKEY") - if(os.getenv('SERVER_2_ENABLED', "false")=="true"): - server['serv_enabled'] = True - else: - server['serv_enabled'] = False + server['serv_port_up'] = int(os.environ.get("SERVER_2_PORTUP")) + server['serv_port_down'] = int(os.environ.get("SERVER_2_PORTDOWN")) + server['serv_enabled'] = True if(os.getenv('SERVER_2_DOWNLINK', "false")=="true"): server['serv_down_enabled'] = True else: @@ -245,20 +253,16 @@ if(os.getenv('SERVER_3_ENABLED', False)): server = {} if(os.getenv('SERVER_3_TYPE', "semtech")=="ttn"): server['serv_type'] = "ttn" + server['serv_gw_id'] = os.environ.get("SERVER_3_GWID") + server['serv_gw_key'] = os.environ.get("SERVER_3_GWKEY") server['server_address'] = os.environ.get("SERVER_3_ADDRESS") - server['serv_port_up'] = os.environ.get("SERVER_3_PORTUP") - server['serv_port_down'] = os.environ.get("SERVER_3_PORTDOWN") - server['serv_gw_id'] = os.environ.get("SERVER_3_GWID") - server['serv_gw_key'] = os.environ.get("SERVER_3_GWKEY") - if(os.getenv('SERVER_3_ENABLED', "false")=="true"): - server['serv_enabled'] = True - else: - server['serv_enabled'] = False + server['serv_port_up'] = int(os.environ.get("SERVER_3_PORTUP")) + server['serv_port_down'] = int(os.environ.get("SERVER_3_PORTDOWN")) + server['serv_enabled'] = True if(os.getenv('SERVER_3_DOWNLINK', "false")=="true"): server['serv_down_enabled'] = True else: server['serv_down_enabled'] = False - gateway_conf['servers'].append(server) @@ -268,6 +272,10 @@ local_conf = {'gateway_conf': gateway_conf} with open('local_conf.json', 'w') as the_file: the_file.write(json.dumps(local_conf, indent=4)) + +# TODO: Cayenne monitoring script + + # Endless loop to reset and restart packet forwarder while True: # Reset the gateway board - this only works for the Raspberry Pi.