From 1f906839651637f13705b638a1c1a7eb079206f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=B3r=C3=A1nt=20Pint=C3=A9r?= Date: Wed, 3 Jan 2024 22:21:14 +0100 Subject: [PATCH] Also send device ID with peripheral init --- src/devices/Device.hpp | 6 +++--- src/devices/Peripheral.hpp | 15 ++++++++------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/devices/Device.hpp b/src/devices/Device.hpp index 91b76d8d..ac4797bc 100644 --- a/src/devices/Device.hpp +++ b/src/devices/Device.hpp @@ -241,21 +241,21 @@ class Device : ConsoleProvider { // We want RTC to be in sync before we start setting up peripherals kernel.getRtcInSyncState().awaitSet(); - peripheralManager.begin(); + peripheralManager.createPeripherals("ugly-duckling/" + deviceConfig.instance.get()); kernel.getKernelReadyState().awaitSet(); mqttDeviceRoot->publish( "init", [&](JsonObject& json) { - // TODO Remove redundanty mentions of "ugly-duckling" + // TODO Remove redundant mentions of "ugly-duckling" json["type"] = "ugly-duckling"; json["model"] = deviceConfig.model.get(); json["instance"] = deviceConfig.instance.get(); json["mac"] = getMacAddress(); auto device = json.createNestedObject("deviceConfig"); deviceConfig.store(device, false); - // TODO Remove redundanty mentions of "ugly-duckling" + // TODO Remove redundant mentions of "ugly-duckling" json["app"] = "ugly-duckling"; json["version"] = kernel.version; json["wakeup"] = esp_sleep_get_wakeup_cause(); diff --git a/src/devices/Peripheral.hpp b/src/devices/Peripheral.hpp index 57b38818..143266aa 100644 --- a/src/devices/Peripheral.hpp +++ b/src/devices/Peripheral.hpp @@ -97,7 +97,7 @@ class PeripheralFactoryBase { : type(type) { } - virtual unique_ptr createPeripheral(const String& name, const String& jsonConfig, shared_ptr mqttRoot) = 0; + virtual unique_ptr createPeripheral(const String& deviceId, const String& name, const String& jsonConfig, shared_ptr mqttRoot) = 0; const String type; }; @@ -111,7 +111,7 @@ class PeripheralFactory : public PeripheralFactoryBase { , deviceConfigArgs(std::forward(deviceConfigArgs)...) { } - unique_ptr createPeripheral(const String& name, const String& jsonConfig, shared_ptr mqttRoot) override { + unique_ptr createPeripheral(const String& deviceId, const String& name, const String& jsonConfig, shared_ptr mqttRoot) override { // Use short prefix because SPIFFS has a 32 character limit ConfigurationFile* configFile = new ConfigurationFile(FileSystem::get(), "/p/" + name); mqttRoot->subscribe("config", [name, configFile](const String&, const JsonObject& configJson) { @@ -126,7 +126,8 @@ class PeripheralFactory : public PeripheralFactoryBase { deviceConfig.loadFromString(jsonConfig); unique_ptr> peripheral = createPeripheral(name, deviceConfig, mqttRoot); peripheral->configure(configFile->config); - mqttRoot->publish("init", [&configFile](JsonObject& json) { + mqttRoot->publish("init", [&](JsonObject& json) { + json["device"] = deviceId; auto config = json.createNestedObject("config"); configFile->config.store(config, false); }); @@ -155,7 +156,7 @@ class PeripheralManager factories.insert(std::make_pair(factory.type, std::reference_wrapper(factory))); } - void begin() { + void createPeripherals(const String& deviceId) { Log.infoln("Loading configuration for %d peripherals", peripheralsConfig.get().size()); @@ -165,7 +166,7 @@ class PeripheralManager const String& name = deviceConfig.name.get(); const String& type = deviceConfig.type.get(); try { - unique_ptr peripheral = createPeripheral(name, type, deviceConfig.params.get().get()); + unique_ptr peripheral = createPeripheral(deviceId, name, type, deviceConfig.params.get().get()); peripherals.push_back(move(peripheral)); } catch (const PeripheralCreationException& e) { Log.errorln("Failed to create peripheral: %s of type %s because %s", @@ -188,7 +189,7 @@ class PeripheralManager Property params { this, "params" }; }; - unique_ptr createPeripheral(const String& name, const String& type, const String& configJson) { + unique_ptr createPeripheral(const String& deviceId, const String& name, const String& type, const String& configJson) { Log.traceln("Creating peripheral: %s of type %s", name.c_str(), type.c_str()); auto it = factories.find(type); @@ -197,7 +198,7 @@ class PeripheralManager } shared_ptr mqttRoot = mqtt.forRoot("peripherals/" + type + "/" + name); PeripheralFactoryBase& factory = it->second.get(); - return factory.createPeripheral(name, configJson, mqttRoot); + return factory.createPeripheral(deviceId, name, configJson, mqttRoot); } MqttDriver& mqtt;