Exemple #1
0
class Application(tornado.web.Application):

    def __init__(self, debug=False):
        handlers = [
            # NOTE: the order is important, the first matched pattern is used!!!
            #(r"/", MainHandler),
            (r"/", WeixinHandler),
            (r"/bind", BindHandler),
            (r"/unbind", UnBindHandler),
            (r"/weixin", WeixinHandler),
            (r"/terminals", TerminalsHandler),
            (r"/event", EventHandler),

        ]

        settings = dict(
            template_path=os.path.join(os.path.dirname(__file__), "templates"),
            static_path=os.path.join(os.path.dirname(__file__), "static"),
            server_path=os.path.dirname(__file__),
            activity_path="/static/activity/pic/",
            avatar_path="/static/avatar/",
            cookie_secret="s8g1gVxKOiQoZptLRi2nSuXmiK2ThYJJBSHIUHnqoUw=",
            login_url="/",
            #debug=debug,
            app_name="ACBWEIXIN",
        )

        tornado.web.Application.__init__(self, handlers, **settings)
        self.queue = PriorityQueue()
        self.db = DBConnection().db
        self.redis = MyRedis()
        self.redis.setvalue('is_alived', ALIVED)
Exemple #2
0
def wash_location():
    db = DBConnection().db
    redis = MyRedis()
    
    #sql = "select id, tid,  mobile, owner_mobile, begintime, login_time from T_TERMINAL_INFO where tid = '36E2400480' "
    sql = "select id, tid,  mobile, owner_mobile, begintime, login_time from T_TERMINAL_INFO"
    #sql = "select id, tid,  mobile, owner_mobile, begintime, login_time from T_TERMINAL_INFO where login = 0"
    #print 'sql', sql
    terminals  = db.query(sql)
    print 'len ', len(terminals)
    count = 0
    cnt = 0
    for i, t in enumerate(terminals):
        tid = t.tid
        key = 'location:%s' % tid
        location = redis.get(key) 
        if not location:
        #if True:
            print 'tid', tid

            location = db.get("SELECT timestamp, MAX(timestamp) as maxtime"
                              "  FROM T_LOCATION"
                              "  WHERE tid = %s"
                              "    AND type = 0"
                              "    AND latitude != 0",
                              tid)
            
            if location:
                if location.timestamp != location.maxtime:
                    print 'timestamp != maxtime, tid', tid
                    location = db.get("SELECT * FROM T_LOCATION where timestamp = %s AND tid = %s limit 1", location.maxtime, tid)
                else:
                    continue
                mem_location = DotDict({'id':location.id,
                                        'latitude':location.latitude,
                                        'longitude':location.longitude,
                                        'type':location.type,
                                        'clatitude':location.clatitude,
                                        'clongitude':location.clongitude,
                                        'timestamp':location.timestamp,
                                        'name':location.name,
                                        'degree':float(location.degree),
                                        'speed':float(location.speed),
                                        'locate_error':int(location.locate_error)})

                redis.setvalue(key, mem_location, 86400*356*2)
                count = count +1 
                print 'handled tid:', tid
            else:
                cnt = cnt + 1    
    print 'total hanlded count:', count
    print 'total not hanlded count:', cnt
def modify_terminal():

    db = DBConnection().db
    redis = MyRedis()
    
    mobile='14778746907'
    #mobile='14778741845'
    #mobile='14778749172'
    #mobile='14778746786'
    #mobile='14778740942'
    #mobile='14778745985'
    #mobile='14778744628'
    #mobile='14778744861'
    #mobile='14778742261'
    #mobile='14778744473'
    #mobile='14778747112'
    #mobile='14778745219'
    #mobile='14778742290'
    #mobile='14778749137'
    #mobile='14778742587'
    #mobile='14778745073'
    #mobile='14778747467'
    #mobile='14778741340'
    #mobile='14778748943'
    #mobile='14778743681'
    sql = "select id, tid, mobile, owner_mobile, begintime, login_time from T_TERMINAL_INFO where mobile= %s"
    
    terminals  = db.query(sql, mobile)
    #print 'len ', len(terminals)
    count = 0
    cnt = 0
    no_loc = 0
    for i, t in enumerate(terminals):
        tid = t.tid
        mobile = t.mobile
        owner_mobile = t.owner_mobile
        terminal_info_key = get_terminal_info_key(tid) 
        terminal_info = redis.getvalue(terminal_info_key) 
        if terminal_info: 
            print 'umobile in redis:%s, umobile in db:%s' % (terminal_info['owner_mobile'], owner_mobile)
            if terminal_info['owner_mobile'] != owner_mobile:
                print 'mobile: %s, umobile in redis:%s, umobile in db:%s' % (mobile, terminal_info['owner_mobile'], owner_mobile)
                cnt = cnt + 1    
                terminal_info['owner_mobile'] = owner_mobile 
                redis.setvalue(terminal_info_key, terminal_info)
        else:
            pass

    print 'count:', count
    print 'cnt:', cnt
Exemple #4
0
    def __init__(self):
        self.tid = 'B123SIMULATOR' 
        self.tmobile = '15901258591'
        self.imsi = '888823615223538'
        self.imei = '888888900872209'

        self.login_mg = "[%s,,1,1.0.0,%s,T1,%s,15901258591,%s,%s,CLW,2,]"
        self.heartbeat_mg = "[%s,%s,1,1.0.0,%s,T2,23:9:95,1,0]"
        self.location_mg = "[%s,%s,1,1.0.0,%s,T3,1,E,113.25,N,22.564152,120.3,270.5,1,460:0:9876:3171,23:9:100,%s]"

        self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        host = ConfHelper.GW_SERVER_CONF.host
        port = int(ConfHelper.GW_SERVER_CONF.port)
        self.socket.connect((host, port))

        self.redis = MyRedis()

        self.alarm_key = 'simulator_alarm' 
        self.packet_key = 'simulator_packet' 
        self.packet_max_res_time = 5 # 5 seconds 
        self.alarm_interval = 60 * 5 # 5 minutes 

        self.mobiles = [13693675352, 
                        18310505991, 
                        13581731204]
        self.emails = ['*****@*****.**', 
                       '*****@*****.**', 
                       '*****@*****.**']
Exemple #5
0
class Terminal(object):
    
    def __init__(self):

        self.redis = MyRedis()

        self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

        #NOTE: connect the gateway
        # online
        self.socket.connect(('192.168.108.43', 10025))
        # test
        #self.socket.connect(('192.168.1.105', 10025))
        self.logging = self.initlog() 

    def __send__(self, packet):
        """Send packet to gateway and receive the response.
        """
        logging.info('send: %s', packet.strip())
        self.socket.send(packet)
        recv = self.socket.recv(1024)
        logging.info('recv: %s', recv)

    def read_mg(self):
        """Read package from logfile then send them to gateway.
        """
        logging.info('come into read mg')
        time.sleep(5)
        #NOTE: the gateway log has been pre-handled 
        logfile = '/home/ydcws/07.log'

        f = open(logfile)
        for line in f:
            lst = line.split(',')

            old_sessionid = lst[1]
            old_tid = lst[4]

            key = "sessionID:%s" % old_tid
            sessionid = self.redis.get(key)
            logging.info('key:%s, sessionid: %s', key, sessionid)
            if not sessionid:
                sessionid = ''

            line = line.replace(old_sessionid, sessionid)
            line = line.replace(" ", "")

            self.__send__(line)
            time.sleep(1)

    def initlog(self): 
        logger = logging.getLogger() 
        hdlr = logging.FileHandler("terminal.log") 
        formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') 
        hdlr.setFormatter(formatter) 
        logger.addHandler(hdlr) 
        logger.setLevel(logging.NOTSET) 
        return logger
Exemple #6
0
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
Exemple #7
0
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
Exemple #8
0
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
Exemple #9
0
def send_sms():
    # test 

    print '[wspush-checer] wspush failed'
    return

    redis = MyRedis()
    key = 'wspush_alarm'
    interval = 60*5 # 5 minutes
    alarm_flag = redis.get(key)
    if alarm_flag:
        print '[wspush-checer] do not send alarm again in %(interval)s(seconds)' % locals()  
        return

    url=ConfHelper.UWEB_CONF.url_out
    content = u'WSPush 服务异常,请及时查看。url:%s' % url
    for mobile in mobiles:
        send(content, mobile)
        redis.setvalue(key, True, interval)
        print '[wspush-checer] send alarm sms to %(mobile)s' % locals()
Exemple #10
0
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
Exemple #11
0
    def __init__(self):

        self.redis = MyRedis()

        self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

        #NOTE: connect the gateway
        # online
        self.socket.connect(('192.168.108.43', 10025))
        # test
        #self.socket.connect(('192.168.1.105', 10025))
        self.logging = self.initlog() 
Exemple #12
0
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
Exemple #13
0
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 
Exemple #14
0
    def __init__(self, tid, tmobile, umobile, imsi, imei):

        self.redis = MyRedis()

        self.tid = tid
        self.tmobile = tmobile
        self.umobile = umobile
        self.imsi = imsi
        self.imei = imei

        self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self.socket.connect(('192.168.1.105', 10025))
        self.logging = self.initlog() 
Exemple #15
0
class Terminal(object):
#TODO:
    
    def __init__(self, tid, tmobile, umobile, imsi, imei):

        self.redis = MyRedis()

        self.tid = tid
        self.tmobile = tmobile
        self.umobile = umobile
        self.imsi = imsi
        self.imei = imei

        self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self.socket.connect(('192.168.1.105', 10025))
        self.logging = self.initlog() 

    def __send__(self, packet):
        """Send packet to gateway and receive the response.
        """
        logging.info('send: %s', packet.strip())
        self.socket.send(packet)
        recv = self.socket.recv(1024)
        logging.info('recv: %s', recv)

    def initlog(self): 
        logger = logging.getLogger() 
        hdlr = logging.FileHandler("terminal.log") 
        formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') 
        hdlr.setFormatter(formatter) 
        logger.addHandler(hdlr) 
        logger.setLevel(logging.NOTSET) 
        return logger
       

    def test_T11(self):
        """

        """

        key = "sessionID:%s" % self.tid
        sessionid = self.redis.get(key)
        logging.info('key:%s, sessionid: %s', key, sessionid)
        if not sessionid:
            sessionid = ''

        t_time = int(time.time())
        mg = "[%s,%s,1,1.0.0,%s,T11,{E,113.252432,N,22.564152,133.3,270.5,1351492202}]"
        mg = mg % (t_time, sessionid, self.tid)

        self.__send__(mg)
Exemple #16
0
def wash_location():
    db = DBConnection().db
    redis = MyRedis()
    
    sql = "select id, tid,  mobile, owner_mobile, begintime, login_time from T_TERMINAL_INFO "
    #print 'sql', sql
    terminals = db.query(sql)
    #print 'len ', len(terminals)
    count = 0
    cnt = 0
    for i, t in enumerate(terminals):
        tid = t.tid
        key = 'location:%s' % tid
        location = redis.getvalue(key) 
        #print 'location', location, type(location) 
        if location and int(location.locate_error) > 500: 
            print '------1 tid', tid, location
            location['locate_error'] = 500
            print '------2 tid', tid, location
            #mem_location = DotDict({'id':location.id,
            #                        'latitude':location.latitude,
            #                        'longitude':location.longitude,
            #                        'type':location.type,
            #                        'clatitude':location.clatitude,
            #                        'clongitude':location.clongitude,
            #                        'timestamp':location.timestamp,
            #                        'name':location.name,
            #                        'degree':float(location.degree),
            #                        'speed':float(location.speed),
            #                        'locate_error':location['locate_error']})

            #redis.setvalue(key, mem_location, 86400*356*2)
            count = count +1 
            print 'handled tid:', tid
    print 'total hanlded count:', count
    print 'total not hanlded count:', cnt
Exemple #17
0
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
Exemple #18
0
    def __init__(self):
        """Provide some necessary info and create a socket.
        """
        self.tid = 'T123SIMULATOR' 
        self.tmobile = '13011292217'
        self.imsi = '18310505991'
        self.imei = '18310505991'

        self.login_mg = "[%s,,1,1.0.0,%s,T1,%s,18310505991,%s,%s,CLW,2,]"
        self.heartbeat_mg = "[%s,%s,1,1.0.0,%s,T2,23:9:95,1,0]"
        self.location_mg = "[%s,%s,1,1.0.0,%s,T3,1,E,113.25,N,22.564152,120.3,270.5,1,460:0:9876:3171,23:9:100,%s]"
        self.location_mg = "[%(time)s,%(sessionid)s,1,1.0.0,%(tid)s,T3,1,E,%(lon)s,N,%(lat)s,%(speed)s,%(degree)s,1,%(cellid)s,23:9:100,%(time)s]"

        self.db = DBConnection().db
        self.redis = MyRedis()

        self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        host = ConfHelper.GW_SERVER_CONF.host
        port = int(ConfHelper.GW_SERVER_CONF.port)
        self.socket.connect((host, port))
Exemple #19
0
    def __init__(self, tid, tmobile, umobile, imsi, imei):

        import redis
        self.redis = redis.Redis(host='192.168.108.43',port=6379)
        #self.redis = MyRedis()

        self.tid = tid
        self.tmobile = tmobile
        self.umobile = umobile
        self.imsi = imsi
        self.imei = imei

        #NOTE: JH
        self.login_mg = "[%s,,D,2.3.0,%s,T1,%s,%s,%s,%s,CLW,2,1,DBJ-3771728252,e3-94-9c-6f-53-9c]"
        #NOTE: normal login 
        self.login_mg = "[%s,,D,2.3.0,%s,T1,%s,%s,%s,%s,CLW,2,0,DBJ-3771728252,e3-94-9c-6f-53-9c]"

        self.redis = MyRedis()

        self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self.socket.connect(('192.168.108.43', 10025))
        self.logging = self.initlog() 
Exemple #20
0
 def __init__(self):
     self.db = DBConnection().db
     self.redis = MyRedis()
Exemple #21
0
class Test(object):
    def __init__(self):
        self.db = DBConnection().db
        self.redis = MyRedis()

    def test_session(self):
        tid = 'T123SIMULATOR'
        sessionID_key = get_terminal_sessionID_key(tid)
        old_sessionid = self.redis.get(sessionID_key)
        print  'session_id: %s' % old_sessionid
    
    def test_session(self):
        tid = 'T123SIMULATOR'
        sessionID_key = get_terminal_sessionID_key(tid)
        old_sessionid = self.redis.get(sessionID_key)
        print  'session_id: %s' % old_sessionid
    
    def test_mileage(self):
        tid = '361A000066'
        mileage_key = 'mileage:%s'  % tid
        #mileage = self.redis.getvalue(mileage_key)
    
        # for hmy
        #mileage = dict(lat=80547804,
        #               lon=408846024,
        #               dis=530, 
        #               gps_time=1400733148)
    
        # for lp 
        mileage = dict(lat=144089748,
                       lon=418702032,
                       dis=561, 
                       gps_time=1400732859)
        print  'mileage: %s' % mileage 
        #mileage['dis'] = 530
        self.redis.setvalue(mileage_key, mileage)
    
    def test_acc_status(self):
        #tid = '3489722632'
        tid = '1641486598'
        acc_status_info_key = 'acc_status_info:%s'  % tid
        #acc_status_info_key = "acc_status_info:T123SIMULATOR"
        acc_status_info = self.redis.getvalue(acc_status_info_key)
        print 'tid: %s, acc_status_info_key: %s, acc_status_info: %s' % (tid, 
               acc_status_info_key, acc_status_info)
    
    def test_alarm_info(self):
        tid = '1641486598'
        #tid = 'T123SIMULATOR'
        alarm_info_key = 'alarm_info:%s' % tid
        alarm_info = self.redis.getvalue(alarm_info_key)
        print 'tid: %s, alarm_info_key: %s, alarm_info: %s' % (tid, 
               alarm_info_key, alarm_info)

    def test_terminal_info(self):
        #tid = 'T123SIMULATOR'
        tid = '18701639494'
        terminal_info_key = "terminal_info:%s" % tid
        #self.redis.delete(terminal_info_key)
        terminal_info = self.redis.getvalue(terminal_info_key)
        print 'tid: %s, terminal_info_key: %s, terminal_info: %s' % (tid, 
               terminal_info_key, terminal_info)

    def test_location(self):
        #tid = 'T123SIMULATOR'
        tid = '369A400585'
        location_key = "location:%s" % tid
        #self.redis.delete(location_key)
        location = self.redis.getvalue(location_key)
        print 'tid: %s, location_key: %s, location: %s' % (tid, 
               location_key, location)

    def test_expire(self):
        key = 'jia_expire'
        timestamp = int(time.mktime(time.strptime("%s-%s-%s-%s-%s-%s"%(2014,11,21,16,40,0), "%Y-%m-%d-%H-%M-%S")))
        self.redis.set(key, '1')
        self.redis.expireat(key, timestamp)
Exemple #22
0
class Terminal(object):
    
    def __init__(self, tid, tmobile, umobile, imsi, imei):

        import redis
        self.redis = redis.Redis(host='192.168.108.43',port=6379)
        #self.redis = MyRedis()

        self.tid = tid
        self.tmobile = tmobile
        self.umobile = umobile
        self.imsi = imsi
        self.imei = imei

        #NOTE: JH
        self.login_mg = "[%s,,D,2.3.0,%s,T1,%s,%s,%s,%s,CLW,2,1,DBJ-3771728252,e3-94-9c-6f-53-9c]"
        #NOTE: normal login 
        self.login_mg = "[%s,,D,2.3.0,%s,T1,%s,%s,%s,%s,CLW,2,0,DBJ-3771728252,e3-94-9c-6f-53-9c]"

        self.redis = MyRedis()

        self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self.socket.connect(('192.168.108.43', 10025))
        self.logging = self.initlog() 

    def login(self):
        t_time = int(time.time())
        #t_time = 1355454637
        login_mg = self.login_mg % (t_time, self.tid, self.tmobile, self.umobile, self.imsi, self.imei)
        self.logging.info("Send login message:\n%s", login_mg) 
        self.socket.send(login_mg)
        time.sleep(1)
  
    def login_response(self, bp):
        info = bp.packet_info 
        self.sessionID = info[3]
        self.logging.info("login success!")
        #t_time = int(time.time())
        #mg = self.config_mg% (t_time, self.sessionID, self.tid)
        #self.socket.send(mg)

    def heartbeat(self):
        time.sleep(10)
        #time.sleep(30)
        while True:
            pbat = random.choice(range(30,100))
            heartbeat_mg = self.heartbeat_mg % (int(time.time()), self.sessionID, self.tid, pbat)
            self.logging.info("Send heartbeat:\n%s", heartbeat_mg)
            print "Send heartbeat:\n%s" %  heartbeat_mg
            self.socket.send(heartbeat_mg)
            time.sleep(10)
            #time.sleep(30)

    def restart(self):
        self.logging.info("Restart terminal...")

    def read_mg(self):
        """Read package from logfile then send them to gateway.
        """
        print 'come into read mg'
        time.sleep(2)
        #NOTE:  
        logfile = '/home/ydcws/3.log'

        count = 0
        f = open(logfile)
        for line in f:
            print 'line', line
            lst = line.split(',')

            old_sessionid = lst[1]
            old_tid = lst[4]

            key = "sessionID:%s" % old_tid
            sessionid = self.redis.get(key)
            if not sessionid:
                continue
            #sessionid = r.get(key)
            #print 'key', key,  'sessionid', sessionid
            if not sessionid:
                sessionid = ''

            line = line.replace(old_sessionid, sessionid)
            line = line.replace(" ", "")
            self.socket.send(line)
            count = count + 1
            if not count % 100:
                print count
            time.sleep(0.01)

    def start_each_thread(self):
        thread.start_new_thread(self.read_mg, ())

    def handle_recv(self, bp, io_loop):
        if bp.type == "S1":
            self.login_response(bp)
            self.start_each_thread()
        else:
            pass
    
    def tcpClient(self, io_loop):
        try:
            self.login()
            while True:
                infds, _, _ = select.select([self.socket], [], [], 1)
                if len(infds) > 0:
                    dat = self.socket.recv(1024)
                    self.logging.info("Recv:%s", dat)
                    print 'recv: ', dat
                    bp = BaseParser(dat)
                    self.handle_recv(bp, io_loop)
        except Exception as e:
            self.logging.exception("What's wrong, reconnect it.")
        finally:
            self.socket.close()

    def initlog(self): 
        logger = logging.getLogger() 
        hdlr = logging.FileHandler("terminal.log") 
        formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') 
        hdlr.setFormatter(formatter) 
        logger.addHandler(hdlr) 
        logger.setLevel(logging.NOTSET) 
        return logger


    def start_io_loop(self, io_loop):
        io_loop.start()
Exemple #23
0
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)
Exemple #24
0
def wash_location():
    db = DBConnection().db
    redis = MyRedis()
    
    #NOTE: all offline terminal
    #sql = "select id, tid,  mobile, owner_mobile, begintime, login_time from T_TERMINAL_INFO where login = 0"

    #NOTE: all offline terminals who has login before. 
    sql = "select id, tid,  mobile, owner_mobile, begintime, login_time from T_TERMINAL_INFO where login = 0 and login_time>0"

    #sql = "select id, tid,  mobile, owner_mobile, begintime, login_time from T_TERMINAL_INFO where tid = '354A000121'"
    #sql = "select id, tid,  mobile, owner_mobile, begintime, login_time from T_TERMINAL_INFO where mobile = '14778742419'"
    #sql = "select id, tid,  mobile, owner_mobile, begintime, login_time from T_TERMINAL_INFO where mobile = '14778749929'"
    #sql = "select id, tid,  mobile, owner_mobile, begintime, login_time from T_TERMINAL_INFO where login_time>0"
    #print 'sql', sql
    terminals  = db.query(sql)
    #print 'len ', len(terminals)
    count = 0
    cnt = 0
    no_loc = 0
    for i, t in enumerate(terminals):
        tid = t.tid
        key = 'location:%s' % tid
        location = redis.getvalue(key) 
        if not location: 
            time.sleep(2)
            no_loc = no_loc + 1
            print 'no location, tid', tid
            #continue
            
            ##return
            ##redis.delete(key)

            #NOTE: get latest point.
            location = db.get("SELECT timestamp, MAX(timestamp) as maxtime"
                              "  FROM T_LOCATION"
                              "  WHERE tid = %s"
                              "    AND type = 0"
                              "    AND latitude != 0",
                              tid)
            
            if location and location['timestamp']:
                location = db.get("SELECT * FROM T_LOCATION where timestamp = %s AND tid = %s AND latitude != 0 limit 1", location.maxtime, tid)
                mem_location = DotDict({'id':location.id,
                                        'latitude':location.latitude,
                                        'longitude':location.longitude,
                                        'type':location.type,
                                        'clatitude':location.clatitude,
                                        'clongitude':location.clongitude,
                                        'timestamp':location.timestamp,
                                        'name':location.name,
                                        'degree':float(location.degree),
                                        'speed':float(location.speed),
                                        'locate_error':int(location.locate_error)})

                redis.setvalue(key, mem_location, 86400*356*2)
                count = count +1 
                print 'handled tid:', tid
            else:
                cnt = cnt + 1    
    print '-------no_loc', no_loc
    print 'total hanlded count:', count
    print 'total not hanlded count:', cnt
Exemple #25
0
class SimulatorTerminalTest(object):
    """Provide location for TEST Account. 
    """
    
    def __init__(self):
        """Provide some necessary info and create a socket.
        """
        self.tid = 'T123SIMULATOR' 
        self.tmobile = '13011292217'
        self.imsi = '18310505991'
        self.imei = '18310505991'

        self.login_mg = "[%s,,1,1.0.0,%s,T1,%s,18310505991,%s,%s,CLW,2,]"
        self.heartbeat_mg = "[%s,%s,1,1.0.0,%s,T2,23:9:95,1,0]"
        self.location_mg = "[%s,%s,1,1.0.0,%s,T3,1,E,113.25,N,22.564152,120.3,270.5,1,460:0:9876:3171,23:9:100,%s]"
        self.location_mg = "[%(time)s,%(sessionid)s,1,1.0.0,%(tid)s,T3,1,E,%(lon)s,N,%(lat)s,%(speed)s,%(degree)s,1,%(cellid)s,23:9:100,%(time)s]"

        self.db = DBConnection().db
        self.redis = MyRedis()

        self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        host = ConfHelper.GW_SERVER_CONF.host
        port = int(ConfHelper.GW_SERVER_CONF.port)
        self.socket.connect((host, port))

    #####NOTE: Send packet. 
    def login(self):
        """Send packet T1.
        """
        t_time = int(time.time())
        login_mg = self.login_mg % (t_time, self.tid, self.tmobile, self.imsi, self.imei)
        logging.info("[CK] Send login message:\n%s", login_mg) 
        self.socket.send(login_mg)
        time.sleep(1)
  
    def heartbeat(self):
        """Send packet T2.
        """
        time.sleep(300)
        logging.info("[CK] Simulator terminal heartbeat thread start...")
        while True:
            heartbeat_mg = self.heartbeat_mg % (int(time.time()), self.sessionID, self.tid)
            logging.info("[CK] Send heartbeat:\n%s", heartbeat_mg)
            self.socket.send(heartbeat_mg)
            time.sleep(300)

    def upload_position(self):
        """Send packet T3.
        """
        time.sleep(10)
        logging.info("[CK] Simulator terminal upload position thread start...")
        locations = self.db.query("SELECT latitude, longitude, speed, degree, cellid"
                                  " FROM T_LOCATION_SIMULATOR"
                                  "  WHERE type = 0" 
                                  " GROUP BY timestamp" 
                                  " ORDER BY timestamp")

        count = len(locations)
        self.redis.set('count',count)

        n = self.redis.get('n')
        if n:
            n = int(n)
        else:
            n = 0 
            self.redis.set('n',n)

        while True:
            if n == count:
              n = 0
              self.redis.set('n',n)
            loc = locations[n]
            t_time = int(time.time())
            r = dict(time=t_time,
                     sessionid=self.sessionID,
                     tid=self.tid,
                     lon=float(loc['longitude'])/3600000,
                     lat=float(loc['latitude'])/3600000,
                     speed=loc['speed'],
                     degree=loc['degree'],
                     cellid=loc['cellid'] if loc['cellid'] else '460:0:9876:3171')
            msg = self.location_mg % r

            logging.info("[CK] Upload location:\n%s", msg)
            self.socket.send(msg)
            n = n+1 
            self.redis.set('n',n)
            time.sleep(300) # 5 min

    #NOTE: Handle the response
    def pase_packet(self, packet):
        """Make some simple split
        """
        packet_info = packet[1:][:-1].split(",")
        return packet_info

    def handle_recv(self, packet_info):
        """Receive respnses from Gateway and handle them.
        """
        type = packet_info[1]
        if type == GATEWAY.S_MESSAGE_TYPE.LOGIN:
            self.login_response(packet_info)
        elif type == GATEWAY.S_MESSAGE_TYPE.HEARTBEAT:
            self.heartbeat_response(packet_info)
        elif type == GATEWAY.S_MESSAGE_TYPE.POSITION:
            self.upload_response(packet_info)
        else:
            pass

    def heartbeat_response(self, packet_info):
        """Receve response S2.
        """
        if packet_info[2] == "0":
            logging.info("[CK] Heartbeat send success!")
        else:
            logging.info("[CK] Login faild, login agin...")
            time.sleep(5)
            self.login()

    def upload_response(self, packet_info):
        """Receve response S3.
        """
        if packet_info[2] == "0":
            logging.info("[CK] Location upload success!")
        else:
            logging.info("[CK] Login faild, login agin...")
            time.sleep(5)
            self.login()

    def login_response(self, packet_info):
        """Receve response S1.
        """
        if packet_info[2] == "0":
            self.sessionID = packet_info[3]
            self.start_each_thread()
            logging.info("[CK] Login success!")
        else:
            logging.info("[CK] Login faild, login agin...")
            time.sleep(5)
            self.login()


    def start_each_thread(self):
        thread.start_new_thread(self.heartbeat, ())
        thread.start_new_thread(self.upload_position, ())

    def udp_client(self):
        """Main method.

        workflow:
        login:
        while True:
            receive response from GATEWAY
            handle_recv
        """
        try:
            self.login()
            while True:
                infds, _, _ = select.select([self.socket], [], [], 1)
                if len(infds) > 0:
                    dat = self.socket.recv(1024)
                    logging.info("[CK] Recv data: %s", dat)
                    packet_info = self.pase_packet(dat)
                    self.handle_recv(packet_info)
        except KeyboardInterrupt:
            logging.error("Ctrl-C is pressed.")
        except Exception as e:
            logging.error("[CK] What's wrong, reconnect it.%s", e.args)
        finally:
            self.socket.close()
Exemple #26
0
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)
Exemple #27
0
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
Exemple #28
0
 def __init__(self, conf_file):
     ConfHelper.load(conf_file)
     self.redis = MyRedis() 
     self._socket = None
     self.get_agps_thread = None
Exemple #29
0
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()
Exemple #30
0
class SimulatorTerminal(object):
    
    def __init__(self):
        self.tid = 'B123SIMULATOR' 
        self.tmobile = '15901258591'
        self.imsi = '888823615223538'
        self.imei = '888888900872209'

        self.login_mg = "[%s,,1,1.0.0,%s,T1,%s,15901258591,%s,%s,CLW,2,]"
        self.heartbeat_mg = "[%s,%s,1,1.0.0,%s,T2,23:9:95,1,0]"
        self.location_mg = "[%s,%s,1,1.0.0,%s,T3,1,E,113.25,N,22.564152,120.3,270.5,1,460:0:9876:3171,23:9:100,%s]"

        self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        host = ConfHelper.GW_SERVER_CONF.host
        port = int(ConfHelper.GW_SERVER_CONF.port)
        self.socket.connect((host, port))

        self.redis = MyRedis()

        self.alarm_key = 'simulator_alarm' 
        self.packet_key = 'simulator_packet' 
        self.packet_max_res_time = 5 # 5 seconds 
        self.alarm_interval = 60 * 5 # 5 minutes 

        self.mobiles = [13693675352, 
                        18310505991, 
                        13581731204]
        self.emails = ['*****@*****.**', 
                       '*****@*****.**', 
                       '*****@*****.**']

    def alarm(self):
        """
        #NOTE: Send alarm message if need.
        """
        send_flag = self.redis.getvalue(self.alarm_key) 
        if (not send_flag): 
            content = SMSCode.SMS_GW_DELAY_REPORT % ConfHelper.UWEB_CONF.url_out 
            for mobile in self.mobiles: 
                SMSHelper.send(mobile, content) 
            for email in self.emails:
                EmailHelper.send(email, content) 
        logging.info("[CK] Notify S packet delay to administrator!") 
        self.redis.setvalue(self.alarm_key, True, self.alarm_interval)

    def login(self):
        """Send packet of login(T1) to GATEWAY.
        """
        t_time = int(time.time())
        login_mg = self.login_mg % (t_time, self.tid, self.tmobile, self.imsi, self.imei)
        logging.info("[CK] Send login message:\n%s", login_mg) 
        self.socket.send(login_mg)
        time.sleep(1)
  
    def login_response(self, packet_info):
        """Handle the response of login(T1).
        """
        if packet_info[2] == "0":
            self.sessionID = packet_info[3]
            self.start_each_thread()
            logging.info("[CK] Login success!")
        else:
            logging.info("[CK] Login faild, login agin...")
            time.sleep(5)
            self.login()

    def heartbeat_response(self, packet_info):
        """Handle the response of heartbeat(T2).
        """
        if packet_info[2] == "0":
            logging.info("[CK] Heartbeat send success!")
        else:
            logging.info("[CK] Login faild, login again.")
            time.sleep(5)
            self.login()

    def upload_response(self, packet_info):
        """Handle the response of position(T3).
        """

        #NOTE: check the interval of T and S
        s_time = int(time.time())
        t_time = self.redis.getvalue(self.packet_key) 
        if (not t_time) or (s_time - t_time) > self.packet_max_res_time:
            logging.warn("[CK] Location's respnse, s_time:%s, t_time:%s, max_res_time:%s ", 
                         s_time, t_time, self.packet_max_res_time)
            self.alarm()

        if packet_info[2] == "0":
            logging.info("[CK] Location upload success!")
            
        else:
            logging.info("[CK] Location upload faild, login again.")
            time.sleep(5)
            self.login()

    def heartbeat(self):
        """Send packet of heartbeat(T2) to GATEWAY.
        """
        time.sleep(10)
        logging.info("[CK] Simulator terminal heartbeat thread start.")
        while True:
            heartbeat_mg = self.heartbeat_mg % (int(time.time()), self.sessionID, self.tid)
            logging.info("[CK] Send heartbeat:\n%s", heartbeat_mg)
            self.socket.send(heartbeat_mg)
            time.sleep(300)

    def upload_position(self):
        """Send packet of position(T3) to GATEWAY.
        """
        time.sleep(60)
        logging.info("[CK] Simulator terminal upload position thread start...")
        while True:
            t_time = int(time.time())
            msg = self.location_mg % (t_time, self.sessionID, self.tid, t_time)
            logging.info("[CK] Upload location:\n%s", msg)

            #NOTE: keep the send action. the interval is 10 times the alarm_interval
            self.redis.setvalue(self.packet_key, t_time, self.alarm_interval*10)

            self.socket.send(msg)
            time.sleep(300)
 
    def start_each_thread(self):
        """Start the thread to send hearbeat(T2), position(T3) to GATEWAY.   
        """
        thread.start_new_thread(self.heartbeat, ())
        thread.start_new_thread(self.upload_position, ())

    def handle_recv(self, packet_info):
        """Get the response, and dispatch them to diffent method according
        to the type of Sx.
        """
        type = packet_info[1]
        if type == GATEWAY.S_MESSAGE_TYPE.LOGIN: # S1
            self.login_response(packet_info)
        elif type == GATEWAY.S_MESSAGE_TYPE.HEARTBEAT: #S2
            self.heartbeat_response(packet_info)
        elif type == GATEWAY.S_MESSAGE_TYPE.POSITION: # S3
            self.upload_response(packet_info)
        else:
            pass
    
    def udp_client(self):
        """Main method.
        
        workflow:
        login
        while True:
           receive response from GATEWAY
           handle response 
               if login success:
                   start hearet_beat
                   start upload_location
               else:
                   handle the response 
        """
        try:
            self.login()
            while True:
                infds, _, _ = select.select([self.socket], [], [], 1)
                if len(infds) > 0:
                    dat = self.socket.recv(1024)
                    logging.info("[CK] Recv data: %s", dat)
                    packet_info = self.pase_packet(dat)
                    self.handle_recv(packet_info)
        except KeyboardInterrupt:
            logging.error("Ctrl-C is pressed.")
        except Exception as e:
            logging.error("[CK] What's wrong, reconnect it.%s", e.args)
        finally:
            self.socket.close()

    def pase_packet(self, packet):
        """Parse the packet.
        """
        packet_info = packet[1:][:-1].split(",")
        return packet_info