Better handling of environment variable strings.

This commit is contained in:
jpmeijers 2017-06-13 17:58:30 +02:00
parent d1168a50f9
commit b1216374d8
2 changed files with 58 additions and 37 deletions

View File

@ -2,15 +2,18 @@
##Required global variables ##Required global variables
* GW_ID required * GW_ID required
* GW_KEY required * GW_KEY required
* GW_CONTACT_EMAIL required - default an empty string 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.
The gateway owner's contact information.
##Optional global variables ##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 * 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 * GW_GPS optional - default False
* If True, use the hardware GPS. * If true, use the hardware GPS.
* If False, * If false,
use either fake gps if a location was configured in the TTN console, 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 try using fake gps with the reference location as set via environment variables,
otherwise don't send coordinates. 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. The longitude to use for fake gps if the coordinates are not set in the TTN console.
* GW_REF_ALTITUDE optional - default 0 * GW_REF_ALTITUDE optional - default 0
The altitude to use for fake gps if the coordinates are not set in the TTN console. 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 ##Server variables
All server variables are optional, but when a server is enabled, it is recommended to set all variables to configure it completely. 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 Should the gateway connect to the TTN backend
* SERVER_1_ENABLED optional - default false * 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_GWKEY
* SERVER_3_DOWNLINK - default false * 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 #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 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

60
run.py
View File

@ -76,7 +76,7 @@ print ("*** Fetching config from TTN account server")
print ("*******************") print ("*******************")
# Define default configs # Define default configs
description = "" description = os.getenv('GW_DESCRIPTION', "")
placement = "" placement = ""
latitude = os.getenv('GW_REF_LATITUDE', 0) latitude = os.getenv('GW_REF_LATITUDE', 0)
longitude = os.getenv('GW_REF_LONGITUDE', 0) longitude = os.getenv('GW_REF_LONGITUDE', 0)
@ -164,23 +164,37 @@ gateway_conf = {}
gateway_conf['gateway_ID'] = my_eui gateway_conf['gateway_ID'] = my_eui
gateway_conf['contact_email'] = os.getenv('GW_CONTACT_EMAIL', "") gateway_conf['contact_email'] = os.getenv('GW_CONTACT_EMAIL', "")
gateway_conf['description'] = description gateway_conf['description'] = description
gateway_conf['forward_crc_error'] = True gateway_conf['antenna_gain'] = float(os.getenv('GW_ANTENNA_GAIN', 0))
gateway_conf['forward_crc_valid'] = True
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 # Use hardware GPS
if(os.getenv('GW_GPS', False)==True): if(gw_gps):
print ("Using real GPS") print ("Using real GPS")
gateway_conf['gps'] = True gateway_conf['gps'] = True
gateway_conf['fake_gps'] = False gateway_conf['fake_gps'] = False
gateway_conf['gps_tty_path'] = os.getenv('GW_GPS_PORT', "/dev/ttyAMA0") gateway_conf['gps_tty_path'] = os.getenv('GW_GPS_PORT', "/dev/ttyAMA0")
# Use fake GPS with coordinates from TTN # 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") print ("Using fake GPS")
gateway_conf['gps'] = True gateway_conf['gps'] = True
gateway_conf['fake_gps'] = True gateway_conf['fake_gps'] = True
gateway_conf['ref_latitude'] = latitude gateway_conf['ref_latitude'] = float(latitude)
gateway_conf['ref_longitude'] = longitude gateway_conf['ref_longitude'] = float(longitude)
gateway_conf['ref_altitude'] = altitude gateway_conf['ref_altitude'] = float(altitude)
# No GPS coordinates # No GPS coordinates
else: else:
print ("Not sending coordinates") print ("Not sending coordinates")
@ -192,7 +206,7 @@ else:
gateway_conf['servers'] = [] gateway_conf['servers'] = []
# Add TTN server # Add TTN server
if(os.getenv('SERVER_TTN', True)): if(os.getenv('SERVER_TTN', True)!="false"):
server = {} server = {}
server['serv_type'] = "ttn" server['serv_type'] = "ttn"
server['server_address'] = router server['server_address'] = router
@ -203,7 +217,7 @@ if(os.getenv('SERVER_TTN', True)):
gateway_conf['servers'].append(server) gateway_conf['servers'].append(server)
# Add up to 3 additional servers # Add up to 3 additional servers
if(os.getenv('SERVER_1_ENABLED', False)): if(os.getenv('SERVER_1_ENABLED', "false")=="true"):
server = {} server = {}
if(os.getenv('SERVER_1_TYPE', "semtech")=="ttn"): if(os.getenv('SERVER_1_TYPE', "semtech")=="ttn"):
server['serv_type'] = "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['server_address'] = os.environ.get("SERVER_1_ADDRESS")
server['serv_port_up'] = int(os.environ.get("SERVER_1_PORTUP")) server['serv_port_up'] = int(os.environ.get("SERVER_1_PORTUP"))
server['serv_port_down'] = int(os.environ.get("SERVER_1_PORTDOWN")) server['serv_port_down'] = int(os.environ.get("SERVER_1_PORTDOWN"))
if(os.getenv('SERVER_1_ENABLED', "false")=="true"):
server['serv_enabled'] = True server['serv_enabled'] = True
else:
server['serv_enabled'] = False
if(os.getenv('SERVER_1_DOWNLINK', "false")=="true"): if(os.getenv('SERVER_1_DOWNLINK', "false")=="true"):
server['serv_down_enabled'] = True server['serv_down_enabled'] = True
else: else:
@ -226,15 +237,12 @@ if(os.getenv('SERVER_2_ENABLED', False)):
server = {} server = {}
if(os.getenv('SERVER_2_TYPE', "semtech")=="ttn"): if(os.getenv('SERVER_2_TYPE', "semtech")=="ttn"):
server['serv_type'] = "ttn" server['serv_type'] = "ttn"
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_id'] = os.environ.get("SERVER_2_GWID")
server['serv_gw_key'] = os.environ.get("SERVER_2_GWKEY") server['serv_gw_key'] = os.environ.get("SERVER_2_GWKEY")
if(os.getenv('SERVER_2_ENABLED', "false")=="true"): server['server_address'] = os.environ.get("SERVER_2_ADDRESS")
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 server['serv_enabled'] = True
else:
server['serv_enabled'] = False
if(os.getenv('SERVER_2_DOWNLINK', "false")=="true"): if(os.getenv('SERVER_2_DOWNLINK', "false")=="true"):
server['serv_down_enabled'] = True server['serv_down_enabled'] = True
else: else:
@ -245,20 +253,16 @@ if(os.getenv('SERVER_3_ENABLED', False)):
server = {} server = {}
if(os.getenv('SERVER_3_TYPE', "semtech")=="ttn"): if(os.getenv('SERVER_3_TYPE', "semtech")=="ttn"):
server['serv_type'] = "ttn" server['serv_type'] = "ttn"
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_id'] = os.environ.get("SERVER_3_GWID")
server['serv_gw_key'] = os.environ.get("SERVER_3_GWKEY") server['serv_gw_key'] = os.environ.get("SERVER_3_GWKEY")
if(os.getenv('SERVER_3_ENABLED', "false")=="true"): server['server_address'] = os.environ.get("SERVER_3_ADDRESS")
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 server['serv_enabled'] = True
else:
server['serv_enabled'] = False
if(os.getenv('SERVER_3_DOWNLINK', "false")=="true"): if(os.getenv('SERVER_3_DOWNLINK', "false")=="true"):
server['serv_down_enabled'] = True server['serv_down_enabled'] = True
else: else:
server['serv_down_enabled'] = False server['serv_down_enabled'] = False
gateway_conf['servers'].append(server) gateway_conf['servers'].append(server)
@ -268,6 +272,10 @@ local_conf = {'gateway_conf': gateway_conf}
with open('local_conf.json', 'w') as the_file: with open('local_conf.json', 'w') as the_file:
the_file.write(json.dumps(local_conf, indent=4)) the_file.write(json.dumps(local_conf, indent=4))
# TODO: Cayenne monitoring script
# Endless loop to reset and restart packet forwarder # Endless loop to reset and restart packet forwarder
while True: while True:
# Reset the gateway board - this only works for the Raspberry Pi. # Reset the gateway board - this only works for the Raspberry Pi.