Make gateway executable
This commit is contained in:
Regular → Executable
+6
-2
@@ -1,5 +1,8 @@
|
|||||||
|
#!/usr/bin/python
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
if sys.version_info < (3,4):
|
if sys.version_info < (3,4):
|
||||||
print("To use this script you need python 3.4 or newer! got %s" % sys.version_info)
|
print("To use this script you need python 3.4 or newer! got %s" % sys.version_info)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
@@ -20,10 +23,10 @@ if parsed.debug:
|
|||||||
else:
|
else:
|
||||||
_LOGGER.setLevel(logging.INFO)
|
_LOGGER.setLevel(logging.INFO)
|
||||||
|
|
||||||
_LOGGER.info('Starting')
|
_LOGGER.debug('Starting')
|
||||||
|
|
||||||
mqtt = MqttClient(settings['mqtt'])
|
mqtt = MqttClient(settings['mqtt'])
|
||||||
manager = WorkersManager(settings['workers'], mqtt)
|
WorkersManager.start(mqtt, settings['workers'])
|
||||||
|
|
||||||
running = True
|
running = True
|
||||||
|
|
||||||
@@ -38,3 +41,4 @@ try:
|
|||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
running = False
|
running = False
|
||||||
_LOGGER.info('Exiting allowing jobs to finish. If you need force exit use kill')
|
_LOGGER.info('Exiting allowing jobs to finish. If you need force exit use kill')
|
||||||
|
|
||||||
|
|||||||
+20
-24
@@ -7,7 +7,7 @@ from workers_queue import _WORKERS_QUEUE
|
|||||||
|
|
||||||
class WorkersManager:
|
class WorkersManager:
|
||||||
class Command:
|
class Command:
|
||||||
def __init__(self, callback, args=[], options={}):
|
def __init__(self, callback, args=(), options=dict()):
|
||||||
self._callback = callback
|
self._callback = callback
|
||||||
self._args = args
|
self._args = args
|
||||||
self._options = options
|
self._options = options
|
||||||
@@ -20,43 +20,39 @@ class WorkersManager:
|
|||||||
_LOGGER.debug(messages)
|
_LOGGER.debug(messages)
|
||||||
return messages
|
return messages
|
||||||
|
|
||||||
def __init__(self, config, mqtt):
|
@classmethod
|
||||||
self._mqtt = mqtt
|
def start(cls, mqtt, config):
|
||||||
self._workers = self._register_klasses(config)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def workers(self):
|
|
||||||
return self._workers
|
|
||||||
|
|
||||||
def _register_klasses(self, config):
|
|
||||||
arr = {}
|
|
||||||
scheduler = BackgroundScheduler()
|
scheduler = BackgroundScheduler()
|
||||||
mqtt_callbacks = []
|
mqtt_callbacks = []
|
||||||
|
|
||||||
for (worker_name, worker_config) in config.items():
|
for (worker_name, worker_config) in config.items():
|
||||||
moduleObj = importlib.import_module("workers.%s" % worker_name)
|
module_obj = importlib.import_module("workers.%s" % worker_name)
|
||||||
klass = getattr(moduleObj, "%sWorker" % worker_name.title())
|
klass = getattr(module_obj, "%sWorker" % worker_name.title())
|
||||||
worker_obj = klass(**worker_config['args'])
|
worker_obj = klass(**worker_config['args'])
|
||||||
|
|
||||||
if moduleObj.REQUIREMENTS is not None:
|
if module_obj.REQUIREMENTS is not None:
|
||||||
self._pip_install_helper(moduleObj.REQUIREMENTS)
|
cls._pip_install_helper(module_obj.REQUIREMENTS)
|
||||||
|
|
||||||
if 'update_interval' in worker_config:
|
if 'update_interval' in worker_config:
|
||||||
_LOGGER.debug("Added: %s with %d seconds interval" % (worker_name, worker_config['update_interval']))
|
_LOGGER.debug("Added: %s with %d seconds interval" % (worker_name, worker_config['update_interval']))
|
||||||
_WORKERS_QUEUE.put(self.Command(worker_obj.status_update, []))
|
_WORKERS_QUEUE.put(cls.Command(worker_obj.status_update, []))
|
||||||
scheduler.add_job(lambda x: _WORKERS_QUEUE.put(self.Command(worker_obj.status_update, [])), 'interval', seconds=worker_config['update_interval'], args=[worker_name])
|
scheduler.add_job(
|
||||||
|
lambda x: _WORKERS_QUEUE.put(cls.Command(worker_obj.status_update, [])), 'interval',
|
||||||
|
seconds=worker_config['update_interval'],
|
||||||
|
args=[worker_name]
|
||||||
|
)
|
||||||
|
|
||||||
if 'topic_subscription' in worker_config:
|
if 'topic_subscription' in worker_config:
|
||||||
_LOGGER.debug("Subscribing to: %s" % worker_config['topic_subscription'] + '/#')
|
_LOGGER.debug("Subscribing to: %s" % worker_config['topic_subscription'] + '/#')
|
||||||
mqtt_callbacks.append((worker_config['topic_subscription'] + '/#', lambda client, _ , c: _WORKERS_QUEUE.put(self.Command(worker_obj.on_command, [c.topic, c.payload]))))
|
mqtt_callbacks.append((
|
||||||
|
worker_config['topic_subscription'] + '/#',
|
||||||
arr[worker_name] = worker_obj
|
lambda client, _ , c: _WORKERS_QUEUE.put(cls.Command(worker_obj.on_command, [c.topic, c.payload]))
|
||||||
|
))
|
||||||
|
|
||||||
scheduler.start()
|
scheduler.start()
|
||||||
self._mqtt.callbacks_subscription(mqtt_callbacks)
|
mqtt.callbacks_subscription(mqtt_callbacks)
|
||||||
|
|
||||||
return arr
|
@staticmethod
|
||||||
|
def _pip_install_helper(package_names):
|
||||||
def _pip_install_helper(self, package_names):
|
|
||||||
for package in package_names:
|
for package in package_names:
|
||||||
pip.main(['install', '-q', package])
|
pip.main(['install', '-q', package])
|
||||||
|
|||||||
Reference in New Issue
Block a user