Пример #1
0
    def run(self):
        logger.info("Actuator of lights in room %s has started!" % self.room)
        self.manController.start()
        logger.debug("Manual Controller of lights in room %s has started!" %
                     self.room)
        self.autoController.start()
        logger.debug("Domotics Controller of lights in room %s has started!" %
                     self.room)
        while not self._timeToDie():
            msg = "%s|%d|%s" % (self.room, int(
                self.state.getState()), self.state.getAuthor())
            send(msg, "lights", self.topicSender, self.servers)
            logger.debug(
                "Domotics Actuator message sent. Message: %s ; from lights in room %s."
                % (msg, self.room))
            time.sleep(2)

        logger.info(
            "Killing manual and domotics controllers of lights in room %s." %
            self.room)
        self.manController.kill()
        self.autoController.kill()
        logger.debug(
            "Waiting for  manual and domotics controllers of lights in room %s to end"
            % self.room)
        self.manController.join()
        self.autoController.join()
        logger.info("Ending life cyle of Actuator of lights in room %s." %
                    self.room)
 def run(self):
     while (EXIT_TAG == 0):
         returned = self.sim(self.division)
         toStore = '%s|%d' % (returned[0], int(returned[1]))
         try:
             sender_kafka.send(toStore, self.type, 'SensorsValues',
                               self.server)
             logger.debug("Data sent to Kafka broker: %s for %s" %
                          (self.type, self.division[0]))
         except Exception:
             logger.warning(
                 "Simulator cannot send due to Kafka not operational yet!")
             time.sleep(10)
             continue
         time.sleep(self.sleep)
Пример #3
0
    def consume_movement(self, consumer, key, offset, memory):
        for message in consumer:
            #verifies if message is handled by this consumer thread.
            logger.debug(
                "Consumer checking if message.key %s is equal to his key %s " %
                (message.key, key))
            if message.key == key and message.offset > offset:
                logger.debug("Consumer %s received message: %s" %
                             (key, message))
                offset = message.offset
                m = message.value.decode().split('|')

                #Check if this kind of state exists in memory
                if not memory.hasState(key, m[0]):
                    logger.debug(
                        "Consumer movement: add in memory a new State of type %s and %s"
                        % (key, m[0]))
                    memory.addState(key, m[0], MovementState())

                #Check if state has changed in sensor
                logger.debug("Comparing in consumer enum with message")
                if not (int(m[1]) == int(memory.getEnumState(key, m[0]))):
                    #Room m[0] has movement, check lights in that room
                    if int(m[1]) == 0:

                        memory.changeState(key, m[0], BasicState.ON)

                        #check if lights state exists in memory
                        if not memory.hasState(self.keyAct, m[0]):
                            memory.addState(self.keyAct, m[0], LightsState())

                        if datetime.now().hour > 18 and datetime.now(
                        ).hour < 8:
                            logger.debug("Consumer: before sending 1")
                            #its night time, send msg to turn on lighs
                            msg = "%s|%s|%s" % (m[0], str(0), "auto")
                            send(msg, self.keyAct, self.topicSender,
                                 self.servers)
                            logger.debug(
                                "Sent order to turn ON the lights at %s" %
                                (m[0]))
                        else:
                            logger.debug("Consumer: before sending 2")
                            #its day time, send msg to turn off lights
                            msg = "%s|%s|%s" % (m[0], str(1), "auto")
                            send(msg, self.keyAct, self.topicSender,
                                 self.servers)
                            logger.info(
                                "Sent order to turn OFF the lights at %s" %
                                (m[0]))
                    #Room m[0] has no movement
                    else:
                        memory.changeState(key, m[0], BasicState.OFF)
                        msg = "%s|%s|%s" % (m[0], 1, "auto")
                        send(msg, self.keyAct, self.topicSender, self.servers)
                        logger.debug(
                            "Sent order to turn OF the lights at %s because room has no movement"
                            % (m[0]))
                    #Register the state change in database
                    logger.debug("Movement as changed at %s." % (m[0]))
                    postgresCOM.insertMovementData(m[0],
                                                   (datetime.now().hour % 24))
                    logger.info("Added %s data to DB for room %s" %
                                (key, m[0]))

            if self._timeToDie():
                break