From 264c1c1e80bad01bdaf35e1a330d7a70a0eef26b Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Fri, 3 Oct 2025 17:02:28 +0200 Subject: [PATCH] separate rc-gateway devices --- include/devices.h | 70 +----------------------------------------- include/rc_devices.h | 73 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 69 deletions(-) create mode 100644 include/rc_devices.h diff --git a/include/devices.h b/include/devices.h index 4f810d6..a4e1124 100644 --- a/include/devices.h +++ b/include/devices.h @@ -1,20 +1,9 @@ #pragma once -#define MAIN_DEVICE_ID "rc-gateway" - -#include "esp.h" -#include "ha.h" +#include "rc_devices.h" using namespace Ha; -typedef unordered_multimap mapswitches; - -mapswitches onSwitches; -mapswitches offSwitches; -unordered_map p1Switches; - -auto gatewayDevice = &DeviceConfig::create(MAIN_DEVICE_ID).withName("RC Gateway").withManufacturer("Adafruit").withModel("Huzzah Esp8266"); - auto roomSensor = Builder::instance(TEMP_SENSOR) .asDevice(&DeviceConfig::create(TEMP_SENSOR) .withName("Servers room") @@ -63,63 +52,6 @@ auto presenceTracker = Builder::instance(PRESENCE_SENSOR) .withDeviceClass("presence") .build(); -struct PollinSwitch : Switch { - constexpr static const char* man = "Pollin"; - const char* group; - unsigned char channel; - - PollinSwitch(const char* group, const unsigned char channel, const char* name = nullptr, const char* area = nullptr) - : Switch(nullptr, [group, channel]{ - // copy id from string into a new pointer, to avoid memory leaks - return (new string{Protocol_1::buildId(group, channel)})->c_str(); - }()), group(group), channel(channel) { - if (!name) name = (new string{string(man).append(" ").append(id)})->c_str(); - mainDevice = &DeviceConfig::create(id).withName(name).withManufacturer(man).withArea(area).withParent(gatewayDevice); - withStateTopic(); - deviceClass = "outlet"; - p1Switches.insert({ string(id), this }); - } - - void onCommand(const char* msg) override { - strcmp("ON", msg) == 0 ? mySwitch.switchOn(group, channel) : mySwitch.switchOff(group, channel); - publisher(stateTopic, msg); - } - -}; - -struct EasyHomeSwitch : Switch { - unsigned long on[8] = { 4326554, 4537114, 4767530, 4972714 }; - unsigned long off[8] = { 4483146, 4626810, 4661562, 4819642 }; - - EasyHomeSwitch(const char remotePosition, unsigned long on[4], unsigned long off[4], const char* name = nullptr, const char* area = nullptr) - : Switch(nullptr, [remotePosition] { - auto uId = new string("easy_home_"); - (*uId) += tolower(remotePosition); - return uId->c_str(); - }()) { - memcpy(&this->on[4], on, 4 * sizeof(unsigned long)); - memcpy(&this->off[4], off, 4 * sizeof(unsigned long)); - if (!name) { - auto n = new string("Easy Home "); - (*n) += remotePosition; - name = n->c_str(); - } - mainDevice = &DeviceConfig::create(id).withName(name).withManufacturer("Intertek").withModel("Easy Home").withArea(area).withParent(gatewayDevice); - withStateTopic(); - deviceClass = "outlet"; - for (int i = 0; i < 8; i++) { - onSwitches.insert({ this->on[i], this }); - offSwitches.insert({ this->off[i], this }); - } - } - - void onCommand(const char* msg) override { - mySwitch.setProtocol(4); - strcmp("ON", msg) == 0 ? mySwitch.send(on[4], 24) : mySwitch.send(off[4], 24); - publisher(stateTopic, msg); - } -}; - Command* commands[] = { HaESP::restartButton() .asDevice(gatewayDevice) diff --git a/include/rc_devices.h b/include/rc_devices.h new file mode 100644 index 0000000..f6da508 --- /dev/null +++ b/include/rc_devices.h @@ -0,0 +1,73 @@ +#pragma once + +#define MAIN_DEVICE_ID "rc-gateway" + +#include "esp.h" +#include "ha.h" + +using namespace Ha; + +typedef unordered_multimap mapswitches; + +mapswitches onSwitches; +mapswitches offSwitches; +unordered_map p1Switches; + +auto gatewayDevice = &DeviceConfig::create(MAIN_DEVICE_ID).withName("RC Gateway").withManufacturer("Adafruit").withModel("Huzzah Esp8266"); + +struct PollinSwitch : Switch { + constexpr static const char* man = "Pollin"; + const char* group; + unsigned char channel; + + PollinSwitch(const char* group, const unsigned char channel, const char* name = nullptr, const char* area = nullptr) + : Switch(nullptr, [group, channel]{ + // copy id from string into a new pointer, to avoid memory leaks + return (new string{Protocol_1::buildId(group, channel)})->c_str(); + }()), group(group), channel(channel) { + if (!name) name = (new string{string(man).append(" ").append(id)})->c_str(); + mainDevice = &DeviceConfig::create(id).withName(name).withManufacturer(man).withArea(area).withParent(gatewayDevice); + withStateTopic(); + deviceClass = "outlet"; + p1Switches.insert({ string(id), this }); + } + + void onCommand(const char* msg) override { + strcmp("ON", msg) == 0 ? mySwitch.switchOn(group, channel) : mySwitch.switchOff(group, channel); + publisher(stateTopic, msg); + } + +}; + +struct EasyHomeSwitch : Switch { + unsigned long on[8] = { 4326554, 4537114, 4767530, 4972714 }; + unsigned long off[8] = { 4483146, 4626810, 4661562, 4819642 }; + + EasyHomeSwitch(const char remotePosition, unsigned long on[4], unsigned long off[4], const char* name = nullptr, const char* area = nullptr) + : Switch(nullptr, [remotePosition] { + auto uId = new string("easy_home_"); + (*uId) += tolower(remotePosition); + return uId->c_str(); + }()) { + memcpy(&this->on[4], on, 4 * sizeof(unsigned long)); + memcpy(&this->off[4], off, 4 * sizeof(unsigned long)); + if (!name) { + auto n = new string("Easy Home "); + (*n) += remotePosition; + name = n->c_str(); + } + mainDevice = &DeviceConfig::create(id).withName(name).withManufacturer("Intertek").withModel("Easy Home").withArea(area).withParent(gatewayDevice); + withStateTopic(); + deviceClass = "outlet"; + for (int i = 0; i < 8; i++) { + onSwitches.insert({ this->on[i], this }); + offSwitches.insert({ this->off[i], this }); + } + } + + void onCommand(const char* msg) override { + mySwitch.setProtocol(4); + strcmp("ON", msg) == 0 ? mySwitch.send(on[4], 24) : mySwitch.send(off[4], 24); + publisher(stateTopic, msg); + } +};