mirror of
https://gitlab.com/wirelos/sprocket-plugin-rcswitch.git
synced 2025-12-14 14:01:26 +01:00
rf switch plugin
This commit is contained in:
48
src/RcSwitchConfig.h
Normal file
48
src/RcSwitchConfig.h
Normal file
@@ -0,0 +1,48 @@
|
||||
#ifndef __RC_SWITCH_CONFIG__
|
||||
#define __RC_SWITCH_CONFIG__
|
||||
|
||||
#include <JsonStruct.h>
|
||||
#include "utils/print.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace std::placeholders;
|
||||
|
||||
struct RcSwitchConfig
|
||||
{
|
||||
int txPin;
|
||||
};
|
||||
|
||||
struct RcSwitchPayload
|
||||
{
|
||||
const char *group;
|
||||
const char *device;
|
||||
};
|
||||
|
||||
struct RcSwitchPayloadJson : public RcSwitchPayload, public JsonStruct
|
||||
{
|
||||
int verifyJsonObject(JsonObject &json)
|
||||
{
|
||||
return json.containsKey("group")
|
||||
&& json.containsKey("device")
|
||||
&& json.success();
|
||||
};
|
||||
void mapJsonObject(JsonObject &root)
|
||||
{
|
||||
root["group"] = group;
|
||||
root["device"] = device;
|
||||
}
|
||||
void fromJsonObject(JsonObject &json)
|
||||
{
|
||||
if (!verifyJsonObject(json))
|
||||
{
|
||||
Serial.println("ERROR: cannot parse JSON object");
|
||||
valid = 0;
|
||||
return;
|
||||
}
|
||||
group = getAttr(json, "group", group);
|
||||
device = getAttr(json, "device", device);
|
||||
valid = 1;
|
||||
};
|
||||
};
|
||||
|
||||
#endif
|
||||
34
src/RcSwitchPlugin.cpp
Normal file
34
src/RcSwitchPlugin.cpp
Normal file
@@ -0,0 +1,34 @@
|
||||
#include "RcSwitchPlugin.h"
|
||||
|
||||
RcSwitchPlugin::RcSwitchPlugin(RcSwitchConfig cfg)
|
||||
{
|
||||
config = cfg;
|
||||
rcSwitch = new RCSwitch();
|
||||
}
|
||||
|
||||
void RcSwitchPlugin::activate(Scheduler *scheduler)
|
||||
{
|
||||
rcSwitch->enableTransmit(config.txPin);
|
||||
subscribe("rf/switch/on", bind(&RcSwitchPlugin::switchOn, this, _1));
|
||||
subscribe("rf/switch/off", bind(&RcSwitchPlugin::switchOff, this, _1));
|
||||
PRINT_MSG(Serial, "RC", "plugin activated");
|
||||
}
|
||||
|
||||
void RcSwitchPlugin::switchOn(String msg)
|
||||
{
|
||||
Serial.println("Switch ON");
|
||||
RcSwitchPayloadJson payload;
|
||||
payload.fromJsonString(msg);
|
||||
if(payload.valid){
|
||||
rcSwitch->switchOn(payload.group, payload.device);
|
||||
}
|
||||
}
|
||||
void RcSwitchPlugin::switchOff(String msg)
|
||||
{
|
||||
Serial.println("Switch OFF");
|
||||
RcSwitchPayloadJson payload;
|
||||
payload.fromJsonString(msg);
|
||||
if(payload.valid){
|
||||
rcSwitch->switchOff(payload.group, payload.device);
|
||||
}
|
||||
}
|
||||
27
src/RcSwitchPlugin.h
Normal file
27
src/RcSwitchPlugin.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#ifndef __RC_SWITCH_PLUGIN__
|
||||
#define __RC_SWITCH_PLUGIN__
|
||||
|
||||
#define _TASK_SLEEP_ON_IDLE_RUN
|
||||
#define _TASK_STD_FUNCTION
|
||||
|
||||
#include <Plugin.h>
|
||||
#include <RcSwitchConfig.h>
|
||||
#include <RCSwitch.h>
|
||||
#include "utils/print.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace std::placeholders;
|
||||
|
||||
|
||||
class RcSwitchPlugin : public Plugin
|
||||
{
|
||||
public:
|
||||
RCSwitch *rcSwitch;
|
||||
RcSwitchConfig config;
|
||||
RcSwitchPlugin(RcSwitchConfig cfg);
|
||||
void activate(Scheduler *scheduler);
|
||||
void switchOn(String msg);
|
||||
void switchOff(String msg);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,11 +0,0 @@
|
||||
#include "TemplatePlugin.h"
|
||||
|
||||
TemplatePlugin::TemplatePlugin(TemplateConfig cfg)
|
||||
{
|
||||
templateConfig = cfg;
|
||||
}
|
||||
|
||||
void TemplatePlugin::activate(Scheduler *scheduler)
|
||||
{
|
||||
PRINT_MSG(Serial, "TEMPLATE", "plugin activated");
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
#ifndef __TEMPLATE_PLUGIN__
|
||||
#define __TEMPLATE_PLUGIN__
|
||||
|
||||
#define _TASK_SLEEP_ON_IDLE_RUN
|
||||
#define _TASK_STD_FUNCTION
|
||||
|
||||
#include <Plugin.h>
|
||||
#include "utils/utils_print.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace std::placeholders;
|
||||
|
||||
struct TemplateConfig
|
||||
{
|
||||
const char *var;
|
||||
};
|
||||
|
||||
class TemplatePlugin : public Plugin
|
||||
{
|
||||
public:
|
||||
TemplateConfig templateConfig;
|
||||
TemplatePlugin(TemplateConfig cfg);
|
||||
void activate(Scheduler *scheduler);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,26 +1,13 @@
|
||||
#include "config.h"
|
||||
#include "WiFiNet.h"
|
||||
#include "Sprocket.h"
|
||||
#include "TemplatePlugin.h"
|
||||
#include "RcSwitchPlugin.h"
|
||||
|
||||
WiFiNet *network;
|
||||
Sprocket *sprocket;
|
||||
|
||||
void setup()
|
||||
{
|
||||
sprocket = new Sprocket({STARTUP_DELAY, SERIAL_BAUD_RATE});
|
||||
sprocket->addPlugin(new TemplatePlugin({"sprocket"}));
|
||||
|
||||
network = new WiFiNet(
|
||||
SPROCKET_MODE,
|
||||
STATION_SSID,
|
||||
STATION_PASSWORD,
|
||||
AP_SSID,
|
||||
AP_PASSWORD,
|
||||
HOSTNAME,
|
||||
CONNECT_TIMEOUT);
|
||||
network->connect();
|
||||
|
||||
sprocket->addPlugin(new RcSwitchPlugin({D4}));
|
||||
sprocket->activate();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user