Skip to content

Commit

Permalink
Updates for version 0.1.1 (#18)
Browse files Browse the repository at this point in the history
* copyright on sonar code
* add HADependenciesArray class
* HADiscovery: make Entry struct to improve API
* update library version -> 0.1.1
  • Loading branch information
chl33 authored Jul 20, 2024
1 parent 9f0cc97 commit f3d11f5
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 16 deletions.
26 changes: 26 additions & 0 deletions include/og3/ha_dependencies.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,30 @@ class HADependencies : public Dependencies {
HADiscovery* m_ha_discovery = nullptr;
};

template <unsigned N>
class HADependenciesArray : public HADependencies {
public:
HADependenciesArray(const std::array<const char*, N>& module_names) : m_more_deps(module_names) {}
bool resolve(const NameToModule& name_to_module) override;
size_t num_depends_on() const override {
return HADependencies::num_depends_on() + m_more_deps.num_depends_on();
}
const Module* depends_on(size_t idx) const override {
const auto d1 = HADependencies::num_depends_on();
return idx < d1 ? HADependencies::depends_on(idx) : m_more_deps.depends_on(idx - d1);
}

MqttManager* mqtt_manager() { return m_mqtt_manager; }
const MqttManager* mqtt_manager() const { return m_mqtt_manager; }
HADiscovery* ha_discovery() { return m_ha_discovery; }
const HADiscovery* ha_discovery() const { return m_ha_discovery; }

const bool ok() { return m_mqtt_manager && m_ha_discovery; }

protected:
MqttManager* m_mqtt_manager = nullptr;
HADiscovery* m_ha_discovery = nullptr;
DependenciesArray<N> m_more_deps;
};

} // namespace og3
14 changes: 14 additions & 0 deletions include/og3/ha_discovery.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,20 @@ class HADiscovery : public Module {
HADiscovery(const Options& opts, ModuleSystem* module_system);

void addRoot(JsonDocument* json, const char* device_name);

struct Entry {
Entry(const VariableBase& var_, const char* device_type_, const char* device_class_)
: var(var_), device_type(device_type_), device_class(device_class_) {}

const VariableBase& var;
const char* device_type;
const char* device_class;
const char* value_template = nullptr;
const char* subject_topic = nullptr;
const char* device_name = nullptr;
const char* name_prefix = nullptr;
};
bool addEntry(JsonDocument* json, const Entry& entry);
bool addEntry(JsonDocument* json, const VariableBase& var, const char* device_type,
const char* device_class, const char* value_template, const char* subject_topic,
const char* device_name);
Expand Down
3 changes: 3 additions & 0 deletions include/og3/sonar.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) 2024 Chris Lee and contibuters.
// Licensed under the MIT license. See LICENSE file in the project root for details.

#pragma once

#include <math.h>
Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "og3",
"version": "0.1.0",
"version": "0.1.1",
"description": "A library for esp projects",
"keywords": "esp32, esp8266, modules, tasks, mqtt",
"authors": [
Expand Down
45 changes: 30 additions & 15 deletions src/ha_discovery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,38 +125,53 @@ void HADiscovery::addRoot(JsonDocument* json, const char* device_name) {
#endif
}

bool HADiscovery::addEntry(JsonDocument* json, const VariableBase& var, const char* device_type,
const char* device_class, const char* value_template,
const char* subject_topic, const char* device_name) {
bool HADiscovery::addEntry(JsonDocument* json, const HADiscovery::Entry& entry) {
if (!enabled()) {
return true;
}
auto& js = *json;
js.clear();
addRoot(json, device_name);
addRoot(json, entry.device_name);

char value[128];
if (strOk(var.units())) {
js["unit_of_meas"] = var.units();
if (strOk(entry.var.units())) {
js["unit_of_meas"] = entry.var.units();
}
{
const char* subject = subject_topic ? subject_topic : "~";
if (!var.group()) {
const char* subject = entry.subject_topic ? entry.subject_topic : "~";
if (!entry.var.group()) {
js["stat_t"] = subject;
} else {
snprintf(value, sizeof(value), "%s/%s", subject, var.group()->name());
snprintf(value, sizeof(value), "%s/%s", subject, entry.var.group()->name());
js["stat_t"] = value;
}
}
js["val_tpl"] = value_template;
if (device_class) {
js["dev_cla"] = device_class;
js["val_tpl"] = entry.value_template;
if (entry.device_class) {
js["dev_cla"] = entry.device_class;
}
char var_name[64];
if (entry.name_prefix) {
snprintf(var_name, sizeof(var_name), "%s_%s", entry.name_prefix, entry.var.name());
} else {
snprintf(var_name, sizeof(var_name), "%s", entry.var.name());
}
js["name"] = var.name();
snprintf(value, sizeof(value), "%s_%s", m_device_id, var.name());
js["name"] = entry.var.name();
snprintf(value, sizeof(value), "%s_%s", m_device_id, var_name);
js["uniq_id"] = value;

return mqttSendConfig(var.name(), device_type, json);
return mqttSendConfig(var_name, entry.device_type, json);
}

bool HADiscovery::addEntry(JsonDocument* json, const VariableBase& var, const char* device_type,
const char* device_class, const char* value_template,
const char* subject_topic, const char* device_name) {
Entry entry(var, device_type, device_class);
entry.device_class = device_class;
entry.value_template = value_template;
entry.subject_topic = subject_topic;
entry.device_name = device_name;
return addEntry(json, entry);
}

bool HADiscovery::addMeas(JsonDocument* json, const VariableBase& var, const char* device_type,
Expand Down
3 changes: 3 additions & 0 deletions src/sonar.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) 2024 Chris Lee and contibuters.
// Licensed under the MIT license. See LICENSE file in the project root for details.

#include "og3/sonar.h"

#include "og3/units.h"
Expand Down

0 comments on commit f3d11f5

Please sign in to comment.