def main(): logger.info(f"Starting server") settings = load_settings() client: Optional[TransportClient] = None if settings.mqtt: client = MqttClient(host_name, settings.mqtt) if settings.signalr: client = SignalRClient(settings.signalr) if not client: raise Exception("No transport client was created") client.start(on_request_receive) while is_running: ip_address = get_host_ip() update_known_printers(settings) status = HostStatus(online=True, ip=ip_address, host=host_name, update_s=settings.printer_check_period_s, printers=[ p.info() for p in printers.values() if p.info().model is not None ]) client.publish(status) time.sleep(settings.printer_check_period_s)
from mqtt2datapoint_converter import Mqtt2DataPointConverter from mqtt_client import MqttClient logging.basicConfig(level=os.environ.get("LOGLEVEL", "DEBUG")) def load_config(): with open('../config.json') as json_file: return json.load(json_file) def on_mqtt_message(topic, json_data): try: datapoints = Mqtt2DataPointConverter(config) \ .for_topic(topic) \ .with_json_data(json_data) \ .create_datapoint_db_mappings() for database in datapoints.keys(): influxDbClient.write_datapoints_to_database(datapoints[database], database) except Exception: logging.exception("Failed to process message on topic %s" % topic) config = load_config() influxDbClient = InfluxDbClient(config) mqttClient = MqttClient(config) mqttClient.on_message_callback(on_mqtt_message) mqttClient.start()