def process(self, name, zone, action='arrive'): global recent_changes event = (name + "." + action + "." + zone).lower() if event not in notify_rules.keys(): return 'No notifications setup for event: ' + event #Event suppression if event in recent_changes.keys(): diff = datetime.datetime.now() - recent_changes[event] if diff.total_seconds() < suppress_time: print "Suppressing event as seen recently" return "Suppressing event as seen recently" recent_changes[event] = datetime.datetime.now() output = '<br>Input: ' + event qualify = True for notification in notify_rules[event]: if 'message' not in notification: output += '<br>Error: No message configured' qualify = False if qualify and 'person' not in notification: output += '<br>Error: No person configured' qualify = False if qualify and 'period' in notification: if not in_period(notification['period']): output += '<br>Not notifying : ' + notification['person'] + ' (Period <> ' + notification['period'] + ')' qualify = False if qualify and 'conditions' in notification: for condition in notification['conditions']: con_switch = dom.get_device('switch', condition['switch']) if con_switch and con_switch.state.lower() != condition['state'].lower(): qualify = False output += '<br>Not notifying: ' + notification['person'] + ' (' + con_switch.name + ' <> ' + condition['state'] + ')' break if qualify: output += '<br>Notifying: ' + notification['person'] if 'link' in notification: pb.send(notification['message'], notification['link'], notification['person'], 'url') else: pb.send(notification['message'], '', notification['person'], 'text') return output
def _message(self, people, message, title=None, data_type='text'): title = title if title else message try: pb.send(title, message, people, data_type) except: raise Exception('Message send failed')
def process(self): print 'Process queue called...' file_handle = None def file_is_locked(file_path): global file_handle file_handle= open(file_path, 'w') try: fcntl.lockf(file_handle, fcntl.LOCK_EX | fcntl.LOCK_NB) return False except IOError: return True file_path = '/tmp/has.queue.lock' if file_is_locked(file_path): print 'another instance is running exiting now' sys.exit(0) else: print 'no other instance is running' db = dropbox.client.DropboxClient(cred.dropbox_token) results = [] try: ts = datetime.now().strftime('%Y-%m-%d %H:%M:%S') queue = os.path.join(inbox_dir, queue_file) out_filename = "done." + ts + ".txt" output_file = os.path.join(outbox_dir, out_filename) db.file_move(queue, output_file) except dropbox.rest.ErrorResponse as e: if e.status == 404: #File must already have been processed print 'No queue file to process' results.append('No queue file to process') sys.exit() contents = db.get_file(output_file).read() jobs = contents.split("\n") notify = 'julian' for job in jobs: try: print job tokens = job.split('|', 1) if len(tokens) == 2: notify = tokens[0] print 'Set notify person to: ' + notify job = tokens[1] else: print 'No notify person specified, using: ' + notify response = requests.post(job) results.append(response.text) except: error_file = os.path.join(error_dir, os.path.basename(output_file)) db.file_move(output_file, error_file) results.append('ERROR: Job failed - check error folder') if results: print "Results:" print results if len(results) == 1: message = results[0] if results[0] else 'Module did not return a message' pb.send(message, message, notify, 'text') else: data = "" for result in results: data += result pb.send("Multiple requests processed", data, notify, 'text')
if e.status == 404: #File must already have been processed print 'No queue file to process' results.append('No queue file to process') sys.exit() contents = db.get_file(output_file).read() jobs = contents.split("\n") print jobs for job in jobs: try: print job response = requests.post(job) results.append(response.text) except: error_file = os.path.join(error_dir, os.path.basename(output_file)) db.file_move(output_file, error_file) results.append('ERROR: Job failed - check error folder') #print results if results: if len(results) == 1: pb.send(results[0], results[0], 'julian', 'text') else: data = "" for result in results: data += result pb.send("Multiple requests processed", data, 'julian', 'text')
def sync(self, reason='activity'): nest_away = nest.is_away() dom_away_switch = dom.get_device('mode','away') dom_occupied_switch = dom.get_device('mode','occupied') dom_guest_switch = dom.get_device('mode','guest') if nest_away == dom_away_switch.is_on(): return 'Already synced, nothing to do' message = 'Heating.sync - unknown outcome - check code' if reason == 'manual': if nest_away: #Nest is away and Domoticz is home nest.set_home() message = 'Away mode disabled (Manual)' else: #Nest is home and Domoticz is away nest.set_away() if dom_guest_switch.is_on(): dom_guest_switch.off() message = 'Away mode enabled (Manual)' #elif reason == 'geofence': # if nest_away: #Nest is away and Domoticz is home # if dom_occupied_switch.is_on(): # dom_julian_switch = dom.get_device('presence','julianhome') # dom_sarah_switch = dom.get_device('presence','sarahhome') # people_present = [] # if dom_guest_switch.is_on(): # people_present.append('Guest') # if dom_julian_switch.is_on(): # people_present.append('Julian') # if dom_sarah_switch.is_on(): # people_present.append('Sarah') # nest.set_home() # message = 'Away mode suppressed (' + ', '.join(people_present) + ')' # else: # dom_away_switch.on() # message = 'Away mode enabled (no activity)' # # nest.set_home() # dom_away_switch.off() # message = 'Away mode disabled (Someone nearby)' # else: #Nest is home and Domoticz is away # nest.set_away() # dom_away_switch.on() # if dom_guest_switch.is_on(): # dom_guest_switch.off() # message = 'Away mode enabled (and guest mode disabled)' # else: # message = 'Away mode enabled (No-one nearby)' else: #From nest (activity) if nest_away: #Nest is away and Domoticz is home if dom_occupied_switch.is_on(): dom_julian_switch = dom.get_device('presence','julianhome') dom_sarah_switch = dom.get_device('presence','sarahhome') people_present = [] if dom_guest_switch.is_on(): people_present.append('Guest') if dom_julian_switch.is_on(): people_present.append('Julian') if dom_sarah_switch.is_on(): people_present.append('Sarah') nest.set_home() message = 'Away mode suppressed (' + ', '.join(people_present) + ')' else: dom_away_switch.on() message = 'Away mode enabled (No activity)' else: #Nest is home and Domoticz is away dom_away_switch.off() message = 'Away mode disabled (Activity)' pb.send(message,'launch://?url=[action:' + str(lcp_away_action) + ']',notify_person,'url') return message