def handle_charge_info(self, info): info = DotDict(info) self.db.execute("INSERT INTO T_CHARGE" " VALUES (NULL, %s, %s, %s)", info.dev_id, info.content, info.timestamp) user = QueryHelper.get_user_by_tid(info.dev_id, self.db) if not user: logging.error("[EVENTER] Cannot find USER of terminal: %s", info.dev_id) return sms_option = QueryHelper.get_sms_option_by_uid(user.owner_mobile, EVENTER.SMS_CATEGORY.CHARGE.lower(), self.db) if sms_option == UWEB.SMS_OPTION.SEND: logging.error("[EVENTER] do not send charge sms temporarily")
def check_poweroff_timeout(self): logging.info("[CELERY] checkertask check poweroff timeout started.") try: terminals = self.db.query("SELECT tpt.tid, tpt.sms_flag, tpt.timestamp" " FROM T_POWEROFF_TIMEOUT as tpt, T_TERMINAL_INFO as tti" " WHERE tti.tid = tpt.tid" " AND tti.service_status = 1" " AND tti.login = %s" " AND tpt.sms_flag = %s" " AND tpt.timestamp < %s", GATEWAY.TERMINAL_LOGIN.OFFLINE, GATEWAY.POWEROFF_TIMEOUT_SMS.UNSEND, (time.time() - 2*60*60)) for terminal in terminals: terminal_info = QueryHelper.get_terminal_info(terminal.tid, self.db, self.redis) if int(terminal_info['pbat']) < 5: user = QueryHelper.get_user_by_tid(terminal.tid, self.db) sms = SMSCode.SMS_POWEROFF_TIMEOUT % terminal_info['alias'] SMSHelper.send(user.owner_mobile, sms) self.update_sms_flag(terminal.tid) logging.info("[CELERY] Send poweroff timeout sms to user:%s, tid:%s", user.owner_mobile, terminal.tid) except Exception as e: logging.exception("[CELERY] Check terminal poweroff timeout exception.")
def handle_report_info(self, info): """These reports should be handled here: POWERLOW/POWERFULL/POWEROFF/POWERDOWN ILLEGALMOVE ILLEGALSHAKE EMERGENCY REGION_ENTER REGION_OUT STOP SPEED_LIMIT """ if not info: return # 1: get available location from lbmphelper report = lbmphelper.handle_location(info, self.redis, cellid=True, db=self.db) if not (report['cLat'] and report['cLon']): #NOTE: Get latest location last_location = QueryHelper.get_location_info(report.dev_id, self.db, self.redis) if last_location: #NOTE: Try to make the location is complete. locations = [last_location,] locations = lbmphelper.get_locations_with_clatlon(locations, self.db) last_location = locations[0] report['lat'] = last_location['latitude'] report['lon'] = last_location['longitude'] report['cLat'] = last_location['clatitude'] report['cLon'] = last_location['clongitude'] report['name'] = last_location['name'] report['type'] = last_location['type'] logging.info("[EVENTER] The report has invalid location and use last_location. report: %s", report) else: logging.info("[EVENTER] The report has invalid location and last_location is invalid. report: %s", report) current_time = int(time.time()) alarm_mobile = get_alarm_mobile(report.dev_id, self.db, self.redis) #NOTE: in pvt, timestamp is no used, so use gps_time as timestamp if not report.get('timestamp',None): report['timestamp'] = report['gps_time'] if report['timestamp'] > (current_time + 24*60*60): logging.info("[EVENTER] The report's (gps_time - current_time) is more than 24 hours, so drop it:%s", report) return #NOTE: If undefend, just save location into db if info['rName'] in [EVENTER.RNAME.ILLEGALMOVE, EVENTER.RNAME.ILLEGALSHAKE]: if str(info.get('is_notify','')) == '1': # send notify even if CF logging.info("[EVENTER] Send notify forever, go ahead. Terminal: %s, is_notify: %s", report.dev_id, info.get('is_notify','')) elif alarm_mobile: logging.info("[EVENTER] Send notify forever , go ahead. Terminal: %s, alarm_mobile: %s", report.dev_id, alarm_mobile) else: mannual_status = QueryHelper.get_mannual_status_by_tid(info['dev_id'], self.db) if int(mannual_status) == UWEB.DEFEND_STATUS.NO: report['category'] = EVENTER.CATEGORY.REALTIME insert_location(report, self.db, self.redis) update_terminal_dynamic_info(self.db, self.redis, report) logging.info("[EVENTER] %s mannual_status is undefend, drop %s report.", info['dev_id'], info['rName']) return if info['rName'] in [EVENTER.RNAME.POWERDOWN, EVENTER.RNAME.POWERLOW]: # if alert_freq_key is exists,return alert_freq_key = get_alert_freq_key(report.dev_id + info['rName']) alert_freq = QueryHelper.get_alert_freq_by_tid(info['dev_id'], self.db) if alert_freq != 0: if self.redis.exists(alert_freq_key): logging.info("[EVENTER] Don't send duplicate %s alert to terminal:%s in %s seconds", info["rName"], report.dev_id, alert_freq) return else: self.redis.setvalue(alert_freq_key, 1, time=alert_freq) #NOTE: keep alarm info alarm = dict(tid=report['dev_id'], category=report['category'], type=report['type'], timestamp=report.get('timestamp',0), latitude=report.get('lat',0), longitude=report.get('lon',0), clatitude=report.get('cLat',0), clongitude=report.get('cLon',0), name=report['name'] if report.get('name',None) is not None else '', degree=report.get('degree',0), speed=report.get('speed',0)) if info['rName'] in [EVENTER.RNAME.REGION_OUT, EVENTER.RNAME.REGION_ENTER]: region = report['region'] alarm['region_id'] = region.region_id record_alarm_info(self.db, self.redis, alarm) # 2: save into database. T_LOCATION, T_EVENT lid = insert_location(report, self.db, self.redis) update_terminal_dynamic_info(self.db, self.redis, report) self.event_hook(report.category, report.dev_id, report.get('terminal_type',1), report.get('timestamp'), lid, report.pbat, report.get('fobid'), report.get('region_id', -1)) # 3: notify the owner user = QueryHelper.get_user_by_tid(report.dev_id, self.db) if not user: logging.error("[EVENTER] Cannot find USER of terminal: %s", report.dev_id) return # send sms to owner if report.rName in [EVENTER.RNAME.STOP]: logging.info("[EVENTER] %s alert needn't to push to user. Terminal: %s", report.rName, report.dev_id) return #NOTE: notify user by sms sms_option = QueryHelper.get_sms_option_by_uid(user.owner_mobile, EVENTER.SMS_CATEGORY[report.rName].lower(), self.db) if sms_option == UWEB.SMS_OPTION.SEND: logging.info("[EVENTER] Notify report to user by sms. category: %s, tid: %s, mobile: %s", report.rName, report.dev_id, user['owner_mobile']) self.notify_report_by_sms(report, user['owner_mobile']) else: logging.info("[EVENTER] Remind option of %s is closed. Terminal: %s", report.rName, report.dev_id) if alarm_mobile: logging.info("[EVENTER] Notify report to user by sms. category: %s, tid: %s, alarm_mobile: %s", report.rName, report.dev_id, alarm_mobile) self.notify_report_by_sms(report, alarm_mobile) #NOTE: notify user by push terminal = self.db.get("SELECT push_status FROM T_TERMINAL_INFO" " WHERE tid = %s", report.dev_id) if terminal and terminal.push_status == 1: logging.info("[EVENTER] Notify report to user by push. category: %s, tid: %s, mobile: %s", report.rName, report.dev_id, user['owner_mobile']) self.notify_report_by_push(report, user['owner_mobile']) else: logging.info("[EVENTER] Push option of %s is closed. Terminal: %s", report.rName, report.dev_id) #NOTE: notify alarm_mobile if alarm_mobile: logging.info("[EVENTER] Notify report to user by push. category: %s, tid: %s, alarm_mobile: %s", report.rName, report.dev_id, alarm_mobile) self.notify_report_by_push(report, alarm_mobile)
def handle_locationdesc(info, address, connection, channel, exchange, gw_binding, db, redis): """ S10 locationdesc packet 0: success, then return locationdesc to terminal and record new terminal's address 1: invalid SessionID """ try: head = info.head body = info.body dev_id = head.dev_id if len(body) == 6: body.append(20) logging.info("[GW] old version is compatible, append locate_error") resend_key, resend_flag = get_resend_flag(redis, dev_id, head.timestamp, head.command) go_ahead = False args = DotDict(success=GATEWAY.RESPONSE_STATUS.SUCCESS, locationdesc="", ew="E", lon=0.0, ns="N", lat=0.0) 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: if resend_flag: logging.warn("[GW] Recv resend packet, head: %s, body: %s and drop it!", info.head, info.body) else: go_ahead = True #NOTE: Check ydcw or ajt ajt = QueryHelper.get_ajt_whitelist_by_mobile(head.dev_id, db) if ajt: url_out = ConfHelper.UWEB_CONF.ajt_url_out else: url_out = ConfHelper.UWEB_CONF.url_out if go_ahead: redis.setvalue(resend_key, True, GATEWAY.RESEND_EXPIRY) ldp = LocationDescParser(body, head) location = ldp.ret logging.info("[GW] T10 packet parsered:%s", location) if not location.has_key('gps_time'): location['gps_time'] = int(time.time()) logging.info("[GW] what's up? location:%s hasn't gps_time.", location) location['t'] = EVENTER.INFO_TYPE.POSITION if location['valid'] != GATEWAY.LOCATION_STATUS.SUCCESS: cellid = True else: cellid = False location = lbmphelper.handle_location(location, redis, cellid=cellid, db=db) location.name = location.get('name') if location.get('name') else "" location.name = safe_unicode(location.name) user = QueryHelper.get_user_by_tid(head.dev_id, db) tname = QueryHelper.get_alias_by_tid(head.dev_id, redis, db) dw_method = u'GPS' if not cellid else u'基站' if location.cLat and location.cLon: if user: current_time = get_terminal_time(int(time.time())) sms = SMSCode.SMS_DW_SUCCESS % (tname, dw_method, location.name, safe_unicode(current_time)) url = url_out + '/wapimg?clon=' +\ str(location.cLon/3600000.0) + '&clat=' + str(location.cLat/3600000.0) tiny_id = URLHelper.get_tinyid(url) if tiny_id: base_url = url_out + UWebHelper.URLS.TINYURL tiny_url = base_url + '/' + tiny_id logging.info("[GW] get tiny url successfully. tiny_url:%s", tiny_url) redis.setvalue(tiny_id, url, time=EVENTER.TINYURL_EXPIRY) sms += u"点击 " + tiny_url + u" 查看定位器位置。" else: logging.info("[GW] get tiny url failed.") SMSHelper.send(user.owner_mobile, sms) else: if user: sms = SMSCode.SMS_DW_FAILED % (tname, dw_method) SMSHelper.send(user.owner_mobile, sms) if not (location.lat and location.lon): args.success = GATEWAY.RESPONSE_STATUS.CELLID_FAILED else: insert_location(location, db, redis) lc = LocationDescRespComposer(args) request = DotDict(packet=lc.buf, address=address, dev_id=dev_id) update_terminal_status(redis, head.dev_id, address) append_gw_request(request, connection, channel, exchange, gw_binding) except: logging.exception("[GW] Handle locationdesc 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 post(self): """Acquire push account through uid @uid: user uid """ status = ErrorCode.SUCCESS try: data = DotDict(json_decode(self.request.body)) tid = data.tid s_type = data.s_type if data.get("category", None): category = int(data.get("category")) else: category = 1 logging.info("[UWEB] Test wspush request: %s.", data) except Exception as e: status = ErrorCode.ILLEGAL_DATA_FORMAT logging.exception("[UWEB] Invalid data format. body: %s, Exception: %s", self.request.body, e.args) self.write_ret(status) return try: # user = self.db.get("select owner_mobile") user = QueryHelper.get_user_by_tid(tid, self.db) uid = user.owner_mobile if s_type == "S3": WSPushHelper.pushS3(tid, self.db, self.redis) elif s_type == "S4": WSPushHelper.pushS4(tid, self.db, self.redis) elif s_type == "S5": body = dict( tid=tid, category=category, pbat=100, type=1, timestamp=int(time.time()), longitude=419004000, latitude=143676000, clongitude=419004000, clatitude=143676000, name="test name", speed=111, degree=203, gsm=0, locate_error=100, gps=25, alias="111", region_id=11, ) WSPushHelper.pushS5(tid, body, self.db, self.redis) elif s_type == "S6": WSPushHelper.pushS6(tid, self.db, self.redis) elif s_type == "S7": WSPushHelper.pushS7(tid, self.db, self.redis) elif s_type == "S8": acc_message = 1 WSPushHelper.pushS8(tid, acc_message, self.db, self.redis) self.write_ret(status=status) except Exception as e: logging.exception("[UWEB] WSPushHandler get push account failed, Exception:%s", e.args) status = ErrorCode.SERVER_BUSY self.write_ret(status)