Ejemplo n.º 1
0
    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
Ejemplo n.º 2
0
	def process(self, type, name, state):
		type = type.lower()
		name = name.lower()
		state = state.lower()
		key = type + '-->' + name + '-->' + state
		print "Processing switch: " + key
		output = '<br>Input: ' + key
		try:
			if type not in switch_rules:
				return 'No rules for type in: ' + key
			if name not in switch_rules[type]:
				return 'No rules for name in: ' + key
			if state not in switch_rules[type][name]:
				return 'No rules for state in: ' + key			
			changes = switch_rules[type][name][state]
			print "Found " + str(len(changes)) + " items to process"
			for id, item in enumerate(changes):
				print "Processing item: " + str(id)
				#Check conditions
				qualify = True
				if 'conditions' in item:
					for condition in item['conditions']:
						if 'type' not in condition:
							output += '<br>Error: No condition type specified - skipping'
							break					
						if condition['type'] == 'period':
							if 'expression' not in condition:
								output += '<br>Error: No expression for condition type period - skipping'
								break
							if not in_period(condition['expression']):
								output += '<br>No change: ' + key + '[' + str(id) + ']: Period <> ' + condition['expression']
								qualify = False
								break
						else:
							if 'name' not in condition:
								output += '<br>Error: No device name set for condition - skipping'
								break
							if 'state' not in condition:
								output += '<br>Error: No state set for condition - skipping'
								break
							con_item = dom.get_device(condition['type'], condition['name'])
							if con_item and con_item.state.lower() != condition['state'].lower():
								output += '<br>No Change: ' + key + '[' + str(id) + ']: ' + condition['type'] + '-->' + condition['name'] + ' <> ' + condition['state']
								qualify = False
								break				
					if qualify:
						output += '<br>All conditions met for: ' + key + '[' + str(id) + ']'
				
				if qualify:
					if 'change' in item:
						change = item['change']
						if 'type' not in change:
							output += '<br>Error: No type set for change - skipping'
							continue
						if 'name' not in change:
							output += '<br>Error: No name set for change - skipping'
							continue
						if 'state' not in change:
							output += '<br>Error: No state set for change - skipping'
							continue
		
						device = dom.get_device(change['type'], change['name'])
						if device:
							device.set_state(change['state'])
							output += '<br>Change: ' + change['name'] + '-->' + change['state']
						else:
							output += '<br>No change: ' + key + '[' + str(id) + ']: Unknown switch (' + change['name'] + ')'
							
					if 'notify' in item:
						notify = item['notify']
						if 'type' not in notify:
							output += '<br>Error: No type set for notify - skipping'
							continue
						if 'message' not in notify:
							output += '<br>Error: No message set for notify - skipping'
							continue
						if 'recipients' not in notify:
							output += '<br>Error: No receipients set for notify - skipping'
							continue
		
						global recent_changes
					
						#Suppression
						if key in recent_changes:
							diff = datetime.datetime.now() - recent_changes[key]
							if diff.total_seconds() < presence_suppression_time:
								output += "Suppressing notify for repeated presence event: " + key
								continue
						recent_changes[key] = datetime.datetime.now()
							
						volume = change['volume'] if 'volume' in notify else None
						recipients = ','.join(notify['recipients'])
											
						module = notify_module.Notify()
						if notify['type'] == 'speak':
							module.message(recipients, notify['message'], volume)
						elif notify['type'] == 'alert':
							module.alert(notify['message'])
						elif notify['type'] == 'decide':
							module.notify(recipients, notify['message'], volume)
						elif notify['type'] == 'push':
							if 'link' in notify:
								module.link(recipients, notify['link'], notify['message'])
							else:
								module.message(recipients, notify['message'])
						output += '<br>Notify: ' + notify['type'] + ' to ' + recipients
						
					if 'function' in item:
						function = item['function']
						if 'module' not in function:
							output += '<br>Error: No module set for function - skipping'
							continue
						if 'method' not in function:
							output += '<br>Error: No method set for function - skipping'
							continue							
						
						module = False
						for plugin_name, module in inspect.getmembers(sys.modules['plugins'], inspect.ismodule):
							if plugin_name == function['module']:
								for class_name, obj in inspect.getmembers(module, inspect.isclass):
									module = obj()
									break
								
						if module:
							print module
							try:
								if 'params' in function:
									response = getattr(module, function['method'])(**function['params'])
								else:
									response = getattr(module, function['method'])
								output += '<br>Called function and got response: ' + response
							except Exception as e:
								print e
								traceback.print_exc()								
								output += '<br>Error: Function call failed'
						else:
							output += '<br>Error: Could not locate module/function: ' + function['module'] + "." + function['method']
												
		except Exception as e:
			print e
			traceback.print_exc()
							
		print output
		return output