Skip to content

Commit

Permalink
Update libraries, fix connection error and cloud registration handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ppescher committed Dec 16, 2019
1 parent 5964c07 commit 12964a1
Showing 1 changed file with 32 additions and 23 deletions.
55 changes: 32 additions & 23 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
# Created at 2019-07-26 11:34:26.282569

from fortebit.polaris import polaris
from fortebit.polaris import modem
from fortebit.polaris import gnss
import vm
import sfw
vm.set_option(vm.VM_OPT_RESET_ON_EXCEPTION, 1)
Expand All @@ -29,7 +31,7 @@
update_period = 6 * gps_period # other telemetry data period in ms
no_ignition_period = 300000 # no ignition telemetry data period in ms

fw_version = "1.10"
fw_version = "1.11"

# POLARIS INIT
try:
Expand All @@ -54,9 +56,9 @@

try:
print("Initializing Modem...")
modem = polaris.GSM()
modem = modem.init()
print("Initializing GNSS...")
gnss = polaris.GNSS()
gnss = gnss.init()
# verify preconditions and start utility thread
utils.start()

Expand All @@ -66,7 +68,7 @@
print("Starting GNSS...")
gnss.start()
gnss.set_rate(2000)

print("Starting Modem...")
modem.startup()

Expand Down Expand Up @@ -194,7 +196,7 @@
ctx = ssl.create_ssl_context(cacert=cacert, options=ssl.CERT_REQUIRED | ssl.SERVER_AUTH)
# NOTE: if the underlying SSL driver does not support certificate validation
# uncomment the following line!
#ctx = None
# ctx = None

from fortebit.iot import iot
from fortebit.iot import mqtt_client
Expand All @@ -206,16 +208,18 @@
utils.client = device.client

# connect the device
for _ in range(0, 5):
sfw.kick()
try:
if device.connect():
break
except Exception as e:
print("Retrying...", e)
sleep(3000 * (_ + 1))
else:
raise TimeoutError
if not device.connect():
for _ in range(5):
sfw.kick()
try:
sleep(3000 * (_ + 1))
print("Retrying...")
if device.connect():
break
except Exception as e:
print("Failed connect...", e)
else:
raise TimeoutError

print("Device is connected")
polaris.ledGreenOn()
Expand All @@ -228,14 +232,19 @@
# if not registered, register device to Fortebit Cloud
if not cloud.isRegistered(device, email):
sfw.kick()
print("Device is not registered, register device")
while not cloud.register(device, email):
sfw.kick()
sleep(1000)
sfw.kick()
while not cloud.isRegistered(device, email):
sfw.kick()
sleep(1000)
retry = timers.now()
print("Device is not registered, register device...")
while timers.now() - retry < 60000:
if cloud.register(device, email):
sfw.kick()
if cloud.isRegistered(device, email):
break
if not device.is_connected():
raise ConnectionError
sleep(5000)
print("Retry device registration...")
else:
raise TimeoutError

sfw.kick()
print("Device is registered")
Expand Down

0 comments on commit 12964a1

Please sign in to comment.