Esempio n. 1
0
 def threadHandler(self):
     """
      Thread handler
     """
     commandRoutingKey = conf.environment + '.mon.device.command.' + \
         self._protocolAlias
     commandQueue = Queue(
         commandRoutingKey,
         exchange = broker._exchanges['mon.device'],
         routing_key = commandRoutingKey
     )
     while True:
         try:
             with BrokerConnection(conf.amqpConnection) as conn:
                 conn.connect()
                 conn.ensure_connection()
                 log.debug('[%s] Connected to %s',
                     self._protocolAlias, conf.amqpConnection)
                 with conn.Consumer([commandQueue],
                         callbacks = [self.onCommand]):
                     while True:
                         conn.ensure_connection()
                         conn.drain_events()
                 conn.release()
         except Exception as E:
             log.exception('[%s] %s', self._protocolAlias, E)
             time.sleep(60) # sleep for 60 seconds after exception
Esempio n. 2
0
    def getCommands(self, handler):
        """
         Receives packets from the message broker.
         Runs until receives packet or timeout passes
         @param handler: AbstractHandler
         @return: received packets
        """
        content = None
        try:
            with BrokerConnection(conf.amqpConnection) as conn:
                routing_key = conf.environment + '.mon.device.command.' + \
                    str(handler.uid)
                log.debug('[%s] Check commands queue %s',
                    handler.handlerId, routing_key)
                command_queue = Queue(
                    routing_key,
                    exchange = self._exchanges['mon.device'],
                    routing_key = routing_key)

                conn.connect()
                with conn.Consumer([command_queue],
                    callbacks = [self.onCommand]):
                    conn.ensure_connection()
                    conn.drain_events(timeout = 1)
                    command = self.getCommand(handler)
                    if command:
                        log.debug('[%s] We got command: %s',
                            handler.handlerId, command)
                        content = command
                    else:
                        log.debug('[%s] No commands found', handler.handlerId)
                conn.release()
        except Exception as E:
            log.exception('[%s] %s', handler.handlerId, E)
        return content
Esempio n. 3
0
def movecar(packet):
    """
     Car moving function - it parse input file and send data to server
     @param packet: dict
     @return: string
    """
    while (True):
        try:
            for track in packet['track_files']:
                log.debug('OPEN: ' + track)
                # Read data from CSV file
                prevTime = 0 # Prev packet time
                lastSleep = 0 # Last sleep time
                parkingStartTime = 0 # Parking start time
                # Column headers
                reader = csv.DictReader(
                  open(track, newline='', encoding='utf-8'),
                  dialect="excel",
                  delimiter=';'
                )
                for row in reader:
                    #log.debug(row)
                    packetTime = row['time']
                    #log.debug('time = %s', packetTime)
                    dt = datetime.strptime(packetTime, fmtDate)
                    curTime = time.mktime(dt.timetuple())

                    #log.debug('Cur time ' + str(curTime))
                    #log.debug('Prev time ' + str(prevTime))

                    # If first packet, send it now
                    if (prevTime == 0):
                        sleep = 0
                    else:
                        # Count sleep time
                        sleep = curTime - prevTime
                        # Sleeping time can not be more than maxSleep
                        if (sleep > maxSleep):
                            # Check pervious sleep time
                            if (lastSleep >= maxSleep):
                                lastSleep = sleep
                                sleep = 0
                            else:
                                sleep = maxSleep
                                lastSleep = sleep
                        else:
                          lastSleep = sleep

                    # Save prev time
                    prevTime = curTime

                    #log.debug('speed ' + row['speed'] + ' state ' + row['state'])

                    # Check if parking begin
                    if (float(row['speed']) <= minParkingSpeed or \
                            int(row['state']) == 4):
                        # Save parking start time
                        if (parkingStartTime == 0):
                            parkingStartTime = curTime
                            #log.debug('Set parking start time to cur')

                        if ((curTime - parkingStartTime) + sleep >= maxParkingTime):
                            #sleep = 0;
                            #log.debug('>>>Set sleep to 0 (continue)')
                            continue

                        #log.debug('normal sleep')
                    # Check if moving begin
                    if (float(row['speed']) > minParkingSpeed \
                            and int(row['state']) != 4):
                        #log.debug('Reset parking start time')
                        parkingStartTime = 0

                    #log.debug('Sleep for ' + str(sleep))
                    # sleep
                    #time.sleep(sleep)
                    time.sleep(20)

                    # Sensors
                    sensors = {
                      'acc': row['sensor_acc'] \
                          if 'sensor_acc' in row else None,
                      'sos': row['sensor_sos'] \
                          if 'sensor_sos' in row else None,
                      'ext_battery_level': row['sensor_ext_battery_level'] \
                          if 'sensor_ext_battery_level' in row else None,
                      'ain0': row['ain0'] \
                          if 'ain0' in row else None,
                      'latitude': row['latitude'],
                      'longitude': row['longitude'],
                      'altitude': row['altitude'],
                      'speed': row['speed'],
                      'azimuth': row['azimuth'],
                      'sat_count': row['satellitescount'],
                      'hdop': row['hdop']
                    }

                    odometer = row['sensor_odometer'] \
                      if 'sensor_odometer' in row else None

                    if (odometer):
                        sensors['odometer'] = odometer

                    # Data
                    data = {
                        'uid': packet['uid'],
                        'time': datetime.utcnow().strftime(fmtDate),
                        'sensors': sensors,
                        # all of the fields above must be in sensors
                        'latitude': row['latitude'],
                        'longitude': row['longitude'],
                        'altitude': row['altitude'],
                        'speed': row['speed'],
                        'azimuth': row['azimuth'],
                        'satellitescount': row['satellitescount'],
                        'hdop': row['hdop']
                    }

                    # Send data by post request
                    sendData(data)

                # Sleep for 120-140 seconds between tracks
                interval = randint(120, 140)
                time.sleep(interval)
        except Exception as E:
            log.exception(E)
Esempio n. 4
0
    def send(self, packets, routing_key = None, exchangeName = None):
        """
         Sends packets to the message broker
         @param packets: list of dict
         @param routing_key: str
         @param exchangeName: str
        """
        exchange = self._exchanges['mon.device']
        if (exchangeName is not None) and (exchangeName in self._exchanges):
            exchange = self._exchanges[exchangeName]

        try:
            with BrokerConnection(conf.amqpConnection) as conn:
                log.debug('BROKER: Connect to %s' % conf.amqpConnection)
                conn.connect()
                connChannel = conn.channel()
                queuesConfig = {}

                # spike-nail START
                timePrev = None
                # spike-nail END

                reUid = re.compile('[\w-]+')
                for packet in packets:
                    uid = None if 'uid' not in packet else packet['uid']

                    # we should check uid for correctness
                    uidIsCorrect = uid is not None and reUid.match(uid)

                    timeCurr = None
                    if 'time' in packet:
                        timeCurr = packet['time']

                    if uidIsCorrect and uid in queuesConfig:
                        config = queuesConfig[uid]
                    else:
                        routingKey = routing_key
                        if not routing_key:
                            if not uidIsCorrect: continue # skip incorrect uid
                            routingKey = self.getRoutingKey(uid)

                        routingKey = conf.environment + '.' + routingKey
                        config = {
                            'routingKey': routingKey,
                            'queue': Queue(
                                routingKey,
                                exchange = exchange,
                                routing_key = routingKey
                            )
                        }
                        if uidIsCorrect:
                            queuesConfig[uid] = config

                    # spike-nail START
                    if timePrev and timeCurr:
                        fmtDate = "%Y-%m-%dT%H:%M:%S.%f"
                        t1 = datetime.strptime(timeCurr, fmtDate)
                        t2 = datetime.strptime(timePrev, fmtDate)
                        if t2 > t1:
                            tt = t1
                            t1 = t2
                            t2 = tt
                        if (t1 - t2).seconds < 10:
                            log.debug('Skip packet with time = ' + timeCurr)
                            continue # skip this packet if it's too close
                    # spike-nail END

                    with conn.Producer(channel = connChannel) as producer:
                        conn.ensure_connection()
                        producer.publish(
                            packet,
                            exchange = exchange,
                            routing_key = config['routingKey'],
                            declare = [config['queue']],
                            retry = True
                        )
                    if uid:
                        msg = 'Packet for "%s" is sent. ' % uid
                        if 'time' in packet:
                            msg += 'packet[\'time\'] = ' + timeCurr

                            # spike-nail START
                            timePrev = packet['time']
                            # spike-nail END

                        log.debug(msg)
                    else:
                        log.debug('Message is sent via message broker')
                conn.release()
        except Exception as E:
            log.exception('Error during packet send: %s', E)
        log.debug('BROKER: Disconnected')