Cleanups and improvements
This commit is contained in:
+7
-4
@@ -1,10 +1,13 @@
|
||||
from config import settings
|
||||
from mqtt import MqttClient
|
||||
from workers.thermostat import ThermostatWorker
|
||||
from workers_manager import WorkersManager
|
||||
|
||||
print(settings)
|
||||
|
||||
mqtt = MqttClient()
|
||||
mqtt = MqttClient(settings['mqtt'])
|
||||
|
||||
args = settings['workers']['thermostat']['args']
|
||||
mqtt.publish(ThermostatWorker(**args).status_update())
|
||||
workers = WorkersManager(settings['workers'])
|
||||
mqtt.publish(workers.get_updates())
|
||||
|
||||
# args = settings['workers']['thermostat']['args']
|
||||
# mqtt.publish(ThermostatWorker(**args).status_update())
|
||||
@@ -1,23 +1,22 @@
|
||||
from config import settings
|
||||
import paho.mqtt.publish as publish
|
||||
|
||||
class MqttClient:
|
||||
def __init__(self):
|
||||
self._connected = False
|
||||
self._config = settings['mqtt']
|
||||
def __init__(self, config):
|
||||
self._config = config
|
||||
|
||||
def publish(self, messages):
|
||||
print(messages)
|
||||
publish.multiple(messages, hostname=self.hostname, auth = {'username':self.username, 'password':self.password})
|
||||
def publish(self, messages):
|
||||
publish.multiple(messages,
|
||||
hostname=self.hostname,
|
||||
auth={'username': self.username, 'password': self.password})
|
||||
|
||||
@property
|
||||
def hostname(self):
|
||||
return self._config['host']
|
||||
@property
|
||||
def hostname(self):
|
||||
return self._config['host']
|
||||
|
||||
@property
|
||||
def username(self):
|
||||
return self._config['username']
|
||||
@property
|
||||
def username(self):
|
||||
return self._config['username']
|
||||
|
||||
@property
|
||||
def password(self):
|
||||
return self._config['password']
|
||||
@property
|
||||
def password(self):
|
||||
return self._config['password']
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
import importlib
|
||||
|
||||
|
||||
class WorkersManager:
|
||||
def __init__(self, config):
|
||||
self._config = config
|
||||
self._workers = self._register_klasses()
|
||||
|
||||
def get_updates(self):
|
||||
lazy_map = map(lambda worker: worker.status_update(), self._workers)
|
||||
flat_list = sum(lazy_map, []) # List flatten
|
||||
return flat_list
|
||||
|
||||
def _register_klasses(self):
|
||||
arr = []
|
||||
|
||||
for worker_klass in self._config.keys():
|
||||
klass = getattr(importlib.import_module("workers.%s" % worker_klass), "%sWorker" % worker_klass.title())
|
||||
arr.append(klass(**self._args_for(worker_klass)))
|
||||
|
||||
return arr
|
||||
|
||||
def _args_for(self, klass):
|
||||
return self._config[klass]['args']
|
||||
Reference in New Issue
Block a user