コード例 #1
0
ファイル: checkertask.py プロジェクト: jcsy521/ydws
    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.")
コード例 #2
0
ファイル: packettask.py プロジェクト: jcsy521/ydws
    def notify_report_by_sms(self, report, mobile):

        flag = self.check_timestamp(int(report['timestamp']))
        if not flag: 
            return

        name = QueryHelper.get_alias_by_tid(report.dev_id, self.redis, self.db)
        terminal_time = get_terminal_time(int(report['timestamp']))
        terminal_time = safe_unicode(terminal_time) 

        report_name = report.name
        if not report_name:
            if report.cLon and report.cLat:
                report_name = ErrorCode.ERROR_MESSAGE[ErrorCode.LOCATION_NAME_NONE]
            else:
                report_name = ErrorCode.ERROR_MESSAGE[ErrorCode.LOCATION_FAILED]
        sms = '' 
        sms_white = '' 
        if isinstance(report_name, str):
            report_name = report_name.decode('utf-8')
            report_name = unicode(report_name)

        if report.rName == EVENTER.RNAME.POWERLOW:
            if report.terminal_type == "1": # type: terminal
                if int(report.pbat) == 100:
                    pbat_message_key = get_pbat_message_key(report.dev_id)
                    if self.redis.exists(pbat_message_key) is False:
                        self.redis.setvalue(pbat_message_key, 1, time=24*60*60)        
                    else:
                        logging.info("[EVENTER] Don't send duplicate power full message to terminal:%s in 24 hours", report.dev_id)
                        return
                elif int(report.pbat) > 20 and int(report.pbat) < 100:
                    logging.info("[EVENTER] Terminal:%s reported power low pbat:%s between 20% and 100%, so skip it", report.dev_id, report.pbat)
                    return
                sms = self.handle_power_status(report, name, report_name, terminal_time)
            else: # type: fob
                sms = SMSCode.SMS_FOB_POWERLOW % (report.fobid, terminal_time)
        elif report.rName == EVENTER.RNAME.ILLEGALMOVE:
            if report_name in [ErrorCode.ERROR_MESSAGE[ErrorCode.LOCATION_NAME_NONE], ErrorCode.ERROR_MESSAGE[ErrorCode.LOCATION_FAILED]]:
                sms = SMSCode.SMS_ILLEGALMOVE_NOLOC % (name, terminal_time)
            else:
                sms = SMSCode.SMS_ILLEGALMOVE % (name, report_name, terminal_time)

            _date = datetime.datetime.fromtimestamp(int(report['timestamp']))
            _seconds = _date.hour * 60 * 60 + _date.minute * 60 + _date.second 
            if _seconds < 7 * 60 * 60 or _seconds > 19 * 60 * 60:  
                _resend_alarm = functools.partial(self.sms_to_user, report.dev_id, sms+u"重复提醒,如已收到,请忽略。", mobile)
                #NOTE: re-notify
                # 30 seconds later, send sms 1 time.
                task = RepeatedTimer(30, _resend_alarm, 1)  
                task.start()
        elif report.rName == EVENTER.RNAME.ILLEGALSHAKE:
            if report_name in [ErrorCode.ERROR_MESSAGE[ErrorCode.LOCATION_NAME_NONE], ErrorCode.ERROR_MESSAGE[ErrorCode.LOCATION_FAILED]]:
                sms = SMSCode.SMS_ILLEGALSHAKE_NOLOC % (name, terminal_time)
            else:
                sms = SMSCode.SMS_ILLEGALSHAKE % (name, report_name, terminal_time)

            #NOTE: re-notify
            _date = datetime.datetime.fromtimestamp(int(report['timestamp']))
            _seconds = _date.hour * 60 * 60 + _date.minute * 60 + _date.second 
            if _seconds < 7 * 60 * 60 or _seconds > 19 * 60 * 60:  
                _resend_alarm = functools.partial(self.sms_to_user, report.dev_id, sms+u"此条短信为重复提醒,请注意您的车辆状态。", mobile)
                # 30 seconds later, send sms 1 time.
                task = RepeatedTimer(30, _resend_alarm, 1)  
                task.start()
            
        elif report.rName == EVENTER.RNAME.EMERGENCY:
            whitelist = QueryHelper.get_white_list_by_tid(report.dev_id, self.db)      
            if whitelist:
                white_str = ','.join(white['mobile'] for white in whitelist) 

                if report_name in [ErrorCode.ERROR_MESSAGE[ErrorCode.LOCATION_NAME_NONE], ErrorCode.ERROR_MESSAGE[ErrorCode.LOCATION_FAILED]]:
                    sms = SMSCode.SMS_SOS_OWNER_NOLOC % (name, white_str, terminal_time)
                    sms_white = SMSCode.SMS_SOS_WHITE_NOLOC % (name, terminal_time) 
                else:
                    sms = SMSCode.SMS_SOS_OWNER % (name, white_str, report_name, terminal_time)
                    sms_white = SMSCode.SMS_SOS_WHITE % (name, report_name, terminal_time) 
            else:
                if report_name in [ErrorCode.ERROR_MESSAGE[ErrorCode.LOCATION_NAME_NONE], ErrorCode.ERROR_MESSAGE[ErrorCode.LOCATION_FAILED]]:
                    sms = SMSCode.SMS_SOS_NOLOC % (name, terminal_time)
                else:
                    sms = SMSCode.SMS_SOS % (name, report_name, terminal_time)
        elif report.rName == EVENTER.RNAME.POWERDOWN:
            if report_name in [ErrorCode.ERROR_MESSAGE[ErrorCode.LOCATION_NAME_NONE], ErrorCode.ERROR_MESSAGE[ErrorCode.LOCATION_FAILED]]:
                sms = SMSCode.SMS_POWERDOWN_NOLOC % (name, terminal_time)
            else:
                sms = SMSCode.SMS_POWERDOWN % (name, report_name, terminal_time)
        elif report.rName == EVENTER.RNAME.REGION_OUT:
            if report_name in [ErrorCode.ERROR_MESSAGE[ErrorCode.LOCATION_NAME_NONE], ErrorCode.ERROR_MESSAGE[ErrorCode.LOCATION_FAILED]]:
                sms = SMSCode.SMS_REGION_OUT_NOLOC % (name, safe_unicode(report['region']['region_name']), terminal_time)
            else:
                sms = SMSCode.SMS_REGION_OUT % (name,  safe_unicode(report['region']['region_name']), report_name, terminal_time)
        elif report.rName == EVENTER.RNAME.REGION_ENTER:
            if report_name in [ErrorCode.ERROR_MESSAGE[ErrorCode.LOCATION_NAME_NONE], ErrorCode.ERROR_MESSAGE[ErrorCode.LOCATION_FAILED]]:
                sms = SMSCode.SMS_REGION_ENTER_NOLOC % (name, safe_unicode(report['region']['region_name']), terminal_time)
            else:
                sms = SMSCode.SMS_REGION_ENTER % (name, safe_unicode(report['region']['region_name']), report_name, terminal_time)
        elif report.rName == EVENTER.RNAME.SPEED_LIMIT:
             sms_dct = dict(name=name,
                            report_name=report_name,
                            speed=int(report.get('speed',0)),
                            terminal_time=terminal_time)
             if report_name in [ErrorCode.ERROR_MESSAGE[ErrorCode.LOCATION_NAME_NONE], ErrorCode.ERROR_MESSAGE[ErrorCode.LOCATION_FAILED]]:
                 sms = SMSCode.SMS_SPEED_LIMIT_NOLOC % sms_dct 
             else:
                 sms = SMSCode.SMS_SPEED_LIMIT % sms_dct 
        else:
            pass

        #wap_url = 'http://api.map.baidu.com/staticimage?center=%s,%s%26width=800%26height=800%26zoom=17%26markers=%s,%s'
        #wap_url = wap_url % (report.lon/3600000.0, report.lat/3600000.0, report.lon/3600000.0, report.lat/3600000.0)
        #wap_url = 'http://api.map.baidu.com/staticimage?center=' +\
        #          str(report.cLon/3600000.0) + ',' + str(report.cLat/3600000.0) +\
        #          '&width=320&height=480&zoom=17&markers=' +\
        #          str(report.cLon/3600000.0) + ',' + str(report.cLat/3600000.0) 
        if report.cLon and report.cLat:
            clon = '%0.3f' % (report.cLon/3600000.0) 
            clat = '%0.3f' % (report.cLat/3600000.0)
            url = ConfHelper.UWEB_CONF.url_out + '/wapimg?clon=' + clon + '&clat=' + clat 
            tiny_id = URLHelper.get_tinyid(url)
            if tiny_id:
                base_url = ConfHelper.UWEB_CONF.url_out + UWebHelper.URLS.TINYURL
                tiny_url = base_url + '/' + tiny_id
                logging.info("[EVENTER] get tiny url successfully. tiny_url:%s", tiny_url)
                self.redis.setvalue(tiny_id, url, time=EVENTER.TINYURL_EXPIRY)
                sms += u"点击" + tiny_url + u" 查看定位器位置。" 
                if sms_white:
                    sms_white += u"点击" + tiny_url + u" 查看定位器位置。"
                    self.sms_to_whitelist(sms_white, whitelist)
            else:
                logging.info("[EVENTER] get tiny url failed.")
        else:
            logging.info("[EVENTER] location failed.")

        self.sms_to_user(report.dev_id, sms, mobile)
コード例 #3
0
ファイル: locationdesc.py プロジェクト: jcsy521/ydws
def handle_locationdesc(info, address, connection, channel, exchange, gw_binding, db, redis):
    """
    S10
    locationdesc packet

    0: success, then return locationdesc to terminal and record new terminal's address
    1: invalid SessionID 
    """
    try:
        head = info.head
        body = info.body
        dev_id = head.dev_id
        if len(body) == 6:
            body.append(20) 
            logging.info("[GW] old version is compatible, append locate_error")

        resend_key, resend_flag = get_resend_flag(redis, dev_id, head.timestamp, head.command) 

        go_ahead = False 
        args = DotDict(success=GATEWAY.RESPONSE_STATUS.SUCCESS,
                       locationdesc="",
                       ew="E",
                       lon=0.0,
                       ns="N",
                       lat=0.0)
        sessionID = QueryHelper.get_terminal_sessionID(dev_id, redis)
        if sessionID != head.sessionID:
            args.success = GATEWAY.RESPONSE_STATUS.INVALID_SESSIONID
            logging.error("[GW] Invalid sessionID, terminal: %s", head.dev_id)
        else:
            if resend_flag:
                logging.warn("[GW] Recv resend packet, head: %s, body: %s and drop it!",
                             info.head, info.body)
            else:
                go_ahead = True


        #NOTE: Check ydcw or ajt 
        ajt = QueryHelper.get_ajt_whitelist_by_mobile(head.dev_id, db) 
        if ajt: 
            url_out = ConfHelper.UWEB_CONF.ajt_url_out 
        else: 
            url_out = ConfHelper.UWEB_CONF.url_out

        if go_ahead:
            redis.setvalue(resend_key, True, GATEWAY.RESEND_EXPIRY)
            ldp = LocationDescParser(body, head)
            location = ldp.ret
            logging.info("[GW] T10 packet parsered:%s", location)
            if not  location.has_key('gps_time'):
                location['gps_time'] = int(time.time())
                logging.info("[GW] what's up? location:%s hasn't gps_time.", location)
            location['t'] = EVENTER.INFO_TYPE.POSITION
            if location['valid'] != GATEWAY.LOCATION_STATUS.SUCCESS:
                cellid = True
            else:
                cellid = False
            location = lbmphelper.handle_location(location, redis, cellid=cellid, db=db)
            location.name = location.get('name') if location.get('name') else ""
            location.name = safe_unicode(location.name)
            user = QueryHelper.get_user_by_tid(head.dev_id, db)
            tname = QueryHelper.get_alias_by_tid(head.dev_id, redis, db)
            dw_method = u'GPS' if not cellid else u'基站'
            if location.cLat and location.cLon:
                if user:
                    current_time = get_terminal_time(int(time.time()))
                    sms = SMSCode.SMS_DW_SUCCESS % (tname, dw_method,
                                                    location.name, 
                                                    safe_unicode(current_time)) 
                    url = url_out + '/wapimg?clon=' +\
                          str(location.cLon/3600000.0) + '&clat=' + str(location.cLat/3600000.0)
                    tiny_id = URLHelper.get_tinyid(url)
                    if tiny_id:
                        base_url = url_out + UWebHelper.URLS.TINYURL
                        tiny_url = base_url + '/' + tiny_id
                        logging.info("[GW] get tiny url successfully. tiny_url:%s", tiny_url)
                        redis.setvalue(tiny_id, url, time=EVENTER.TINYURL_EXPIRY)
                        sms += u"点击 " + tiny_url + u" 查看定位器位置。" 
                    else:
                        logging.info("[GW] get tiny url failed.")
                    SMSHelper.send(user.owner_mobile, sms)
            else:
                if user:
                    sms = SMSCode.SMS_DW_FAILED % (tname, dw_method)
                    SMSHelper.send(user.owner_mobile, sms)
            if not (location.lat and location.lon):
                args.success = GATEWAY.RESPONSE_STATUS.CELLID_FAILED
            else:
                insert_location(location, db, redis)

        lc = LocationDescRespComposer(args)
        request = DotDict(packet=lc.buf,
                          address=address,
                          dev_id=dev_id)
        update_terminal_status(redis, head.dev_id, address)
        append_gw_request(request, connection, channel, exchange, gw_binding)
    except:
        logging.exception("[GW] Handle locationdesc exception.")
        GWException().notify()