async def on_message_receive(self, data): try: data_str = data.decode('utf-8') logging.info(f"Received: {data_str}") print(">>>>>> RECV", data_str, "======", sep="\n") tmp = json.loads(data_str, strict=False) for k, v in tmp.items(): self.json_request[k] = v if 'method' in self.json_request: email_producer.insert_into_redis(data_str, EMAIL_REDIS_LIST_KEY) request = self.json_request['method'] if request == 'update_time': await self.on_update_time_request(self.json_request) elif request == 'pull_param': await self.on_pull_param_request(self.json_request) elif request == 'push_data': await self.on_push_data_request(self.json_request) elif request == 'push_data_size': await self.on_push_data_size_request(self.json_request) elif request == 'push_image': await self.on_push_image_request(self.json_request) elif request == 'update_device_info': await self.on_update_device_info_request(self.json_request) # elif request == 'param_updated': # self.handle_param_updated(self.json_request) # elif request == 'close_connection': # self.handle_close_connection(self.json_request) else: self.on_error_request() except Exception as e: logging.info(e) traceback.print_exc() await self.on_error_request() raise e
def on_message_receive(self, data): try: data_str = native_str(data.decode('UTF-8')) logging.info(data_str) print(data_str) tmp = json.loads(data_str) for k, v in tmp.items(): self.json_request[k] = v if self.json_request.__contains__('method'): email_producer.insert_into_redis(data_str, EMAIL_REDIS_LIST_KEY) request = self.json_request['method'] # if request == 'push_data': # self.on_push_data_request(self.json_request) # elif request == 'push_data_size': # print('here in push_data_size') # self.on_push_data_size_request() # elif request == 'pull_param': # self.on_pull_param_request(self.json_request) # elif request == 'push_image': # logging.info('push_image') # self.on_push_image_request(self.json_request) # elif request == 'param_updated': # logging.info('param_updated') # self.close() # elif request == 'close_connection': # logging.info('close_connection') # self.close() if request == 'push_data': self.on_push_data_request(self.json_request) elif request == 'push_location': self.on_push_location_request(self.json_request) elif request == 'push_data_size': print('here in push_data_size') self.on_push_data_size_request() elif request == 'pull_param': logging.info('pull_param') self.on_pull_param_request(self.json_request) elif request == 'push_image': # print('push_image') logging.info('push_image') self.on_push_image_request(self.json_request) elif request == 'update_device_info': self.on_update_device_info_request() elif request == 'update_time': self.on_update_time_request(self.json_request) elif request == 'param_updated': self.handle_param_updated(self.json_request) elif request == 'close_connection': self.handle_close_connection(self.json_request) else: self.on_error_request() except Exception as e: logging.info(e) print(e) self.on_error_request() raise e
def get_data_to_save(request, ts, data): try: tmp_data = {} tmp_data['type'] = 'data' tmp_data['device_id'] = request['device_id'] tmp_data['device_config_id'] = request['device_config_id'] tmp_data['data'] = data # add type of data tmp_data['type'] = 'data' tmp_data['ts'] = get_datetime_str_from_ts(ts) return tmp_data except Exception as e: logging.info(e) email_producer.insert_into_redis(e, macro.EMAIL_REDIS_LIST_KEY) return None
def get_image_info_to_save(request): try: if request: tmp_data = {} tmp_data['type'] = 'image' tmp_data['device_id'] = request['device_id'] tmp_data['device_config_id'] = request['device_config_id'] tmp_data['data'] = request['image_info'] #add the type of data tmp_data['type'] = 'image' tmp_data['ts'] = get_datetime_str_from_ts(request['ts']) # tmp_data['ts'] = get_datetime_str_from_ts(request['acquisition_time']) return tmp_data else: return None except Exception as e: logging.info(e) email_producer.insert_into_redis(e, macro.EMAIL_REDIS_LIST_KEY) return None
def get_data_to_save(request, ts, data): try: for item in data: if not isinstance(data[item], dict) or 'value' not in data[item]: data[item] = dict(value=data[item]) tmp_data = { 'type': 'data', 'device_id': request['device_id'], 'device_config_id': request['device_config_id'], 'data': data, 'ts': get_datetime_str_from_ts(ts) } # add type of data return tmp_data except Exception as e: logging.info(e) email_producer.insert_into_redis(e, macro.EMAIL_REDIS_LIST_KEY) return None
def get_reply_json(request=None, is_failed=False): try: # print('line 1') reply = {} if is_failed: reply = {'method': 'failed', 'ts': get_current_ts()} else: method = request['method'] reply = {'method': '', 'ts': get_current_ts()} if method == 'push_data': reply['method'] = 'data_uploaded' reply['device_id'] = request['device_id'] if method == 'push_image': reply['method'] = 'push_image_ready' reply['device_id'] = request['device_id'] if method == 'pushing_image': reply['method'] = 'image_uploaded' if method == 'push_data_size': print('here in get_reply_json') reply['method'] = 'push_data_ready' reply['device_id'] = request['device_id'] if method == 'update_device_info': reply['method'] = 'update_device_info' reply['device_id'] = request['device_id'] if method == 'update_time': reply = {'ts': get_current_ts()} print(reply) reply_str = json.dumps(reply) + b'\x03' email_producer.insert_into_redis(reply_str, macro.EMAIL_REDIS_LIST_KEY) return reply_str except Exception as e: logging.info(e) print(e) return json.dumps({ 'method': 'failed', 'ts': get_current_ts() }) + b'\x03'
def get_reply_json(request=None, is_failed=False, msg=None): try: reply = {} if is_failed: reply = {'method': 'failed', 'ts': get_current_ts()} if msg is not None: reply = dict(reply, **dict(msg=msg)) else: method = request['method'] reply = {'method': '', 'ts': get_current_ts()} if method == 'push_data': # Send data reply['method'] = 'data_uploaded' reply['device_id'] = request['device_id'] if method == 'push_image': # Upload image reply['method'] = 'push_image_ready' reply['device_id'] = request['device_id'] if method == 'pushing_image': # Uploading image reply['method'] = 'image_uploaded' if method == 'push_data_size': # reply['method'] = 'push_data_ready' reply['device_id'] = request['device_id'] if method == 'update_device_info': reply['method'] = 'update_device_info' reply['device_id'] = request['device_id'] if method == 'update_time': reply = {'ts': get_current_ts()} reply_str = json.dumps(reply) email_producer.insert_into_redis(reply_str, macro.EMAIL_REDIS_LIST_KEY) return reply_str + '\x03' except Exception as e: traceback.print_exc() logging.info(e) return json.dumps({ 'method': 'failed', 'ts': get_current_ts() }) + '\x03'