Skip to content

Commit

Permalink
Also send device ID with peripheral init
Browse files Browse the repository at this point in the history
  • Loading branch information
lptr committed Jan 3, 2024
1 parent 7d99c5f commit 1f90683
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/devices/Device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
15 changes: 8 additions & 7 deletions src/devices/Peripheral.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class PeripheralFactoryBase {
: type(type) {
}

virtual unique_ptr<PeripheralBase> createPeripheral(const String& name, const String& jsonConfig, shared_ptr<MqttDriver::MqttRoot> mqttRoot) = 0;
virtual unique_ptr<PeripheralBase> createPeripheral(const String& deviceId, const String& name, const String& jsonConfig, shared_ptr<MqttDriver::MqttRoot> mqttRoot) = 0;

const String type;
};
Expand All @@ -111,7 +111,7 @@ class PeripheralFactory : public PeripheralFactoryBase {
, deviceConfigArgs(std::forward<TDeviceConfigArgs>(deviceConfigArgs)...) {
}

unique_ptr<PeripheralBase> createPeripheral(const String& name, const String& jsonConfig, shared_ptr<MqttDriver::MqttRoot> mqttRoot) override {
unique_ptr<PeripheralBase> createPeripheral(const String& deviceId, const String& name, const String& jsonConfig, shared_ptr<MqttDriver::MqttRoot> mqttRoot) override {
// Use short prefix because SPIFFS has a 32 character limit
ConfigurationFile<TConfig>* configFile = new ConfigurationFile<TConfig>(FileSystem::get(), "/p/" + name);
mqttRoot->subscribe("config", [name, configFile](const String&, const JsonObject& configJson) {
Expand All @@ -126,7 +126,8 @@ class PeripheralFactory : public PeripheralFactoryBase {
deviceConfig.loadFromString(jsonConfig);
unique_ptr<Peripheral<TConfig>> 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);
});
Expand Down Expand Up @@ -155,7 +156,7 @@ class PeripheralManager
factories.insert(std::make_pair(factory.type, std::reference_wrapper<PeripheralFactoryBase>(factory)));
}

void begin() {
void createPeripherals(const String& deviceId) {
Log.infoln("Loading configuration for %d peripherals",
peripheralsConfig.get().size());

Expand All @@ -165,7 +166,7 @@ class PeripheralManager
const String& name = deviceConfig.name.get();
const String& type = deviceConfig.type.get();
try {
unique_ptr<PeripheralBase> peripheral = createPeripheral(name, type, deviceConfig.params.get().get());
unique_ptr<PeripheralBase> 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",
Expand All @@ -188,7 +189,7 @@ class PeripheralManager
Property<JsonAsString> params { this, "params" };
};

unique_ptr<PeripheralBase> createPeripheral(const String& name, const String& type, const String& configJson) {
unique_ptr<PeripheralBase> 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);
Expand All @@ -197,7 +198,7 @@ class PeripheralManager
}
shared_ptr<MqttDriver::MqttRoot> 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;
Expand Down

0 comments on commit 1f90683

Please sign in to comment.