def post(self): """Get a GPS location or cellid location. workflow: if gps: try to get a gps location elif cellid: get a latest cellid and get a cellid location """ status = ErrorCode.SUCCESS try: data = DotDict(json_decode(self.request.body)) tid = data.get('tid',None) # check tid whether exist in request and update current_user self.check_tid(tid, finish=True) logging.info("[UWEB] realtime request: %s, uid: %s, tid: %s", data, self.current_user.uid, self.current_user.tid) except Exception as e: status = ErrorCode.ILLEGAL_DATA_FORMAT logging.exception("[UWEB] Realtime failed. Exception: %s", e.args) self.write_ret(status) self.finish() return current_query = DotDict() current_query.timestamp = int(time()) terminal = QueryHelper.get_available_terminal(self.current_user.tid, self.db) if not terminal: status = ErrorCode.LOGIN_AGAIN logging.error("[UWEB] The terminal with tid: %s does not exist, redirect to login.html", self.current_user.tid) self.write_ret(status) self.finish() return current_query.locate_flag = data.locate_flag def _on_finish(realtime): realtime['cellid_status'] = 1 self.set_header(*self.JSON_HEADER) self.write(json_encode(realtime)) self.finish() def __callback(db): self.db = db self.request_realtime(current_query, callback=_on_finish) #NOTE: deprecated. self.keep_waking(self.current_user.sim, self.current_user.tid) self.queue.put((10, __callback))
def __parse(self, packet): head = DotDict() body = [] if packet.startswith('[') and packet.endswith(']'): p_info = packet[1:-1].split(',') keys = ['timestamp', 'command'] if len(p_info) >= len(keys): for i, key in enumerate(keys): head[key] = p_info[i] head.timestamp = int(head.timestamp) if head.timestamp else int(time.time()) body = p_info[len(keys):] else: logging.error("[CLWPARSE] Not a complete packet: %s", packet) else: logging.error("[CLWPARSE] Invalid packet: %s", packet) return head, body
def parse_head(self, packet): head = DotDict() body = [] if packet.startswith('[') and packet.endswith(']'): p_info = packet[1:-1].split(',') keys = ['timestamp', 'agps_sign', 'dev_type', 'softversion', 'dev_id', 'command'] if len(p_info) >= len(keys): for i, key in enumerate(keys): head[key] = p_info[i] head.timestamp = int(head.timestamp) if head.timestamp else int(time.time()) body = p_info[len(keys):] else: logging.error("Not a complete packet: %s", packet) else: logging.error("Invalid packet: %s", packet) return head, body
def __parse(self, packet): """Parse the head part of the packet. :arg packet: str :return head: dict :return body: list """ head = DotDict() body = [] if packet.startswith('[') and packet.endswith(']'): p_info = packet[1:-1].split(',') keys = ['timestamp', 'sessionID', 'dev_type', 'softversion', 'dev_id', 'command'] if len(p_info) >= len(keys): for i, key in enumerate(keys): head[key] = p_info[i] head.timestamp = int(head.timestamp) if head.timestamp else int(time.time()) body = p_info[len(keys):] else: logging.error("[CLWPARSE] Not a complete packet: %s", packet) else: logging.error("[CLWPARSE] Invalid packet: %s", packet) return head, body
def handle_heartbeat(info, address, connection, channel, exchange, gw_binding,db, redis): """ S2 heartbeat packet 0: success, then record new terminal's address 1: invalid SessionID 3: acc_status is changed """ try: head = info.head body = info.body dev_id = head.dev_id args = DotDict(success=GATEWAY.RESPONSE_STATUS.SUCCESS) old_softversion = False # if version < 2.4.0, true; else false. sessionID = QueryHelper.get_terminal_sessionID(dev_id, redis) if sessionID != head.sessionID: args.success = GATEWAY.RESPONSE_STATUS.INVALID_SESSIONID else: hp = HeartbeatParser(body, head) heartbeat_info = hp.ret is_sleep = False if heartbeat_info['sleep_status'] == '0': heartbeat_info['login'] = GATEWAY.TERMINAL_LOGIN.SLEEP is_sleep = True elif heartbeat_info['sleep_status'] == '1': heartbeat_info['login'] = GATEWAY.TERMINAL_LOGIN.ONLINE is_sleep = False elif heartbeat_info['sleep_status'] == '2': # query mode acc_status_info_key = get_acc_status_info_key(dev_id) acc_status_info = redis.getvalue(acc_status_info_key) if acc_status_info and int(acc_status_info['op_status']) == 0: args.timestamp = acc_status_info['timestamp'] args.op_type = acc_status_info['op_type'] # modify t2_status in acc_status_info acc_status_info['t2_status'] = 1 # T2 query occurs redis.setvalue(acc_status_info_key, acc_status_info, UWEB.ACC_STATUS_EXPIRY) else: # if acc_status_info['op_status'] is 1, or no acc_status_info, set op_type is 2 args.timestamp = '' args.op_type = 2 # wait else: #NOTE: it should never occur logging.error("[GW] Recv wrong sleep status: %s", heartbeat_info) del heartbeat_info['sleep_status'] update_terminal_status(redis, head.dev_id, address, is_sleep) update_terminal_info(db, redis, heartbeat_info) if args['success'] == GATEWAY.RESPONSE_STATUS.SUCCESS: acc_status_info_key = get_acc_status_info_key(dev_id) acc_status_info = redis.getvalue(acc_status_info_key) if acc_status_info and (not acc_status_info['t2_status']): # T2(query) is need args['success'] = 3 # acc_status is changed logging.info("[GW] ACC_status is changed, dev_id: %s, acc_status_info: %s", dev_id, acc_status_info) #NOTE: check the version. # if version is less than 2.4.0(not include 2.4.0), only has success in args softversion = heartbeat_info['softversion'] item = softversion.split(".") if int(item[0]) > 2: pass else: # A.B.C A <= 2 if int(item[1]) < 4: # A.B.C B <= 4 old_softversion = True else: pass if old_softversion: logging.info("[GW] Old softversion(<2.4.0): %s, only success is provided in S2", softversion) args = dict(success=args['success']) hc = HeartbeatRespComposer(args) request = DotDict(packet=hc.buf, address=address, dev_id=dev_id) append_gw_request(request, connection, channel, exchange, gw_binding) except: logging.exception("[GW] Hand heartbeat failed.") GWException().notify()