Esempio n. 1
0
    def publisher(self):
        counter = 0

        while True:
            datagram, address = self.rq.get()
            logger.debug("Packet received: " + datagram + " from ip: " + str(address))

            # parse packet
            try:
            
                packet = Packet()
                packet.from_json(datagram)

            except Exception, err:
                logger.error("could not parse packet: " +  str(datagram) + " : " + str(err))
                logger.debug(traceback.format_exc())
                continue

            # update address  cache
            if packet.src not in self.cache:
                self.cache[packet.src] = str(address[0])+":"+str(address[1])
                logger.debug('ip address registred: ' + str(packet.src) + " = " + str(address[0])+":"+str(address[1]))
                # TODO: persist
            elif(self.cache[packet.src] != str(address[0])+":"+str(address[1])):
                self.cache[packet.src] = str(address[0])+":"+str(address[1])

            # create event based on packet cmd
            if packet.cmd == "device.update":

                # create event
                event = Event("device.update")
                event.src = self.name
                event.data = packet.data
                event.data["sys.id"] = packet.src

            else:
                logger.error("unknown command")
                continue;

            # logger.debug("publish counter: " + str(counter))

            # publish event
            self.eventbus.publish(event, "adapter")
Esempio n. 2
0
    def listen(self):
        while True:

            obj = self.redis.brpop(self.name)
            #logger.debug(self.name)
            #logger.debug("redis core pop msg: " + str(obj))

            try:
            
                event = Event()
                event.from_json(obj[1])

                # suppress self messages
                if event.src != self.name:
                    yield event

            except Exception, err:
                logger.error("could not parse event: " +  str(obj) + " : " + str(err))
                logger.debug(traceback.format_exc())
Esempio n. 3
0
    def run2(self):

        # connect
        self.init()
        logger.info("Plugin started. PID: " + str(os.getpid()))
        counter = 0

        while True:
            # blocking call to receive event
            event = self.eventbus.receive()
            logger.debug("Event received: " + event.to_json())

            if event.name == "device.update":
                counter = counter + 1

                # create command
                e = Event("device.push")
                e.data = event.data

                # send command
                self.eventbus.dispatch(e)
Esempio n. 4
0
    def run(self):
        # connect
        self.init()
        logger.info("Plugin started. PID: " + str(os.getpid()))
        counter = 0

        self.eventbus.subscribe("core")
        self.eventbus.subscribe("adapter")
        self.eventbus.subscribe("plugin")

        for event in self.eventbus.listen():

            logger.debug("Event received: " + event.to_json())

            if event.name == "device.update":
                counter = counter + 1

                # create command
                e = Event("device.push")
                e.data = event.data

                # send command
                self.eventbus.publish(e, "plugin")
Esempio n. 5
0
 def receive(self):
     obj = self.zmq_socket.recv()
     event = Event()
     event.from_json(obj)
     return event;