Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3d00091

Browse files
committed
Lorawan OTA: Minor config changes to server code
1 parent 1347ec3 commit 3d00091

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

examples/OTA-lorawan/config.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616
LORASERVER_USER = 'admin'
1717
LORASERVER_PASS = 'admin'
1818

19-
LORASERVER_SERVICE_PROFILE = 'multicast_sp'
19+
LORASERVER_SERVICE_PROFILE = 'ota_sp'
2020
LORASERVER_DOWNLINK_DR = 5
2121
LORASERVER_DOWNLINK_FREQ = 869525000
22+
LORASERVER_APP_ID = 1 # Read from Web Interface / Applications
2223

2324
#update configuration
2425
UPDATE_DELAY = 300

examples/OTA-lorawan/ota.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ class OTAHandler:
2424
MSG_HEADER = b'$OTA'
2525
MSG_TAIL = b'*'
2626
MSG_END = b'<!EOF>'
27+
28+
FULL_UPDATE = b'F'
29+
DIFF_UPDATE = b'D'
30+
NO_UPDATE = b'N'
2731

2832
UPDATE_INFO_MSG = 1
2933
UPDATE_INFO_REPLY = 2
@@ -182,7 +186,7 @@ def _send_update_info(self, dev_eui, msg):
182186
version = self.get_latest_version()
183187
if LooseVersion(version) > LooseVersion(dev_version):
184188
self._init_update_params(dev_eui, dev_version, version)
185-
msg = self._create_update_info_msg(version)
189+
msg = self._create_update_info_msg(version, dev_version)
186190
self.send_payload(dev_eui, msg)
187191

188192
def get_device_version(self, msg):
@@ -241,23 +245,35 @@ def update_proc(self):
241245
upater = updateHandler(dev_version, latest_version, self._clientApp, self._loraserver_jwt, multicast_group_id, self)
242246

243247
self.multicast_updaters.append(upater)
248+
249+
def _get_update_type(self, need_updating, device_version):
250+
update_type = b',' + self.NO_UPDATE
251+
print(os.path.isdir(self.firmware_dir + '/' + device_version))
252+
if need_updating:
253+
if os.path.isdir(self.firmware_dir + '/' + device_version):
254+
return b',' + self.DIFF_UPDATE
255+
else:
256+
return b',' + self.FULL_UPDATE
257+
258+
return update_type
244259

245-
def _create_update_info_msg(self, version):
260+
def _create_update_info_msg(self, version, device_version):
246261
msg = bytearray()
247262
msg.extend(self.MSG_HEADER)
248263
msg.extend(b',' + str(self.UPDATE_INFO_REPLY).encode())
249-
msg.extend(b',' + str(int(self._next_update > 0)).encode())
250264
msg.extend(b',' + version.encode())
251-
msg.extend(b',' + str(int(time.time())).encode())
252-
if self._next_update > 0:
265+
need_updating = self._next_update > 0
266+
update_type = self._get_update_type(need_updating, device_version)
267+
msg.extend(update_type)
268+
if need_updating:
253269
msg.extend(b',' + str(int(self._next_update)).encode())
254270
else:
255271
msg.extend(b',-1')
272+
msg.extend(b',' + str(int(time.time())).encode())
256273
msg.extend(b',' + self.MSG_TAIL)
257-
258274
return msg
259275

260276
def send_payload(self, dev_eui, data):
261277
b64Data = base64.b64encode(data)
262278
payload = '{"reference": "abcd1234" ,"fPort":1,"data": "' + b64Data.decode() + '"}'
263-
self.p_client.publish(topic="application/1/device/" + dev_eui + "/tx",payload=payload)
279+
self.p_client.publish(topic="application/" + str(config.LORASERVER_APP_ID) + "/device/" + dev_eui + "/tx",payload=payload)

examples/OTA-lorawan/updaterService.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import signal
1414
import time
1515
import config
16+
import sys
1617

1718
exit = False
1819
client = None
@@ -48,3 +49,4 @@ def on_publish(mosq, obj, mid):
4849
pass
4950

5051
ota.stop()
52+
sys.exit(0)

0 commit comments

Comments
 (0)