Esempio n. 1
0
    def sendMessage(self, uid, body = None, ignoreFlag = False):
        """
         Send message to the broker
         @param uid: Device identifier
         @param body: New packet body
         @param ignoreFlag: Send message even if the checkFile is in progress
        """
        threadName = 'ReceiverThread'
        if not body:
            self._messagesLocks.pop(uid, None)
            log.debug('%s::%s lock file cleared', threadName, uid)
            return

        currentTime = time.time()
        isLocked = uid in self._messagesLocks and not ignoreFlag
        if isLocked:
            lockTime = self._messagesLocks[uid]
            isLocked = currentTime - lockTime < QUEUE_MAX_TIMEOUT
            if not isLocked:
                log.debug('%s::%s is unlocked by timeout!', threadName, uid)
                body = self._messages[uid][0]['body']
        if not isLocked:
            self._messagesLocks[uid] = currentTime
            broker.send([body], 'mon.device.packet.receive')
            log.debug('%s::%s packet has been sent', threadName, uid)
        else:
            log.debug('%s::%s is locked!', threadName, uid)
Esempio n. 2
0
 def sendPacketsViaBroker(self, packets):
     """
      Sends data to the message broker (AMQP)
      @param packets: list of packets
      @return:
     """
     try:
         broker.send(packets)
     except Exception as E:
         #~print(E)
         log.error(E)
Esempio n. 3
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")
Esempio n. 4
0
def sendData(data):
    """
     Sends packets to the RabbitMQ
     @param data: dict
    """
    broker.send([data])