Exemplo n.º 1
0
 def __init__(self, config):
     """
     @type config: config.AutomationConfig
     """
     self.job_queue = Queue()
     self.receivers = {}
     self.prefixes = {
         'root': '',
         'input': 'input.',
         'rule': 'rule.',
         'receiver': 'receiver.'
     }
     self.trigger = Queue()
     self.config = config
     self.receivers = None
     self.rule_context = None
     self.publish_queue = Queue(100)
     self.reporter = GraphiteReporter(config)
     self.values = self.load()
Exemplo n.º 2
0
 def __init__(self, config):
     """
     @type config: config.AutomationConfig
     """
     self.job_queue = Queue()
     self.receivers = {}
     self.prefixes = {
         'root': '',
         'input': 'input.',
         'rule':'rule.',
         'receiver': 'receiver.'
     }
     self.trigger = Queue()
     self.config = config
     self.receivers = None
     self.rule_context = None
     self.publish_queue = Queue(100)
     self.reporter = GraphiteReporter(config)
     self.values = self.load()
Exemplo n.º 3
0
class AutomationContext(object):
    def __start_publish_queue__(self):
        thread = threading.Thread(target=self.__publish_values__, name='publish-values')
        thread.daemon = True
        thread.start()
        self.__publish_thread__ = thread
        return thread

    def __start_local_threads(self):
        self.__start_publish_queue__()
        thread = threading.Thread(target=self.__rule_eval__, name='rule-eval')
        thread.daemon = True
        thread.start()
        self.__rule_eval_thread__ = thread
        thread = threading.Thread(target=self.__perform_actions__, name='job-function-executor')
        thread.daemon = True
        thread.start()
        self.__action_thread__ = thread

    def __init__(self, config):
        """
        @type config: config.AutomationConfig
        """
        self.job_queue = Queue()
        self.receivers = {}
        self.prefixes = {
            'root': '',
            'input': 'input.',
            'rule':'rule.',
            'receiver': 'receiver.'
        }
        self.trigger = Queue()
        self.config = config
        self.receivers = None
        self.rule_context = None
        self.publish_queue = Queue(100)
        self.reporter = GraphiteReporter(config)
        self.values = self.load()

    def __export_data__(self):
        self.__export_data_path__("automation_context", self.values)

    def __export_data_path__(self, path, values):
        def is_number(s):
            try:
                float(s)
                return True
            except ValueError:
                return False
        if path.endswith(CHANGE_TIME) or path.endswith(PREVIOUS_VALUE):
            # don't publish calculated values
            # deprecated, should be removed soon
            return

        if isinstance(values, dict):
            for key in values:
                self.__export_data_path__(path + "." + key, values[key])
        elif isinstance(values, Value):
            self.__export_data_path__(path, values.value)
        elif is_number(values):
            self.reporter.send(path, values)
        elif values == True:
            self.reporter.send(path, 1)
        elif values == False:
            self.reporter.send(path, 0)
        elif values == "ON":
            self.reporter.send(path, 1)
        elif values == "OFF":
            self.reporter.send(path, 0)

    def publishOne(self, elem, get_value):
        path = elem[0]
        values = elem[1]
        changed = elem[2]
        change_time = time.time()
        state = self.values
        for item in path.split("."):
            if not item in state:
                state[item] = {}
            state = state[item]
        for key in values:
            if not key in state or not state[key] == values[key] or changed:
                v = values[key]
                assignable = get_value(v)

                old_value = None
                if key in state:
                    old_value = get_value(state[key])

                state[key] = Value(assignable, change_time, old_value)
                __logger__.debug("setting %s %s to %s", path, key, state[key])
                if self.trigger.qsize() == 0:
                    __logger__.debug("Triggering due to path %s", path)
                    self.trigger.put(True)
        self.publish_queue.task_done()

    def __publish_values__(self):
        def get_value(orig):
            if isinstance(orig, Value):
                return orig.value
            return orig

        __logger__.info("Starting central value publish thread")
        while True:
            elems = []
            elems.append(self.publish_queue.get())
            size = 0
            maxIt = self.publish_queue.qsize()
            no_exception = True
            while size < maxIt and no_exception:
                try:
                    elems.append(self.publish_queue.get_nowait())
                    size = size + 1
                except Queue.Empty, e:
                    no_exception = False
            __logger__.debug('publishing %d items', size)
            for elem in elems:
                self.publishOne(elem, get_value)
            time.sleep(0.001)
Exemplo n.º 4
0
class AutomationContext(object):
    def __start_publish_queue__(self):
        thread = threading.Thread(target=self.__publish_values__,
                                  name='publish-values')
        thread.daemon = True
        thread.start()
        self.__publish_thread__ = thread
        return thread

    def __start_local_threads(self):
        self.__start_publish_queue__()
        thread = threading.Thread(target=self.__rule_eval__, name='rule-eval')
        thread.daemon = True
        thread.start()
        self.__rule_eval_thread__ = thread
        thread = threading.Thread(target=self.__perform_actions__,
                                  name='job-function-executor')
        thread.daemon = True
        thread.start()
        self.__action_thread__ = thread

    def __init__(self, config):
        """
        @type config: config.AutomationConfig
        """
        self.job_queue = Queue()
        self.receivers = {}
        self.prefixes = {
            'root': '',
            'input': 'input.',
            'rule': 'rule.',
            'receiver': 'receiver.'
        }
        self.trigger = Queue()
        self.config = config
        self.receivers = None
        self.rule_context = None
        self.publish_queue = Queue(100)
        self.reporter = GraphiteReporter(config)
        self.values = self.load()

    def __export_data__(self):
        self.__export_data_path__("automation_context", self.values)

    def __export_data_path__(self, path, values):
        def is_number(s):
            try:
                float(s)
                return True
            except ValueError:
                return False

        if path.endswith(CHANGE_TIME) or path.endswith(PREVIOUS_VALUE):
            # don't publish calculated values
            # deprecated, should be removed soon
            return

        if isinstance(values, dict):
            for key in values:
                self.__export_data_path__(path + "." + key, values[key])
        elif isinstance(values, Value):
            self.__export_data_path__(path, values.value)
        elif is_number(values):
            self.reporter.send(path, values)
        elif values == True:
            self.reporter.send(path, 1)
        elif values == False:
            self.reporter.send(path, 0)
        elif values == "ON":
            self.reporter.send(path, 1)
        elif values == "OFF":
            self.reporter.send(path, 0)

    def publishOne(self, elem, get_value):
        path = elem[0]
        values = elem[1]
        changed = elem[2]
        change_time = time.time()
        state = self.values
        for item in path.split("."):
            if not item in state:
                state[item] = {}
            state = state[item]
        for key in values:
            if not key in state or not state[key] == values[key] or changed:
                v = values[key]
                assignable = get_value(v)

                old_value = None
                if key in state:
                    old_value = get_value(state[key])

                state[key] = Value(assignable, change_time, old_value)
                __logger__.debug("setting %s %s to %s", path, key, state[key])
                if self.trigger.qsize() == 0:
                    __logger__.debug("Triggering due to path %s", path)
                    self.trigger.put(True)
        self.publish_queue.task_done()

    def __publish_values__(self):
        def get_value(orig):
            if isinstance(orig, Value):
                return orig.value
            return orig

        __logger__.info("Starting central value publish thread")
        while True:
            elems = []
            elems.append(self.publish_queue.get())
            size = 0
            maxIt = self.publish_queue.qsize()
            no_exception = True
            while size < maxIt and no_exception:
                try:
                    elems.append(self.publish_queue.get_nowait())
                    size = size + 1
                except Queue.Empty, e:
                    no_exception = False
            __logger__.debug('publishing %d items', size)
            for elem in elems:
                self.publishOne(elem, get_value)
            time.sleep(0.001)