Пример #1
0
    def processProtocolPacket(self, protocolPacket):
        """
         Process ATrack packet.
         @type protocolPacket: packets.Packet
         @param protocolPacket: ATrack protocol packet
        """
        if isinstance(protocolPacket, packets.PacketKeepAlive):
            self.uid = protocolPacket.unitId
            log.debug("Keep alive packet received. UnitId = %s" % self.uid)

        # sends the acknowledgment
        self.sendAcknowledgement(protocolPacket)

        if not isinstance(protocolPacket, packets.PacketData):
            return

        if not self.uid:
            self.uid = protocolPacket.unitId
            log.debug("Data packet received. UnitId = %s" % self.uid)

        observerPackets = self.translate(protocolPacket)
        if len(observerPackets) == 0:
            log.info("Location packet not found. Exiting...")
            return

        log.info(observerPackets)
        self.store(observerPackets)
Пример #2
0
 def clear(self):
     """
      Clear storage.
      @warning By calling this function you delete folder at conf.pathStorage
     """
     log.debug('Storage::clear()')
     shutil.rmtree(conf.pathStorage)
Пример #3
0
    def processCommand(self, command):
        """
         Processing AMQP command
         @param command: command
        """
        if not command:
            log.error("[%s] Empty command description!", self.handlerId)
            return

        if (not self.uid) and ('uid' in command):
            self.uid = command['uid']

        log.debug("[%s] Processing AMQP command: %s ", self.handlerId, command)
        try:
            if not self._commandsFactory:
                raise Exception("_commandsFactory is not defined!")
            commandName = command["command"]
            commandInstance = self._commandsFactory.getInstance(command)
            if commandInstance:
                log.debug("[%s] Command class is %s",
                    self.handlerId, commandInstance.__class__)
                self.sendCommand(commandInstance, command)
            else:
                broker.sendAmqpError(self, "Command is not supported")
                log.error("[%s] No command with name %s",
                    self.handlerId, commandName)
        except Exception as E:
            log.error("[%s] Send command error is %s", self.handlerId, E)
Пример #4
0
    def processData(self, data):
        """
         Processing of data from socket / storage.
         Might be overridden in child classes
         @param data: Data from socket
        """
        if self._packetsFactory:
            try:
                if self._buffer is None:
                    self._buffer = b''
                self._buffer += data
                protocolPackets = (
                    self._packetsFactory.getPacketsFromBuffer(self._buffer)
                )
                for protocolPacket in protocolPackets:
                    self.processProtocolPacket(protocolPacket)
                self._buffer = None
            except NeedMoreDataException as E:
                log.info('[%s] Need more data...', self.handlerId)
                return
            except Exception as E:
                log.error("[%s] processData error: %s", self.handlerId, E)

        log.debug('[%s] Checking handler commands', self.handlerId)
        if not self.needProcessCommands():
            return self
        log.debug('[%s] Ok we can process commands!', self.handlerId)

        self.processCommands()
        return self
Пример #5
0
    def processDataBuffer(self, buffer, format = 'report'):
        """
         Processing of data from socket / storage.
         @param buffer: Data from socket
         @param format: Source of data format ('report' or 'sms')
        """
        # let's work with text data
        data = buffer.decode()
        rc = self.re_compiled[format]
        position = 0

        log.debug("Data received:\n%s", data)
        m = rc.search(data, position)
        if not m:
            self.processError(data)

        while m:
            # - OK. we found it, let's see for checksum
            log.debug("Raw match found.")
            data_device = m.groupdict()
            packetObserver = self.translate(data_device)
            log.info(packetObserver)
            self.uid = packetObserver['uid']
            self.store([packetObserver])
            position += len(m.group(0))
            m = rc.search(data, position)
Пример #6
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.error('[%s] %s', self._protocolAlias, E)
             time.sleep(60) # sleep for 60 seconds after exception
Пример #7
0
 def initialization(self):
     """
      Initialization of the handler
      @return:
     """
     log.debug('[%s] initialization', self.handlerId)
     broker.handlerInitialize(self)
Пример #8
0
 def threadReceiverOnMessage(self, body, message):
     """
      Executes when there is a packet in signal queue
      @param body: amqp message body
      @param message: message instance
     """
     threadName = 'ReceiverThread'
     uid = 'unknown uid [!]'
     try:
         if isinstance(body, str):
             body = json.loads(body)
         if 'uid' in body:
             uid = body['uid']
         if 'time' not in body:
             raise Exception('Incorrect packet structure')
         if uid not in self._messages:
             self._messages[uid] = deque()
         # store message to the queue
         self._messages[uid].append({
             "message": message,
             "body": body
         })
         log.debug('%s::Packet %s added %s', threadName, body['time'], uid)
         self.sendMessage(uid, body)
     except Exception as E:
         log.error('%s::%s', threadName, E)
         message.ack()
Пример #9
0
 def initAmqpThread(cls, protocol):
     """
      AMQP thread initialization
     """
     log.debug('%s::initAmqpThread() / %s', cls, protocol)
     # start message broker thread for receiving sms commands
     from lib.broker import MessageBrokerThread
     MessageBrokerThread(cls, protocol)
Пример #10
0
 def finalization(self):
     """
      Finalization of the handler.
      Free allocated resources.
      @return:
     """
     log.debug('[%s] finalization', self.handlerId)
     broker.handlerFinalize(self)
Пример #11
0
 def needProcessCommands(self):
     """
      Returns false if we can not process commands
      @return: boolean
     """
     log.debug('[%s] needProcessCommands??? %s %s',
         self.handlerId, self.uid, self.isHeadPacket)
     return self.uid and not self.isHeadPacket
Пример #12
0
 def run(self):
     """
      Start of main cycle of the application
     """
     log.debug('Starter::run()')
     try:
         Server(conf.port).run()
     except Exception as E:
         log.critical(E)
Пример #13
0
 def initReceiverThread(self):
     """
      Initialize receiver thread
      @return:
     """
     log.debug('%s::initReceiverThread()', self.__class__)
     # starting request signal thread
     self._threadReceive = Thread(target = self.threadReceiveHandler)
     self._threadReceive.start()
Пример #14
0
 def onCommand(self, body, message):
     """
      Callback on AMQP message receiving
      @param body: dict Message body
      @param message: AMQP message object
     """
     log.debug("Got AMQP message %s" % body)
     self.storeCommand(body)
     message.ack()
Пример #15
0
 def __init__(self, port = 30003):
     """
      Server class constructor
      @param port: Listening port. Optional, default is 30003.
     """
     log.debug("Server::__init__(%s)", port)
     self.host = ""
     self.port = port
     self.server = ThreadingServer((self.host, self.port), ClientThread)
Пример #16
0
 def run(self):
     """
      Method wich starts TCP-server
     """
     log.debug("Server::run()")
     self.server_thread = Thread(target = self.server.serve_forever)
     self.server_thread.setDaemon(False)
     self.server_thread.start()
     log.info("Server is started on port %s", self.port)
Пример #17
0
 def __init__(self):
     """
      Storage constructor
     """
     log.debug('%s::__init__()', self.__class__)
     try:
         os.makedirs(self.__path, 0o777, True)
     except:
         pass
Пример #18
0
 def handle(self):
     try:
         if HandlerClass:
             log.debug('Protocol handler: %s', HandlerClass.__doc__)
             self.__handler = HandlerClass(pipe.Manager(), self)
             self.__handler.dispatch()
         else:
             log.error('No protocol handlers found!')
     except Exception as E:
         log.error("Dispatch error: %s", traceback.format_exc())
Пример #19
0
 def __init__(self):
     """
      Constructor. Creates local storage to be used by protocol handlers
     """
     log.debug('%s::__init__()', self.__class__)
     self._commands = {}
     self._exchanges = {
         'mon.device': Exchange('mon.device', 'topic', durable = True),
         'n.work': Exchange('n.work', 'topic', durable = True)
     }
Пример #20
0
 def processData(self, data):
     """
      Processing of data from socket / storage.
      @param data: Data from socket
      @param packnum: Number of socket packet (defaults to 0)
      @return: self
     """
     try:
         log.debug(data.decode())
     except Exception as E:
         log.debug(data)
Пример #21
0
 def dispatch(self):
     """
       Data processing method (after validation) from the device:
       clientThread thread of the socket;
     """
     log.debug('[%s] dispatch()', self.handlerId)
     buffer = self.recv()
     while len(buffer) > 0:
         self.processData(buffer)
         buffer = self.recv()
     log.debug('[%s] dispatch() - EXIT (empty buffer?)', self.handlerId)
Пример #22
0
 def __init__(self, manager, uid):
     """
      Receiver initialization
      @param manager:
      @param uid:
     """
     log.debug('%s::__init__(%s)', self.__class__, uid)
     self._manager = manager
     self._uid = uid
     self._queue = QUEUE_PREFIX + '.create.' + uid
     log.debug('Queue: %s', self._queue)
Пример #23
0
 def run():
     """
      Start of main cycle of the application
     """
     log.debug('Starter::run()')
     try:
         # check specified port
         if not conf.port:
             raise Exception("Please specify port number! (use --port)")
         Server(conf.port).run()
     except Exception as E:
         log.critical(E)
Пример #24
0
 def run(self):
     """
      Method which starts TCP-server
     """
     log.debug("Server::run()")
     if not HandlerClass:
         log.critical('No protocol handlers specified! (use --handler)')
         return
     server_thread = Thread(target=self.server.serve_forever)
     server_thread.setDaemon(False)
     server_thread.start()
     log.info("Server is started on port %s", self.port)
Пример #25
0
 def saveByUid(self, uid, data):
     """
      Save protocol data into storage
      @param uid: Device unqiue identifier
      @param data: Protocol data
     """
     log.debug('Storage::saveByUid(). %s', uid)
     try:
         with open(self.getStorageFileName(uid), 'ab') as f:
             f.write(data)
     except Exception as E:
         log.error(E)
Пример #26
0
    def sendCommand(self, command, initialParameters = None):
        """
         Sends command to the handler
         @param command: AbstractCommand instance
         @param initialParameters: dict Initial command parameters
        """
        if not initialParameters:
            raise Exception("Empty initial parameters!")

        config = {}
        if "config" in initialParameters:
            config = initialParameters["config"]
        transport = initialParameters["transport"]

        commandData = command.getData(transport) or []
        if not isinstance(commandData, list):
            commandData = [{"message": commandData}]

        for item in commandData:
            if not isinstance(item, dict):
                item = {"message": item}
            buffer = item["message"]
            if transport == "tcp":
                self.send(buffer)
                log.debug('[%s] Command data is sent: %s',
                    self.handlerId, buffer)
            elif transport == "sms":
                data = {
                    'type': transport,
                    'message': buffer,
                    'remaining': 1
                }
                if 'address' in config:
                    data['send_to'] = config['address']
                if 'callback' in config:
                    data['callback'] = config['callback']
                if 'id_object' in config:
                    data['id_object'] = config['id_object']
                if 'id_firm' in config:
                    data['id_firm'] = config['id_firm']
                if 'from' in config:
                    data['params'] = {}
                    data['params']['from'] = config['from']
                log.debug('[%s] Sending AMQP message to [work.process]...',
                    self.handlerId)
                broker.send([data],
                    routing_key = 'n.work.work.process',
                    exchangeName = 'n.work')

        if transport == "sms":
            # immediate sending of command update message
            broker.sendAmqpAnswer(self,
                "Command was successfully received and processed")
Пример #27
0
 def __init__(self, protocolHandlerClass, protocolAlias):
     """
      Creates broker thread for listening AMQP commands sent to
      the specified protocol (usually for sms transport)
      @param protocolHandlerClass: protocol handler class
      @param protocolAlias: protocol alias
     """
     log.debug('%s::__init__(%s)', self.__class__, protocolAlias)
     self._protocolHandlerClass = protocolHandlerClass
     self._protocolAlias = protocolAlias
     # starting amqp thread
     self._thread = Thread(target = self.threadHandler)
     self._thread.start()
Пример #28
0
 def store(self, packets):
     """
      Sends a list of packets to store
      @param packets: A list of packets
      @return: Instance of lib.falcon.answer.FalconAnswer
     """
     result = self.getStore().send(packets)
     if result.isSuccess():
         log.debug('[%s] store() ... OK', self.handlerId)
     else:
         errorsList = result.getErrorsList()
         log.error('[%s] store():\n %s', self.handlerId, errorsList)
     return result
Пример #29
0
    def onCommand(self, body, message):
        """
         Executes when command is received from queue
         @param body: amqp message body
         @param message: message instance
        """
        import kernel.pipe as pipe
        log.debug('[%s] Received command = %s', self._protocolAlias, body)

        command = broker.storeCommand(body)
        handler = self._protocolHandlerClass(pipe.Manager(), False)
        handler.processCommand(command)
        message.ack()
Пример #30
0
 def run(self):
     """
      Starting check for incoming emails
     """
     log.debug('CameraChecker::run()')
     try:
         for email in self.getEmails():
             data = self.parseEmail(email)
             if not data:
                 continue
             log.debug('Data found: %s, %s', data["uid"], data["time"])
             self.store.send(data)
     except Exception as E:
         log.critical(E)