def post(self, cmd): try: request = self.request.body.decode('utf-8') request = json.loads(request) body = request["d"] if cmd == 'group': pass elif cmd == 'white_list': ret = gw.create_whitelist(body['url'], body['md5']) if ret: LOG.info("white list create success") resp_status = "ok" resp_data = {"result": "ok"} elif cmd == 'check_code': gw.set_auth_key(body['check_code']) resp_status = "ok" resp_data = {"result": "ok"} except EpdException as e: LOG.error(e.__repr__()) resp_status = "err", resp_data = {"msg": e.message} finally: upload = uplink.Upload() resp = { "id": request['id'], "from": request['from'], "status": resp_status, "command": request['command'], "d": resp_data } upload.send(resp, topic="dma/cmd/resp")
def report_task_status(self): data = { 'topic': 'gateway/report/task/status', "payload": { "d": { 'task_id': int(self.__taskId), 'status': int(self.__taskStatus), 'success_list': [], 'failed_list': list(self.pending_list) } } } upload = uplink.Upload() upload.send('http://127.0.0.1:7788/mqtt/publish/offlinecache', data)
def timer_handler(self, args=None): ''' 定时任务 ''' data = { "d": { 'task_id': self.__taskId, 'whitelist_md5': self.__whiteListMD5, 'check_code': int(self.key) } } upload = uplink.Upload() upload.send(data, topic='gateway/report/status') timer = threading.Timer(handler_interval, self.timer_handler) timer.start()
def timer_handler(self, args=None): ''' 定时任务 ''' data = { 'topic': 'gateway/report/status', "payload": { "d": { 'task_id': self.__taskId, 'whitelist_md5': self.__whiteListMD5, 'check_code': int(self.key), 'interval': self.interval } } } upload = uplink.Upload() upload.send('http://127.0.0.1:7788/mqtt/publish/offlinecache', data) timer = threading.Timer(handler_interval, self.timer_handler) timer.start()
def post(self, cmd): try: request = self.request.body.decode('utf-8') request = json.loads(request) body = request["d"] if cmd == 'group': raise Exception("%s cmd not found" % cmd) elif cmd == 'white_list': ret = gw.create_whitelist(body['url'], body['md5']) if ret: LOG.info("white list create success") resp_status = "ok" resp_data = {"result": "ok"} else: raise Exception("white list create failed") elif cmd == 'check_code': gw.set_auth_key(body['check_code']) resp_status = "ok" resp_data = {"result": "ok"} elif cmd == 'interval': gw.set_interval_time(body['interval']) resp_status = "ok" resp_data = {"result": "ok"} else: raise Exception("%s cmd not found" % cmd) except Exception as e: LOG.error(e.__repr__()) resp_status = "err", resp_data = {"msg": e.__str__()} finally: upload = uplink.Upload() url = r'http://127.0.0.1:7788/mqtt/publish/offlinecache' resp = { "id": request.get('id'), "from": request.get('from'), "status": resp_status, "command": request.get('command'), "d": resp_data } data = {'topic': "dma/cmd/resp", 'payload': resp} upload.send(url, data)
def query_task(self, url, data): up = uplink.Upload() ret = up.send(url, data) return ret
def post(self, cmd): try: request = self.request.body.decode('utf-8') request = json.loads(request) body = request['d'] if cmd == 'create': pass # ret = gw.create_task(body['task_id'], body['image_data_id'], body['image_data_url'], \ # body['image_data_md5'], body['iot_dev_list_md5'], body['iot_dev_list_url'], \ # body['start_time'], body['end_time']) # if ret: # data = { # "cmd":"task", # "method":"create", # "task_id":body['task_id'], # "data_id":body['image_data_id'], # "start_time":body['start_time'], # "end_time":body['end_time'] # } # ret = dl.send_service('serial', data, need_resp=True) # if ret['status'] != 'ok': # gw.set_try_data('serial', data) # # raise HTTPError(200) # status = 'ok' # msg = "task_id %s create success" % body['task_id'] # else: # # 不可创建任务 # LOG.info("task could not create") # status = 'failed' # msg = "task_id %s create not allowed" % body['task_id'] # # 上传命令处理结果 # upload = uplink.Upload() # resp = { # "id": request['id'], # "from": request['from'], # "status": status, # "command": request['command'], # "d":{ # "code":"task_status", # "msg":msg # } # } # # gw.get_task_status() # upload.send(resp, topic='dma/cmd/resp') elif cmd == 'cancel': # cancel task data = { "cmd": "task", "method": "cancel", "task_id": body['task_id'] } ret = dl.send_service('serial', data, need_resp=True) if ret['status'] != 'ok': # 向模块发出取消命令 gw.set_try_data('serial', data) ret = gw.cancel_task(body['task_id']) if ret: status = 'ok' result = {'result': 'ok'} try: os.unlink("/tmp/success") os.unlink("/tmp/fail") except: pass else: status = 'err' result = {'code': 'cancel_failed', 'msg': 'task not found'} # 上传处理结果 upload = uplink.Upload() resp = { "id": request['id'], "from": request['from'], "status": status, "command": request['command'], "msg": result } upload_data = {'topic': 'dma/cmd/resp', 'payload': resp} upload.send('http://127.0.0.1:7788/mqtt/publish/offlinecache', upload_data) elif cmd == 'confirm': pass # confirm task # data = { # "table_name":"sql", # "sql_cmd":"update `task` set (`start_time`='%s', `end_time`='%s', `status`=2) where `task_id`='%s';" % \ # (body['start_time'], body['end_time'], body['task_id']) # } # dl.send_service('database', data) else: raise HTTPError(404) except Exception as e: self.write(e.__str__()) LOG.error("%s" % e.__str__())
def upload(self, topic, data): up = uplink.Upload() url = r'http://127.0.0.1:7788/mqtt/publish' payload = {'topic': topic, 'payload': data} up.send(url, payload)
def upload(self, topic, data): up = uplink.Upload() up.send(topic=topic, payload=data)
def post(self, cmd): try: request = self.request.body.decode('utf-8') request = json.loads(request) body = request['d'] if cmd == 'create': ret = gw.create_task(body['task_id'], body['image_data_id'], body['image_data_url'], \ body['image_data_md5'], body['iot_dev_list_md5'], body['iot_dev_list_url'], \ body['start_time'], body['end_time']) if ret: data = { "cmd": "task", "method": "create", "task_id": body['task_id'], "data_id": body['image_data_id'], "start_time": body['start_time'], "end_time": body['end_time'] } ret = dl.send_service('serial', data, need_resp=True) if ret['status'] != 'ok': gw.set_try_data('serial', data) # raise HTTPError(200) status = 'ok' msg = "task_id %s create success" % body['task_id'] else: # 不可创建任务 LOG.info("task could not create") status = 'failed' msg = "task_id %s create not allowed" % body['task_id'] # 上传命令处理结果 upload = uplink.Upload() resp = { "id": request['id'], "from": request['from'], "status": status, "command": request['command'], "d": { "code": "task_status", "msg": msg } } # gw.get_task_status() upload.send(resp, topic='dma/cmd/resp') elif cmd == 'cancel': # cancel task data = { "cmd": "task", "method": "cancel", "task_id": body['task_id'] } ret = dl.send_service('serial', data, need_resp=True) if ret['status'] != 'ok': # 向模块发出取消命令 gw.set_try_data('serial', data) ret = gw.cancel_task(body['task_id']) if ret: status = 'ok' result = {'result': 'ok'} else: status = 'err' result = {'code': 'cancel_failed', 'msg': 'task not found'} # 上传处理结果 upload = uplink.Upload() resp = { "id": request['id'], "from": request['from'], "status": status, "command": request['command'], "d": result } upload.send(resp, topic='dma/cmd/resp') elif cmd == 'confirm': # confirm task data = { "table_name":"sql", "sql_cmd":"update `task` set (`start_time`='%s', `end_time`='%s', `status`=2) where `task_id`='%s';" % \ (body['start_time'], body['end_time'], body['task_id']) } dl.send_service('database', data) else: raise HTTPError(404) except Exception as e: self.write(e.__str__()) LOG.error("%s" % e.__str__())