def _startListen(self): if self._threadListen is None: try: CloudLog.log(self._component + ":StartListen", "Starting UDP Listener") self._thread = thread.start_new_thread(self._runListenLoop, (None,)) except Exception, e: CloudLog.error(self._component, "Unable to start listen loop", e)
def _runLoop(self, params): CloudLog.log(self._component, "Running.") if self.unread is None: self.unread = 0 while self.running: try: if self._controller.state == "HOME": output = Popen(['lynx', '-source', 'https://www.google.com/voice/request/unread'], stdout=PIPE) response = output.stdout.read() response = json.loads(response) previous_count = self.unread self.unread = int(response["unreadCounts"]["all"]) if self.unread > previous_count: cmd = self._settings["cmd_new"] self._controller.executeCommandByName(cmd) elif self.unread == 0 and previous_count > 0: cmd = self._settings["cmd_zero"] self._controller.executeCommandByName(cmd) interval = self._settings["interval"] else: interval = self._settings["interval"] * 2 except Exception, e: cmd = self._settings["cmd_error"] self._controller.executeCommandByName(cmd) if interval < self._settings["interval"] * 10: interval += self._interval time.sleep(interval)
def _runLoop(self, params): CloudLog.log(self._component, "Running.") interval = self._settings["interval"] if self.unread is None: self.unread = 0 while self.running: try: if self._controller.state == "HOME": output = Popen(['lynx', '-source', 'https://www.google.com/voice/request/unread'], stdout=PIPE) response = None response = output.stdout.read() response = json.loads(response) previous_count = self.unread self.unread = int(response["unreadCounts"]["all"]) if self.unread > previous_count: cmd = self._settings["cmd_new"] self._controller.executeCommandByName(cmd) elif self.unread == 0 and previous_count > 0: cmd = self._settings["cmd_zero"] self._controller.executeCommandByName(cmd) interval = self._settings["interval"] else: interval = self._settings["interval"] * 2 response = None error = None except ValueError, e: CloudLog.error(self._component, "Error parsing response", e) response = str(response) if len(response) > 512: response = response[:512] + "..." CloudLog.log(self._component, response) error = "Value" except Exception, e: CloudLog.error(self._component, "Error in run loop", e) error = "Unknown"
def __init__(self, ip_address, port): CloudLog.log(self._component, "Initializing.") self._ip_address = ip_address self._port = port with open("config.json", "r") as text_file: results = text_file.read() self._commands = json.loads(results)
def search(ip_address, port, strings): component = "UDPListener:Search" waitTime = 9 try: CloudLog.log(component, "Listening on " + ip_address + ":" + str(port)) udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # Allow applications reuse the same port udp_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # Join the mulitcast group ip_mreq = struct.pack('4sl', socket.inet_aton(ip_address), socket.INADDR_ANY) udp_socket.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, ip_mreq) # Leave the mulitcast group # udp_socket.setsockopt(socket.IPPROTO_IP, socket.IP_DROP_MEMBERSHIP, ip_mreq) udp_socket.bind((ip_address, port)) udp_socket.settimeout(waitTime) searchLoop = True stopTime = datetime.now() + timedelta(seconds=waitTime * 10) except Exception, e: CloudLog.error(component, "Unable to open multicast socket", e) searchLoop = False
def _startAnnounce(self): if self._threadAnnounce is None: try: CloudLog.log(self._component + ":StartAnnounce", "Starting UDP Announcer") self._threadAnnounce = thread.start_new_thread(self._runAnnounceLoop, (None,)) except Exception, e: CloudLog.error(self._component, "Unable to start announce loop", e)
def __init__(self, connection_settings=None): CloudLog.log(self._component, "Initializing.") if connection_settings is None: connection_settings = self._find() if connection_settings is not None: self._ip_address = connection_settings["ip"] self._port = connection_settings["port"] self._uuid = connection_settings["uuid"]
def __init__(self, controller): CloudLog.log(self._component, "Initializing.") self._controller = controller try: import RPIO self._start() except Exception, e: self.state = "NO_RPIO" CloudLog.error(self._component, "RPIO is unavailable.", e)
def _startListen(self): if self._threadListen is None: try: CloudLog.log(self._component + ":StartListen", "Starting UDP Listener") self._thread = thread.start_new_thread(self._runListenLoop, (None, )) except Exception, e: CloudLog.error(self._component, "Unable to start listen loop", e)
def __init__(self, ip_address, port): CloudLog.log(self._component, "Initializing.") self._ip_address = ip_address self._port = port try: import RPIO self.state = "READY" except Exception, e: self.state = "NO_RPIO" CloudLog.error(self._component, "RPIO is unavailable.", e)
def _startAnnounce(self): if self._threadAnnounce is None: try: CloudLog.log(self._component + ":StartAnnounce", "Starting UDP Announcer") self._threadAnnounce = thread.start_new_thread( self._runAnnounceLoop, (None, )) except Exception, e: CloudLog.error(self._component, "Unable to start announce loop", e)
def _runLoop(self, params): CloudLog.log(self._component, "Running.") while self.running: try: self._controller.executeCommandByName("TEST") interval = 10 except Exception, e: CloudLog.error(self._component, "Error in run loop", e) if interval < self._interval * 10: interval += self._interval time.sleep(interval)
def __init__(self, ip_address=None): CloudLog.log(self._component, "Initializing.") try: if ip_address is None: ip_address = UDPListener.search( self._search_ip, self._search_port, self._search_strings) self._phue = phue.Bridge(ip=ip_address, username=self._hueAppID) self._ready = True CloudLog.log(self._component, "Ready.") except Exception, e: CloudLog.error(self._component, "Error searching for Hue Bridge", e)
def __init__(self, commands, ip_address=None): CloudLog.log(self._component, "Initializing.") try: self._commands = commands if ip_address is None: self._ip_address = UDPListener.search( self._search_ip, self._search_port, self._search_strings) else: self._ip_address = ip_address self._ready = True CloudLog.log(self._component, "Ready. [" + str(self._ip_address) + "]") except Exception, e: CloudLog.error(self._component, "Error searching for iTach Device", e)
def __init__(self, ip_address=None): CloudLog.log(self._component, "Initializing.") try: if ip_address is None: ip_address = UDPListener.search(self._search_ip, self._search_port, self._search_strings) self._phue = phue.Bridge(ip=ip_address, username=self._hueAppID) self._ready = True CloudLog.log(self._component, "Ready.") except Exception, e: CloudLog.error(self._component, "Error searching for Hue Bridge", e)
def _runLoop(self, params): CloudLog.log(self._component, "Running.") while self.running: try: if self._settings["push"]: self._push() if self._settings["pull"]: self._pull() interval = self._settings["interval"] except Exception, e: CloudLog.error(self._component, "Error in run loop", e) if interval < self._interval * 10: interval += self._interval time.sleep(interval)
def __init__(self, outside_config, inside_config): CloudLog.log(self._component, "Initializing.") self._outside_config = outside_config self._inside_config = inside_config try: os.system('modprobe w1-gpio') os.system('modprobe w1-therm') base_dir = '/sys/bus/w1/devices/' device_folder = glob.glob(base_dir + '28*')[0] self._inside_config["sensor_path"] = device_folder + '/w1_slave' CloudLog.log(self._component, "Indoor temperature sensor ready.") except Exception, e: CloudLog.error(self._component, "Error initializing indoor temperature sensor", e) self._inside_config["enabled"] = False
def save(self): try: CloudLog.log(self._component, "Saving config file.") new_config = {} new_config["config"] = self.settings new_config["ac_commands"] = self.ac_commands new_config["light_recipes"] = self.light_recipes new_config["harmony_activities"] = self.harmony_activities new_config["commands"] = self.commands result = json.dumps(new_config) with open(self._config_file, "w") as text_file: text_file.write(result) except Exception, e: CloudLog.error(self._component, "Error saving config file.", e)
def __init__(self, commands, ip_address=None): CloudLog.log(self._component, "Initializing.") try: self._commands = commands if ip_address is None: self._ip_address = UDPListener.search(self._search_ip, self._search_port, self._search_strings) else: self._ip_address = ip_address self._ready = True CloudLog.log(self._component, "Ready. [" + str(self._ip_address) + "]") except Exception, e: CloudLog.error(self._component, "Error searching for iTach Device", e)
def run(self): CloudLog.log(self._component, "Running.") while self.state == "READY": try: previous_doorOpen = self._doorOpen import RPIO RPIO.setup(23, RPIO.IN, pull_up_down=RPIO.PUD_UP) self._doorOpen = RPIO.input(23) if previous_doorOpen != self._doorOpen: if self._doorOpen is True: self._announce("OPEN") else: self._announce("CLOSED") interval = 0.2 except Exception, e: CloudLog.error(self._component, "Error in run loop", e) if interval < 10: interval += interval time.sleep(interval)
def read(self): try: with open(self._config_file, "r") as text_file: results = text_file.read() cfg = json.loads(results) self.settings = cfg["config"] self.ac_commands = cfg["ac_commands"] self.light_recipes = cfg["light_recipes"] self.harmony_activities = cfg["harmony_activities"] self.commands = cfg["commands"] for command in self.commands: key = command.get("key") name = command.get("name") if key is not None: self.commands_by_key[key] = command if name is not None: self.commands_by_name[name] = command CloudLog.log(self._component, "Config file loaded.") except Exception, e: CloudLog.error(self._component, "Error reading config file.", e)
def _runLoop(self, params): CloudLog.log(self._component, "Running.") interval = 300 while self.running: time.sleep(interval) try: if self._controller.state == "AWAY": state = self._controller.status() hue_status = state["hue"] any_on = False for light in hue_status["lights"]: if hue_status["lights"][light]["state"]["on"] is True: any_on = True if any_on is True and self._controller.state == "AWAY": self._controller.executeCommandByName("LIGHTSOFF") interval = 300 except Exception, e: CloudLog.error(self._component, "Error in _runLoop", e) if interval < 3000: interval += random.randint(120,300)
def _runLoop(self, params): CloudLog.log(self._component, "Running.") interval = 300 while self.running: time.sleep(interval) try: if self._controller.state == "AWAY": state = self._controller.status() hue_status = state["hue"] any_on = False for light in hue_status["lights"]: if hue_status["lights"][light]["state"]["on"] is True: any_on = True if any_on is True and self._controller.state == "AWAY": self._controller.executeCommandByName("LIGHTSOFF") interval = 300 except Exception, e: CloudLog.error(self._component, "Error in _runLoop", e) if interval < 3000: interval += random.randint(120, 300)
def listen(controller): component = "KeyPadController" runLoop = True modifier = None modTimer = datetime.min keyModifier = { "\t": "Off", "/": "VirginBlue", "*": "NightRed", "\x7f": "DimWhite", "+": "UP", "-": "DOWN" } while runLoop: CloudLog.debug(component, "Waiting for key") charInput = GetCh() CloudLog.debug(component, "Got Key") if modTimer + timedelta(seconds=4) < datetime.now(): modifier = None modTimer = datetime.min if charInput == "q" or charInput == "Q": CloudLog.log(component, "User Interupt (Q)") controller.shutdown() runLoop = False elif charInput == "r" or charInput == "R": controller.config.read() elif keyModifier.get(charInput) is not None: modifier = keyModifier.get(charInput) modTimer = datetime.now() else: if charInput == "\r": charInput = "H" modifier = None elif charInput == "0": modifier = None CloudLog.log(component + ":KeyPress", charInput + ":" + str(modifier)) controller.executeCommandByKeyCode(charInput, modifier) modifier = None modTimer = datetime.min
def _runLoop(self, params): CloudLog.log(self._component, "Running.") interval = self._settings["interval"] if self.unread is None: self.unread = 0 while self.running: try: if self._controller.state == "HOME": output = Popen([ 'lynx', '-source', 'https://www.google.com/voice/request/unread' ], stdout=PIPE) response = None response = output.stdout.read() response = json.loads(response) previous_count = self.unread self.unread = int(response["unreadCounts"]["all"]) if self.unread > previous_count: cmd = self._settings["cmd_new"] self._controller.executeCommandByName(cmd) elif self.unread == 0 and previous_count > 0: cmd = self._settings["cmd_zero"] self._controller.executeCommandByName(cmd) interval = self._settings["interval"] else: interval = self._settings["interval"] * 2 response = None error = None except ValueError, e: CloudLog.error(self._component, "Error parsing response", e) response = str(response) if len(response) > 512: response = response[:512] + "..." CloudLog.log(self._component, response) error = "Value" except Exception, e: CloudLog.error(self._component, "Error in run loop", e) error = "Unknown"
def _runLoop(self, params): CloudLog.log(self._component, "Running.") while self.running: try: previous_doorOpen = self._doorOpen import RPIO RPIO.setup(23, RPIO.IN, pull_up_down=RPIO.PUD_UP) self._doorOpen = RPIO.input(23) if previous_doorOpen != self._doorOpen: if self._doorOpen is True: self.state = "OPEN" if self._controller.state == "AWAY": self._controller.executeCommandByName("HOME") else: self.state = "CLOSED" CloudLog.track("FRONT_DOOR", self.state) # self._component.status(key="door", value=self.state) interval = 0.5 except Exception, e: CloudLog.error(self._component, "Error in run loop", e) if interval < 10: interval += interval time.sleep(interval)
def search(ip_address, port, strings): component = "UDPListener:Search" waitTime = 9 try: CloudLog.log(component, "Listening on " + ip_address + ":" + str(port)) udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # Allow applications reuse the same port udp_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # Join the mulitcast group ip_mreq = struct.pack('4sl', socket.inet_aton(ip_address), socket.INADDR_ANY) udp_socket.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, ip_mreq) # Leave the mulitcast group # udp_socket.setsockopt(socket.IPPROTO_IP, socket.IP_DROP_MEMBERSHIP, ip_mreq) udp_socket.bind((ip_address, port)) udp_socket.settimeout(waitTime) searchLoop = True stopTime = datetime.now() + timedelta(seconds=waitTime*10) except Exception, e: CloudLog.error(component, "Unable to open multicast socket", e) searchLoop = False
def run(self): component = "KeyPadController" runLoop = True modifier = None modTimer = datetime.min keyModifier = { "\t": "Off", "/": "VirginBlue", "*": "NightRed", "\x7f": "DimWhite", "+": "UP", "-": "DOWN" } while runLoop: CloudLog.debug(component, "Waiting for key") charInput = GetCh() CloudLog.debug(component, "Got Key") if modTimer + timedelta(seconds=4) < datetime.now(): modifier = None modTimer = datetime.min if charInput == "q" or charInput == "Q": CloudLog.log(component, "User Interupt (Q)") runLoop = False elif keyModifier.get(charInput) is not None: modifier = keyModifier.get(charInput) modTimer = datetime.now() else: CloudLog.log(component + ":KeyPress", charInput + ":" + str(modifier)) try: command = self._commands[charInput] self._announce({"command": command, "modifier": modifier}) except: pass modifier = None modTimer = datetime.min
def _runLoop(self, params): CloudLog.log(self._component, "Running.") while self.running: try: if self._settings["push"]: self._push() if self._settings["pull"]: self._pull() interval = self._settings["interval"] except Exception, e: CloudLog.error(self._component, "Error in run loop", e) if interval < self._interval * 10: interval += self._interval time.sleep(interval) CloudLog.log(self._component, "Stopped.") def _push(self): try: status = self._controller.status() status["user_key"] = Keys.APPENGINE_USER content = json.dumps(status) self._query("POST", "/set/status", content) except Exception, e: CloudLog.error(self._component, "Error pushing system status.", e) def _pull(self): commands = [] try: content = Keys.APPENGINE_USER result = self._query("POST", "/cmds/get?clear=true", content)
def __init__(self, controller): self._settings = controller.config.settings["cloud_sync"] if self._settings["push"] or self._settings["pull"]: CloudLog.log(self._component, "Initializing.") self._controller = controller self._start()
def _start(self): if self.running is False: self.running = True try: self._thread = thread.start_new_thread(self._runLoop, (None, )) except Exception, e: CloudLog.error(self._component, "Unable to start run loop", e) self.running = False def _runLoop(self, params): CloudLog.log(self._component, "Running.") interval = 300 while self.running: time.sleep(interval) try: if self._controller.state == "AWAY": state = self._controller.status() hue_status = state["hue"] any_on = False for light in hue_status["lights"]: if hue_status["lights"][light]["state"]["on"] is True: any_on = True if any_on is True and self._controller.state == "AWAY": self._controller.executeCommandByName("LIGHTSOFF") interval = 300 except Exception, e: CloudLog.error(self._component, "Error in _runLoop", e) if interval < 3000: interval += random.randint(120, 300) CloudLog.log(self._component, "Stopped.")
def __init__(self, controller): self._settings = controller.config.settings["gvoice"] if self._settings["enabled"]: CloudLog.log(self._component, "Initializing.") self._controller = controller self._start()
# Leave the mulitcast group # udp_socket.setsockopt(socket.IPPROTO_IP, socket.IP_DROP_MEMBERSHIP, ip_mreq) udp_socket.bind((ip_address, port)) udp_socket.settimeout(waitTime) searchLoop = True stopTime = datetime.now() + timedelta(seconds=waitTime*10) except Exception, e: CloudLog.error(component, "Unable to open multicast socket", e) searchLoop = False while searchLoop: try: response = udp_socket.recvfrom(1024) result = True for item in strings: if response[0].find(item) == -1: result = False if result is True: CloudLog.log(component, "Device found on IP: " + response[1][0]) return response[1][0] except socket.timeout, e: pass except Exception, e: CloudLog.error(component, "Error reading from multi-cast socket", e) finally: if datetime.now() > stopTime: searchLoop = False CloudLog.error(component, "Device not found (timeout).") return None
def stop(self): CloudLog.log(self._component, "Stopping.") self.running = False
def __init__(self): CloudLog.log(self._component, "Initializing") self.read()
try: self._thread = thread.start_new_thread(self._runLoop, (None,)) except Exception, e: CloudLog.error(self._component, "Unable to start run loop", e) def _runLoop(self, params): CloudLog.log(self._component, "Running.") while self.running: try: previous_doorOpen = self._doorOpen import RPIO RPIO.setup(23, RPIO.IN, pull_up_down=RPIO.PUD_UP) self._doorOpen = RPIO.input(23) if previous_doorOpen != self._doorOpen: if self._doorOpen is True: self.state = "OPEN" if self._controller.state == "AWAY": self._controller.executeCommandByName("HOME") else: self.state = "CLOSED" CloudLog.track("FRONT_DOOR", self.state) # self._component.status(key="door", value=self.state) interval = 0.5 except Exception, e: CloudLog.error(self._component, "Error in run loop", e) if interval < 10: interval += interval time.sleep(interval) CloudLog.log(self._component, "Stopped.")
def __init__(self, controller): CloudLog.log(self._component, "Initializing.") self._controller = controller self._start()
ip = s.getsockname()[0] s.close() port = 9557 udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) udp_socket.bind((ip, port)) except Exception, e: self._running = False udp_socket = None CloudLog.error(self._component, "Error opening socket", e) while self.running and udp_socket is not None: try: packet = udp_socket.recvfrom(1024) result = json.loads(packet[0]) cmd = result["command"] if cmd == "RELOAD_CONFIG": self._controller.config.read() CloudLog.log(self._component + ":Config", "Reload") else: modifier = result.get("modifier") CloudLog.log(self._component + ":Execute", cmd + ":" + str(modifier)) self._controller.executeCommandByName(cmd, modifier) interval = 0.1 except Exception, e: CloudLog.error(self._component, "Error in run loop", e) if interval < 10: interval += 1 time.sleep(interval) udp_socket.close() CloudLog.log(self._component, "Stopped.")
def _runLoop(self, params): CloudLog.log(self._component, "Running.") while self.running: try: if self._settings["push"]: self._push() if self._settings["pull"]: self._pull() interval = self._settings["interval"] except Exception, e: CloudLog.error(self._component, "Error in run loop", e) if interval < self._interval * 10: interval += self._interval time.sleep(interval) CloudLog.log(self._component, "Stopped.") def _push(self): try: status = self._controller.status() status["user_key"] = Keys.APPENGINE_USER content = json.dumps(status) self._query("POST", "/set/status", content) except Exception, e: CloudLog.error(self._component, "Error pushing system status.", e) def _pull(self): commands = [] try:
ip = "192.168.1.201" port = 9557 udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) udp_socket.bind((ip, port)) except Exception, e: self._running = False udp_socket = None CloudLog.error(self._component, "Error opening socket", e) while self.running and udp_socket is not None: try: packet = udp_socket.recvfrom(1024) result = json.loads(packet[0]) cmd = result["command"] if cmd == "RELOAD_CONFIG": self._controller.config.read() CloudLog.log(self._component + ":Config", "Reload") else: modifier = result.get("modifier") CloudLog.log(self._component + ":Execute", cmd + ":" + str(modifier)) self._controller.executeCommandByName(cmd, modifier) interval = 0.1 except Exception, e: CloudLog.error(self._component, "Error in run loop", e) if interval < 10: interval += 1 time.sleep(interval) udp_socket.close() CloudLog.log(self._component, "Stopped.")
# udp_socket.setsockopt(socket.IPPROTO_IP, socket.IP_DROP_MEMBERSHIP, ip_mreq) udp_socket.bind((ip_address, port)) udp_socket.settimeout(waitTime) searchLoop = True stopTime = datetime.now() + timedelta(seconds=waitTime * 10) except Exception, e: CloudLog.error(component, "Unable to open multicast socket", e) searchLoop = False while searchLoop: try: response = udp_socket.recvfrom(1024) result = True for item in strings: if response[0].find(item) == -1: result = False if result is True: CloudLog.log(component, "Device found on IP: " + response[1][0]) return response[1][0] except socket.timeout, e: pass except Exception, e: CloudLog.error(component, "Error reading from multi-cast socket", e) finally: if datetime.now() > stopTime: searchLoop = False CloudLog.error(component, "Device not found (timeout).") return None