def sense(self): #Has a 1/4 chance of changing the presence sensor's current state #Informs the gateway to change its motion_mode attribute whenever the state changes if(random.random() > 0.75): s = int(not(self.state)) self.state = s self.clock += 1 self.db_put(self.name, self.state) if(s == 0): mode = 'HOME' else: mode = 'AWAY' try: #Set the gateway's motion_mode attribute r = iot.gateway_connection() c = r.set_attr(self.clock, 'motion_mode', mode) self.update_clock(c) logging.info(str(self.clock) + ' | ' + str(self.timestamp()) + ': ' + 'Set motion mode to: ' + str(mode)) print('Set motion mode to: ' + str(mode)) except: logging.warning(str(self.clock) + ' | ' + str(self.timestamp()) + ': ' + 'Failed to contact gateway') print('WARNING: Failed to contact gateway') self.register()
def sense(self): #Informs the gateway to change its door_state attribute whenever the state changes #If open, it has a 75% chance of closing #If closed, it has a 25% chance of opening if self.state == 1: p = 0.75 else: p = 0.25 if (random.random() <= p): s = int(not (self.state)) self.state = s logging.info( str(self.clock) + ' | ' + str(self.timestamp()) + ': ' + 'State was set to ' + str(s)) print('State was set to ' + str(s)) self.clock += 1 self.db_put(self.name, self.state) try: #Set the gateway's door_state attribute r = iot.gateway_connection() c = r.handle_door_state(self.clock, s) self.update_clock(c) logging.info( str(self.clock) + ' | ' + str(self.timestamp()) + ': ' + 'Set gateway door state to: ' + str(s)) except: logging.warning( str(self.clock) + ' | ' + str(self.timestamp()) + ': ' + 'Failed to contact gateway') print('WARNING: Failed to contact gateway') self.register()
def sense(self): #Has a 1/4 chance of changing the motion sensor's current state #Then informs the gateway of motion if the current state is 1 if (random.random() > 0.75): self.state = int(not (self.state)) self.clock += 1 self.db_put(self.name, self.state) if self.state == 1: try: logging.debug( str(self.clock) + ' | ' + str(self.timestamp()) + ': ' + 'Detected motion, reporting to gateway...') r = iot.gateway_connection() c = r.handle_motion(self.clock) self.update_clock(c) logging.debug( str(self.clock) + ' | ' + str(self.timestamp()) + ': ' + 'Reported motion to gateway') print('Detected motion and reported to gateway') except: logging.warning( str(self.clock) + ' | ' + str(self.timestamp()) + ': ' + 'Failed to report motion to gateway') print('WARNING: Failed to report motion to gateway') self.register() else: self.state = 0
def sense(self): #Informs the gateway to change its door_state attribute whenever the state changes #If open, it has a 75% chance of closing #If closed, it has a 25% chance of opening if self.state == 1: p = 0.75 else: p = 0.25 if(random.random() <= p): s = int(not(self.state)) self.state = s logging.info(str(self.clock) + ' | ' + str(self.timestamp()) + ': ' + 'State was set to ' + str(s)) print('State was set to ' + str(s)) self.clock += 1 self.db_put(self.name, self.state) try: #Set the gateway's door_state attribute r = iot.gateway_connection() c = r.handle_door_state(self.clock, s) self.update_clock(c) logging.info(str(self.clock) + ' | ' + str(self.timestamp()) + ': ' + 'Set gateway door state to: ' + str(s)) except: logging.warning(str(self.clock) + ' | ' + str(self.timestamp()) + ': ' + 'Failed to contact gateway') print('WARNING: Failed to contact gateway') self.register()
def set_mode(self, mode): #Change the motion-detection mode try: r = iot.gateway_connection() c = r.set_attr(self.clock, 'motion_mode', mode) self.update_clock(c) logging.info(str(self.clock) + ' | ' + str(self.timestamp()) + ': ' + 'Set motion mode to: ' + str(mode)) except: logging.warning(str(self.clock) + ' | ' + str(self.timestamp()) + ': ' + 'Failed to contact gateway') print('WARNING: Failed to contact gateway') self.register()
def get_mode(self): #Return the current motion-detection mode try: r = iot.gateway_connection() c, mode = r.get_attr(self.clock, 'motion_mode') self.update_clock(c) logging.info(str(self.clock) + ' | ' + str(self.timestamp()) + ': ' + 'Queried motion mode and received: ' + str(mode)) return mode except: logging.warning(str(self.clock) + ' | ' + str(self.timestamp()) + ': ' + 'Failed to contact gateway') print('WARNING: Failed to contact gateway') self.register()
def sense(self): #Has a 1/4 chance of changing the motion sensor's current state #Then informs the gateway of motion if the current state is 1 if(random.random() > 0.75): self.state = int(not(self.state)) self.clock += 1 self.db_put(self.name, self.state) if self.state == 1: try: logging.debug(str(self.clock) + ' | ' + str(self.timestamp()) + ': ' + 'Detected motion, reporting to gateway...') r = iot.gateway_connection() c = r.handle_motion(self.clock) self.update_clock(c) logging.debug(str(self.clock) + ' | ' + str(self.timestamp()) + ': ' + 'Reported motion to gateway') print('Detected motion and reported to gateway') except: logging.warning(str(self.clock) + ' | ' + str(self.timestamp()) + ': ' + 'Failed to report motion to gateway') print('WARNING: Failed to report motion to gateway') self.register() else: self.state = 0