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 update_terminal_db(self, data): """Update database. """ # NOTE: these fields should to be modified in db terminal_keys = ['cellid_status', 'white_pop', 'trace', 'freq', 'vibchk', 'vibl', 'push_status', 'login_permit', 'alert_freq', 'stop_interval', 'biz_type', 'speed_limit'] terminal_fields = [] if data.has_key('tid'): del data['tid'] for key, value in data.iteritems(): # NOTE: These fields should be modified in database. if key in terminal_keys: if data.get(key, None) is not None: terminal_fields.append(key + ' = ' + str(value)) # NOT: These fields should be handled specially. if key == 'white_list': white_list = data[key] if len(data['white_list']) < 1: pass else: self.db.execute("DELETE FROM T_WHITELIST WHERE tid = %s", self.current_user.tid) for white in white_list[1:]: self.db.execute("INSERT INTO T_WHITELIST" " VALUES(NULL, %s, %s)" " ON DUPLICATE KEY" " UPDATE tid = VALUES(tid)," " mobile = VALUES(mobile)", self.current_user.tid, white) elif key == 'alias': self.db.execute("UPDATE T_TERMINAL_INFO" " SET alias = %s" " WHERE tid = %s", value, self.current_user.tid) terminal_info_key = get_terminal_info_key( self.current_user.tid) terminal_info = self.redis.getvalue(terminal_info_key) if terminal_info: terminal_info[key] = value self.redis.setvalue(terminal_info_key, terminal_info) elif key == 'icon_type': self.db.execute("UPDATE T_TERMINAL_INFO" " SET icon_type = %s" " WHERE tid = %s", value, self.current_user.tid) terminal_info_key = get_terminal_info_key( self.current_user.tid) terminal_info = self.redis.getvalue(terminal_info_key) if terminal_info: terminal_info[key] = value self.redis.setvalue(terminal_info_key, terminal_info) elif key == 'corp_cnum': self.db.execute("UPDATE T_CAR" " SET cnum = %s" " WHERE tid = %s", safe_utf8(value), self.current_user.tid) self.db.execute("UPDATE T_TERMINAL_INFO" " SET alias = %s" " WHERE tid = %s", safe_utf8(value), self.current_user.tid) terminal_info_key = get_terminal_info_key( self.current_user.tid) terminal_info = self.redis.getvalue(terminal_info_key) if terminal_info: terminal_info[ 'alias'] = value if value else self.current_user.sim self.redis.setvalue(terminal_info_key, terminal_info) elif key == 'owner_mobile' and value is not None: umobile = value user = dict(umobile=umobile, password=u'111111') add_user(user, self.db, self.redis) t = QueryHelper.get_terminal_by_tid(self.current_user.tid, self.db) old_uids = [t.owner_mobile] # send sms self.db.execute("UPDATE T_TERMINAL_INFO" " SET owner_mobile = %s" " WHERE tid = %s", umobile, self.current_user.tid) register_sms = SMSCode.SMS_REGISTER % ( umobile, self.current_user.sim) SMSHelper.send_to_terminal(self.current_user.sim, register_sms) # update redis terminal_info_key = get_terminal_info_key( self.current_user.tid) terminal_info = self.redis.getvalue(terminal_info_key) if terminal_info: terminal_info[key] = umobile self.redis.setvalue(terminal_info_key, terminal_info) # wspush to client WSPushHelper.pushS3(self.current_user.tid, self.db, self.redis) WSPushHelper.pushS3_dummy(old_uids, self.db, self.redis) elif key == "alert_freq": alert_freq_key = get_alert_freq_key(self.current_user.tid) if self.redis.exists(alert_freq_key): logging.info( "[UWEB] Termianl %s delete alert freq in redis.", self.current_user.tid) self.redis.delete(alert_freq_key) # if vibl has been changed,then update use_scene as well elif key == "vibl": use_scene = get_use_scene_by_vibl(value) self.db.execute("UPDATE T_TERMINAL_INFO SET use_scene=%s WHERE tid=%s", use_scene, self.current_user.tid) logging.info("[UWEB] Terminal %s update use_scene %s and vibl %s", self.current_user.tid, use_scene, value) logging.info( "[UWEB] Termianl %s delete session in redis.", self.current_user.tid) # NOTE: deprecated. elif key == "parking_defend" and value is not None: if value == 1: mannual_status = UWEB.DEFEND_STATUS.SMART else: mannual_status = UWEB.DEFEND_STATUS.YES update_mannual_status( self.db, self.redis, self.current_user.tid, mannual_status) elif key == 'login_permit': # wspush to client WSPushHelper.pushS3(self.current_user.tid, self.db, self.redis) else: pass # NOTE:update database. terminal_clause = ','.join(terminal_fields) if terminal_clause: self.db.execute("UPDATE T_TERMINAL_INFO" " SET " + terminal_clause + " WHERE tid = %s ", self.current_user.tid) # NOTE: clear sessionID if freq, stop_interval, vibl can be found. if data.has_key('freq') or data.has_key('stop_interval') or data.has_key('vibl'): clear_sessionID(self.redis, self.current_user.tid)