def post(self): status = ErrorCode.SUCCESS try: data = DotDict(json_decode(self.request.body)) tid = data.get('tid', None) tids = data.get('tids', None) # check tid whether exist in request and update current_user self.check_tid(tid) logging.info("[UWEB] Defend 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 logging.exception("[UWEB] Invalid data format. body:%s, Exception: %s", self.request.body, e.args) self.write_ret(status) return try: res = [] tids = str_to_list(tids) tids = tids if tids else [self.current_user.tid, ] tids = [str(tid) for tid in tids] for tid in tids: r = DotDict(tid=tid, status=ErrorCode.SUCCESS) try: terminal = QueryHelper.get_available_terminal(tid, self.db) if not terminal: r.status = ErrorCode.LOGIN_AGAIN res.append(r) logging.error("[UWEB] The terminal with tid: %s does not exist, redirect to login.html", tid) continue update_mannual_status(self.db, self.redis, tid, data.mannual_status) logging.info("[UWEB] uid:%s, tid:%s set mannual status to %s successfully", self.current_user.uid, tid, data.mannual_status) except Exception as e: r.status = ErrorCode.FAILED logging.exception("[UWEB] uid:%s, tid:%s set mannual status to %s failed. Exception: %s", self.current_user.uid, tid, data.mannual_status, e.args) finally: res.append(r) # NOTE: wspush if status == ErrorCode.SUCCESS: for tid in tids: WSPushHelper.pushS7(tid, self.db, self.redis) self.write_ret(status, dict_=DotDict(res=res)) except Exception as e: logging.exception("[UWEB] uid:%s, tid:%s set mannual status to %s failed. Exception: %s", self.current_user.uid, self.current_user.tid, data.mannual_status, e.args) status = ErrorCode.SERVER_BUSY self.write_ret(status)
def get(self, name): """ """ ret = DotDict(status=ErrorCode.SUCCESS, message=None) corp = self.db.get("SELECT id" " FROM T_CORP" " WHERE name = %s" " LIMIT 1", name) if corp: ret.status = ErrorCode.EC_NAME_EXISTED ret.message = ErrorCode.ERROR_MESSAGE[ret.status] self.set_header(*self.JSON_HEADER) self.write(json_encode(ret))
def get(self, cnum): """ """ ret = DotDict(status=ErrorCode.SUCCESS, message=None) if cnum: car = self.db.get("SELECT id" " FROM T_CAR" " WHERE cnum = %s" " LIMIT 1", cnum) if car: ret.status = ErrorCode.CNUM_EXISTED ret.message = ErrorCode.ERROR_MESSAGE[ret.status] self.set_header(*self.JSON_HEADER) self.write(json_encode(ret))
def get(self, mobile): """ """ ret = DotDict(status=ErrorCode.SUCCESS, message=None) corp = self.db.get("SELECT id, tid, service_status" " FROM T_TERMINAL_INFO" " WHERE mobile = %s" " LIMIT 1", mobile) if corp: if corp.service_status == UWEB.SERVICE_STATUS.ON: ret.status = ErrorCode.TERMINAL_ORDERED ret.message = ErrorCode.ERROR_MESSAGE[ret.status] else: delete_terminal(corp.tid, self.db, self.redis, del_user=True) self.set_header(*self.JSON_HEADER) self.write(json_encode(ret))
def put(self): """Modify the remark of the offline-terminal. """ ret = DotDict(status=ErrorCode.SUCCESS, message=None) try: data = DotDict(json_decode(self.request.body)) id = data.id remark = data.remark self.db.execute("UPDATE T_TERMINAL_INFO" " SET remark = %s" " WHERE id = %s", remark, id) except Exception as e: ret.status = ErrorCode.FAILED ret.message = ErrorCode.ERROR_MESSAGE[ret.status] logging.exception("[UWEB] Modify remark failed. body: %s, Exception: %s", self.request.body, e.args) self.set_header(*self.JSON_HEADER) self.write(json_encode(ret))
def post(self): """Read excel, parse the data and provide to browser. """ try: upload_file = self.request.files['upload_file'][0] logging.info("[UWEB] batch import, corp_id: %s", self.current_user.cid) 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: status = ErrorCode.SUCCESS original_fname = upload_file['filename'] extension = os.path.splitext(original_fname)[1] if extension not in ['.xlsx', '.xls']: status = ErrorCode.ILLEGAL_EXCEL_FILE self.write_ret(status) return # write into tmp file fname = ''.join( random.choice(string.ascii_lowercase + string.digits) for x in range(6)) final_filename = fname + extension file_path = final_filename output_file = open(file_path, 'w') output_file.write(upload_file['body']) output_file.close() res = [] # read from tmp file wb = xlrd.open_workbook(file_path) for sheet in wb.sheets(): # NOTE: first line is title, and is ignored for j in range(1, sheet.nrows): row = sheet.row_values(j) tmobile = unicode(row[0]) tmobile = tmobile[0:11] umobile = '' if len(row) > 1: umobile = unicode(row[1]) umobile = umobile[0:11] biz_type = UWEB.BIZ_TYPE.YDWS if len(row) > 2: biz_type = unicode(row[2]) biz_type = biz_type[0:1] biz_type = biz_type if biz_type else UWEB.BIZ_TYPE.YDWS r = DotDict(tmobile=tmobile, umobile=umobile, biz_type=int(biz_type), status=UWEB.TERMINAL_STATUS.UNJH) if not check_phone(tmobile) or (umobile and not check_phone(umobile)): r.status = UWEB.TERMINAL_STATUS.INVALID res.append(r) continue # check tmobile is whitelist or not white_list = check_zs_phone(tmobile, self.db) if not white_list: logging.error( "[UWEB] mobile: %s is not whitelist.", tmobile) r['status'] = UWEB.TERMINAL_STATUS.MOBILE_NOT_ORDERED res.append(r) continue existed_terminal = self.db.get("SELECT id, tid, service_status" " FROM T_TERMINAL_INFO" " WHERE mobile = %s", tmobile) if existed_terminal: if existed_terminal.service_status == UWEB.SERVICE_STATUS.TO_BE_UNBIND: delete_terminal( existed_terminal.tid, self.db, self.redis) res.append(r) else: r.status = UWEB.TERMINAL_STATUS.EXISTED res.append(r) else: res.append(r) # remove tmp file os.remove(file_path) self.render("fileUpload.html", status=ErrorCode.SUCCESS, res=res) except Exception as e: logging.exception("[UWEB] Batch import failed. cid: %s, Exception: %s", self.current_user.cid, e.args) status = ErrorCode.ILLEGAL_FILE self.render("fileUpload.html", status=status, message=ErrorCode.ERROR_MESSAGE[status])
def post(self): status = ErrorCode.SUCCESS try: data = DotDict(json_decode(self.request.body)) logging.info("[UWEB] batch delete request: %s, corp_id: %s", data, self.current_user.cid) 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: status = ErrorCode.SUCCESS tids = list(data.tids) flag = data.flag res = [] for tid in tids: r = DotDict(tid=tid, status=ErrorCode.SUCCESS) terminal = QueryHelper.get_available_terminal(tid, self.db) if not terminal: r.status = ErrorCode.SUCCESS res.append(r) logging.error( "[UWEB] The terminal with tid: %s does not exist!", tid) continue key = get_del_data_key(tid) self.redis.set(key, flag) biz_type = QueryHelper.get_biz_type_by_tmobile( terminal.mobile, self.db) if int(biz_type) == UWEB.BIZ_TYPE.YDWQ: delete_terminal(tid, self.db, self.redis) res.append(r) continue elif int(biz_type) == UWEB.BIZ_TYPE.YDWS: if terminal.login != GATEWAY.TERMINAL_LOGIN.ONLINE: if terminal.mobile == tid: delete_terminal(tid, self.db, self.redis) else: r.status = self.send_jb_sms( terminal.mobile, terminal.owner_mobile, tid) res.append(r) continue # NOT: unbind. TODO: It should be re-factor some day. seq = str(int(time.time() * 1000))[-4:] args = DotDict(seq=seq, tid=tid) response = GFSenderHelper.forward( GFSenderHelper.URLS.UNBIND, args) response = json_decode(response) if response['success'] == ErrorCode.SUCCESS: logging.info("[UWEB] uid:%s, tid: %s, tmobile:%s GPRS unbind successfully", self.current_user.uid, tid, terminal.mobile) res.append(r) else: # unbind failed. clear sessionID for relogin, then unbind # it again sessionID_key = get_terminal_sessionID_key(tid) self.redis.delete(sessionID_key) logging.error('[UWEB] uid:%s, tid: %s, tmobile:%s GPRS unbind failed, message: %s, send JB sms...', self.current_user.uid, tid, terminal.mobile, ErrorCode.ERROR_MESSAGE[response['success']]) unbind_sms = SMSCode.SMS_UNBIND biz_type = QueryHelper.get_biz_type_by_tmobile( terminal.mobile, self.db) if biz_type != UWEB.BIZ_TYPE.YDWS: ret = DotDict(status=ErrorCode.SUCCESS) else: ret = SMSHelper.send_to_terminal( terminal.mobile, unbind_sms) ret = DotDict(json_decode(ret)) if ret.status == ErrorCode.SUCCESS: res.append(r) self.db.execute("UPDATE T_TERMINAL_INFO" " SET service_status = %s" " WHERE id = %s", UWEB.SERVICE_STATUS.TO_BE_UNBIND, terminal.id) logging.info("[UWEB] uid: %s, tid: %s, tmobile: %s SMS unbind successfully.", self.current_user.uid, tid, terminal.mobile) else: r.status = ErrorCode.FAILED res.append(r) logging.error("[UWEB] uid: %s, tid: %s, tmobile: %s SMS unbind failed. Message: %s", self.current_user.uid, tid, terminal.mobile, ErrorCode.ERROR_MESSAGE[status]) self.write_ret(status, dict_=DotDict(res=res)) except Exception as e: logging.exception("[UWEB] cid: %s batch delete failed. Exception: %s", self.current_user.cid, e.args) status = ErrorCode.ILLEGAL_FILE self.write_ret(status)
def request_realtime(self, query, callback=None): """ All realtime requests in REALTIME_VALID_INTERVAL will be considered as only one. If not, invoke gf and use handle_location of lbmphelper. """ location = QueryHelper.get_location_info(self.current_user.tid, self.db, self.redis) if location and location['name'] is None: location['name'] = '' ret = DotDict(status=ErrorCode.SUCCESS, message='', location=None) locations = [location,] locations = get_locations_with_clatlon(locations, self.db) location = locations[0] if (location and location.clatitude and location.clongitude): if location.has_key('id'): del location['id'] location['degree'] = float(location.degree) location['tid'] = self.current_user.tid ret.location = location if callback: callback(ret) return lat, lon = get_latlon_from_cellid(0,0,0,0, self.current_user.sim) clat, clon = get_clocation_from_ge([lat,],[lon,]) clat = int(clat[0]) if len(clat)>0 else 0 clon = int(clon[0]) if len(clon)>0 else 0 name = get_location_name(clat, clon, self.redis) location = DotDict(category = 1, # cellid dev_id = self.current_user.tid, lat = lat, lon = lon, cLat = clat, cLon = clon, alt = 0, gps_time = int(time.time()), type = 1, speed = 0.0, degree = 0.0, name = name, cellid = None, locate_error = 20) if clat and clon: ret.location = DotDict() ret.location.latitude = lat ret.location.longitude = lon ret.location.clongitude = clon ret.location.clatitude = clat ret.location.timestamp = int(time.time()) ret.location.name = name ret.location.speed = 0 ret.location.type = 1 ret.location.tid = self.current_user.tid ret.location.degree = 0.0 ret.location.locte_error = 20 insert_location(location, self.db, self.redis) logging.info("[UWEB] tid %s cellid query success", self.current_user.tid) else: ret.status = ErrorCode.LOCATION_CELLID_FAILED ret.message = ErrorCode.ERROR_MESSAGE[ret.status] logging.info("[UWEB] Do not find any location, and cellid query failed. tid: %s", self.current_user.tid) if callback: callback(ret)
def post(self): """Read excel. """ try: upload_file = self.request.files['upload_file'][0] logging.info("[UWEB] batch import.") except Exception as e: logging.info("[ADMIN] Invalid data format, Exception: %s", e.args) status = ErrorCode.ILLEGAL_DATA_FORMAT self.write_ret(status) return try: status = ErrorCode.SUCCESS original_fname = upload_file['filename'] extension = os.path.splitext(original_fname)[1] if extension not in ['.xlsx', '.xls']: status = ErrorCode.ILLEGAL_EXCEL_FILE self.write_ret(status) return # write into tmp file fname = ''.join(random.choice(string.ascii_lowercase + string.digits) for x in range(6)) final_filename= fname + extension file_path = final_filename output_file = open(file_path, 'w') output_file.write(upload_file['body']) output_file.close() res = [] # read from tmp file wb = xlrd.open_workbook(file_path) for sheet in wb.sheets(): #NOTE: first line is title, and it should be ignored for j in range(1, sheet.nrows): row = sheet.row_values(j) mobile = unicode(row[0]) mobile = mobile[0:11] r = DotDict(mobile=mobile, status=ErrorCode.SUCCESS) if not check_phone(mobile): r.status = UWEB.TERMINAL_STATUS.INVALID res.append(r) continue ajt = QueryHelper.get_ajt_whitelist_by_mobile(mobile, self.db) if ajt: r.status = UWEB.TERMINAL_STATUS.EXISTED else: pass res.append(r) # remove tmp file os.remove(file_path) self.render("whitelist/fileUpload.html", status=ErrorCode.SUCCESS, res=res) except Exception as e: logging.exception("[UWEB] Batch import failed. Exception: %s", e.args) status = ErrorCode.ILLEGAL_FILE self.render("whitelist/fileUpload.html", status=status, message=ErrorCode.ERROR_MESSAGE[status])