forked from PiSupply/iot-lora-gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiot-lora-configure
executable file
·219 lines (194 loc) · 8.62 KB
/
iot-lora-configure
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/usr/bin/python3
# IOT LoRa Gateway Configuration Script,
# Setup Dialog Box
from dialog import Dialog
import shutil
import codecs
import json
import os
import sys
import pprint
d = Dialog(dialog="dialog")
d.set_background_title("IOT LoRa Gateway Configurator")
if os.geteuid() != 0:
d.msgbox("This installer must be run as root, please sudo to "
"root and try again.")
os.system("clear")
sys.exit(1)
d.msgbox("This installer script will ask you questions to setup your gateway."
"\r\n\r\nYou need to have already signed up for an account with "
"The Things Network and created a gateway to fill out this script.", height=10, width=80)
# Open the configuration as old data so we can populate if the user has already
# entered data
configLocation = '/boot/iotloragateway/local_conf.json'
# Check if there's a configuration file already, if not copy it to the correct directory ready for the configuration script.
try:
with open(configLocation, 'r') as f:
olddata = json.load(f)
except FileNotFoundError:
shutil.copy2("/boot/iotloragateway/global_confs/local_conf.json.template","/boot/iotloragateway/local_conf.json")
try:
with open(configLocation, 'r') as f:
olddata = json.load(f)
except:
os.system("clear")
print("Config File Error")
exit()
# Get the name
if olddata["gateway_conf"]["servers"][0]["serv_gw_id"] != "<gateway name from console>":
init = olddata["gateway_conf"]["servers"][0]["serv_gw_id"]
else:
init = ""
gatewayIdDetails = d.inputbox("Please enter the Gateway ID that you setup "
"with The Things Network", init=init, width=40)
if gatewayIdDetails[0] == "cancel":
os.system("clear")
sys.exit(1)
# Get the description
if olddata["gateway_conf"]["description"] != "CHANGE-ME":
init = olddata["gateway_conf"]["description"]
else:
init = ""
descriptionDetails = d.inputbox("Please enter a short description of the gateway.",
init=init, width=40)
if descriptionDetails[0] == "cancel":
os.system("clear")
sys.exit(1)
# Get the users email address
if olddata["gateway_conf"]["contact_email"] != "CHANGE-ME":
init = olddata["gateway_conf"]["contact_email"]
else:
init = ""
emailDetails = d.inputbox("Please enter a contact email address",
init=init, width=40)
if emailDetails[0] == "cancel":
os.system("clear")
sys.exit(1)
# Get the frequency
frequencyDetails = d.menu("Please select the frequency of your gateway:",
choices=[("Europe", "868MHz"),
("Asia 1", "920-923MHz"),
("Asia 2", "923-925MHz"),
("Australia", "915MHz"),
("China", "470-510MHz"),
("India", "865-867MHz"),
("Korea", "920-923MHz"),
("United States", "915Mhz")
])
if frequencyDetails[0] == "cancel":
os.system("clear")
sys.exit(1)
# Get the Server
serverDetails = d.menu("Please select the bridge for your gateway",
choices=[("Europe", "ttn-router-eu"),
("United Kingdom",
"digitalcatapult-uk-router"),
("Australia", "meshed-router"),
("Switzerland", "switch-router"),
("SE Asia", "ttn-router-asia-se"),
("Brazil", "ttn-router-brazil"),
("Japan", "ttn-router-jp"),
("US West", "ttn-router-us-west")
])
if serverDetails[0] == "cancel":
os.system("clear")
sys.exit(1)
# Get the users location
if (olddata["gateway_conf"]["ref_latitude"] == 0 and
olddata["gateway_conf"]["ref_longitude"] == 0 and
olddata["gateway_conf"]["ref_altitude"] == 0):
elements = ("", "", "")
else:
elements = (olddata["gateway_conf"]["ref_latitude"],
olddata["gateway_conf"]["ref_longitude"],
olddata["gateway_conf"]["ref_altitude"])
locationDetails = d.form("Location", [("Latitude", 1, 1, elements[0], 1, 15, 13, 13),
("Longitude", 3, 1, elements[1], 3, 15, 13, 13),
("Altitude", 5, 1, elements[2], 5, 15, 5, 5)])
if locationDetails[0] == "cancel":
os.system("clear")
sys.exit(1)
# The Things Network gateway key
response = "ok"
init = ""
if olddata["gateway_conf"]["servers"][0]["serv_gw_key"] != "<gateway secret key from console>":
response = d.yesno("Do you wish to update your TTN Key?", height=6, width=40)
init = olddata["gateway_conf"]["servers"][0]["serv_gw_key"]
if response == "ok":
keyDetails = d.inputbox("Enter TTN gateway key:", init=init, height=10, width=80)
if keyDetails[0] == "cancel":
os.system("clear")
sys.exit(1)
else:
keyDetails = ("ok", olddata["gateway_conf"]["servers"][0]["serv_gw_key"])
d.msgbox("The configuration script will now configure the packet forwarder, "
"enable SPI and confirm SSH is enabled for remote management.",
height=10, width=80)
# Now lets set the arrays with each of the pins and servers and such
freqList = [
["Asia 1", "/boot/iotloragateway/global_confs/AS1-global_conf.json"],
["Asia 2", "/boot/iotloragateway/global_confs/AS1-global_conf.json"],
["Australia", "/boot/iotloragateway/global_confs/AU-global_conf.json"],
["China", "/boot/iotloragateway/global_confs/CN-global_conf.json"],
["Europe", "/boot/iotloragateway/global_confs/EU-global_conf.json"],
["India", "/boot/iotloragateway/global_confs/IN-global_conf.json"],
["Korea", "/boot/iotloragateway/global_confs/KR-global_conf.json"],
["United States", "/boot/iotloragateway/global_confs/US-global_conf.json"]
]
serverList = [
["United Kingdom", "ttn.thingsconnected.net"],
["Australia", "thethings.meshed.com.au"],
["Switzerland", "ttn.opennetworkinfrastructure.org"],
["SE Asia", "router.as.thethings.network"],
["Brazil", "router.brazil.thethings.network"],
["Europe", "router.eu.thethings.network"],
["Japan", "router.thethingsnetwork.jp"],
["US West", "router.us.thethings.network"]
]
freqDict = dict(freqList)
serverDict = dict(serverList)
# Now ready for setup!
# Build the local_conf.json file
shutil.copyfile("/boot/iotloragateway/global_confs/local_conf.json.template",
"/boot/iotloragateway/local_conf.json")
# Generate a gateway UID
gatewayGenId = str.encode("IOT"+gatewayIdDetails[1][-5:])
gatewayGenId = codecs.encode(gatewayGenId, "hex")
configLocation = '/boot/iotloragateway/local_conf.json'
with open(configLocation, 'r') as f:
data = json.load(f)
server_address = serverDict[serverDetails[1]]
# Lets set the easy stuff first
data["gateway_conf"]["servers"][0]["serv_gw_id"] = gatewayIdDetails[1]
data["gateway_conf"]["contact_email"] = emailDetails[1]
data["gateway_conf"]["description"] = descriptionDetails[1]
data["gateway_conf"]["ref_latitude"] = locationDetails[1][0]
data["gateway_conf"]["ref_longitude"] = locationDetails[1][1]
data["gateway_conf"]["ref_altitude"] = locationDetails[1][2]
data["gateway_conf"]["servers"][0]["serv_gw_key"] = keyDetails[1]
data["gateway_conf"]["gateway_ID"] = gatewayGenId.decode()
data["gateway_conf"]["servers"][0]["server_address"] = server_address
os.remove(configLocation)
with open(configLocation, 'w') as f:
json.dump(data, f, indent=4)
# Copy the frequency plan over
boot_conf_file = "/boot/iotloragateway/global_conf.json"
shutil.copyfile(freqDict[frequencyDetails[1]], boot_conf_file)
# Set execute bit on iot-lora-gateway-reset.sh
st = os.stat("/opt/iotloragateway/iot-lora-gateway-reset.sh")
os.chmod("/opt/iotloragateway/iot-lora-gateway-reset.sh", st.st_mode | 0o111)
restart = d.pause("The system now will reboot to finish configuring your "
"gateway andautomatically start the forwarder."
"if you do not want to restart select cancel.")
if(restart == "ok"):
os.system("sudo shutdown -r now")
else:
print("Installation completed, please re-start to use.")
# Prompt for reboot
restart = d.pause("The system now will reboot in 10 seconds to finish "
"configuring your gateway andautomatically start the "
"forwarder.\r\n\r\n"
"If you do not want to restart select cancel.", seconds=10)
os.system("clear")
if restart == "ok":
os.system("sudo reboot")