def get_sms_status(self, tmobile): """ sms_status: 0, // failded 1, // sent 2, // reached to terminal 3, // terminal has connected to gataway """ sms_status = 0 terminal = QueryHelper.get_terminal_by_tmobile(tmobile, self.db) if terminal.login == GATEWAY.TERMINAL_LOGIN.ONLINE: sms_status = 3 elif terminal.msgid: sms_status = 1 else: sms = self.db.get("SELECT send_status, recv_status" " FROM T_SMS" " WHERE msgid = %s" " AND category = %s" " LIMIT 1", terminal.msgid, SMS.CATEGORY.MT) if not sms: pass elif sms.recv_status == 0: sms_status = 2 elif sms.send_status == 0: sms_status = 1 return sms_status
def post(self): """Get last infomation of a terminal. """ status = ErrorCode.SUCCESS try: data = DotDict(json_decode(self.request.body)) mobile = str(data.mobile) manual_status = data.manual_status token = data.token logging.info("[MANUAL] Request, data:%s", data) except Exception as e: status = ErrorCode.DATA_FORMAT_INVALID logging.exception("[REBOO] Invalid data format, body: %s, mobile: %s.", self.request.body, mobile) self.write_ret(status) return try: status = self.basic_check(token, mobile) if status != ErrorCode.SUCCESS: self.write_ret(status) return terminal = QueryHelper.get_terminal_by_tmobile(mobile, self.db) tid = terminal.tid update_mannual_status(self.db, self.redis, tid, manual_status) self.write_ret(status) except Exception as e: logging.exception("[MANUAL] mobile: %s. Exception: %s", mobile, e.args) status = ErrorCode.FAILED self.write_ret(status)
def post(self): """Reboot a terminal. """ status = ErrorCode.SUCCESS try: data = DotDict(json_decode(self.request.body)) mobile = str(data.mobile) token = data.token logging.info("[REBOOT] Request, data:%s", data) except Exception as e: logging.exception("[REBOOT] Invalid data format, body: %s, mobile: %s.", self.request.body, mobile) status = ErrorCode.DATA_FORMAT_INVALID self.write_ret(status) return try: status = self.basic_check(token, mobile) if status != ErrorCode.SUCCESS: self.write_ret(status) return terminal = QueryHelper.get_terminal_by_tmobile(mobile, self.db) tid = terminal.tid restart_terminal(self.db, self.redis, tid, mobile) logging.info("[REBOOT] Restart a terminal. tid: %s, mobile: %s", tid, mobile) self.write_ret(status) except Exception as e: logging.exception("[REBOOT] Reboot failed. mobile: %s. Exception: %s", mobile, e.args) status = ErrorCode.FAILED self.write_ret(status)
def post(self): """Get locations in same period. """ status = ErrorCode.SUCCESS res = [] try: data = DotDict(json_decode(self.request.body)) mobile = str(data.mobile) start_time = int(data.start_time) end_time = int(data.end_time) token = data.token logging.info("[TRACK] Request, data:%s", data) except Exception as e: status = ErrorCode.DATA_FORMAT_INVALID logging.exception("[TRACK] Invalid data format, body: %s, mobile: %s.", self.request.body, mobile) self.write_ret(status) return try: status = self.basic_check(token, mobile) if status != ErrorCode.SUCCESS: self.write_ret(status) return if (end_time - start_time) > OPENAPI.LIMIT.TRACK_INTERVAL: status = ErrorCode.LOCATION_EXCEED self.write_ret(status) return terminal = QueryHelper.get_terminal_by_tmobile(mobile, self.db) tid = terminal.tid track = get_track(self.db, self.redis, tid, start_time, end_time, cellid=True) for t in track: if (t and t.clatitude and t.clongitude): d = dict(lon=t.get('longitude', 0), lat=t.get('latitude', 0), clon=t.get('clongitude', 0), clat=t.get('clatitude', 0), timestamp=t.get('timestamp',0), name=t.get('name',''), type=t.get('type',0)) res.append(d) else: pass if len(res) > OPENAPI.LIMIT.TRACK: logging.info("[UWEB] Track is too large, just provide the latest part.") res = res[-OPENAPI.LIMIT.TRACK:] self.write_ret(status, dict_=dict(res=res)) except Exception as e: logging.exception("[TRACK] Track failed. mobile: %s. Exception: %s", mobile, e.args) status = ErrorCode.FAILED self.write_ret(status)
def post(self): """Get latest location of a terminal. """ status = ErrorCode.SUCCESS res = {} try: data = DotDict(json_decode(self.request.body)) mobile = str(data.mobile) token = data.token logging.info("[REALTIME] Request, data:%s", data) except Exception as e: status = ErrorCode.DATA_FORMAT_ILLEGAL logging.exception("[REALTIME] Invalid data format, body: %s, mobile: %s.", self.request.body, mobile) self.write_ret(status) return try: status = self.basic_check(token, mobile) if status != ErrorCode.SUCCESS: self.write_ret(status) return terminal = QueryHelper.get_terminal_by_tmobile(mobile, self.db) tid = terminal.tid location = QueryHelper.get_location_info(tid, self.db, self.redis) # check and make name valid if location and location['name'] is None: location['name'] = '' # check and make clatclon valid locations = [location,] locations = get_locations_with_clatlon(locations, self.db) location = locations[0] if (location and location.clatitude and location.clongitude): res = dict(lon=location.get('longitude', 0), lat=location.get('latitude', 0), clon=location.get('clongitude', 0), clat=location.get('clatitude', 0), timestamp=location.get('timestamp',0), name=location.get('name',''), type=location.get('type',0)) self.write_ret(status, dict_=dict(res=res)) except Exception as e: logging.exception("[REALTIME] sid: %s. Exception: %s", mobile, e.args) status = ErrorCode.FAILED self.write_ret(status)
def post(self): """Reregist a pair of umobile and tmobile. Send sms to terminal. """ status = ErrorCode.SUCCESS try: data = DotDict(json_decode(self.request.body)) logging.info("[UWEB] Register request: %s", data) except Exception as e: status = ErrorCode.ILLEGAL_DATA_FORMAT self.write_ret(status) return try: tmobile = data.tmobile user = QueryHelper.get_user_by_tmobile(tmobile, self.db) if user: umobile = user.owner_mobile terminal = QueryHelper.get_terminal_by_tmobile( tmobile, self.db) if int(terminal.biz_type) == UWEB.BIZ_TYPE.YDWS: register_sms = SMSCode.SMS_REGISTER % (umobile, tmobile) ret = SMSHelper.send_to_terminal(tmobile, register_sms) else: activation_code = QueryHelper.get_activation_code(self.db) register_sms = SMSCode.SMS_REGISTER_YDWQ % ( ConfHelper.UWEB_CONF.url_out, activation_code) ret = SMSHelper.send(tmobile, register_sms) ret = DotDict(json_decode(ret)) if ret.status == ErrorCode.SUCCESS: logging.info("[UWEB] Reregist successfully. umobile: %s, tmobile: %s .", umobile, tmobile) else: status = ErrorCode.REGISTER_FAILED logging.error("[UWEB] Reregister failed. umobile: %s, tmobile: %s. Message: %s", umobile, tmobile, ErrorCode.ERROR_MESSAGE[status]) else: logging.exception("[UWEB] Terminal has no user, ignore it. tmobile: %s. ", tmobile) self.write_ret(status) except Exception as e: logging.exception("[UWEB] Reregister failed. tmobile: %s , Exception: %s", tmobile, e.args) status = ErrorCode.REGISTER_FAILED self.write_ret(status)
def get(self): """Get activation_code according to tid or mobile. """ status = ErrorCode.SUCCESS try: tid = self.get_argument('tid', '') mobile = self.get_argument('mobile', '') logging.info("[ACTIVATIONCODE] Activation_code query, tid: %s, mobile: %s.", tid, mobile) except Exception as e: status = ErrorCode.ILLEGAL_DATA_FORMAT logging.exception("[ACTIVATIONCODE] Illegal format, body: %s.", self.request.body) self.write_ret(status) return try: terminal = None if tid: terminal = QueryHelper.get_terminal_by_tid(tid, self.db) if not terminal: if mobile: terminal = QueryHelper.get_terminal_by_tmobile( mobile, self.db) activation_code = terminal.get( 'activation_code', '') if terminal else '' tid = terminal.get('tid', '') if terminal else '' mobile = terminal.get('mobile', '') if terminal else '' biz_type = terminal.get( 'biz_type', UWEB.BIZ_TYPE.YDWS) if terminal else UWEB.BIZ_TYPE.YDWS self.write_ret(status, dict_=DotDict(activation_code=activation_code, tid=tid, mobile=mobile, biz_type=biz_type)) except Exception as e: status = ErrorCode.SERVER_BUSY logging.exception("[ACTIVATIONCODE] Get activation_code failed, body: %s. Exception: %s", self.request.body, e.args) self.write_ret(status)
def put(self): """Modify the settings. """ status = ErrorCode.SUCCESS try: data = DotDict(json_decode(self.request.body)) logging.info("[UWEB] Setting modify request: %s", self.request.body) tmobile = data.tmobile key = data.key value = data.value logging.info("[UWEB] Setting modify request: %s", data) except Exception as e: status = ErrorCode.ILLEGAL_DATA_FORMAT logging.exception("[UWEB] Invalid data format. Exception: %s", e.args) self.write_ret(status) return try: clear_session = False terminal = QueryHelper.get_terminal_by_tmobile(tmobile, self.db) if not terminal: status = ErrorCode.TERMINAL_NOT_EXISTED else: tid = terminal.tid sql_cmd = "UPDATE T_TERMINAL_INFO SET %(key)s = %(value)s WHERE tid='%(tid)s' LIMIT 1 " % locals() if key in ['tracking_interval']: clear_session = True if clear_session: sessionID_key = get_terminal_sessionID_key(tid) self.redis.delete(sessionID_key) self.db.execute(sql_cmd) self.write_ret(status) except Exception as e: logging.exception("Modify setting failed. Exception: %s", e.args) self.render('errors/error.html', message=ErrorCode.FAILED)
def post(self): """Regist a pair of umobile and tmobile. """ status = ErrorCode.SUCCESS try: data = DotDict(json_decode(self.request.body)) logging.info("[UWEB] Register request: %s", data) umobile = data.umobile tmobile = data.tmobile captcha = data.captcha except Exception as e: status = ErrorCode.ILLEGAL_DATA_FORMAT logging.exception("[UWEB] Invalid data format. Exception: %s", e.args) self.write_ret(status) return try: # check tmobile is whitelist or not white_list = check_zs_phone(tmobile, self.db) if not white_list: logging.info("[UWEB] Mobile is not whitelist. tmobile: %s.", tmobile) status = ErrorCode.MOBILE_NOT_ORDERED message = ErrorCode.ERROR_MESSAGE[status] % tmobile self.write_ret(status, message=message) return captcha_key = get_captcha_key(umobile) captcha_old = self.redis.get(captcha_key) if captcha_old: if captcha == str(captcha_old): terminal = QueryHelper.get_terminal_by_tmobile( tmobile, self.db) if terminal: if terminal.service_status == UWEB.SERVICE_STATUS.TO_BE_UNBIND: # delete to_be_unbind terminal! delete_terminal(terminal.tid, self.db, self.redis) else: status = ErrorCode.TERMINAL_ORDERED logging.info("[UWEB] Regist failed. umobile: %s, tmobile: %s Message: %s", umobile, tmobile, ErrorCode.ERROR_MESSAGE[status]) self.write_ret(status) return register_sms = SMSCode.SMS_REGISTER % (umobile, tmobile) ret = SMSHelper.send_to_terminal(tmobile, register_sms) ret = DotDict(json_decode(ret)) if ret.status == ErrorCode.SUCCESS: logging.info("[UWEB] Regist successfully. umobile: %s, tmobile: %s ", umobile, tmobile) self.redis.delete(captcha_key) else: status = ErrorCode.REGISTER_FAILED logging.error("[UWEB] Regist failed. umobile: %s, tmobile: %s. Message: %s", umobile, tmobile, ErrorCode.ERROR_MESSAGE[status]) else: status = ErrorCode.WRONG_CAPTCHA logging.error("[UWEB] Regist failed. umobile: %s, captcha: %s, captcha_old: %s, Message: %s", umobile, captcha, captcha_old, ErrorCode.ERROR_MESSAGE[status]) else: status = ErrorCode.NO_CAPTCHA logging.error("[UWEB] Register failed. umobile: %s, captcha: %s, Message: %s", umobile, captcha, ErrorCode.ERROR_MESSAGE[status]) self.write_ret(status) except Exception as e: logging.exception("[UWEB] Register failed. umobile: %s tmobile: %s , Exception: %s", umobile, tmobile, e.args) status = ErrorCode.REGISTER_FAILED self.write_ret(status)
def post(self): """Get mileage of a terminal. """ status = ErrorCode.SUCCESS res = [] try: data = DotDict(json_decode(self.request.body)) mobile = data.mobile start_time = int(data.start_time) end_time = int(data.end_time) token = data.token logging.info("[MILEAGE] Request, data:%s", data) except Exception as e: status = ErrorCode.DATA_FORMAT_INVALID logging.exception("[MILEAGE] Invalid data format, body: %s, ", self.request.body) self.write_ret(status) return try: status = self.basic_check(token, mobile) if status != ErrorCode.SUCCESS: self.write_ret(status) return terminal = QueryHelper.get_terminal_by_tmobile(mobile, self.db) tid = terminal.tid # end_time must bigger than start_time delta = end_time - start_time d, m = divmod(delta, 60 * 60 * 24) start_date = get_date_from_utc(start_time) end_date = get_date_from_utc(end_time) start_day = datetime.datetime.fromtimestamp(start_time) end_day = datetime.datetime.fromtimestamp(end_time) # get how many days the end_time and start_time cover days = abs(end_day - start_day).days + 1 for item in range(days): timestamp = start_time + 1 * 60 * 60 * 24 * (item) date = get_date_from_utc(timestamp) year, month, day = date.year, date.month, date.day start_time_, end_time_ = start_end_of_day( year=year, month=month, day=day) date = '-'.join([str(year), str(month), str(day)]) mileage_log = self.db.get("SELECT distance FROM T_MILEAGE_LOG" " WHERE tid = %s" " AND timestamp = %s", tid, end_time_) mileage = mileage_log['distance'] if mileage_log else 0 r = dict(date=date, mileage=mileage) res.append(r) self.write_ret(status, dict_=dict(res=res)) except Exception as e: logging.exception("[MILEAGE] mobile: %s. Exception: %s", mobile, e.args) status = ErrorCode.FAILED self.write_ret(status)
def post(self): """Retrive various event. """ status = ErrorCode.SUCCESS try: data = DotDict(json_decode(self.request.body)) mobile = data.mobile sn = data.sn category = int(data.category) location = data.location gps = int(data.gps) gsm = int(data.gsm) pbat = int(data.pbat) attendance_time = data.get('attendance_time', '') logging.info("[UWEB] upload request: %s", data) except Exception as e: status = ErrorCode.ILLEGAL_DATA_FORMAT logging.exception("[UWEB] Invalid data format. Exception: %s, data: %s", e.args, self.request.body) self.write_ret(status) return try: terminal = self.db.get("SELECT id, service_status, mobile" " FROM T_TERMINAL_INFO" " WHERE mobile = %s" " AND sn = %s" " AND service_status = %s" " AND biz_type = %s LIMIT 1", mobile, sn, UWEB.SERVICE_STATUS.ON, UWEB.BIZ_TYPE.YDWQ) if not terminal: status = ErrorCode.ACCOUNT_NOT_MATCH logging.info('mobile: %s, sn: %s not match, drop it.', mobile, sn) self.write_ret(status) return terminal = QueryHelper.get_terminal_by_tmobile(mobile, self.db) tid = terminal['tid'] # NOTE: location may be a dict or list if type(location) != list: locations = [location,] else: locations = location if category == UWEB.UPLOAD_CATEGORY.HEARTBEAT: pass elif category == UWEB.UPLOAD_CATEGORY.LOCATION: for location in locations: location = DotDict(dev_id=tid, lat=location['clatitude'], lon=location['clongitude'], alt=0, cLat=location['clatitude'], cLon=location['clongitude'], gps_time=location['timestamp'], name=location.get('name', ''), category=1, type=int(location['type']), speed=location['speed'], degree=location['degree'], cellid='', locate_error=int(location['locate_error'])) insert_location(location, self.db, self.redis) elif category == UWEB.UPLOAD_CATEGORY.ATTENDANCE: location = locations[0] if len(locations) >= 1 else None if location: location = DotDict(dev_id=tid, lat=location['clatitude'], lon=location['clongitude'], alt=0, cLat=location['clatitude'], cLon=location['clongitude'], gps_time=location['timestamp'], name=location.get('name', ''), category=1, type=int(location['type']), speed=location['speed'], degree=location['degree'], cellid='', locate_error=int(location['locate_error'])) lid = insert_location(location, self.db, self.redis) a_info=dict(mobile=mobile, comment=u'', timestamp=attendance_time if attendance_time else location['gps_time'], lid=lid) record_attendance(self.db, a_info) else: logging.error("[UWEB] Invalid attendance data, location is missed.") else: #TODO: handle power-event location = locations[0] if len(locations) >= 1 else None if location: location = DotDict(dev_id=tid, lat=location['clatitude'], lon=location['clongitude'], alt=0, cLat=location['clatitude'], cLon=location['clongitude'], gps_time=location['timestamp'], name=location.get('name', ''), category=1, type=int(location['type']), speed=location['speed'], degree=location['degree'], cellid='', locate_error=int(location['locate_error'])) lid = insert_location(location, self.db, self.redis) #a_info=dict(mobile=mobile, # comment=u'', # timestamp=attendance_time if attendance_time else location['gps_time'], # lid=lid) #record_attendance(self.db, a_info) t_info = DotDict(gps=gps, gsm=gsm, login=1, pbat=pbat, tid=tid) update_terminal_info_ydwq(self.db, self.redis, t_info) update_terminal_status(self.redis, tid) self.write_ret(status) except Exception as e: logging.exception("[UWEB] tmobile:%s upload failed. Exception: %s", mobile, e.args) status = ErrorCode.SERVER_BUSY self.write_ret(status)
def post(self): """Add a terminal. """ status = ErrorCode.SUCCESS try: data = DotDict(json_decode(self.request.body)) logging.info("[UWEB] Corp add terminal request: %s, cid: %s", data, self.current_user.cid) except Exception as e: status = ErrorCode.ILLEGAL_DATA_FORMAT self.write_ret(status) return try: if data.has_key('cnum') and not check_cnum(data.cnum): status = ErrorCode.ILLEGAL_CNUM self.write_ret(status) return # 1 year begintime = int(time.time()) now_ = datetime.datetime.now() endtime = now_ + relativedelta(years=1) endtime = int(time.mktime(endtime.timetuple())) # 1: add terminal #umobile = data.umobile if data.umobile else self.current_user.cid if data.umobile: umobile = data.umobile else: corp = QueryHelper.get_corp_by_cid(self.current_user.cid, self.db) umobile = corp.get('c_mobile', '') terminal = QueryHelper.get_terminal_by_tmobile(data.tmobile, self.db) if terminal: if terminal.service_status == UWEB.SERVICE_STATUS.TO_BE_UNBIND: delete_terminal(terminal.tid, self.db, self.redis) else: logging.error( "[UWEB] mobile: %s already existed.", data.tmobile) status = ErrorCode.TERMINAL_ORDERED self.write_ret(status) return vibl = data.get("vibl") use_scene = get_use_scene_by_vibl(vibl) biz_type = data.get('biz_type', UWEB.BIZ_TYPE.YDWS) tid = data.tmobile terminal_info = dict(tid=tid, group_id=data.group_id, tmobile=data.tmobile, owner_mobile=umobile, mannual_status=UWEB.DEFEND_STATUS.YES, begintime=begintime, endtime=4733481600, offline_time=begintime, cnum=data.cnum, icon_type=data.icon_type, login_permit=data.login_permit, push_status=data.push_status, vibl=data.vibl, use_scene=use_scene, biz_type=biz_type, speed_limit=data.speed_limit, stop_interval=data.stop_interval, service_status=UWEB.SERVICE_STATUS.ON) if int(biz_type) == UWEB.BIZ_TYPE.YDWS: # 0. check tmobile is whitelist or not white_list = check_zs_phone(data.tmobile, self.db) if not white_list: logging.error("[UWEB] mobile: %s is not whitelist.", data.tmobile) status = ErrorCode.MOBILE_NOT_ORDERED message = ErrorCode.ERROR_MESSAGE[status] % data.tmobile self.write_ret(status, message=message) return # 4: send message to terminal register_sms = SMSCode.SMS_REGISTER % (umobile, data.tmobile) ret = SMSHelper.send_to_terminal(data.tmobile, register_sms) else: tid = get_tid_from_mobile_ydwq(data.tmobile) activation_code = QueryHelper.get_activation_code(self.db) terminal_info['tid'] = tid terminal_info['activation_code'] = activation_code terminal_info['service_status'] = UWEB.SERVICE_STATUS.TO_BE_ACTIVATED register_sms = SMSCode.SMS_REGISTER_YDWQ % (ConfHelper.UWEB_CONF.url_out, activation_code) ret = SMSHelper.send(data.tmobile, register_sms) add_terminal(terminal_info, self.db, self.redis) # record the add action bind_info = dict(tid=data.tmobile, tmobile=data.tmobile, umobile=umobile, group_id=data.group_id, cid=self.current_user.cid, add_time=int(time.time())) record_add_action(bind_info, self.db) if ret: ret = DotDict(json_decode(ret)) if ret.status == ErrorCode.SUCCESS: self.db.execute("UPDATE T_TERMINAL_INFO" " SET msgid = %s" " WHERE mobile = %s", ret['msgid'], data.tmobile) else: logging.error("[UWEB] Send %s to terminal %s failed.", register_sms, data.tmobile) else: logging.error("[UWEB] Send %s to terminal %s failed.", register_sms, data.tmobile) # NOTE: add user user_info = dict(umobile=umobile, password='******', uname=umobile) add_user(user_info, self.db, self.redis) self.write_ret(status) except Exception as e: logging.exception("[UWEB] Update terminal info failed. cid:%s, Exception: %s", self.current_user.cid, e.args) status = ErrorCode.SERVER_BUSY self.write_ret(status)