def handle_sleep(info, address, connection, channel, exchange, gw_binding, db, redis): """ S21 sleep status packet: 0-sleep, 1-LQ 0: success, then record new terminal's address 1: invalid SessionID """ try: head = info.head body = info.body dev_id = head.dev_id resend_key, resend_flag = get_resend_flag(redis, dev_id, head.timestamp, head.command) args = DotDict(success=GATEWAY.RESPONSE_STATUS.SUCCESS, command=head.command) sessionID = QueryHelper.get_terminal_sessionID(dev_id, redis) is_sleep = False if sessionID != head.sessionID: args.success = GATEWAY.RESPONSE_STATUS.INVALID_SESSIONID else: if resend_flag: logging.warn("[GW] Recv resend packet, head: %s, body: %s and drop it!", info.head, info.body) else: redis.setvalue(resend_key, True, GATEWAY.RESEND_EXPIRY) hp = AsyncParser(body, head) sleep_info = hp.ret if sleep_info['sleep_status'] == '0': sleep_info['login'] = GATEWAY.TERMINAL_LOGIN.SLEEP #self.send_lq_sms(head.dev_id) #logging.info("[GW] Recv sleep packet, LQ it: %s", head.dev_id) is_sleep = True elif sleep_info['sleep_status'] == '1': sleep_info['login'] = GATEWAY.TERMINAL_LOGIN.ONLINE else: logging.info("[GW] Recv wrong sleep status: %s", sleep_info) del sleep_info['sleep_status'] update_terminal_info(db, redis, sleep_info) update_terminal_status(redis, dev_id, address, is_sleep) 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) hc = AsyncRespComposer(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] Handle sleep status report exception.") GWException().notify()
def handle_acc_status(info, address, connection, channel, exchange, gw_binding, db, redis): """ S30 ACC_status: 0: success, then record new terminal's address 1: invalid SessionID """ try: head = info.head body = info.body dev_id = head.dev_id args = DotDict(success=GATEWAY.RESPONSE_STATUS.SUCCESS, command=head.command) sessionID = QueryHelper.get_terminal_sessionID(dev_id, redis) if sessionID != head.sessionID: args.success = GATEWAY.RESPONSE_STATUS.INVALID_SESSIONID logging.error("[GW] Invalid sessionID, terminal: %s", head.dev_id) else: uap = ACCStatusParser(body, head) t_info = uap.ret acc_status_info_key = get_acc_status_info_key(dev_id) acc_status_info = redis.getvalue(acc_status_info_key) if acc_status_info: acc_status_info['op_status'] = 1 # success redis.setvalue(acc_status_info_key, acc_status_info, UWEB.ACC_STATUS_EXPIRY) WSPushHelper.pushS8(dev_id, 1, db, redis) else: # It should never occur. logging.error("[GW] ACC_status can not be found. dev_id: %s", dev_id) pass asc = ACCStatusComposer(args) request = DotDict(packet=asc.buf, address=address, dev_id=dev_id) append_gw_request(request, connection, channel, exchange, gw_binding) except: logging.exception("[GW] Handle acc status exception.") GWException().notify()
def handle_misc(info, address, connection, channel, exchange, gw_binding, db, redis): """ S28 misc: debugging for terminal. 0: success, then record new terminal's address 1: invalid SessionID """ try: head = info.head body = info.body dev_id = head.dev_id args = DotDict(success=GATEWAY.RESPONSE_STATUS.SUCCESS, command=head.command) sessionID = QueryHelper.get_terminal_sessionID(dev_id, redis) if sessionID != head.sessionID: args.success = GATEWAY.RESPONSE_STATUS.INVALID_SESSIONID logging.error("[GW] Invalid sessionID, terminal: %s", head.dev_id) else: uap = MiscParser(body, head) t_info = uap.ret db.execute("UPDATE T_TERMINAL_INFO" " SET misc = %s" " WHERE tid = %s ", t_info['misc'], head.dev_id) 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) uac = MiscComposer(args) request = DotDict(packet=uac.buf, address=address, dev_id=dev_id) append_gw_request(request, connection, channel, exchange, gw_binding) except: logging.exception("[GW] Handle misc report exception.") GWException().notify()
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()
def post(self): status = ErrorCode.SUCCESS try: data = DotDict(json_decode(self.request.body)) tids = data.get("tids") op_type = data.get("op_type") logging.info( "[UWEB] ACC request: %s, uid: %s, tid: %s, tids: %s", data, self.current_user.uid, self.current_user.tid, tids, ) except Exception as e: status = ErrorCode.ILLEGAL_DATA_FORMAT self.write_ret(status) return try: res = [] for tid in tids: r = DotDict(tid=tid, status=ErrorCode.SUCCESS, message=ErrorCode.ERROR_MESSAGE[status]) try: t = QueryHelper.get_terminal_by_tid(tid, self.db) if str(t["dev_type"]) != "D": r["status"] = ErrorCode.ACC_NOT_ALLOWED logging.info( "[UWEB] Acc is not allowed. uid: %s, tid: %s, dev_type: %s", self.current_user.uid, tid, t["dev_type"], ) else: acc_status_info_key = get_acc_status_info_key(tid) acc_status_info = self.redis.getvalue(acc_status_info_key) if acc_status_info: r["status"] = ErrorCode.ACC_TOO_FREQUENCY logging.info("[UWEB] Acc is too frequency. uid: %s, tid: %s", self.current_user.uid, tid) else: acc_status_info = dict( client_id=self.client_id, op_type=op_type, timestamp=int(time.time()), op_status=0, # failed t2_status=0, # wait for T2 acc_message=u"", ) self.redis.setvalue(acc_status_info_key, acc_status_info, UWEB.ACC_STATUS_EXPIRY) except Exception as e: r["status"] = ErrorCode.FAILED logging.info( "[UWEB] Set acc status failed, uid:%s, tid:%s, op_type:%s.", self.current_user.uid, tid, op_type ) finally: r["message"] = ErrorCode.ERROR_MESSAGE[r["status"]] res.append(r) self.write_ret(status, dict_=DotDict(res=res)) except Exception as e: logging.exception("[UWEB] Set acc status failed, uid:%s, Exception: %s.", self.current_user.uid, e.args) status = ErrorCode.SERVER_BUSY self.write_ret(status)
def handle_config(info, address, connection, channel, exchange, gw_binding, db, redis): """ S17 Config packet 0: success, then record new terminal's address 1: invalid SessionID """ try: head = info.head body = info.body dev_id = head.dev_id args = DotDict(success=GATEWAY.RESPONSE_STATUS.SUCCESS, domain="", freq="", trace="", static_val="", move_val="", trace_para="", vibl="", use_scene="", stop_interval="", test="", gps_enhanced="", tracking_interval="") sessionID = QueryHelper.get_terminal_sessionID(head.dev_id, redis) if sessionID != head.sessionID: args.success = GATEWAY.RESPONSE_STATUS.INVALID_SESSIONID else: update_terminal_status(redis, head.dev_id, address) #TODO: terminal = db.get("SELECT track, freq, trace, mannual_status," " trace_para, vibl, domain," " use_scene, stop_interval, test," " gps_enhanced, tracking_interval" " FROM T_TERMINAL_INFO" " WHERE tid = %s", head.dev_id) args.domain = terminal.domain args.freq = terminal.freq args.trace = terminal.trace args.use_scene = terminal.use_scene args.stop_interval = terminal.stop_interval args.test = terminal.test args.gps_enhanced = terminal.gps_enhanced args.tracking_interval = terminal.tracking_interval if terminal.track == 1: # turn on track args.trace_para = "60:1" else: args.trace_para = terminal.trace_para args.vibl = terminal.vibl #NOTE: get move_val and static_val according to mannual_status if int(terminal.mannual_status) != UWEB.DEFEND_STATUS.YES: # 撤防,智能设防 move_val = 0 static_val = 180 else: # 强力设防 move_val = 60 static_val = 0 args.move_val = move_val args.static_val = static_val #NOTE: check the version. # if version is after 2.4, add tracking-interval in S17 softversion = head['softversion'] item = softversion.split(".") old_softversion = False if int(item[0]) < 2: # 1.x.x old_softversion = True elif int(item[0]) == 2: # 2.x.x if int(item[1]) < 4: # 2.3.x old_softversion = True else: # 2.4.x old_softversion = False else: # 3.x old_softversion = False if old_softversion: del args['tracking_interval'] 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) hc = ConfigRespComposer(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 query config exception.") GWException().notify()
def foward_packet_to_si(self, info, packet, address, connection, channel, db): """ Response packet or position/report/charge packet 0: success, then forward it to SIServer and record new terminal's address 1: invalid SessionID """ try: head = info.head args = DotDict(success=GATEWAY.RESPONSE_STATUS.SUCCESS, command=head.command) dev_id = head.dev_id resend_key, resend_flag = get_resend_flag(self.redis, dev_id, head.timestamp, head.command) sessionID = QueryHelper.get_terminal_sessionID(dev_id, self.redis) if sessionID != head.sessionID: args.success = GATEWAY.RESPONSE_STATUS.INVALID_SESSIONID logging.error("[GW] Invalid sessionID, Terminal: %s", dev_id) else: seq = str(int(time.time() * 1000))[-4:] uargs = DotDict(seq=seq, dev_id=dev_id, content=packet) content = UploadDataComposer(uargs).buf logging.info("[GW] Forward message to SI:\n%s", content) if resend_flag: logging.warn("[GW] Recv resend packet: %s, and drop it!", packet) else: append_si_request(content, connection, channel, self.exchange, self.si_binding) update_terminal_status(self.redis, dev_id, address) #NOTE: Handle the packet. if head.command in (GATEWAY.T_MESSAGE_TYPE.POSITION, GATEWAY.T_MESSAGE_TYPE.MULTIPVT, GATEWAY.T_MESSAGE_TYPE.CHARGE, GATEWAY.T_MESSAGE_TYPE.ILLEGALMOVE, GATEWAY.T_MESSAGE_TYPE.POWERLOW, GATEWAY.T_MESSAGE_TYPE.ILLEGALSHAKE, GATEWAY.T_MESSAGE_TYPE.EMERGENCY, GATEWAY.T_MESSAGE_TYPE.POWERDOWN, GATEWAY.T_MESSAGE_TYPE.STOP): logging.info("[GW] Head command: %s.", head.command) if args['success'] == GATEWAY.RESPONSE_STATUS.SUCCESS: acc_status_info_key = get_acc_status_info_key(dev_id) acc_status_info = self.redis.getvalue(acc_status_info_key) if acc_status_info and (not acc_status_info['t2_status']): # T2(query) is need logging.info("[GW] ACC_status is changed, dev_id: %s, acc_status_info: %s", dev_id, acc_status_info) args['success'] = 3 # acc_status is changed #NOTE: composer response for terminal rc = AsyncRespComposer(args) request = DotDict(packet=rc.buf, address=address, dev_id=dev_id) append_gw_request(request, connection, channel, self.exchange, self.gw_binding) # resend flag if not resend_flag: self.redis.setvalue(resend_key, True, GATEWAY.RESEND_EXPIRY) elif head.command == GATEWAY.T_MESSAGE_TYPE.UNBIND: # S24-->T24 logging.info("[GW] Head command: %s.", head.command) up = UNBindParser(info.body, info.head) status = up.ret['status'] if status == GATEWAY.STATUS.SUCCESS: delete_terminal_new(dev_id, db, self.redis) else: logging.exception("[GW] Invalid command: %s.", head.command) except: logging.exception("[GW] Handle SI message exception.") GWException().notify()
def handle_defend(info, address, connection, channel, exchange, gw_binding, db, redis): """ S18 defend status report packet 0: success, then record new terminal's address 1: invalid SessionID """ try: head = info.head body = info.body dev_id = head.dev_id resend_key, resend_flag = get_resend_flag(redis, dev_id, head.timestamp, head.command) # old version is compatible if len(body) == 1: body.append('0') logging.info("[GW] old version is compatible, append mannual status 0") args = DotDict(success=GATEWAY.RESPONSE_STATUS.SUCCESS, command=head.command) sessionID = QueryHelper.get_terminal_sessionID(dev_id, redis) if sessionID != head.sessionID: args.success = GATEWAY.RESPONSE_STATUS.INVALID_SESSIONID else: if resend_flag: logging.warn("[GW] Recv resend packet, head: %s, body: %s and drop it!", info.head, info.body) else: redis.setvalue(resend_key, True, GATEWAY.RESEND_EXPIRY) hp = AsyncParser(body, head) defend_info = hp.ret defend_info['mannual_status'] = defend_info['defend_status'] if defend_info['defend_source'] != 0: # come from sms or web if defend_info['defend_source'] == "1": _status = u"设防" if defend_info['defend_status'] == "1" else u"撤防" tname = QueryHelper.get_alias_by_tid(head.dev_id, redis, db) sms = SMSCode.SMS_DEFEND_SUCCESS % (tname, _status) user = QueryHelper.get_user_by_tid(head.dev_id, db) if user: SMSHelper.send(user.owner_mobile, sms) del defend_info['defend_status'] del defend_info['defend_source'] update_mannual_status(db, redis, head.dev_id, defend_info['mannual_status']) update_terminal_status(redis, head.dev_id, address) 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) hc = AsyncRespComposer(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] Handle defend status report exception.") GWException().notify()
def handle_runtime(info, address, connection, channel, exchange, gw_binding, db, redis): """ S23 runtime status packet: {login [0:unlogin | 1:login], defend_status [0:undefend | 1:defend], gps:gsm:pbat [0-100:0-9:0-100]} 0: success, then record new terminal's address 1: invalid SessionID """ try: head = info.head body = info.body dev_id = head.dev_id resend_key, resend_flag = get_resend_flag(redis, dev_id, head.timestamp, head.command) if len(body) == 3: body.append('-1') body.append('0') logging.info("[GW] old version is compatible, append fob_pbat, is_send") if len(body) == 4: body.append('0') logging.info("[GW] old version is compatible, append is_send") args = DotDict(success=GATEWAY.RESPONSE_STATUS.SUCCESS, command=head.command, mannual_status='') sessionID = QueryHelper.get_terminal_sessionID(dev_id, redis) if sessionID != head.sessionID: args.success = GATEWAY.RESPONSE_STATUS.INVALID_SESSIONID else: if resend_flag: logging.warn("[GW] Recv resend packet, head: %s, body: %s and drop it!", info.head, info.body) terminal_info = QueryHelper.get_terminal_info(head.dev_id, db, redis) args.mannual_status = terminal_info['mannual_status'] else: redis.setvalue(resend_key, True, GATEWAY.RESEND_EXPIRY) hp = AsyncParser(body, head) runtime_info = hp.ret update_terminal_status(redis, head.dev_id, address) terminal_info = update_terminal_info(db, redis, runtime_info) args.mannual_status = terminal_info['mannual_status'] db.execute("INSERT INTO T_RUNTIME_STATUS" " VALUES(NULL, %s, %s, %s, %s, %s, %s, %s, %s)", head.dev_id, runtime_info['login'], runtime_info['defend_status'], runtime_info['gps'], runtime_info['gsm'], runtime_info['pbat'], runtime_info['fob_pbat'], head.timestamp) is_send = int(runtime_info['is_send']) if is_send: terminal_info = QueryHelper.get_terminal_info(head.dev_id, db, redis) alias = QueryHelper.get_alias_by_tid(head.dev_id, redis, db) communication_staus = u'正常' communication_mode = u'撤防' gsm_strength = u'强' gps_strength = u'强' if int(terminal_info['login']) == GATEWAY.TERMINAL_LOGIN.ONLINE: communication_staus = u'正常' else: communication_staus = u'异常' if int(terminal_info['mannual_status']) == UWEB.DEFEND_STATUS.YES: communication_mode = u'强力设防' elif int(terminal_info['mannual_status']) == UWEB.DEFEND_STATUS.SMART: communication_mode = u'智能设防' else: communication_mode= u'撤防' pbat = int(terminal_info.get('pbat', 0)) gsm = int(terminal_info.get('gsm', 0)) if gsm < 3: gsm_strength = u'弱' elif gsm < 6: gsm_strength = u'较弱' gps = int(terminal_info.get('gps', 0)) if gps < 10: gps_strength = u'弱' elif gps < 20: gps_strength = u'较弱' elif gps < 30: gps_strength = u'较强' runtime_sms = SMSCode.SMS_RUNTIME_STATUS % (alias, communication_staus, communication_mode, int(pbat), gsm_strength, gps_strength) SMSHelper.send(terminal_info.owner_mobile, runtime_sms) logging.info("[GW] Send runtime_status sms to user: %s, tid: %s", terminal_info.owner_mobile, head.dev_id) update_terminal_status(redis, head.dev_id, address) 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) rc = RuntimeRespComposer(args) request = DotDict(packet=rc.buf, address=address, dev_id=dev_id) append_gw_request(request, connection, channel, exchange, gw_binding) except: logging.exception("[GW] Handle runtime status report exception.") GWException().notify()