-
Notifications
You must be signed in to change notification settings - Fork 2
/
datacake_gateway.py
169 lines (143 loc) · 5.64 KB
/
datacake_gateway.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
import pycom
from network import LTE
import config
import time
from time import sleep
import machine
from machine import Timer
from machine import RTC
import sys
import socket
class DatacakeGateway:
def machine_callback(self, arg):
evt = machine.events()
if (evt & machine.PYGATE_START_EVT):
self.machine_state = config.GATEWAY_STATE_OK
pycom.rgbled(config.RGB_GATEWAY_OK)
elif (evt & machine.PYGATE_ERROR_EVT):
self.machine_state = config.GATEWAY_STATE_ERROR
pycom.rgbled(config.RGB_GATEWAY_ERROR)
elif (evt & machine.PYGATE_STOP_EVT):
self.machine_state = config.GATEWAY_STATE_STOP
pycom.rgbled(config.RGB_GATEWAY_STOP)
def __init__(self):
print("Init: Initialization of Gateway class...")
# Machine
machine.callback(trigger = (
machine.PYGATE_START_EVT |
machine.PYGATE_STOP_EVT |
machine.PYGATE_ERROR_EVT
), handler=self.machine_callback)
self.machine_state = 0
# LTE
self.lte = LTE()
self.lte_connection_state = 0
# RTC
self.rtc = RTC()
# Gateway
# Read the GW config file from Filesystem
self.gateway_config_file = None
# Timers
self.rgb_breathe_timer = Timer.Chrono()
# Startup
# Should be called outside init
# self.start_up()
def lte_event_callback(self, arg):
#self.blink_rgb_led(5, 0.25, config.RGB_LTE_ERROR)
#self.lte.deinit()
#machine.reset()
print("\n\n\n#############################################################")
print("CB LTE Callback Handler")
ev = arg.events() # NB: reading the events clears them
t = time.ticks_ms()
print("CB", t, time.time(), ev, time.gmtime())
self.blink_rgb_led(3, 0.25, config.RGB_LTE_ERROR)
if ev & LTE.EVENT_COVERAGE_LOSS:
print("CB", t, "coverage loss")
if ev & LTE.EVENT_BREAK:
print("CB", t, "uart break signal")
try:
self.lte.pppsuspend()
if not self.lte.isattached():
print("not attached ... reattach")
self.lte.detach()
self.init_lte()
else:
print("attached ... resume")
self.lte.pppresume()
except Exception as ex:
sys.print_exception(ex)
print("#############################################################\n\n\n")
def init_gateway(self):
print("Init GW: Starting LoRaWAN Concentrator...")
try:
self.gateway_config_file = open(config.GW_CONFIG_FILE_PATH,'r').read()
except Exception as e:
print("Error opening Gateway Config: {}".format(e))
# TODO: Handle Error
return False
else:
machine.pygate_init(self.gateway_config_file)
print("Init GW: LoRaWAN Concentrator UP!")
return True
def init_rtc(self):
print("Init RTC: Syncing RTC...")
try:
self.rtc.ntp_sync(server="pool.ntp.org")
while not self.rtc.synced():
self.blink_rgb_led(1, 0.25, config.RGB_RTC_IS_SYNCING, delay_end=False)
self.blink_rgb_led(3, 0.1, config.RGB_RTC_IS_SYNCING)
except Exception as e:
print("Exception syncing RTC: {}".format(e))
return False
else:
print("Init RTC: Synced!")
return True
def init_lte(self):
self.lte_connection_state = 0
self.lte.init()
#self.lte.lte_callback(LTE.EVENT_COVERAGE_LOSS, self.lte_event_callback)
self.lte.lte_callback(LTE.EVENT_BREAK, self.lte_event_callback)
while True:
# attach LTE
if self.lte_connection_state == 0:
print("Init LTE: Attaching LTE...")
self.lte.attach(band=config.LTE_BAND, apn=config.LTE_APN)
while not self.lte.isattached():
self.blink_rgb_led(1, 0.25, config.RGB_LTE_IS_ATTACHING, delay_end=False)
self.blink_rgb_led(3, 0.1, config.RGB_LTE_IS_ATTACHING)
self.lte_connection_state += 1
print("Init LTE: Attached!")
# connect LTE
if self.lte_connection_state == 1:
print("Init LTE: Connecting LTE...")
self.lte.connect()
while not self.lte.isconnected():
self.blink_rgb_led(1, 0.25, config.RGB_LTE_IS_CONNECTING, delay_end=False)
self.blink_rgb_led(3, 0.1, config.RGB_LTE_IS_CONNECTING)
self.lte_connection_state += 1
print("Init LTE: Connected!")
# done
if self.lte_connection_state == 2:
return True
def blink_rgb_led(self, times, speed, color_on, color_off=config.RGB_OFF, delay_end=True):
for index in range(times):
pycom.rgbled(config.RGB_OFF)
time.sleep(speed)
pycom.rgbled(color_on)
time.sleep(speed)
pycom.rgbled(config.RGB_OFF)
if delay_end is True:
time.sleep(0.1)
def start_up(self):
print("Start Up: Now starting up Gateway...")
self.init_lte()
self.init_rtc()
self.init_gateway()
#self.main_loop()
def main_loop(self):
# Start Timers
self.rgb_breathe_timer.start()
while True:
if self.rgb_breathe_timer.read() > config.TIMER_RGB_BREATHE_INTERVAL:
self.rgb_breathe_timer.reset()