def list_current_subscriptions(from_number, cmds): msg_str = "You'll be notified of the following events:\n" for d in ivan_door, heather_door: e_str = "{}'s door:\n".format(d.name) #e_str = "{}'s door:\n".format(d.name) e_check = len(e_str) # We'll check for changes later if d.is_sub_event(Door.OPEN_E, from_number): e_str += " - When door opens\n" if d.is_sub_event(Door.CLOSE_E, from_number): e_str += " - When door closes\n" if d.is_sub_event(Door.TIMER_E, from_number): e_str += " - When door is left open\n" if d.is_sub_event(Door.BUTTON_OPEN_E, from_number): e_str += " - Confirmation of open\n" if d.is_sub_event(Door.BUTTON_CLOSE_E, from_number): e_str += " - Confirmation of close\n" if d.is_sub_event(Door.DOOR_OPENING_ERROR_E, from_number) or d.is_sub_event(Door.DOOR_CLOSING_ERROR_E, from_number): e_str += " - If there is an error\n" if len(e_str) == e_check: # You are not subscribed to anything for this door msg_str += "Nothing for {}'s door.\n".format(d.name) else: msg_str += e_str GS.send_message(msg_str, [from_number,]) return
def list_current_subscriptions(from_number, cmds): msg_str = "You'll be notified of the following events:\n" for d in ivan_door, heather_door: e_str = "{}'s door:\n".format(d.name) #e_str = "{}'s door:\n".format(d.name) e_check = len(e_str) # We'll check for changes later if d.is_sub_event(Door.OPEN_E, from_number): e_str += " - When door opens\n" if d.is_sub_event(Door.CLOSE_E, from_number): e_str += " - When door closes\n" if d.is_sub_event(Door.TIMER_E, from_number): e_str += " - When door is left open\n" if d.is_sub_event(Door.BUTTON_OPEN_E, from_number): e_str += " - Confirmation of open\n" if d.is_sub_event(Door.BUTTON_CLOSE_E, from_number): e_str += " - Confirmation of close\n" if d.is_sub_event(Door.DOOR_OPENING_ERROR_E, from_number) or d.is_sub_event( Door.DOOR_CLOSING_ERROR_E, from_number): e_str += " - If there is an error\n" if len(e_str) == e_check: # You are not subscribed to anything for this door msg_str += "Nothing for {}'s door.\n".format(d.name) else: msg_str += e_str GS.send_message(msg_str, [ from_number, ]) return
def ret_status(from_number, cmds): """ Build the status message to send back to texter """ s1 = "Ivan's door is {0}.".format(ivan_door.get_state_str().lower()) s2 = "Heather's door is {0}.".format(heather_door.get_state_str().lower()) if GS.is_dark(): # i.e. it's night time s3 = "The light is {0}.".format(light_monitor.get_light_str().lower()) else: s3 = "It's daytime so light state is unknown." GS.send_message("{0}\n{1}\n{2}".format(s1, s2, s3)) return
def help_text(from_number, cmds): """ Respond with the list of valid commands """ ret_str = ("s, i, h\n" "[un]sub [i/h] [timer/open/close/error/button]\n" "list\n" "?\n" "hist i/h [count]\n" "si/sh [# minutes (optional)") GS.send_message(ret_str) GS.send_message(from_number) return
def _publish_event(self, event): """ Sends a message via sms to all numbers set up to get messages about the event """ msg = event.msg if event not in self._event_sub_list: self.l.debug("Event not in list") self.l.debug(str(self._event_sub_list)) self.l.debug("Event = {}".format(event)) else: GS.send_message(msg, self._event_sub_list[event]) self.l.debug("Sent message '{}' to numbers {}".format(msg, str(self._event_sub_list[event]))) return
def _publish_event(self, event): """ Sends a message via sms to all numbers set up to get messages about the event """ msg = event.msg if event not in self._event_sub_list: self.l.debug("Event not in list") self.l.debug(str(self._event_sub_list)) self.l.debug("Event = {}".format(event)) else: GS.send_message(msg, self._event_sub_list[event]) self.l.debug("Sent message '{}' to numbers {}".format( msg, str(self._event_sub_list[event]))) return
def check_light_still_on(self): """ Check to see if light is still on This runs after timer expired, if light is on then send message """ GS.lock.acquire() self.light_left_on_timer = None if self.get_light_state() == ON and GS.is_dark(): self.l.debug("Sending light message") GS.send_message("Garage light left on.") self.light_left_on_timer = Timer(TIMER_INTERVAL, self.check_light_still_on) self.light_left_on_timer.start() GS.lock.release() return
def get_history(from_number, cmds): """ Get the history of open / close events for door """ l.info("Got a history command: {}".format(cmds)) if len(cmds) < 2: door = None else: door = _get_door(cmds[1]) if door is None: GS.send_message("Invalid door name '{}'. Use i or h.".format(door), [from_number,]) return if len(cmds) > 2: #We should have a count count = cmds[2] else: count = None ret_str = door.get_history(count) l.info("In get_history: return msg is: {}".format(ret_str)) GS.send_message(ret_str) return
def subscribe(from_number, cmds): """ Subscribe from events for user sub door_name event_type """ l.info("Got a subscribe command: {}".format(cmds)) door = _get_door(cmds[1]) if door is None: GS.send_message("Invalid door name '{}'. Use i or h.".format(cmds[1]), [from_number,]) return event_type = cmds[2] if event_type == "timer": door.sub_event(Door.TIMER_E, from_number) elif event_type == "open": door.sub_event(Door.OPEN_E, from_number) elif event_type == "close": door.sub_event(Door.CLOSE_E, from_number) elif event_type == "error": door.sub_event(Door.DOOR_OPENING_ERROR_E, from_number) door.sub_event(Door.DOOR_CLOSING_ERROR_E, from_number) elif event_type == "button": door.sub_event(Door.BUTTON_CLOSE_E, from_number) door.sub_event(Door.BUTTON_OPEN_E, from_number) else: # Should not happen, but users can do typos l.info("Unknown event type {}.".format(event_type)) GS.send_message("Unknown event type {}. Use timer, open, close, error or button".format(event_type), [from_number]) return GS.send_message("Subscribe to {} events for {}'s door confirmed!".format(event_type, door)) return
def snooze_timer(self, from_number, cmds): """ Either cancel or snooze the timer""" self.l.debug("In snooze with these commands: {}".format(cmds)) snooze_time = None # If there is a timer then cancel it if self.msg_timer is not None: self.msg_timer.cancel() # If # of minutes was specififed (cmds[1]) then set new timer with that delay try: snooze_time = cmds[1] # Did user give a time? int(snooze_time) # Is the argument an int? except IndexError: GS.send_message("Okay, {}'s door won't bother you again.", [from_number,]) return # No snooze time specified, just return except ValueError: # Snooze time is not a number, I should send a msg back to user self.l.debug("Invalid snooze time: {}".format(snooze_time)) GS.send_message("Snooze time ({}) must be a number.".format(snooze_time), [from_number,]) return # User specified sleep time - set a timer to check again self.msg_timer = Timer(snooze_time, self._quiet_time_over) self.msg_timer.start() GS.send_message("Okay, I'll remind you in {} minutes if {}'s door is still open".format(snooze_time, self.name), [from_number,]) return
def get_history(from_number, cmds): """ Get the history of open / close events for door """ l.info("Got a history command: {}".format(cmds)) if len(cmds) < 2: door = None else: door = _get_door(cmds[1]) if door is None: GS.send_message("Invalid door name '{}'. Use i or h.".format(door), [ from_number, ]) return if len(cmds) > 2: #We should have a count count = cmds[2] else: count = None ret_str = door.get_history(count) l.info("In get_history: return msg is: {}".format(ret_str)) GS.send_message(ret_str) return
def get_message(): try: # This is the uri used by plivo. The port translation is from # the gateway. The ddns is by ddns.net #uri = "https://ifermon.ddns.net:6000/" uri = "https://67.246.62.98:6000/" self.l.info("Got message on {0}\nmsg: \"{1}\"".format( uri, request.values)) # Validate the message self.l.debug("Checking plivo signature exists") if not request.headers.has_key('X-Plivo-Signature'): # This is very bad. It may mean that someone is trying to hack # the system. Shut it down. self.l.error("No plivo sig. Possible hack. Shutting down") GS.send_message("Shutting down system. Possible hack.") self.queue.put({"Msg:": "Shutting down"}) self.terminate() signature = request.headers['X-Plivo-Signature'] self.l.debug("Checking signature hash is valid") if not self.validate_signature(uri, request.form, signature, const.auth_token): # This is very bad. It may mean that someone is trying to hack # the system. Shut it down. self.l.error("Invalid hash. Possible hack. Shutting down") GS.send_message("Shutting down system. Possible hack.") self.queue.put({"Msg:": "Shutting down"}) self.terminate() # Signature is validated, now make sure it's not a duplicate # message self.l.debug("Checking the message is not a duplicate") uuid = str(request.form['MessageUUID']) if uuid_store.has_key(uuid): # This is very bad. It may mean that someone is trying to hack # the system. Shut it down. self.l.info("Duplicate msg. Possible hack. Shutting down") GS.send_message("Shutting down system. Possible hack.") self.queue.put({"Msg:": "Shutting down"}) self.terminate() # Add the uuid to the message store self.l.debug("Adding to msg id list") d = dict(request.form).update( {'Datestamp': datetime.datetime.now()}) uuid_store[uuid] = d self.l.debug("Putting message into queue") self.queue.put_nowait(request.values) self.l.info("Put msg into queue") except Exception as e: self.l.error("Some kind of error") self.l.error(e) return "Received"
def get_message(): try: # This is the uri used by plivo. The port translation is from # the gateway. The ddns is by ddns.net #uri = "https://ifermon.ddns.net:6000/" uri = "https://67.246.62.98:6000/" self.l.info("Got message on {0}\nmsg: \"{1}\"".format(uri, request.values)) # Validate the message self.l.debug("Checking plivo signature exists") if not request.headers.has_key('X-Plivo-Signature'): # This is very bad. It may mean that someone is trying to hack # the system. Shut it down. self.l.error("No plivo sig. Possible hack. Shutting down") GS.send_message("Shutting down system. Possible hack.") self.queue.put({"Msg:": "Shutting down"}) self.terminate() signature = request.headers['X-Plivo-Signature'] self.l.debug("Checking signature hash is valid") if not self.validate_signature(uri, request.form, signature, const.auth_token): # This is very bad. It may mean that someone is trying to hack # the system. Shut it down. self.l.error("Invalid hash. Possible hack. Shutting down") GS.send_message("Shutting down system. Possible hack.") self.queue.put({"Msg:": "Shutting down"}) self.terminate() # Signature is validated, now make sure it's not a duplicate # message self.l.debug("Checking the message is not a duplicate") uuid = str(request.form['MessageUUID']) if uuid_store.has_key(uuid): # This is very bad. It may mean that someone is trying to hack # the system. Shut it down. self.l.info("Duplicate msg. Possible hack. Shutting down") GS.send_message("Shutting down system. Possible hack.") self.queue.put({"Msg:": "Shutting down"}) self.terminate() # Add the uuid to the message store self.l.debug("Adding to msg id list") d = dict(request.form).update({'Datestamp':datetime.datetime.now()}) uuid_store[uuid] = d self.l.debug("Putting message into queue") self.queue.put_nowait(request.values) self.l.info("Put msg into queue") except Exception as e: self.l.error("Some kind of error") self.l.error(e) return "Received"
def unsubscribe(from_number, cmds): """ Unsubscribe from events for user unsub door_name event_type """ l.info("Got an unsubscribe command: {}".format(cmds)) door = _get_door(cmds[1]) if door is None: GS.send_message("Invalid door name '{}'. Use i or h.".format(cmds[1]), [ from_number, ]) return event_type = cmds[2] if event_type == "timer": door.unsub_event(Door.TIMER_E, from_number) elif event_type == "open": door.unsub_event(Door.OPEN_E, from_number) elif event_type == "close": door.unsub_event(Door.CLOSE_E, from_number) elif event_type == "error": door.unsub_event(Door.DOOR_OPENING_ERROR_E, from_number) door.unsub_event(Door.DOOR_CLOSING_ERROR_E, from_number) elif event_type == "button": door.unsub_event(Door.BUTTON_CLOSE_E, from_number) door.unsub_event(Door.BUTTON_OPEN_E, from_number) else: l.info("Unknown event type {}.".format(event_type)) GS.send_message( "Unknown event type {}. Use timer, open, close, error or button". format(event_type), [from_number]) return GS.send_message("Unsubscribe to {} events for {}'s door confirmed!".format( event_type, door)) return
def snooze_timer(self, from_number, cmds): """ Either cancel or snooze the timer""" self.l.debug("In snooze with these commands: {}".format(cmds)) snooze_time = None # If there is a timer then cancel it if self.msg_timer is not None: self.msg_timer.cancel() # If # of minutes was specififed (cmds[1]) then set new timer with that delay try: snooze_time = cmds[1] # Did user give a time? int(snooze_time) # Is the argument an int? except IndexError: GS.send_message("Okay, {}'s door won't bother you again.", [ from_number, ]) return # No snooze time specified, just return except ValueError: # Snooze time is not a number, I should send a msg back to user self.l.debug("Invalid snooze time: {}".format(snooze_time)) GS.send_message( "Snooze time ({}) must be a number.".format(snooze_time), [ from_number, ]) return # User specified sleep time - set a timer to check again self.msg_timer = Timer(snooze_time, self._quiet_time_over) self.msg_timer.start() GS.send_message( "Okay, I'll remind you in {} minutes if {}'s door is still open". format(snooze_time, self.name), [ from_number, ]) return
def zane_open_door(): self.l.info("Zane is requesting open door") #self.queue.put({'Text':'h', 'From':const.Ivan_cell}) GS.send_message('Zane triggered door.') self.l.info("Put msg in queue to open Heather's door from Zane.") return "Received"
def send_msg(): self.l.info("Got msg: {0}".format(request.args)) if request.args.has_key('msg'): msg = request.args['msg'] GS.send_message(msg) return "Received"
try: msg = q.get(True, 60) l.debug ("Recevied message <{0}>.".format(msg)) except Empty: l.debug("Queue get timed out after waiting") continue # We might be asked to shut down (e.g. in case of attempted hack) if not msg.has_key('From'): l.info("Invalid message <{0}>".format(msg)) sys.exit(1) # check to see if message came from allowed number if msg['From'] not in valid_numbers: l.info("Got command from invalid number") GS.send_message("Got msg from invalid number {0}".format( msg['From'])) continue # tell me if Zane is using it if msg['From'] in extra_notification: l.info("Got command from special user") GS.send_message("Got message from Zane") # now process the message cmd_str = msg['Text'].lower().strip().split() msg_func = f_map.get(cmd_str[0]) if msg_func is not None: msg_func(msg['From'], cmd_str) else: GS.send_message("I don't know that command. Sorry.") l.info("Unknown msg <{0}>".format(msg))
try: msg = q.get(True, 60) l.debug("Recevied message <{0}>.".format(msg)) except Empty: l.debug("Queue get timed out after waiting") continue # We might be asked to shut down (e.g. in case of attempted hack) if not msg.has_key('From'): l.info("Invalid message <{0}>".format(msg)) sys.exit(1) # check to see if message came from allowed number if msg['From'] not in valid_numbers: l.info("Got command from invalid number") GS.send_message("Got msg from invalid number {0}".format( msg['From'])) continue # tell me if Zane is using it if msg['From'] in extra_notification: l.info("Got command from special user") GS.send_message("Got message from Zane") # now process the message cmd_str = msg['Text'].lower().strip().split() msg_func = f_map.get(cmd_str[0]) if msg_func is not None: msg_func(msg['From'], cmd_str) else: GS.send_message("I don't know that command. Sorry.") l.info("Unknown msg <{0}>".format(msg))