Exemplo n.º 1
0
Arquivo: base.py Projeto: jcsy521/ydws
    def send_jb_sms(self, tmobile, umobile, tid):
        """
        #NOTE: deprecated. It should never be invoked.
        """
        unbind_sms = SMSCode.SMS_UNBIND
        biz_type = QueryHelper.get_biz_type_by_tmobile(tmobile, self.db)
        if biz_type != UWEB.BIZ_TYPE.YDWS:
            ret = DotDict(status=ErrorCode.SUCCESS)
        else:
            ret = SMSHelper.send_to_terminal(tmobile, unbind_sms)
            ret = json_decode(ret)
        status = ret["status"]
        if status == ErrorCode.SUCCESS:
            self.db.execute(
                "UPDATE T_TERMINAL_INFO" "  SET service_status = %s" "  WHERE mobile = %s",
                UWEB.SERVICE_STATUS.TO_BE_UNBIND,
                tmobile,
            )
            terminals = self.db.query(
                "SELECT id FROM T_TERMINAL_INFO" "  WHERE owner_mobile = %s" "    AND service_status = %s",
                umobile,
                UWEB.SERVICE_STATUS.ON,
            )
            # clear user
            if len(terminals) == 0:
                self.db.execute("DELETE FROM T_USER" "  WHERE mobile = %s", umobile)

                lastinfo_key = get_lastinfo_key(umobile)
                lastinfo_time_key = get_lastinfo_time_key(umobile)
                ios_id_key = get_ios_id_key(umobile)
                ios_badge_key = get_ios_badge_key(umobile)
                keys = [lastinfo_key, lastinfo_time_key, ios_id_key, ios_badge_key]
                self.redis.delete(*keys)
                logging.info("[UWEB] Delete User: %s", umobile)
            logging.info("[UWEB] umobile: %s, tid: %s, tmobile: %s SMS unbind successfully.", umobile, tid, tmobile)
        else:
            logging.error(
                "[UWEB] umobile: %s, tid: %s, tmobile: %s SMS unbind failed. Message: %s",
                umobile,
                tid,
                tmobile,
                ErrorCode.ERROR_MESSAGE[status],
            )

        return status
Exemplo n.º 2
0
    def post(self):
        try:
            data = DotDict(json_decode(self.request.body))
        except Exception as e:
            status = ErrorCode.ILLEGAL_DATA_FORMAT
            logging.info("[UWEB] lastfinfo failed, message: %s, request: \n%s", 
                         ErrorCode.ERROR_MESSAGE[status], self.request.body)
            self.write_ret(status)
            return 

        try:
            cars_info = OrderedDict() 
            usable = 0 # nothing is modified, the cars_info is no use, use the data last time
            status = ErrorCode.SUCCESS
            
            terminals = self.db.query("SELECT tid FROM T_TERMINAL_INFO"
                                      "  WHERE service_status = %s"
                                      "    AND owner_mobile = %s"
                                      "    AND login_permit = 1"
                                      "    ORDER BY LOGIN DESC",
                                      UWEB.SERVICE_STATUS.ON, self.current_user.uid)
            tids = [terminal.tid for terminal in terminals]

            # 1 inquery data     
            for tid in tids:
                # 1: get terminal info 
                terminal = QueryHelper.get_terminal_info(tid, self.db, self.redis) 
                if terminal['login'] == GATEWAY.TERMINAL_LOGIN.SLEEP:
                    terminal['login'] = GATEWAY.TERMINAL_LOGIN.ONLINE

                # 2: get location 
                location = QueryHelper.get_location_info(tid, self.db, self.redis)
                if location and not (location.clatitude or location.clongitude):
                    location_key = get_location_key(str(tid))
                    locations = [location,] 
                    locations = get_locations_with_clatlon(locations, self.db) 
                    location = locations[0]
                    self.redis.setvalue(location_key, location, EVENTER.LOCATION_EXPIRY)

                if location and location['name'] is None:
                    location['name'] = ''

                if location and location['type'] == 1: # cellid 
                    location['locate_error'] = 500  # mile

                car_dct = {}
                car_info=dict(defend_status=terminal['defend_status'] if terminal['defend_status'] is not None else 1,
                              mannual_status=terminal['mannual_status'] if terminal['mannual_status'] is not None else 1,
                              fob_status=terminal['fob_status'] if terminal['fob_status'] is not None else 0,
                              timestamp=location['timestamp'] if location else 0,
                              speed=location.speed if location else 0,
                              # NOTE: degree's type is Decimal, float() it before json_encode
                              degree=float(location.degree) if location else 0.00,
                              locate_error=location.get('locate_error', 20) if location else 20,
                              name=location.name if location else '',
                              type=location.type if location else 1,
                              latitude=location['latitude'] if location else 0,
                              longitude=location['longitude'] if location else 0, 
                              clatitude=location['clatitude'] if location else 0,
                              clongitude=location['clongitude'] if location else 0, 
                              login=terminal['login'] if terminal['login'] is not None else 0,
                              bt_name=terminal.get('bt_name', '') if terminal else '',
                              bt_mac=terminal.get('bt_mac', '') if terminal else '',
                              gps=terminal['gps'] if terminal['gps'] is not None else 0,
                              gsm=terminal['gsm'] if terminal['gsm'] is not None else 0,
                              pbat=terminal['pbat'] if terminal['pbat'] is not None else 0,
                              mobile=terminal['mobile'],
                              owner_mobile=terminal['owner_mobile'],
                              alias=terminal['alias'],
                              #keys_num=terminal['keys_num'] if terminal['keys_num'] is not None else 0,
                              keys_num=0,
                              fob_list=terminal['fob_list'] if terminal['fob_list'] else [])

                car_dct[tid]=car_info
                cars_info.update(car_dct)
            
            lastinfo_key = get_lastinfo_key(self.current_user.uid)

            # BIG NOTE: here, compare lastinfo and cars_info as str
            lastinfo = self.redis.get(lastinfo_key)

            lastinfo_time_key = get_lastinfo_time_key(self.current_user.uid)
            lastinfo_time = self.redis.getvalue(lastinfo_time_key)

            if lastinfo == str(cars_info):  
                pass
            else:
                lastinfo_time = int(time.time())
                self.redis.setvalue(lastinfo_key, cars_info) 
                self.redis.setvalue(lastinfo_time_key, lastinfo_time)

            track_tid = data.get('track_tid', None)  # use cache
            track_info = []
            query_time = data.get('time', None) 
            track_time = data.get('track_time', query_time) 
            
            # 2 check whether provide usable data   
            if data.get('cache', None):  # use cache
                if query_time is not None: # use time
                    if int(query_time) < lastinfo_time:
                        usable = 1
                        if track_tid:
                            if track_tid not in tids:
                                logging.error("The terminal with tid: %s does not exist", track_tid)
                            else:
                                track_key = get_track_key(track_tid)
                                self.redis.setvalue(track_key, 1, UWEB.TRACK_INTERVAL)
                                if int(query_time) == -1:
                                    pass
                                elif lastinfo_time - query_time > 1: # every 30 second, ternimal generate a location 
                                    car_info = cars_info[track_tid]
                                    endtime = int(car_info['timestamp'])-1 if car_info['timestamp'] else int(lastinfo_time)-1 
                                    track  = self.db.query("SELECT id, latitude, longitude," 
                                                           "    clatitude, clongitude"
                                                           "  FROM T_LOCATION"
                                                           "  WHERE tid = %s"
                                                           "    AND NOT (latitude = 0 OR longitude = 0)"
                                                           "    AND (timestamp BETWEEN %s AND %s)"
                                                           "    AND type = 0"
                                                           "    ORDER BY timestamp",
                                                           track_tid, int(track_time)+1, endtime)
                                    track = get_locations_with_clatlon(track, self.db)
                                    for item in track:
                                        if item['clatitude'] and item['clongitude']:
                                            track_info.append(item['clatitude'])
                                            track_info.append(item['clongitude'])
                    else: 
                        cars_info = {}
                        usable = 0
                else: # no time
                    if lastinfo == cars_info: 
                        cars_info = {}
                        usable = 0
                    else: 
                        usable = 1
            else: 
                usable = 1

            self.write_ret(status, 
                           dict_=DotDict(cars_info=cars_info,
                                         track_info=track_info,
                                         usable=usable,
                                         lastinfo_time=lastinfo_time)) 
        except Exception as e:
            logging.exception("[UWEB] uid: %s, data: %s get lastinfo failed. Exception: %s", 
                              self.current_user.uid, data, e.args) 
            status = ErrorCode.SERVER_BUSY
            self.write_ret(status)