コード例 #1
0
ファイル: listen.py プロジェクト: aimreant/aniwear_python
def listen_main(ENABLE_LOG,LOG_ADDRESS,MAXLINE,ENABLE_ENCRYPT,ENABLE_WARN_ON_SERVER,ENABLE_DB,Start_time):
    while True:  
        try: 
            if ENABLE_LOG:
                log_file = open(LOG_ADDRESS,"a") 

            connfd, connaddr = listenfd.accept() 
            Event_time=time.time() 
            outputstr="[SUCCEED]["+str(Event_time-Start_time)+"] Connected by "+connaddr[0]+":"+str(connaddr[1])+"\n"
            print outputstr
            if ENABLE_LOG:
                log_file.write(outputstr)

            buf = []  
            buf = connfd.recv(MAXLINE)  

            print "rec",buf


            if ENABLE_ENCRYPT:
                data="[FAIL]If you see this message,it means failllllllllll"
                Decrypt(buf,data,55)
                buf_hex=buf.encode('hex') #no use
                data_hex=data.encode('hex')
                buf=data_hex
             
                print  "after decrypt",buf

            if buf[:2]=="aa":
#if not, print "It doesn't start with aa"
                buf=buf[2:]
                #data=buf.decode('hex')
                #print "data after hex decoding..",data
                data = buf
                data_list=data.split('+')
                #[ 设备ID , 有效定位/无效定位 , 经度 , 纬度 , 电量 ]
                print "length of data_list",len(data_list)

                if True:#len(data_list)==5:
#if not , print "The length of string is not effective"
                    isuseful=data_list[1]  
                    #print data_list[0],data_list[1],data_list[2],data_list[3],data_list[4]     
                    if isuseful=='1':
                        #验证是否有效
#if not , print "The position is not effective"
                        dev_id=data_list[0]
                        #longitude= (int)(((float)(data_list[2].split(',')[0].strip()))*100000)
                        #latitude=  (int)(((float)(data_list[3].split(',')[0].strip()))*100000)
                        longitude = (int)(((float)(data_list[2].split()[0]))*100000)
                        latitude = (int)(((float)(data_list[3].split()[0]))*100000)
                        if data_list[2].split()[1]=='W':
                            longitude = longitude
                        # plus for East , minus for West
                        if data_list[3].split()[1]=='N':
                            latitude = -latitude
                        # plus for South , minus for North
                        # because Dong Xi( E first ),Nan Bei( S first )
                        battery=data_list[4]
                        
                        print data_list
 
                        sqlstr = "INSERT INTO trace VALUES" \
                            +"(NULL,"\
                            +str(int(dev_id,16))+","\
                            +str(longitude)+","\
                            +str(latitude)+",'"\
                            +time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))+"')"
                        print sqlstr
                        warning=0
                        if not(ENABLE_WARN_ON_SERVER):
                            Event_time=time.time()
                            outputstr="[WARNING] ["+str(Event_time-Start_time)+"]Please update the format to make warning on device come true"+"\n"
                            print outputstr
                            if ENABLE_LOG:
                                log_file.write(outputstr)

                            #when it doesn't update,warning will be 0 by default if ENABLE_WARN_ON_SERVER is not True
                            warning=0
                        else:
                            warning=checkwarning(int(dev_id,16),longitude,latitude,Start_time)


                        batterysqlstr="UPDATE `device` SET `battery`="\
                            +battery+",`warning`="\
                            +str(warning)+" WHERE `device_id`="\
                            +str(int(dev_id,16))
                        print batterysqlstr

                        if ENABLE_DB:
                            queryinsertsql(sqlstr,Start_time)
                            queryinsertsql(batterysqlstr,Start_time)

                        print buf.encode('hex')
                        print data.encode('hex')  

                        Event_time=time.time()
                        outputstr="[SUCCEED] ["+str(Event_time-Start_time)+"]The data is valid"+"\n"
                        print outputstr
                        if ENABLE_LOG:
                            log_file.write(outputstr)
                        connfd.send("Good Boy~")  
                        connfd.close()

                    else:
                        #if the position is not effective,close it.
                        Event_time=time.time()
                        outputstr="[FAILED] ["+str(Event_time-Start_time)+"]The position is not effective"+"\n"
                        print outputstr
                        if ENABLE_LOG:
                            log_file.write(outputstr)
                        connfd.send("Denied!")  
                        connfd.close()
                else:
                    #if the length of string is not effective
                    Event_time=time.time()
                    outputstr="[FAILED] ["+str(Event_time-Start_time)+"]The length of string is not effective"+"\n"
                    print outputstr
                    if ENABLE_LOG:
                        log_file.write(outputstr)
                    connfd.send("Denied!")  
                    connfd.close() 

            else:
                #if it doesn't start with 'aa',close it
                Event_time=time.time()
                outputstr="[FAILED] ["+str(Event_time-Start_time)+"]It doesn't start with aa"+"\n"
                print outputstr
                if ENABLE_LOG:
                    log_file.write(outputstr)
                connfd.send("Denied!")  
                connfd.close()

        except Exception as e:
            Event_time=time.time()
            outputstr="[LISTEN.PY_ERROR] ["+str(Event_time-Start_time)+"]"+e.message+"\n"
            print outputstr
            connfd.send("ERROR!")  
            connfd.close()
            if ENABLE_LOG:
                log_file.write(outputstr)
        if ENABLE_LOG:
            log_file.flush()
            log_file.close()