-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
381 lines (340 loc) · 10.3 KB
/
utils.py
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import mcu
import sfw
import pwr
import timers
import streams
import json
from fortebit.polaris import polaris
from fortebit.polaris import qspiflash
import accel
modem = None
gnss = None
client = None
check_sms = False
has_qspi = True
def decimal(n, v):
v = float(v)
s = "%%.%df" % n
return s % v
# entry sequence is '+++' within 1 second
start_check = 0
count_check = 0
def check_terminal(s):
global start_check, count_check
while s.available() > 0:
c = s.read()[0]
if c == __ORD('+'):
t = timers.now()
count_check += 1
if count_check == 1:
start_check = t
if t - start_check < 1000:
if count_check == 3:
count_check = 0
return True
else:
start_check = t
count_check = 1
else:
count_check = 0
return False
def do_terminal(s):
print("[Type +++ to exit terminal]")
while True:
print("Input command:")
cmd = input_line(s)
if cmd == '+++':
break
elif cmd == 'erase':
eraseSettings()
print("erase done")
mcu.reset()
elif cmd == 'name':
settings = readSettings()
print("Old name:", settings["name"])
print("New name:")
name = input_line(s)
if name:
print("Saving name:", name)
settings["name"] = name
saveSettings(settings)
mcu.reset()
elif cmd == 'modem':
do_modem_passthru(s)
break
elif cmd == 'gnss':
global gnss
gnss.debug = not gnss.debug
print("gnss debug =", gnss.debug)
break
elif cmd == 'mqtt':
global client
if client is not None:
client.debug = not client.debug
print("mqtt debug =", client.debug)
break
def do_modem_passthru(s):
print("Modem passthrough...[^ to exit]")
modem.bypass(1)
ms = streams.serial(polaris.gsm.SERIAL,baud=115200,set_default=False)
ms.write('ATE1\r')
while True:
sfw.kick()
# modem to stream
n = ms.available()
if n > 0:
s.write(ms.read(n))
# stream to modem
n = s.available()
if n > 0:
b = s.read(n)
if n == 1 and b[0] == __ORD('^'):
break
ms.write(b)
else:
sleep(1)
ms.write('ATE0\r')
modem.bypass(0)
def input_line(s):
res = bytearray()
s.read(s.available())
while True:
if s.available() < 1:
sfw.kick()
sleep(1)
continue
c = s.read()[0]
if c == __ORD('\r') or c == __ORD('\n'):
break
if c == __ORD('\b') and len(res) > 0:
del res[-1]
print('\b ', sep='', end='')
else:
res.append(c)
print(chr(c), sep='', end='')
print()
return str(res)
def validate_apn(apn):
apn = bytes(apn)
for c in apn:
if not ((c >= __ORD('a') and c <= __ORD('z')) or (c >= __ORD('A') and c <= __ORD('Z'))
or (c >= __ORD('0') and c <= __ORD('9')) or c == __ORD('-') or c == __ORD('.')):
return False
return True
def validate_email(email):
email = bytes(email)
dom = -1
for c in email:
if dom >= 0:
if not ((c >= __ORD('a') and c <= __ORD('z')) or (c >= __ORD('A') and c <= __ORD('Z'))
or (c >= __ORD('0') and c <= __ORD('9')) or c == __ORD('-') or c == __ORD('.')):
return False
dom += 1
if c == __ORD('.'):
dom = 255
else:
if c >= 128:
return False
if c == __ORD('@'):
dom = 0
return len(email) > 2 and dom >= 256
def saveSettings(settings):
if has_qspi:
my_flash = qspiflash.QSpiFlash()
my_flash.erase_block(0)
my_flash[0] = json.dumps(settings) + "\n"
def readSettings():
if has_qspi:
my_flash = qspiflash.QSpiFlash()
data = my_flash.read_data(0, 128)
if not data.find("\n") < 0:
return json.loads(data[0:data.find("\n")])
else:
return {}
else:
return {"apn": "tm", "email": "[email protected]"}
def eraseSettings():
my_flash = qspiflash.QSpiFlash()
my_flash.erase_block(0)
def read_and_parse_sms():
mcu_reset = False
sms = modem.list_sms(False,10,0)
if sms and len(sms) > 0:
for msg in sms:
print("Index:", msg[3])
print("Text:", msg[0])
print("From:", msg[1])
print("Time:", msg[2])
modem.delete_sms(msg[3])
text = msg[0].split(",")
ret = ""
for line in text:
line = line.split("=")
line[0] = line[0].lower().strip()
if line[0] == "erase":
print('erase flash')
eraseSettings()
ret += "erase done"
mcu_reset = True
if line[0] == "apn" and len(line) > 1:
line[1] = line[1].replace(" ", "")
apn = line[1]
print("APN", apn)
settings = readSettings()
settings["apn"] = apn
saveSettings(settings)
ret += "apn " + apn + " saved,"
mcu_reset = True
if line[0] == "email" and len(line) > 1:
line[1] = line[1].replace(" ", "")
email = line[1]
print("email", email)
settings = readSettings()
settings["email"] = email
saveSettings(settings)
ret += "email " + email + " saved,"
mcu_reset = True
if line[0] == "name" and len(line) > 1:
name = line[1].strip()
print("name", name)
settings = readSettings()
settings["name"] = name
saveSettings(settings)
ret += "name changed " + name + ","
mcu_reset = True
if len(ret) > 0:
if ret[-1] == ",":
ret = ret[0:-1]
modem.send_sms(msg[1], ret)
return mcu_reset
charging = False
def update_charger():
global charging
if polaris.isBatteryBackup():
charging = False
elif charging:
if accel.get_temperature() < 4 or accel.get_temperature() > 45:
print("Exceeding battery temperature range!")
charging = False
if not charging:
print("Stop battery charging...")
else:
if accel.get_temperature() >= 6 and accel.get_temperature() <= 43:
print("Good battery temperature range!")
charging = True
if charging:
print("Start battery charging...")
polaris.setBatteryCharger(charging)
def is_powersupply_toolow():
if polaris.isBatteryBackup():
volt = polaris.readBattVoltage()
if volt < 3.45:
print("Backup battery:", volt)
return True
else:
volt = polaris.readMainVoltage()
if volt < 11.2:
print("Main battery:", volt)
return True
return False
def is_powersupply_enough():
if polaris.isBatteryBackup():
volt = polaris.readBattVoltage()
if volt >= 3.6:
return True
else:
volt = polaris.readMainVoltage()
if volt >= 11.5:
return True
return False
def start():
# only proceed if power supply input level is enough
print("Checking power supply level...")
while not is_powersupply_enough():
polaris.ledRedOff()
# pwr.go_to_sleep(450, pwr.PWR_STOP)
sleep(450)
polaris.ledRedOn()
sleep(50)
print("Starting utility thread...")
thread(run)
def run():
sms_timer = 0
while True:
mcu_reset = False
sleep(1000)
sms_timer += 1
# parse SMS commands
try:
if check_sms and modem and (sms_timer >= 15 or modem.pending_sms() > 0):
sms_timer = 0
mcu_reset = read_and_parse_sms()
except Exception as e:
print("utils SMS failure", e)
# control battery charger and input level
if is_powersupply_toolow():
print("Power supply too low!")
mcu_reset = True
else:
update_charger()
# handle reset
if mcu_reset:
if modem:
modem.shutdown()
print("utils MCU reset")
mcu.reset()
def request_apn(s):
apn = None
while apn is None:
print("Enter APN:")
apn = input_line(s)
if not validate_apn(apn):
print("Invalid APN, try again!")
apn = None
if apn is not None and len(apn) == 0:
print("Empty APN, confirm [y/n]")
if input_line(s) == 'y':
break
else:
apn = None
return apn
def request_email(s):
email = None
while email is None:
print("Enter email:")
email = input_line(s)
if not validate_email(email):
print("Invalid email, try again!")
email = None
return email
led_state = False
led_count = 0
def status_led(sending, ignition, connected):
global led_state, led_count
if sending:
led_state = False
led_count = 0
if ignition:
polaris.ledRedOff()
polaris.ledGreenOff()
else:
polaris.ledRedOn()
polaris.ledGreenOn()
else:
if connected:
if ignition:
polaris.ledRedOff()
polaris.ledGreenOn()
else:
polaris.ledRedOff()
polaris.ledGreenOff()
else:
led_count += 1
if led_count > 5:
led_state = not led_state
led_count = 0
if led_state:
polaris.ledRedOn()
else:
polaris.ledRedOff()
polaris.ledGreenOff()