Exemplo n.º 1
0
 def __init__(self, in_queue, out_queue, log_queue, **kwargs):
     self.msg_in_queue = in_queue
     self.msg_out_queue = out_queue
     # Initialize logging
     worker_configurer(log_queue)
     self.logger = logging.getLogger(__name__)
     # Set default input parameter values
     self.name = "undefined"
     # Update default elements based on any parameters passed in
     if kwargs is not None:
         for key, value in kwargs.items():
             if key == "name":
                 self.name = value
     # Initialize parent class
     multiprocessing.Process.__init__(self, name=self.name)
     # Create remaining class elements
     self.work_queue = multiprocessing.Queue(-1)
     self.msg_in = message.Message()
     self.msg_to_process = message.Message()
     self.msg_to_send = message.Message()
     self.last_hb = datetime.datetime.now()
     self.last_forecast_update = datetime.datetime.now(
     ) + datetime.timedelta(minutes=-15)
     self.dst = dst.USdst()
     self.utc_offset = datetime.timedelta(hours=0)
     self.in_msg_loop = bool()
     self.main_loop = bool()
     self.close_pending = False
     self.create_home_flags()
Exemplo n.º 2
0
 def process_in_msg_queue(self):
     """ Method to cycle through incoming message queue, filtering out heartbeats and
     mis-directed messages.  Messages corrected destined for this process are loaded
     into the work queue """
     self.in_msg_loop = True
     while self.in_msg_loop is True:
         try:
             self.msg_in = message.Message(
                 raw=self.msg_in_queue.get_nowait())
         except:
             self.in_msg_loop = False
         if len(self.msg_in.raw) > 4:
             self.logger.debug(
                 "Processing message [%s] from incoming message queue" %
                 self.msg_in.raw)
             if self.msg_in.dest == "11":
                 if self.msg_in.type == "001":
                     self.last_hb = datetime.datetime.now()
                 elif self.msg_in.type == "999":
                     self.logger.info("Kill code received - Shutting down")
                     self.close_pending = True
                     self.in_msg_loop = False
                 else:
                     self.work_queue.put_nowait(self.msg_in.raw)
                     self.logger.debug(
                         "Moving message [%s] over to internal work queue",
                         self.msg_in.raw)
                 self.msg_in = str()
             else:
                 self.msg_out_queue.put_nowait(self.msg_in.raw)
                 self.logger.debug("Redirecting message [%s] back to main" %
                                   self.msg_in.raw)
             self.msg_in = message.Message()
         else:
             self.in_msg_loop = False
Exemplo n.º 3
0
 def __init__(self, in_queue, out_queue, log_queue, **kwargs):
     self.msg_in_queue = in_queue
     self.msg_out_queue = out_queue
     # Initialize logging
     worker_configurer(log_queue)
     self.logger = logging.getLogger(__name__)
     # Set default input parameter values
     self.name = "undefined"
     # Update default elements based on any parameters passed in
     if kwargs is not None:
         for key, value in kwargs.items():
             if key == "name":
                 self.name = value
     # Initialize parent class
     multiprocessing.Process.__init__(self, name=self.name)
     # Create remaining class elements
     self.work_queue_empty = True
     self.work_queue = multiprocessing.Queue(-1)
     self.msg_in = message.Message()
     self.msg_in_empty = True
     self.msg_to_process = message.Message()
     self.msg_to_send = message.Message()
     self.last_hb = datetime.datetime.now()
     self.in_msg_loop = bool()
     self.main_loop = bool()
     self.device = None
     self.device_list = []
     self.index = 0
     self.last_update = datetime.datetime.now()
     self.close_pending = False
Exemplo n.º 4
0
    def process_work_queue(self):
        """ Method to perform work from the work queue """
        # Get next message from internal queue or timeout trying to do so
        try:
            self.msg_to_process = message.Message(
                raw=self.work_queue.get_nowait())
        except:
            pass
        # If there is a message to process, do so
        if len(self.msg_to_process.raw) > 4:
            self.work_queue_empty = False
            self.logger.debug(
                "Processing message [%s] from internal work queue",
                self.msg_to_process.raw)

            # Discover Device
            if self.msg_to_process.type == "160":
                self.logger.debug(
                    "Message type 160 - attempting to discover device: %s",
                    self.msg_to_process.payload)
                self.device = self.discover_device(self.msg_to_process.name,
                                                   self.msg_to_process.payload)
                self.msg_to_send = message.Message(
                    source="16",
                    dest=self.msg_to_process.source,
                    type="160A",
                    name=self.msg_to_process.name)
                if self.device is not None:
                    self.msg_to_send.payload = "found"
                else:
                    self.msg_to_send.payload = "not found"
                self.msg_out_queue.put_nowait(self.msg_to_send.raw)
                self.logger.debug("Sending discovery successful message: [%s]",
                                  self.msg_to_send.raw)

            # Set Wemo state
            if self.msg_to_process.type == "161":
                # Process wemo off commands
                if self.msg_to_process.payload == "off":
                    self.logger.debug("Sending \"off\" command to device: %s",
                                      self.msg_to_process.name)
                    self.switch_off(self.msg_to_process.name)
                # Process wemo on command
                if self.msg_to_process.payload == "on":
                    self.logger.debug("Sending \"on\" command to device: %s",
                                      self.msg_to_process.name)
                    self.switch_on(self.msg_to_process.name)

            # Get Wemo state
            if self.msg_to_process.type == "162":
                self.msg_162(self.msg_to_process.name,
                             self.msg_to_process.source)

            # Clear msg-to-process string
            self.msg_to_process = message.Message()
        else:
            self.work_queue_empty = True
            self.msg_to_process = message.Message()
Exemplo n.º 5
0
 def update_forecast(self):
     """ Requests a forecast update from the nest module """
     self.msg_to_send = message.Message(source="11", dest="17", type="020")
     self.msg_out_queue.put_nowait(self.msg_to_send.raw)
     self.logger.debug(
         "Requesting current weather status update from NEST [%s]",
         self.msg_to_send.raw)
     self.msg_to_send = message.Message(source="11", dest="17", type="021")
     self.msg_out_queue.put_nowait(self.msg_to_send.raw)
     self.logger.debug(
         "Requesting current weather status update from NEST [%s]",
         self.msg_to_send.raw)
     self.msg_to_send = message.Message(source="11", dest="17", type="022")
     self.msg_out_queue.put_nowait(self.msg_to_send.raw)
     self.logger.debug(
         "Requesting current weather status update from NEST [%s]",
         self.msg_to_send.raw)
     self.last_forecast_update = datetime.datetime.now()
Exemplo n.º 6
0
 def __init__(self, in_queue, out_queue, log_queue, **kwargs):
     self.msg_in_queue = in_queue
     self.msg_out_queue = out_queue
     # Initialize logging
     worker_configurer(log_queue)
     self.logger = logging.getLogger(__name__)
     # Set default input parameter values
     self.name = "undefined"
     self.logfile = "logfile"
     # Update default elements based on any parameters passed in
     if kwargs is not None:
         for key, value in kwargs.items():
             if key == "name":
                 self.name = value
     # Initialize parent class
     multiprocessing.Process.__init__(self, name=self.name)
     # Create remaining class elements
     self.username = str()
     self.password = str()
     self.work_queue = multiprocessing.Queue(-1)
     self.msg_in = message.Message()
     self.msg_to_process = message.Message()
     self.msg_to_send = message.Message()
     self.last_hb = datetime.datetime.now()
     self.in_msg_loop = bool()
     self.main_loop = bool()
     self.close_pending = False
     self.structure = []
     self.current_condition = str()
     self.current_temp = str()
     self.current_wind_dir = str()
     self.current_humid = str()
     self.result = str()
     self.forecast = []
     self.forecast_condition = str()
     self.forecast_temp_low = str()
     self.forecast_temp_high = str()
     self.forecast_humid = str()
Exemplo n.º 7
0
 def msg_162(self, name, dest):
     self.logger.debug("Sending \"request state\" command to device: %s",
                       name)
     self.status = self.query_status(name)
     self.msg_to_send = message.Message(source="16",
                                        dest=dest,
                                        type="162A",
                                        name=name)
     if self.status is not None:
         self.logger.debug(
             "Get status query successfully returned value of: %s",
             self.status)
         self.msg_to_send.payload = self.status
     else:
         self.logger.warning(
             "Device [%s] did not respond to Get status query", name)
         self.msg_to_send.payload = ""
     # send response
     self.msg_out_queue.put_nowait(self.msg_to_send.raw)
     self.logger.debug("Sending get-status successful message: [%s]",
                       self.msg_to_send.raw)
Exemplo n.º 8
0
    def process_work_queue(self):
        """ Method to perform work from the work queue """
        # Get next message from internal queue or timeout trying to do so
        try:
            self.msg_to_process = message.Message(
                raw=self.work_queue.get_nowait())
        except:
            pass
        # If there is a message to process, do so
        if len(self.msg_to_process.raw) > 4:
            self.logger.debug(
                "Processing message [%s] from internal work queue" %
                self.msg_to_process.raw)

            # Process current condition request ack
            if self.msg_to_process.type == "020A":
                self.logger.debug("ACK received for 020 message: [%s]",
                                  self.msg_to_process.raw)
                if self.msg_to_process.payload != "":
                    self.msg_to_send = message.Message(
                        source="11",
                        dest="02",
                        type="020A",
                        payload=self.msg_to_process.payload)
                    self.msg_out_queue.put_nowait(self.msg_to_send.raw)
                    self.logger.debug(
                        "Forwarding ACK message [%s] to gui to update display",
                        self.msg_to_process.raw)
                else:
                    self.logger.warning(
                        "Message 020 ACK from NEST service contained no info to display"
                    )

            # Process current forecast request ack
            elif self.msg_to_process.type == "021A":
                self.logger.debug("ACK received for 021 message: [%s]",
                                  self.msg_to_process.raw)
                if self.msg_to_process.payload != "":
                    self.msg_to_send = message.Message(
                        source="11",
                        dest="02",
                        type="021A",
                        payload=self.msg_to_process.payload)
                    self.msg_out_queue.put_nowait(self.msg_to_send.raw)
                    self.logger.debug(
                        "Forwarding ACK message [%s] to gui to update display",
                        self.msg_to_process.raw)
                else:
                    self.logger.warning(
                        "Message 021 ACK from NEST service contained no info to display"
                    )

            # Process tomorrow forecast request ack
            elif self.msg_to_process.type == "022A":
                self.logger.debug("ACK received for 022 message: [%s]",
                                  self.msg_to_process.raw)
                if self.msg_to_process.payload != "":
                    self.msg_to_send = message.Message(
                        source="11",
                        dest="02",
                        type="022A",
                        payload=self.msg_to_process.payload)
                    self.msg_out_queue.put_nowait(self.msg_to_send.raw)
                    self.logger.debug(
                        "Forwarding ACK message [%s] to gui to update display",
                        self.msg_to_process.raw)
                else:
                    self.logger.warning(
                        "Message 022 ACK from NEST service contained no info to display"
                    )

            # Process user "home/away" messages
            elif self.msg_to_process.type == "100":
                if self.msg_to_process.name == "user1":
                    if self.msg_to_process.payload == "0":
                        self.homeArray[0] = False
                        self.logger.info("User1 is no longer home")
                    elif self.msg_to_process.payload == "1":
                        self.homeArray[0] = True
                        self.logger.info("User1 is home")
                if self.msg_to_process.name == "user2":
                    if self.msg_to_process.payload == "0":
                        self.homeArray[1] = False
                        self.logger.info("User2 is no longer home")
                    elif self.msg_to_process.payload == "1":
                        self.homeArray[1] = True
                        self.logger.info("User2 is home")
                if self.msg_to_process.name == "user3":
                    if self.msg_to_process.payload == "0":
                        self.homeArray[2] = False
                        self.logger.info("User3 is no longer home")
                    elif self.msg_to_process.payload == "1":
                        self.homeArray[2] = True
                        self.logger.info("User3 is home")

            # Process device discovery successful messages
            elif self.msg_to_process.type == "160A":
                self.logger.debug("ACK received for 160 message: [%s]",
                                  self.msg_to_process.raw)
                if self.msg_to_process.payload == "found":
                    self.msg_to_send = message.Message(
                        source="11",
                        dest="02",
                        type="160A",
                        name=self.msg_to_process.name)
                    self.msg_out_queue.put_nowait(self.msg_to_send.raw)
                    self.logger.debug(
                        "Sending message [%s] to gui app to add control widget for device: [%s]",
                        self.msg_to_send.raw, self.msg_to_process.name)
                else:
                    self.logger.debug(
                        "ACK reports device was not found.  No further action being taken"
                    )

            # Process device create messages
            elif self.msg_to_process.type == "168":
                self.create_devices()
            else:
                pass
            # Clear message once all possibilities are checked
            self.msg_to_process = message.Message()
        else:
            pass
Exemplo n.º 9
0
 def process_work_queue(self):
     """ Method to perform work from the work queue """
     # Get next message from internal queue or timeout trying to do so
     try:
         self.msg_to_process = message.Message(raw=self.work_queue.get_nowait())
     except:
         pass
     # If there is a message to process, do so
     if len(self.msg_to_process.raw) > 4:
         self.logger.debug("Processing message [%s] from internal work queue" % self.msg_to_process.raw)
         # 130 = Set Home-Away mode set to away (override)
         if self.msg_to_process.type == "130":
             # Mode 0 == away override
             if self.msg_to_process.payload == "0":
                 if self.msg_to_process.name == "user1":
                     self.user1.mode = 0
                 if self.msg_to_process.name == "user2":
                     self.user2.mode = 0
                 if self.msg_to_process.name == "user3":
                     self.user3.mode = 0
             # Mode 1 == home override
             if self.msg_to_process.payload == "1":
                 if self.msg_to_process.name == "user1":
                     self.user1.mode = 1
                 if self.msg_to_process.name == "user2":
                     self.user2.mode = 1
                 if self.msg_to_process.name == "user3":
                     self.user3.mode = 1
             # Mode 2 == auto by schedule
             if self.msg_to_process.payload == "2":
                 if self.msg_to_process.name == "user1":
                     self.user1.mode = 2
                 if self.msg_to_process.name == "user2":
                     self.user2.mode = 2
                 if self.msg_to_process.name == "user3":
                     self.user3.mode = 2   
             # Mode 3 == auto by arp & ping
             if self.msg_to_process.payload == "3":
                 if self.msg_to_process.name == "user1":
                     self.user1.mode = 3
                 if self.msg_to_process.name == "user2":
                     self.user2.mode = 3
                 if self.msg_to_process.name == "user3":
                     self.user3.mode = 3
             # Mode 4 == auto by ping with delay
             if self.msg_to_process.payload == "4":
                 if self.msg_to_process.name == "user1":
                     self.user1.mode = 4
                 if self.msg_to_process.name == "user2":
                     self.user2.mode = 4
                 if self.msg_to_process.name == "user3":
                     self.user3.mode = 4
             # Mode 5 == auto by schedule with ping
             if self.msg_to_process.payload == "5":
                 if self.msg_to_process.name == "user1":
                     self.user1.mode = 5
                 if self.msg_to_process.name == "user2":
                     self.user2.mode = 5
                 if self.msg_to_process.name == "user3":
                     self.user3.mode = 5
         # Clear msg-to-process string
         self.msg_to_process = message.Message()
     else:
         pass
Exemplo n.º 10
0
    def process_work_queue(self):
        """ Method to perform work from the work queue """
        # Get next message from internal queue or timeout trying to do so
        try:
            self.msg_to_process = message.Message(
                raw=self.work_queue.get_nowait())
        except:
            pass
        # If there is a message to process, do so
        if len(self.msg_to_process.raw) > 4:
            self.logger.debug(
                "Processing message [%s] from internal work queue" %
                self.msg_to_process.raw)

            if self.msg_to_process.type == "020":
                self.logger.debug(
                    "Message type 020 [%s] received requesting current conditions"
                )
                self.msg_to_send = message.Message(
                    source="17", dest=self.msg_to_process.source, type="020A")
                if self.connect() is True:
                    self.logger.debug("Connection to NEST device successful")
                    self.msg_to_send.payload = self.current_conditions()
                else:
                    self.logger.debug(
                        "Error attempting to connect to NEST device")
                    self.msg_to_send.payload = ""
                self.msg_out_queue.put_nowait(self.msg_to_send.raw)
                self.logger.debug("Returning 020 ACK response [%s]" %
                                  self.msg_to_send.raw)

            elif self.msg_to_process.type == "021":
                self.logger.debug(
                    "Message type 021 [%s] received requesting current conditions"
                )
                self.msg_to_send = message.Message(
                    source="17", dest=self.msg_to_process.source, type="021A")
                if self.connect() is True:
                    self.logger.debug("Connection to NEST device successful")
                    self.msg_to_send.payload = self.current_forecast()
                else:
                    self.logger.debug(
                        "Error attempting to connect to NEST device")
                    self.msg_to_send.payload = ""
                self.msg_out_queue.put_nowait(self.msg_to_send.raw)
                self.logger.debug("Returning 021 ACK response [%s]" %
                                  self.msg_to_send.raw)
            elif self.msg_to_process.type == "022":
                self.logger.debug(
                    "Message type 022 [%s] received requesting current conditions"
                )
                self.msg_to_send = message.Message(
                    source="17", dest=self.msg_to_process.source, type="022A")
                if self.connect() is True:
                    self.logger.debug("Connection to NEST device successful")
                    self.msg_to_send.payload = self.tomorrow_forecast()
                else:
                    self.logger.debug(
                        "Error attempting to connect to NEST device")
                    self.msg_to_send.payload = ""
                self.msg_out_queue.put_nowait(self.msg_to_send.raw)
                self.logger.debug("Returning 022 ACK response [%s]" %
                                  self.msg_to_send.raw)

            # Clear msg-to-process string
            self.msg_to_process = message.Message()
        else:
            pass