def receiveCommand(ser):
    i = 0
    while True:
        #print 'receive:'
        data = ser.read(300)
        if (len(data) > 0):
            commandAnalyzer = CommandAnalyzer(data)
            if (commandAnalyzer.getCommandName() == 'gcounters'):
                #print 'Got data'
                print 'send reset:', ser.write(PCNCommand.RESET)
                #print	'Got data:'
                #commandAnalyzer.printCommandInfo()
                passengerOn = commandAnalyzer.getNumberPassengerOn()
                passengerOff = commandAnalyzer.getNumberPassengerOff()
                tmpSt = "passenger on: %s , passenger off: %s" % (
                    str(passengerOn), str(passengerOff))
                print tmpSt
                if (passengerOn != 0):
                    #update data to local database
                    writePassengerDataToDB('nan', 'nan', 1, str(passengerOn))

                if (passengerOff != 0):
                    #update data to local database
                    writePassengerDataToDB('nan', 'nan', 0, str(passengerOff))

        sleep(0.5)
def receiveCommand(ser, latitude, longitude):

    logger.debug("__Init__ receiving PCN command function")
    i = 0
    while True:
        try:

            #update stop latitude and longitude
            if (globalSpeed < 1):
                stopLatitude = globalLatitude
                stopLongitude = globalLongitude

            #print 'receive:'
            data = ser.read(300)
            if (len(data) > 0):
                commandAnalyzer = CommandAnalyzer(data)
                if (commandAnalyzer.getCommandName() == 'gcounters'):
                    #commandAnalyzer.printCommandInfo()
                    #busClient.service.insertpassengerMAC('unishuttle04', latitude,longitude,str(datetime.datetime.now()),1,2)

                    passengerOn = commandAnalyzer.getNumberPassengerOn()
                    passengerOff = commandAnalyzer.getNumberPassengerOff()
                    if (passengerOn != 0 or passengerOff != 0):
                        #tmpSt = "insert into passengerMAC(host,source,destination,datetime,on,off,lat,lon,speed) values (%s, %s, %s, %s, %s, %s, %s, %s, %s)" % (hostname,commandAnalyzer.getSource(),commandAnalyzer.getDestination(), str(datetime.datetime.now()), str(passengerOn), str(passengerOff), str(stopLatitude), str(stopLongitude),str(globalSpeed))
                        #	print tmpSt
                        #logger.debug("__PCN receive data__: %s", tmpSt)

                        if (passengerOn != 0):
                            busClient.service.insertpassengerMAC(
                                hostname, stopLongitude, stopLatitude,
                                str(datetime.datetime.now()), 1,
                                str(passengerOn))

                        if (passengerOff != 0):
                            busClient.service.insertpassengerMAC(
                                hostname, stopLongitude, stopLatitude,
                                str(datetime.datetime.now()), 0,
                                str(passengerOff))

                        ser.write(PCNCommand.RESET)

        except:
            print 'receive data from pcn failed'
            logger.debug(
                "__PCN receive data__: receive data from pcn failed or cannot send data to server"
            )

        sleep(0.5)
Exemple #3
0
def receiveCommand(ser):
    i = 0
    while True:
        #print 'receive:'
        data = ser.read(300)
        if (len(data) > 0):
            commandAnalyzer = CommandAnalyzer(data)
            if (commandAnalyzer.getCommandName() == 'gcounters'):
                print 'send reset:', ser.write(PCNCommand.RESET)
                print 'Got data:'
                stopActivities.add(data)
                if (i % 10 == 0):
                    if (stopActivities.length != 0):
                        csvPassenger = CSVPassenger()
                        csvPassenger.write(stopActivities.getStopActivities())
                        stopActivities.clear()
                        print 'write to file:', i

                    print 'reach:', i

                i = i + 1

        sleep(0.5)
    def add(self, command):
        commandAnalyzer = CommandAnalyzer(command)
        commandName = commandAnalyzer.getCommandName()

        if (commandName == 'gcounters'):
            commandAnalyzer.printCommandInfo()
            passengerOn = commandAnalyzer.getNumberPassengerOn()
            passengerOff = commandAnalyzer.getNumberPassengerOff()

            if (passengerOn != 0 or passengerOff != 0):
                stopActivity = StopActivity()
                stopActivity.setSource(commandAnalyzer.getSource())
                stopActivity.setDestination(commandAnalyzer.getDestination())
                stopActivity.setStatus('')
                stopActivity.setDateTime(str(datetime.datetime.now()))
                stopActivity.setPassengerOn(passengerOn)
                stopActivity.setPassengerOff(passengerOff)
                self.stopActivities.append(stopActivity)

                self.length += 1
Exemple #5
0
def receiveCommand(ser,latitude,longitude):
	i = 0
	while True:
		try:
			#print 'receive:'
			data = ser.read(300)
			if(len(data)>0):
				commandAnalyzer = CommandAnalyzer(data)
				if (commandAnalyzer.getCommandName() == 'gcounters'):
					commandAnalyzer.printCommandInfo()
					#busClient.service.insertpassengerMAC('unishuttle04', latitude,longitude,str(datetime.datetime.now()),1,2)
					passengerOn = commandAnalyzer.getNumberPassengerOn()
					passengerOff = commandAnalyzer.getNumberPassengerOff()
					if(passengerOn !=0 or passengerOff !=0):
						print 'insert values(source,destination,datetime,on,off,lat,lon)',commandAnalyzer.getSource(),commandAnalyzer.getDestination(), str(datetime.datetime.now()), passengerOn, passengerOff, latitude, longitude
						logger.debug("__PCN receive data__: normal receive data")
						#busClient.service.insertpassengerMAC('unishuttle04', latitude,longitude,str(datetime.datetime.now()),1,passengerOn)
						#busClient.service.insertpassengerMAC('unishuttle04', latitude,longitude,str(datetime.datetime.now()),0,passengerOff)
						print 'send reset:',ser.write(PCNCommand.RESET)
		except:
			print 'receive data from pcn failed'
			logger.debug("__PCN receive data__: receive data from pcn failed")

		sleep(1)
from ctypes import string_at
from sys import getsizeof
from binascii import hexlify

from pcncommand import PCNCommand
from commandanalyzer import CommandAnalyzer
from stopactivity import StopActivities
from csvpassenger import CSVPassenger

ser = 'Serial port'

command = PCNCommand.GET_COUNTERS
#pcnCommand = PCNCommand()
#command = pcnCommand.getCommand('gcounters')

commandAnalyzer = CommandAnalyzer(command)
commandAnalyzer.printCommandInfo()

stopActivities = StopActivities()


#print 'len',len(stopActivities.getStopActivities())
#=====================================================
#send command to pcn
#=====================================================
def sendCommand(ser):
    while True:
        #print 'send:',ser
        sleep(0.5)