# coding=utf-8 from driver import GPIO import network import ujson import utime as time import modem import _thread from aliyunIoT import Device import ota import kv ICCID=None g_connect_status = False net = None device = None deviceSecret = None deviceName = None import app_uart productKey = "" productSecret = "" device_dyn_resigter_succed = False #是否iot已连接 iot_connected=False # 定义需要升级的模块和版本号 module_name = 'default' default_ver = '2.0.5' # 定义升级包的下载和安装路径,其中url,hash_type和hash 会通过服务端推送被保存下来 info = { 'url': '', 'store_path': '/data/pyamp/app.zip', 'install_path': '/data/pyamp/', 'length': 0, 'hash_type': '', 'hash': '' } # ota 消息推送的接受函数 def on_trigger(data): global info # 保存服务端推送的ota信息 info['url'] = data['url'] info['length'] = data['length'] info['module_name'] = data['module_name'] info['version'] = data['version'] info['hash'] = data['hash'] info['hash_type'] = data['hash_type'] # 开始ota 包下载 dl_data = {} dl_data['url'] = info['url'] dl_data['store_path'] = info['store_path'] ota.download(dl_data) # ota 升级包下载结果回调函数 def on_download(data): global info if data >= 0: print('Ota download succeed') # 开始ota包校验 param = {} param['length'] = info['length'] param['store_path'] = info['store_path'] param['hash_type'] = info['hash_type'] param['hash'] = info['hash'] ota.verify(param) # ota 升级包校验结果回调函数 def on_verify(data): global info print(data) if data >= 0 : print('Ota verify succeed') print('Start Upgrade') # 开始ota升级 param = {} param['length'] = info['length'] param['store_path'] = info['store_path'] param['install_path'] = info['install_path'] ota.upgrade(param) # ota 升级包结果回调函数 def on_upgrade(data): if data >= 0 : print('Ota succeed') #ota升完级后 重启设备 reboot() #当iot设备连接到物联网平台的时候触发'connect' 事件 def on_connect(data): global module_name,default_ver,productKey,deviceName,deviceSecret,on_trigger,on_download,on_verify,on_upgrade,iot_connected print('***** connect lp succeed****') print('物联网链接成功') #联网成功在打印版本信息 _thread.start_new_thread(masgDefault_ver,(3,)) iot_connected=True data_handle = {} data_handle['device_handle'] = device.getDeviceHandle() # 初始化ota服务 ota.init(data_handle) # ota 回调函数注册 ota.on(1,on_trigger) ota.on(2,on_download) ota.on(3,on_verify) ota.on(4,on_upgrade) report_info = { "device_handle": data_handle['device_handle'], "product_key": productKey , "device_name": deviceName , "module_name": module_name , "version": default_ver } # 上报本机ota相关信息,上报版本信息返回以后程序返回,知道后台推送ota升级包,才会调用on_trigger函数 ota.report(report_info) #当连接断开时,触发'disconnect'事件 def on_disconnect(): print('linkkit is disconnected') #当iot云端下发属性设置时,触发'props'事件 def on_props(request): params=request['params'] print('clound req data is {}'.format(params)) data_all=eval(params) if(data_all['key']==1): app_uart.sund_key() # print('clound req data is {}'.format(request)) #当iot云端调用设备service时,触发'service'事件 def on_service(id,request): print('clound req id is {} , req is {}'.format(id,request)) #当设备跟iot平台通信过程中遇到错误时,触发'error'事件 def on_error(err): print('err msg is {} '.format(err)) #网络连接的回调函数 def on_4g_cb(args): global g_connect_status pdp = args[0] netwk_sta = args[1] if netwk_sta == 1: g_connect_status = True else: g_connect_status = False #网络连接 def connect_network(): global net,on_4g_cb,g_connect_status #NetWorkClient该类是一个单例类,实现网络管理相关的功能,包括初始化,联网,状态信息等. net = network.NetWorkClient() g_register_network = False if net._stagecode is not None and net._stagecode == 3 and net._subcode == 1: g_register_network = True else: g_register_network = False if g_register_network: #注册网络连接的回调函数on(self,id,func); 1代表连接,func 回调函数 ;return 0 成功 net.on(1,on_4g_cb) net.connect(None) else: print('网络注册失败') while True: if g_connect_status: print('网络连接成功') break time.sleep_ms(20) #动态注册回调函数 def on_dynreg_cb(data): global deviceSecret,device_dyn_resigter_succed deviceSecret = data device_dyn_resigter_succed = True # 连接物联网平台 def dyn_register_device(productKey,productSecret,deviceName): global on_dynreg_cb,device,deviceSecret,device_dyn_resigter_succed key = '_amp_customer_devicesecret' deviceSecretdict = kv.get(key) print("deviceSecretdict:",deviceSecretdict) if isinstance(deviceSecretdict,str): deviceSecret = deviceSecretdict if deviceSecretdict is None or deviceSecret is None: key_info = { 'productKey': productKey , 'productSecret': productSecret , 'deviceName': deviceName } # 动态注册一个设备,获取设备的deviceSecret #下面的if防止多次注册,当前若是注册过一次了,重启设备再次注册就会卡住, if not device_dyn_resigter_succed: device.register(key_info,on_dynreg_cb) def iotInit(device_name,product_key,product_secret): global g_connect_status,device,deviceSecret,deviceName,productKey,productSecret,device deviceName=device_name productSecret=product_secret productKey=product_key #初始化物联网平台Device类,获取device实例 device = Device() #链接参数 key_info = { 'region' : 'cn-shanghai' , 'productKey': productKey , 'deviceName': deviceName , 'deviceSecret': productSecret , 'keepaliveSec': 60, } #device.ON_CONNECT 是事件,on_connect是事件处理函数/回调函数 device.on(device.ON_CONNECT,on_connect) device.on(device.ON_DISCONNECT,on_disconnect) device.on(device.ON_PROPS,on_props) device.on(device.ON_SERVICE,on_service) device.on(device.ON_ERROR,on_error) print('准备链接iot平台。。。') device.connect(key_info) #透传 def sudDataIot(usertestdata ): if iot_connected: postdata={'param':usertestdata} ret = device.postRaw(postdata) if ret == 0 : print('RAW数据上报成功') else : print('RAW数据上报失败') # 物模型上传 def sudDataPostProps(usertestdata ): if iot_connected: tem_hum_str=ujson.dumps(usertestdata) data={ 'params':tem_hum_str } device.postProps(data) def get_mqtt_connect_flag(): global iot_connected return iot_connected def masgDefault_ver(id): while True: print('当前App版本号:' + default_ver) print('等待Ota升级包.....') print('--------------------------------------------------------------------') time.sleep(5)