def block_test(): db = DBConnection().db redis = MyRedis() #terminals = db.query("SELECT tid, mobile, group_id, cid FROM V_TERMINAL WHERE group_id != '-1'") #terminals = db.query("SELECT tid, tmobile, group_id, cid FROM V_TERMINAL WHERE cid = '13600335550'") #terminals = db.query("SELECT tid, mobile, group_id FROM T_TERMINAL_INFO WHERE stop_interval != 0 limit 1000") print 'len ', len(terminals) for terminal in terminals: tid = terminal.tid db.execute("UPDATE T_TERMINAL_INFO SET stop_interval=0 WHERE tid = %s", tid) print 'tid: %s stop_interval is closed.' % tid sessionID_key = get_terminal_sessionID_key(tid) old_sessionid = redis.get(sessionID_key) if old_sessionid: redis.delete(sessionID_key) print "Termianl %s delete session in redis." % tid
def block_test(): db = DBConnection().db redis = MyRedis() #terminals = db.query("select tid from T_TERMINAL_INFO where group_id= '438';") #terminals = db.query("select tid from T_TERMINAL_INFO where group_id= '438'") terminals = db.query("select id, tid, test from T_TERMINAL_INFO where mobile = '14778748087' ") for terminal in terminals: tid = terminal.tid #db.execute("UPDATE T_TERMINAL_INFO SET trace_para='60:0' WHERE tid = %s", # tid) #print 'tid: %s test is closed.' % tid sessionID_key = get_terminal_sessionID_key(tid) old_sessionid = redis.get(sessionID_key) if old_sessionid: redis.delete(sessionID_key) print "Termianl %s delete session in redis." % tid
def block_test(): db = DBConnection().db redis = MyRedis() #terminals = db.query("select tid, move_val, static_val from T_TERMINAL_INFO where static_val =0 and move_val=0") terminals = db.query("select tid, move_val, static_val from T_TERMINAL_INFO where tid = '392240008A' ") #terminals = db.query("select tid, move_val, static_val from T_TERMINAL_INFO where id in (12923, 21792 )") #terminals = db.query("select tid, move_val, static_val from T_TERMINAL_INFO where static_val =60") for terminal in terminals: tid = terminal.tid #db.execute("UPDATE T_TERMINAL_INFO SET static_val=120,move_val=0 WHERE tid = %s", # tid) #db.execute("UPDATE T_TERMINAL_INFO SET static_val=0, move_val=60 WHERE tid = %s", # tid) print 'tid: %s parking is modified.' % tid sessionID_key = get_terminal_sessionID_key(tid) old_sessionid = redis.get(sessionID_key) if old_sessionid: redis.delete(sessionID_key) print "Termianl %s delete session in redis." % tid
class Test(object): def __init__(self): #self.db = DBConnection().db self.redis = MyRedis() def test_lk(self): #pattern = "lk:4083025:798780" pattern = "lk:*" keys = self.redis.keys(pattern) count = 0 #print 'len ----', len(keys) for k in keys: v = self.redis.get(k) #print '-----------k', k, v self.redis.delete(k) count += 1 #self.redis.set(k, v, LOCATION_NAME_EXPIRY) if not (count % 10000): print 'count: %s W' % count
def block_test(): db = DBConnection().db redis = MyRedis() #terminals = db.query("select * from T_TERMINAL_INFO where mobile = 14778741722 " ) terminals = db.query("select * from T_TERMINAL_INFO where mobile in (14778473468 ) " ) for terminal in terminals: tid = terminal.tid t = db.query("SELECT tid from T_TERMINAL_INFO where test !=0 and tid=%s", tid) if not t: continue #print 'clear', terminal db.execute("UPDATE T_TERMINAL_INFO SET test=0 WHERE tid = %s", tid) print 'tid: %s test is closed.' % tid sessionID_key = get_terminal_sessionID_key(tid) old_sessionid = redis.get(sessionID_key) if old_sessionid: redis.delete(sessionID_key) print "Termianl %s delete session in redis." % tid
def batch_import(file_path): db = DBConnection().db redis = MyRedis() online_style = xlwt.easyxf('font: colour_index green, bold off; align: wrap on, vert centre, horiz center;') offline_style = xlwt.easyxf('font: colour_index brown, bold off; align: wrap on, vert centre, horiz center;') wt = xlwt.Workbook() ws = wt.add_sheet(u'jia') wb = xlrd.open_workbook(file_path) sheet = wb.sheets()[0] lst = "" num = 0 for i in range(0,sheet.nrows): row = sheet.row_values(i) print 'row', row mobile = unicode(row[0]) mobile = mobile[0:11] #mobile = unicode(row[0])[0:-2] print 'mobile', repr(mobile) sql_t = 'select tid, test, login, service_status from T_TERMINAL_INFO where mobile =' + mobile terminal = db.get(sql_t) print 'terminal', terminal tid = terminal.get('tid','') if terminal else '' sql = 'UPDATE T_TERMINAL_INFO set test=0 where mobile =' + mobile print 'sql', sql #db.execute('UPDATE T_TERMINAL_INFO set test=0 where mobile = %s', int(mobile)) #db.execute(sql) sessionID_key = get_terminal_sessionID_key(tid) print 'sessionID_key', sessionID_key old_sessionid = redis.get(sessionID_key) if old_sessionid: redis.delete(sessionID_key) logging.info("[UWEB] Termianl %s delete session in redis.", tid) #if i == 3: # break print num
def block_test(): db = DBConnection().db redis = MyRedis() #terminals = db.query("SELECT tid, mobile, group_id, cid FROM V_TERMINAL WHERE group_id != '-1'") terminals = db.query("SELECT tid, tmobile, group_id, cid FROM V_TERMINAL WHERE cid = '13600335550'") for terminal in terminals: tid = terminal.tid ttype = get_terminal_type_by_tid(tid) print 'ttype', ttype if ttype == 'zj200': t = db.query("SELECT tid from T_TERMINAL_INFO where test !=0 and tid=%s", tid) if not t: continue #print 'clear' db.execute("UPDATE T_TERMINAL_INFO SET test=0 WHERE tid = %s", tid) print 'tid: %s test is closed.' % tid sessionID_key = get_terminal_sessionID_key(tid) old_sessionid = redis.get(sessionID_key) if old_sessionid: redis.delete(sessionID_key) print "Termianl %s delete session in redis." % tid
class Test(): def __init__(self): self.db = DBConnection().db self.redis = MyRedis() def get_track(self, tid, start_time, end_time, cellid=False): """NOTE: Now, only return gps point. """ if cellid: track = self.db.query("SELECT id, latitude, longitude, clatitude," " clongitude, timestamp, name, type, speed, degree, locate_error" " FROM T_LOCATION" " WHERE tid = %s" " AND NOT (latitude = 0 OR longitude = 0)" " AND (timestamp BETWEEN %s AND %s)" " GROUP BY timestamp" " ORDER BY timestamp", tid, start_time, end_time) else: # gps, pvt track = self.db.query("SELECT id, latitude, longitude, clatitude," " clongitude, timestamp, name, type, speed, degree, locate_error" " FROM T_LOCATION" " WHERE tid = %s" " AND category = 1" " AND NOT (latitude = 0 OR longitude = 0)" " AND (timestamp BETWEEN %s AND %s)" " AND type = 0" " GROUP BY timestamp" " ORDER BY timestamp", tid, start_time, end_time) return track #def get_track_distance(self, track): # """Get distance of a section of track. # """ # distance = 0 # if not track: # pass # else: # start_point = None # for point in track: # if not start_point: # start_point = point # continue # else: # distance += get_distance(start_point["longitude"], start_point["latitude"], # point["longitude"], point["latitude"]) # start_point = point # return distance def handle_stop(self, tid, start_time, end_time): track = self.get_track(tid, start_time, end_time) print 'track', len(track) cnt = 0 for i, pvt in enumerate(track): #print 'i: %s, speed: %s, pvt: %s' % (i, pvt['speed'], pvt) stop_key = 'test_stop_redis:%s' % tid stop = self.redis.getvalue(stop_key) distance_key = 'test_distance_redis:%s' % tid distance = self.redis.get(distance_key) if not distance: distance = 0 last_pvt_key = 'test_last_pvt_redis:%s' % tid last_pvt = self.redis.getvalue(last_pvt_key) if last_pvt: tmp = get_distance(int(last_pvt["longitude"]), int(last_pvt["latitude"]), int(pvt["longitude"]), int(pvt["latitude"])) print 'tmp: %s, distance: %s' % (tmp, distance) distance = float(distance) + tmp print 'last distance: %s' % (distance) #print 'add distance', i, pvt['id'], tmp, distance self.redis.setvalue(distance_key, distance, time=EVENTER.STOP_EXPIRY) if pvt['speed'] > LIMIT.SPEED_LIMIT: # 5 is moving if stop: #NOTE: time_diff is too short, drop the point. if pvt["timestamp"] - stop['start_time'] < 60: # 60 seconds cnt += 1 _stop = self.db.get("select distance from T_STOP where lid =%s", stop['lid']) if _stop: tmp_dis = _stop['distance'] else: tmp_dis = 0 print 'tmp_dis', tmp_dis distance = float(distance) + tmp_dis print 'tmp_dis distance', distance self.db.execute("DELETE FROM T_STOP WHERE lid = %s", stop['lid']) self.redis.delete(stop_key) self.redis.setvalue(distance_key, distance, time=EVENTER.STOP_EXPIRY) logging.info("[EVENTER] Stop point is droped: %s", stop) else: # close a stop point cnt += 1 self.redis.delete(stop_key) self.db.execute("UPDATE T_STOP SET end_time = %s WHERE lid = %s", pvt["timestamp"], stop['lid']) logging.info("[EVENTER] Stop point is closed: %s", stop) else: pass else: # low speed, may stop if stop: stop['end_time'] = pvt["timestamp"] self.redis.setvalue(stop_key, stop, time=EVENTER.STOP_EXPIRY) logging.info("[EVENTER] Stop point is updated: %s", stop) else: # NOTE: start stop. #NOTE: create a new stop point cnt += 1 lid=pvt['id'] stop = dict(lid=lid, tid=tid, start_time=pvt["timestamp"], end_time=0, pre_lon=pvt["longitude"], pre_lat=pvt["latitude"], distance=distance) self.db.execute("INSERT INTO T_STOP(lid, tid, start_time, distance) VALUES(%s, %s, %s, %s)", lid, tid, pvt["timestamp"], distance) self.redis.setvalue(stop_key, stop, time=EVENTER.STOP_EXPIRY) self.redis.delete(distance_key) logging.info("[EVENTER] Stop point is created: %s", stop) last_pvt = pvt self.redis.setvalue(last_pvt_key, last_pvt, time=EVENTER.STOP_EXPIRY) print '---------------------- cnt', cnt def clear_stop(self, tid, start_time, end_time): self.db.execute("DELETE FROM T_STOP WHERE tid = %s and start_time between %s and %s", tid, start_time, end_time) stop_key = 'test_stop_redis:%s' % tid distance_key = 'test_distance_redis:%s' % tid last_pvt_key = 'test_last_pvt_redis:%s' % tid self.redis.delete(stop_key) self.redis.delete(distance_key) self.redis.delete(last_pvt_key)
class AgpsServer(object): def __init__(self, conf_file): ConfHelper.load(conf_file) self.redis = MyRedis() self._socket = None self.get_agps_thread = None def start(self): self.__start_get_agps_thread() try: self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self._socket.bind((ConfHelper.AGPS_CONF['host'], int(ConfHelper.AGPS_CONF['port']))) self._socket.listen(int(ConfHelper.AGPS_CONF['count'])) while True: try: infds, _, _ = select.select([self._socket,],[],[],1) if len(infds) > 0: connection, address = self._socket.accept() data = connection.recv(1024) logging.info("[AGPS] Recv %d length request:\n%s", len(data), data) agps_data = self.handle_request(data) if agps_data: connection.send(agps_data) logging.info("[AGPS] Send response length:%s", len(agps_data)) connection.close() except KeyboardInterrupt: logging.error("[AGPS] Ctrl-C is pressed.") self._socket.close() except: logging.exception("[AGPS] Socket exception.") finally: try: self._socket.close() except: logging.exception("[AGPS] Socket close exception") def handle_request(self, data): try: packet = T_CLWCheck(data) command = packet.head.command if command == GATEWAY.T_MESSAGE_TYPE.AGPS: head = packet.head body = packet.body args = DotDict(success=GATEWAY.RESPONSE_STATUS.SUCCESS, agps_data=None) ap = AgpsParser(body, head) agps_sign = self.get_agps_sign(ap.ret, int(head.timestamp)) if agps_sign != int(head.agps_sign, 16): args.success = GATEWAY.RESPONSE_STATUS.INVALID_SESSIONID logging.error("[AGPS] agps_sign invalid.") else: agps = ap.ret args.agps_data = self.get_agps_from_redis(agps) if args.agps_data: ac = AgpsComposer(args) return ac.buf else: logging.error("[AGPS] there's no invalid agps data.") return None except: logging.exception("[AGPS] Handle agps request exception.") return None def get_agps_sign(self, agps, timestamp): DEFAULT_KEY = 181084178 lon = int(agps.lon) lat = int(agps.lat) agps_sign = DEFAULT_KEY ^ lon ^ lat ^ timestamp return agps_sign def get_agps_from_ublox(self): LON_LAT = GATEWAY.LON_LAT for key, lon_lat in LON_LAT.items(): u_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: request = "cmd=aid;user=%s;pwd=%s;lat=%s;lon=%s;pacc=%s\n" % ( ConfHelper.UBLOX_CONF.user, ConfHelper.UBLOX_CONF.pwd, lon_lat[1], lon_lat[0], ConfHelper.UBLOX_CONF.pacc) u_socket.connect((ConfHelper.UBLOX_CONF.host, int(ConfHelper.UBLOX_CONF.port))) u_socket.send(request) agps_data = self.recv_ublox(u_socket) agps_key = get_agps_data_key(key) if agps_data: self.redis.set(agps_key, base64.b64encode(agps_data)) logging.info("[AGPS] Get agps data [%s] from ublox success.", key) else: self.redis.delete(agps_key) logging.info("[AGPS] Get agps data [%s] from ublox faild.", key) except: logging.exception("[AGPS] Get agps from u-blox exception.") finally: try: u_socket.close() except: logging.exception("[AGPS] U-blox socket close exception.") time.sleep(10) def recv_ublox(self, u_socket): default_length = 1024 split_str = ("\r\n\r\n") agps_data = u_socket.recv(default_length) head = agps_data.split(split_str)[0] binary_length = int(head.split("\n")[1].split(": ")[1]) logging.info("[AGPS] agps_data length:%d", binary_length) remain_length = binary_length - (default_length - (len(head) + len(split_str))) while remain_length: agps_buf = u_socket.recv(remain_length) if not agps_buf: logging.warn("[AGPS] recv empty response of socket!") u_socket.close() raise socket.error(errno.EPIPE, "the pipe might be broken.") agps_data += agps_buf remain_length -= len(agps_buf) return agps_data[-binary_length:] def get_agps_from_redis(self, agps): try: logging.info("[AGPS] Terminal lon=%s, lat=%s", agps.lon, agps.lat) partition_key = self.get_partition_key(float(agps.lon), float(agps.lat)) agps_key = get_agps_data_key(partition_key) agps_data = self.redis.get(agps_key) agps_data = agps_data if agps_data else "" return agps_data except: logging.exception("[AGPS] Get agps from redis exception.") return "" def get_partition_key(self, lon, lat): LON_LAT = GATEWAY.LON_LAT partition_key = "default" for key, lon_lat in LON_LAT.items(): if abs(float(lon_lat[0]) - lon / 3600000) <= 10 and \ abs(float(lon_lat[1]) - lat / 3600000) <= 12.5: partition_key = key break logging.info("[AGPS] agps partition is:%s", partition_key) return partition_key def __start_get_agps_thread(self): self.get_agps_thread = RepeatedTimer(int(ConfHelper.UBLOX_CONF.interval), self.get_agps_from_ublox) self.get_agps_thread.start() logging.info("[AGPS] Get agps thread is running...") def __stop_get_agps_thread(self): if self.get_agps_thread is not None: self.get_agps_thread.cancel() self.get_agps_thread.join() logging.info("[AGPS] Get agps thread stop.") self.get_agps_thread = None def stop(self): self.__stop_get_agps_thread() self._socket.close()
class Test(object): def __init__(self): self.db = DBConnection().db self.redis = MyRedis() def get_track(self, tid, start_time, end_time, cellid=False): """NOTE: Now, only return gps point. """ if cellid: track = self.db.query("SELECT id, latitude, longitude, clatitude," " clongitude, timestamp, name, type, speed, degree, locate_error" " FROM T_LOCATION" " WHERE tid = %s" " AND NOT (latitude = 0 OR longitude = 0)" " AND (timestamp BETWEEN %s AND %s)" " GROUP BY timestamp" " ORDER BY timestamp", tid, start_time, end_time) else: # gps, pvt track = self.db.query("SELECT id, latitude, longitude, clatitude," " clongitude, timestamp, name, type, speed, degree, locate_error" " FROM T_LOCATION" " WHERE tid = %s" " AND category = 1" " AND NOT (latitude = 0 OR longitude = 0)" " AND (timestamp BETWEEN %s AND %s)" " AND type = 0" " GROUP BY timestamp" " ORDER BY timestamp", tid, start_time, end_time) return track def handle_stop(self, tid, start_time, end_time): track = self.get_track(tid, start_time, end_time) #print 'track, tid:%s, len: %s' % (tid, len(track)) cnt = 0 delete_ids = [] update_item = [] create_item = [] for i, pvt in enumerate(track): #print '------------i',i, pvt['id'] #print 'i: %s, speed: %s, pvt: %s' % (i, pvt['speed'], pvt) stop_key = 'test_stop_redis:%s' % tid stop = self.redis.getvalue(stop_key) distance_key = 'test_distance_redis:%s' % tid distance = self.redis.get(distance_key) if not distance: distance = 0 last_pvt_key = 'test_last_pvt_redis:%s' % tid last_pvt = self.redis.getvalue(last_pvt_key) #if i == 0: # print 'last_pvt', last_pvt if last_pvt: tmp = get_distance(int(last_pvt["longitude"]), int(last_pvt["latitude"]), int(pvt["longitude"]), int(pvt["latitude"])) #print 'tmp: %s, distance: %s' % (tmp, distance) distance = float(distance) + tmp #print 'last distance: %s' % (distance) self.redis.setvalue(distance_key, distance, time=EVENTER.STOP_EXPIRY) if pvt['speed'] > LIMIT.SPEED_LIMIT: # 5 is moving if stop: #NOTE: time_diff is too short, drop the point. if pvt["timestamp"] - stop['start_time'] < 60: # 60 seconds cnt += 1 _stop = self.db.get("SELECT distance FROM T_STOP WHERE lid =%s ", stop['lid']) if _stop: tmp_dis = _stop['distance'] else: tmp_dis = 0 #print 'tmp_dis', tmp_dis distance = float(distance) + tmp_dis #print 'tmp_dis distance', distance test_id = self.db.execute("DELETE FROM T_STOP WHERE lid = %s", stop['lid']) #print '---------delete id', test_id delete_ids.append(stop['lid']) self.redis.delete(stop_key) self.redis.setvalue(distance_key, distance, time=EVENTER.STOP_EXPIRY) logging.info("[EVENTER] Stop point is droped: %s", stop) else: # close a stop point cnt += 1 self.redis.delete(stop_key) self.db.execute("UPDATE T_STOP SET end_time = %s WHERE lid = %s", pvt["timestamp"], stop['lid']) update_item.append(dict(timestamp=pvt["timestamp"], lid=stop['lid'])) logging.info("[EVENTER] Stop point is closed: %s", stop) else: pass else: # low speed, may stop if stop: stop['end_time'] = pvt["timestamp"] self.redis.setvalue(stop_key, stop, time=EVENTER.STOP_EXPIRY) logging.info("[EVENTER] Stop point is updated: %s", stop) else: # NOTE: start stop. #NOTE: create a new stop point cnt += 1 lid=pvt['id'] stop = dict(lid=lid, tid=tid, start_time=pvt["timestamp"], end_time=0, pre_lon=pvt["longitude"], pre_lat=pvt["latitude"], distance=distance) self.db.execute("INSERT INTO T_STOP(lid, tid, start_time, distance) VALUES(%s, %s, %s, %s)", lid, tid, pvt["timestamp"], distance) create_item.append(dict(distance=distance, tid=tid, timestamp=pvt["timestamp"], lid=lid)) self.redis.setvalue(stop_key, stop, time=EVENTER.STOP_EXPIRY) self.redis.delete(distance_key) logging.info("[EVENTER] Stop point is created: %s", stop) last_pvt = pvt self.redis.setvalue(last_pvt_key, last_pvt, time=EVENTER.STOP_EXPIRY) #print '---------------------- cnt', cnt #BIG NOTE: never use it #if create_item: # #_start = time.time() # self.db.executemany("INSERT INTO T_STOP(lid, tid, start_time, distance) VALUES(%s, %s, %s, %s)", # [(item['lid'], item['tid'], item['timestamp'], item['distance']) for item in create_item]) # #_end = time.time() # #print 'create_item', create_item # #print 'time_diff', _end - _start ##handle db #if delete_ids: # #print 'delete_ids', delete_ids # self.db.executemany("DELETE FROM T_STOP WHERE lid = %s", # [(item) for item in delete_ids]) #if update_item: # #print 'update_item', update_item # self.db.executemany("UPDATE T_STOP SET end_time = %s WHERE lid = %s", # [(item['timestamp'], item['lid']) for item in update_item]) def clear_stop(self, tid): self.db.execute("DELETE FROM T_STOP WHERE tid = %s", tid) stop_key = 'test_stop_redis:%s' % tid distance_key = 'test_distance_redis:%s' % tid last_pvt_key = 'test_last_pvt_redis:%s' % tid self.redis.delete(stop_key) self.redis.delete(distance_key) self.redis.delete(last_pvt_key) def handle_stop_single(self, tid, start_time, end_time): #begin_time = time.localtime() self.clear_stop(tid) self.handle_stop(tid, start_time, end_time) #end_time = time.localtime() #print 'begin_time',begin_time #print 'end_time',end_time def handle_stop_groups(self, tids, start_time, end_time): if not tids: return for tid in tids: self.handle_stop_single(tid, start_time, end_time) logging.info("handle_stop_groups finished") def get_terminals(self): #terminals = self.db.query("SELECT * from T_TERMINAL_INFO" # " where tid = '35C2000067'" # " limit 50") #terminals = self.db.query("SELECT id, tid from T_TERMINAL_INFO where tid = '35C2000067'") terminals = self.db.query("SELECT id, tid from T_TERMINAL_INFO LIMIT 8000") return terminals
class CheckTask(object): def __init__(self): self.db = DBConnection().db self.redis = MyRedis() def check_track_status(self): logging.info("[CELERY] checkertask check track status started.") try: terminals = self.db.query("SELECT tid FROM T_TERMINAL_INFO" " WHERE track = 1" " AND service_status = 1") for terminal in terminals: track_key = get_track_key(terminal.tid) track = self.redis.get(track_key) logging.info("[CK] track: %s, tid: %s", track, terminal.tid) if not track: self.db.execute("UPDATE T_TERMINAL_INFO" " SET track = 0" " WHERE tid = %s LIMIT 1", terminal.tid) sessionID_key = get_terminal_sessionID_key(terminal.tid) self.redis.delete(sessionID_key) logging.info("[CK] Turn off track of terminal: %s", terminal.tid) except Exception as e: logging.exception("[CELERY] Check track status exception.") 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 update_sms_flag(self, tid): self.db.execute("UPDATE T_POWEROFF_TIMEOUT" " SET sms_flag = %s" " WHERE tid = %s", GATEWAY.POWEROFF_TIMEOUT_SMS.SEND, tid) def send_offline_remind_sms(self): logging.info("[CELERY] checkertask send offline remind sms started.") try: currenttime = int(time.time()) terminals = self.db.query("SELECT tid, alias, mobile, owner_mobile, offline_time" " FROM T_TERMINAL_INFO" " WHERE login = 0" " AND service_status = 1" " AND offline_time < %s", (currenttime - 24*60*60)) for terminal in terminals: sms_option = QueryHelper.get_sms_option_by_uid(terminal.owner_mobile, 'heartbeat_lost', self.db) if sms_option == UWEB.SMS_OPTION.SEND: ctime = get_terminal_time(currenttime) ctime = safe_unicode(ctime) alias = terminal['alias'] if terminal['alias'] else terminal['mobile'] sms = SMSCode.SMS_HEARTBEAT_LOST % (alias, ctime) SMSHelper.send(terminal.owner_mobile, sms) logging.info("[CELERY] Send offline remind sms to user:%s, tid:%s", terminal.owner_mobile, terminal.tid) logging.info("[CELERY] checkertask send offline remind sms finished.") except Exception as e: logging.exception("[CELERY] Check terminal poweroff timeout exception.") def check_charge_remind(self): logging.exception("[CELERY] checkertask charge remind started") try: terminals = self.db.query("SELECT tid, mobile, owner_mobile, begintime" " FROM T_TERMINAL_INFO " " WHERE service_status = 1") for terminal in terminals: begintime = int(terminal.begintime) begintime = time.strftime("%Y,%m,%d", time.localtime(begintime)).split(",") b_year = int(begintime[0]) b_month = int(begintime[1]) b_day = int(begintime[2]) currenttime = int(time.time()) currenttime = time.strftime("%Y,%m,%d", time.localtime(currenttime)).split(",") c_year = int(currenttime[0]) c_month = int(currenttime[1]) c_day = int(currenttime[2]) # get days of current month days = calendar.monthrange(c_year, c_month)[1] if b_year > c_year: continue elif b_year == c_year and b_month == c_month: # do not remind user on register month continue elif b_day < c_day: continue elif (b_day == c_day) or (b_day > days and c_day == days): # 1. equal day # 2. has no equal day on this month, send sms on the last day of this month # send charge remind sms if terminal.owner_mobile: content = SMSCode.SMS_CHARGE_REMIND % terminal.mobile SMSHelper.send(terminal.owner_mobile, content) logging.info("[CELERY] Send charge remind sms to user: %s", terminal.owner_mobile) except Exception as e: logging.exception("[CELERY] Check charge remind exception: %s", e.args) def mileage_notify(self): logging.info("[CELERY] checkertask mileage_notify started.") try: #NOTE: avoid sms is sent when the server is restart. _date = datetime.datetime.fromtimestamp(int(time.time())) if _date.hour not in (9,10): return #NOTE: get all terminals which may be notified, record them into T_MILEAGE_NOTIFICATION. terminals = self.db.query("SELECT tid, distance_notification, notify_count, left_days" " FROM T_MILEAGE_NOTIFICATION" " WHERE distance_notification != 0" " AND notify_count < 3") for terminal in terminals: terminal_info = QueryHelper.get_terminal_by_tid(terminal.tid, self.db) tid = terminal['tid'] owner_mobile = terminal_info['owner_mobile'] assist_mobile = terminal_info['assist_mobile'] #distance_current = terminal_info['distance_current'] t = self.db.get("SELECT distance_current FROM T_TERMINAL_INFO WHERE tid = %s", terminal.tid) distance_current = t['distance_current'] mobile = terminal_info['mobile'] notify_count = terminal['notify_count'] left_days = terminal['left_days'] distance_notification = terminal['distance_notification'] # NOTE: if distance_current is less than distance_notification, just skip it if distance_current < distance_notification: continue if left_days == 1: # it should be notified this day logging.info("[CELERY] Send mileage notification." " tid: %s, mobile: %s, owner_mobile: %s, assist_mobile: %s," " distance_notification: %s, distance_current: %s," " notify_count: %s, left_days: %s.", tid, mobile, owner_mobile, assist_mobile, distance_notification, distance_current, notify_count, left_days) self.db.execute("UPDATE T_MILEAGE_NOTIFICATION" " SET notify_count = %s," " left_days = %s," " notify_time = %s" " WHERE tid = %s", notify_count+1, 3, int(time.time()), tid) distance_current_ = int(round(distance_current/1000.0)) if owner_mobile: sms = SMSCode.SMS_NOTIFY % (distance_current_, terminal_info['alias']) SMSHelper.send(owner_mobile, sms) if assist_mobile: user = QueryHelper.get_user_by_mobile(owner_mobile, self.db) name = safe_unicode(user['name']) sms = SMSCode.SMS_NOTIFY_ASSIST % (mobile, owner_mobile, name, distance_current_) SMSHelper.send(assist_mobile, sms) elif left_days in (2, 3): # do not notify, just postpone one day logging.info("[CELERY] Do not send mileage notification this day," " just modify the left_days." " tid: %s, mobile: %s, owner_mobile: %s, assist_mobile: %s," " distance_notification: %s, distance_current: %s," " notify_count: %s, left_days: %s.", tid, mobile, owner_mobile, assist_mobile, distance_notification, distance_current, notify_count, left_days) self.db.execute("UPDATE T_MILEAGE_NOTIFICATION" " SET left_days = %s" " WHERE tid = %s", left_days-1, tid) else: #NOTE: It should never occur. logging.info("[CELERY] Invalid left_days: %s, mobile: %s.", left_days, mobile) except Exception as e: logging.exception("[CELERY] Mileage notification failed. Exception: %s.", e.args) def day_notify(self): logging.info("[CELERY] checkertask day_notify started.") try: #NOTE: avoid sms is sent when the server is restart. _date = datetime.datetime.fromtimestamp(int(time.time())) if _date.hour not in (9,10): return #NOTE: get all terminals which may be notified, record them into T_MILEAGE_NOTIFICATION. terminals = self.db.query("SELECT tid, day_notification, notify_count, left_days" " FROM T_DAY_NOTIFICATION" " WHERE day_notification != 0" " AND notify_count < 3") for terminal in terminals: if int(time.time()) < terminal['day_notification']: # it's not time to notify continue terminal_info = QueryHelper.get_terminal_by_tid(terminal.tid, self.db) tid = terminal['tid'] owner_mobile = terminal_info['owner_mobile'] assist_mobile = terminal_info['assist_mobile'] t = self.db.get("SELECT distance_current FROM T_TERMINAL_INFO WHERE tid = %s", terminal.tid) #distance_current = terminal_info['distance_current'] distance_current = t['distance_current'] mobile = terminal_info['mobile'] notify_count = terminal['notify_count'] left_days = terminal['left_days'] day_notification= terminal['day_notification'] if left_days == 1: # it should be notified this day logging.info("[CELERY] Send day notification." " tid: %s, mobile: %s, owner_mobile: %s, assist_mobile: %s," " day_notification: %s" " notify_count: %s, left_days: %s.", tid, mobile, owner_mobile, assist_mobile, day_notification, notify_count, left_days) self.db.execute("UPDATE T_DAY_NOTIFICATION" " SET notify_count = %s," " left_days = %s," " notify_time = %s" " WHERE tid = %s", notify_count+1, 3, int(time.time()), tid) if owner_mobile: sms = SMSCode.SMS_NOTIFY_DAY % (terminal_info['alias']) SMSHelper.send(owner_mobile, sms) if assist_mobile: user = QueryHelper.get_user_by_mobile(owner_mobile, self.db) name = safe_unicode(user['name']) sms = SMSCode.SMS_NOTIFY_ASSIST_DAY % (mobile, owner_mobile, name) SMSHelper.send(assist_mobile, sms) elif left_days in (2, 3): # do not notify, just postpone one day logging.info("[CELERY] Do not send day notification this day," " just modify the left_days." " tid: %s, mobile: %s, owner_mobile: %s, assist_mobile: %s," " day_notification: %s" " notify_count: %s, left_days: %s.", tid, mobile, owner_mobile, assist_mobile, day_notification, notify_count, left_days) self.db.execute("UPDATE T_DAY_NOTIFICATION" " SET left_days = %s" " WHERE tid = %s", left_days-1, tid) else: #NOTE: It should never occur. logging.info("[CELERY] Invalid left_days: %s, mobile: %s.", left_days, mobile) except Exception as e: logging.exception("[CELERY] Day notification failed. Exception: %s.", e.args)