Example #1
0
class _ScheduleThread(threading.Thread):
    r"""
    
    """
    def __init__(self, schedule, schedule_interval=1):
        threading.Thread.__init__(self, name="ScheduleThread")
        self.spine = None
        self.terminate = False
        self.schedule_interval = schedule_interval
        self.schedule = schedule
        self.daemon = True

    def run(self):
        while not self.terminate:
            if not self.spine:
                self.spine = Spine()
                if self.spine:
                    self.spine.register_command_handler(
                        "stopThreads", self._stop_command)
            else:
                self.schedule.run_pending()
                time.sleep(self.schedule_interval)

    def _stop_command(self):
        if not self.terminate:
            self.terminate = True
Example #2
0
 def __init__(self, command, protocol):
     self.protocol = protocol
     self.command = command
     spine = Spine()
     #print("rc", command)
     spine.register_command_handler(command,
                                    self.on_command,
                                    injected="socketSpine")
Example #3
0
class _SensorThread(KerviThread):
    r"""
    SensorThread is the base class that  polls sensors.
    Add one or more sensors and set polling interval.

    :param sensors:
            Id of the sensor.
            This id is used in other components to reference this sesnor.
    :type sensors: ``str``

    :param reading_interval:
            Polling interval in seconds between
    :type reading_interval: ``float``

    """
    def __init__(self, sensors, reading_interval=1):
        KerviThread.__init__(self)
        self.spine = Spine()
        if self.spine:
            self.spine.register_command_handler("startThreads",
                                                self._start_command)
            self.spine.register_command_handler("stopThreads",
                                                self._stop_command)
        self.alive = False
        self.reading_interval = reading_interval
        if hasattr(sensors, "__len__"):
            self.sensors = sensors
        else:
            self.sensors = [sensors]

    def new_sensor_reading(self, value, sensor_idx=0):
        self.sensors[sensor_idx]._new_sensor_reading(value)

    def _step(self):
        for sensor in self.sensors:
            sensor._read_sensor()
        self.sensor_step()
        time.sleep(self.reading_interval)

    def sensor_step(self):
        pass

    def _start_command(self):
        if not self.alive:
            self.alive = True
            KerviThread.start(self)

    def _stop_command(self):
        if self.alive:
            self.alive = False
            self.stop()
Example #4
0
class ModuleThread(KerviThread):
    def __init__(self):
        KerviThread.__init__(self)
        self.spine = Spine()
        self.spine.register_command_handler("startThreads", self._startCommand)

    def _step(self):
        self.moduleStep()

    def _startCommand(self):
        if not self.isAlive():
            super(KerviThread, self).start()

    def _stopCommand(self):
        self.stop()
Example #5
0
class ChannelPollingThread(KerviThread):
    def __init__(self, channel, device, callback, polling_time=.1):
        KerviThread.__init__(self)
        self._callback = callback
        self._channel = channel
        self._device = device
        self._value = None
        self._polling_time = polling_time
        self.alive = False
        self.spine = Spine()
        if self.spine:
            self.spine.register_command_handler("startThreads",
                                                self._start_command)
            self.spine.register_command_handler("stopThreads",
                                                self._stop_command)

    def _step(self):
        """Private method do not call it directly or override it."""
        try:
            new_value = self._device.get(self._channel)
            if new_value != self._value:
                self._callback(new_value)
                self._value = new_value
            time.sleep(self._polling_time)
        except:
            self.spine.log.exception("_PollingThread")

    def _start_command(self):
        if not self.alive:
            self.alive = True
            KerviThread.start(self)

    def _stop_command(self):
        if self.alive:
            self.alive = False
            self.stop()
Example #6
0
class StorageManager(Controller):
    def __init__(self, log_queue):

        Controller.__init__(self, "storage_manager", "Storage manager")

        self._spine = Spine()
        self._spine.register_event_handler("valueChanged", self._store_value)
        self._spine.register_query_handler("getValueData",
                                           self._get_value_data)

        self._spine.register_command_handler("storeSetting",
                                             self._store_setting)
        self._spine.register_query_handler("retrieveSetting",
                                           self._retrieve_setting)

        self._spine.register_query_handler("getMessageItems",
                                           self._get_messages)
        self._spine.register_event_handler("newMessage", self._store_message)

        #SPINE.register_command_handler("createCronJob", create_cron_job)
        #SPINE.register_command_handler("deleteCronJob", delete_cron_job)
        #SPINE.register_query_handler("queryCronJob", query_cron_job)

        self._plugin_manager = None
        self._plugin_manager = PluginManager(Configuration,
                                             "storage", [StoragePlugin],
                                             log_queue=log_queue)
        self._plugin_manager.load_managed_plugins()

    def _store_value(self, value_id, value, persist=False):
        for plugin in self._plugin_manager.plugins:
            try:
                if persist and plugin.storage_type == "persisted":
                    plugin.store_value(value_id, value, persist)
                elif not persist and not plugin.storage_type == "persisted":
                    plugin.store_value(value_id, value, persist)
            except NotImplementedError:
                pass

    def _get_value_data(self, value, date_from=None, date_to=None, limit=60):
        for plugin in self._plugin_manager.plugins:
            try:
                plugin.get_value_data(value, date_from, date_to, limit)
            except NotImplementedError:
                pass

    def _store_setting(self, group, name, value):
        for plugin in self._plugin_manager.plugins:
            try:
                if plugin.storage_type == "persisted":
                    plugin.store_setting(group, name, value)
            except NotImplementedError:
                pass

    def _retrieve_setting(self, group, name):
        for plugin in self._plugin_manager.plugins:
            try:
                if plugin.storage_type == "persisted":
                    return plugin.retrieve_setting(group, name)
            except NotImplementedError:
                pass

    def _store_message(self, source_id, item):
        for plugin in self._plugin_manager.plugins:
            try:
                plugin.store_message(source_id, item)
            except NotImplementedError:
                pass

    def _get_messages(self, page, page_size, filters=None):
        for plugin in self._plugin_manager.plugins:
            try:
                plugin.get_messages(self, page, page_size, filters)
            except NotImplementedError:
                pass