提交 3e77a774 authored 作者: blu's avatar blu

ota updater

上级 9caa15a5
...@@ -117,22 +117,23 @@ class OTAClient: ...@@ -117,22 +117,23 @@ class OTAClient:
if not sn: if not sn:
logger.error("exiting since no sn config could be retrived") logger.error("exiting since no sn config could be retrived")
exit(1) exit(1)
self.client = mqtt.Client(sn, userdata=self) # , protocol=mqtt.MQTTv5)
self.client = mqtt.Client('EVB-'+sn, userdata=self) # , protocol=mqtt.MQTTv5)
if self.username and self.password: if self.username and self.password:
self.client.username_pw_set(username=self.username, password=self.password) self.client.username_pw_set(username=self.username, password=self.password)
self.client.on_connect = self.on_connect self.client.on_connect = self.on_connect
self.client.on_message = self.on_message self.client.on_message = self.on_message
th = threading.Thread()
self.client.connect_async(self.host, self.port, 30) self.client.connect_async(self.host, self.port, 30)
#self.client.loop_start() #self.client.loop_start()
self.client.loop_forever() self.client.loop_forever()
def download(self, url): def download(self, url):
logger.info("downloading package {}".format(url))
os.system('mkdir -p ' + DOWNLOAD_PATH) os.system('mkdir -p ' + DOWNLOAD_PATH)
os.system('mkdir -p '+ EXTRACT_PATH) os.system('mkdir -p '+ EXTRACT_PATH)
msg = '' msg = ''
try: try:
with requests.get(url, stream=True) as r: with requests.get(url, stream=True, timeout=5) as r:
r.raise_for_status() r.raise_for_status()
logger.info("download {} -> {}".format(url, DOWNLOAD_PATH)) logger.info("download {} -> {}".format(url, DOWNLOAD_PATH))
with open(DOWNLOAD_PATH, 'wb') as f: with open(DOWNLOAD_PATH, 'wb') as f:
...@@ -166,9 +167,13 @@ class OTAClient: ...@@ -166,9 +167,13 @@ class OTAClient:
rep['message'] = message rep['message'] = message
rep['firmware'] = version rep['firmware'] = version
rep['status'] = status rep['status'] = status
try: try:
self.client.publish(REPLY_TOPIC, json.dumps(rep), 1) logger.info("reply with status {}".format(status))
mc = mqtt.Client('EVBRep-'+self.sn, userdata=self) # , protocol=mqtt.MQTTv5)
if self.username and self.password:
mc.username_pw_set(username=self.username, password=self.password)
mc.connect_async(self.host, self.port, 10)
mc.publish(REPLY_TOPIC, json.dumps(rep), 1)
except Exception as e: except Exception as e:
logger.info('exception in publish reply message: %s', e) logger.info('exception in publish reply message: %s', e)
extype, value, tb = sys.exc_info() extype, value, tb = sys.exc_info()
...@@ -182,28 +187,25 @@ class OTAClient: ...@@ -182,28 +187,25 @@ class OTAClient:
url = req.get('url') url = req.get('url')
batchId = req.get('batchId') batchId = req.get('batchId')
valid = True
msg = 'OK' msg = 'OK'
if not sku or sku != SKU: if not sku or sku != SKU:
msg = "invalid sku {} in update request, expected {}. ignored".format(sku, SKU) msg = "invalid sku {} in update request, expected {}. ignored".format(sku, SKU)
logger.warn(msg); logger.warn(msg);
valid = False elif not sn or sn != self.sn:
if not sn or sn != self.sn:
msg = "invalid sn {} in update request, expected {} ignored".format(sn, self.sn) msg = "invalid sn {} in update request, expected {} ignored".format(sn, self.sn)
logger.warn(msg); logger.warn(msg);
valid = False elif not url or url[:7] != 'http://':
if not url or url[:7] != 'http://':
msg = "invalid url {} in update request, ignored".format(url) msg = "invalid url {} in update request, ignored".format(url)
logger.warn(msg); logger.warn(msg);
valid = False elif not batchId or not str(batchId).isdigit():
if not batchId or not str(batchId).isnumeric:
msg = "invalid batchId {} in update request, ignored".format(batchId) msg = "invalid batchId {} in update request, ignored".format(batchId)
logger.warn(msg); logger.warn(msg);
valid = False elif not self.version:
if not self.version:
logger.warn("no local version file, update initialized") logger.warn("no local version file, update initialized")
else:
pass
if not valid: if msg != 'OK':
self.reply(batchId, ver, 'refuse', msg) self.reply(batchId, ver, 'refuse', msg)
return return
# process on # process on
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论