Cleanups and improvements
This commit is contained in:
+7
-4
@@ -1,10 +1,13 @@
|
|||||||
from config import settings
|
from config import settings
|
||||||
from mqtt import MqttClient
|
from mqtt import MqttClient
|
||||||
from workers.thermostat import ThermostatWorker
|
from workers_manager import WorkersManager
|
||||||
|
|
||||||
print(settings)
|
print(settings)
|
||||||
|
|
||||||
mqtt = MqttClient()
|
mqtt = MqttClient(settings['mqtt'])
|
||||||
|
|
||||||
args = settings['workers']['thermostat']['args']
|
workers = WorkersManager(settings['workers'])
|
||||||
mqtt.publish(ThermostatWorker(**args).status_update())
|
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
|
import paho.mqtt.publish as publish
|
||||||
|
|
||||||
class MqttClient:
|
class MqttClient:
|
||||||
def __init__(self):
|
def __init__(self, config):
|
||||||
self._connected = False
|
self._config = config
|
||||||
self._config = settings['mqtt']
|
|
||||||
|
|
||||||
def publish(self, messages):
|
def publish(self, messages):
|
||||||
print(messages)
|
publish.multiple(messages,
|
||||||
publish.multiple(messages, hostname=self.hostname, auth = {'username':self.username, 'password':self.password})
|
hostname=self.hostname,
|
||||||
|
auth={'username': self.username, 'password': self.password})
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def hostname(self):
|
def hostname(self):
|
||||||
return self._config['host']
|
return self._config['host']
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def username(self):
|
def username(self):
|
||||||
return self._config['username']
|
return self._config['username']
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def password(self):
|
def password(self):
|
||||||
return self._config['password']
|
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