Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flow-based programming #1521

Open
wants to merge 43 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
2a992f6
flow: initial commit
eschava Feb 2, 2019
1c54ac7
flow: major refactoring + button component
eschava Feb 3, 2019
be7be8b
flow: compilation errors
eschava Feb 4, 2019
05938cf
flow: optimization: serve flow.json file right from the SPIFFS filesy…
eschava Feb 4, 2019
faf5cce
flow: optimization: serve flow.json file right from the SPIFFS filesy…
eschava Feb 4, 2019
e5795e8
flow: relay component
eschava Feb 4, 2019
ef966a1
flow: topic placeholders in MQTT components
eschava Feb 5, 2019
d1cebaa
flow: delay component
eschava Feb 5, 2019
70f0d05
flow: change component
eschava Feb 7, 2019
49e4c75
flow: timer and schedule components
eschava Feb 7, 2019
4f759c4
flow: chunked response to avoid out of memory for big number of compo…
eschava Feb 8, 2019
ed7a647
flow: start component
eschava Feb 9, 2019
ebb25da
flow: correct order of components in library
eschava Feb 9, 2019
d1c93b3
flow: gate component
eschava Feb 9, 2019
e19b859
flow: hysteresis component
eschava Feb 10, 2019
41d860a
flow: support compressed JSON in flow config
eschava Feb 10, 2019
a77132b
flow: schedule component - minor optimization
eschava Feb 10, 2019
5a8a33c
flow: sensor component
eschava Feb 13, 2019
66a125e
flow: sensor component
eschava Feb 13, 2019
001e3b9
flow: gate component
eschava Feb 13, 2019
7574de4
flow: memory optimizations and refactoring
eschava Feb 16, 2019
2a1a09c
flow: memory optimizations and refactoring
eschava Feb 16, 2019
cb2cb5e
flow: memory optimizations and refactoring
eschava Feb 18, 2019
f59ae97
flow: have Value property for start/timer/schedule components
eschava Feb 20, 2019
5bc2e7e
flow: setting load/save components
eschava Feb 20, 2019
61a0b77
flow: setting load/save components
eschava Feb 20, 2019
90b90a2
flow: use MQTT retained message to save flow config if SPIFFS is not …
eschava Feb 21, 2019
dd9509a
flow: use MQTT retained message to save flow config if SPIFFS is not …
eschava Feb 21, 2019
fe49b56
flow: math component
eschava Feb 21, 2019
327c924
Merge remote-tracking branch 'origin/dev' into flow
eschava Feb 22, 2019
64dfb47
better compression of flow
eschava Feb 22, 2019
5e8666c
flow: compare component
eschava Feb 22, 2019
2c8a044
flow: hysteresis component
eschava Feb 22, 2019
620e410
flow: different fixes
eschava Feb 28, 2019
f14f676
flow: light component
eschava Mar 2, 2019
95428dc
flow: use Ticker for delayed task execution
eschava Mar 6, 2019
80c6288
flow: use Ticker for delayed task execution
eschava Mar 7, 2019
c375ea3
flow: memory optimization and refactoring
eschava Mar 7, 2019
413b8d2
Merge remote-tracking branch 'origin/dev' into flow
eschava Mar 8, 2019
98394ba
flow: clear library after flow is loaded
eschava Mar 8, 2019
e0e8276
flow: memory optimization and refactoring
eschava Mar 8, 2019
d457811
Merge remote-tracking branch 'origin/dev' into flow
eschava Apr 23, 2019
3330d19
flow: terminal component
eschava Apr 23, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 57 additions & 4 deletions code/espurna/button.ino
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,17 @@ Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>
#include <DebounceEvent.h>
#include <vector>

#if FLOW_SUPPORT
class FlowButtonComponent; // forward declaration
#endif

typedef struct {
DebounceEvent * button;
unsigned long actions;
unsigned int relayID;
#if FLOW_SUPPORT
std::vector<FlowButtonComponent *> flow_components;
#endif
} button_t;

std::vector<button_t> _buttons;
Expand All @@ -42,6 +49,35 @@ bool _buttonWebSocketOnReceive(const char * key, JsonVariant& value) {

#endif

// -----------------------------------------------------------------------------
// FLOW
// -----------------------------------------------------------------------------

#if FLOW_SUPPORT

PROGMEM const char flow_data2[] = "Data";
PROGMEM const char* const flow_data2_array[] = {flow_data2};

PROGMEM const FlowConnections flow_button_component = {
0, NULL,
1, flow_data2_array,
};

class FlowButtonComponent : public FlowComponent {
public:
FlowButtonComponent(JsonObject& properties) {
int button_id = properties["Button"];
_buttons[button_id].flow_components.push_back(this);
}

void buttonEvent(unsigned char event) {
JsonVariant data((int)event);
processOutput(data, 0);
}
};

#endif // FLOW_SUPPORT

int buttonFromRelay(unsigned int relayID) {
for (unsigned int i=0; i < _buttons.size(); i++) {
if (_buttons[i].relayID == relayID) return i;
Expand Down Expand Up @@ -105,6 +141,12 @@ void buttonEvent(unsigned int id, unsigned char event) {
}
#endif

#if FLOW_SUPPORT
for (FlowButtonComponent* component : _buttons[id].flow_components) {
component->buttonEvent(event);
}
#endif

if (BUTTON_MODE_TOGGLE == action) {
if (_buttons[id].relayID > 0) {
relayToggle(_buttons[id].relayID - 1);
Expand All @@ -122,11 +164,11 @@ void buttonEvent(unsigned int id, unsigned char event) {
relayStatus(_buttons[id].relayID - 1, false);
}
}

if (BUTTON_MODE_AP == action) {
wifiStartAP();
}

if (BUTTON_MODE_RESET == action) {
deferredReset(100, CUSTOM_RESET_HARDWARE);
}
Expand All @@ -142,13 +184,13 @@ void buttonEvent(unsigned int id, unsigned char event) {
wifiStartWPS();
}
#endif // defined(JUSTWIFI_ENABLE_WPS)

#if defined(JUSTWIFI_ENABLE_SMARTCONFIG)
if (BUTTON_MODE_SMART_CONFIG == action) {
wifiStartSmartConfig();
}
#endif // defined(JUSTWIFI_ENABLE_SMARTCONFIG)

#if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
if (BUTTON_MODE_DIM_UP == action) {
lightBrightnessStep(1);
Expand Down Expand Up @@ -246,6 +288,17 @@ void buttonSetup() {
wsOnReceiveRegister(_buttonWebSocketOnReceive);
#endif

#if FLOW_SUPPORT
std::vector<String>* buttons = new std::vector<String>();
for (unsigned int i=0; i < _buttons.size(); i++) {
buttons->push_back(String(i));
}

flowRegisterComponent("Button", &flow_button_component,
(flow_component_factory_f)([] (JsonObject& properties) { return new FlowButtonComponent(properties); }));
flowRegisterComponentValues("BUTTON_VALUES", buttons);
#endif

// Register loop
espurnaRegisterLoop(buttonLoop);

Expand Down
12 changes: 12 additions & 0 deletions code/espurna/config/general.h
Original file line number Diff line number Diff line change
Expand Up @@ -1543,3 +1543,15 @@
#ifndef RFM69_IS_RFM69HW
#define RFM69_IS_RFM69HW 0
#endif

#ifndef FLOW_SUPPORT
#define FLOW_SUPPORT 0
#endif

#ifndef FLOW_SPIFFS_FILE
#define FLOW_SPIFFS_FILE "/flow.json"
#endif

#ifndef FLOW_MQTT_TOPIC
#define FLOW_MQTT_TOPIC "flow"
#endif
180 changes: 180 additions & 0 deletions code/espurna/config/progmem.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ PROGMEM const char espurna_modules[] =
#if WEB_SUPPORT
"WEB "
#endif
#if FLOW_SUPPORT
"FLOW "
#endif
"";

//--------------------------------------------------------------------------------
Expand Down Expand Up @@ -348,3 +351,180 @@ PROGMEM const char* const magnitude_units[] = {
};

#endif

// -----------------------------------------------------------------------------
// FLOW
// -----------------------------------------------------------------------------

#if FLOW_SUPPORT

PROGMEM const char flow_library_json[] =
"{"
"\"Start\": "
"{"
"\"name\":\"Start\","
"\"icon\":\"play\","
"\"inports\":[],"
"\"outports\":[{\"name\":\"Data\",\"type\":\"bool\"}],"
"\"properties\":[{\"name\":\"Value\",\"type\":\"any\"}]"
"}"
",\"Debug\": "
"{"
"\"name\":\"Debug\","
"\"icon\":\"bug\","
"\"inports\":[{\"name\":\"Data\",\"type\":\"any\"}],"
"\"outports\":[],"
"\"properties\":[{\"name\":\"Prefix\",\"type\":\"string\"}]"
"}"
",\"Change\": "
"{"
"\"name\":\"Change\","
"\"icon\":\"edit\","
"\"inports\":[{\"name\":\"Data\",\"type\":\"any\"}],"
"\"outports\":[{\"name\":\"Data\",\"type\":\"any\"}],"
"\"properties\":[{\"name\":\"Value\",\"type\":\"any\"}]"
"}"
",\"Math\": "
"{"
"\"name\":\"Math\","
"\"icon\":\"plus-circle\","
"\"inports\":[{\"name\":\"Input 1\",\"type\":\"any\"},{\"name\":\"Input 2\",\"type\":\"any\"}],"
"\"outports\":[{\"name\":\"Data\",\"type\":\"any\"}],"
"\"properties\":[{\"name\":\"Operation\",\"type\":\"list\","
"\"values\":[\"+\",\"-\",\"*\",\"/\"]}]"
"}"
",\"Compare\": "
"{"
"\"name\":\"Compare\","
"\"icon\":\"chevron-circle-right\","
"\"inports\":[{\"name\":\"Data\",\"type\":\"any\"},{\"name\":\"Test\",\"type\":\"any\"}],"
"\"outports\":[{\"name\":\"True\",\"type\":\"any\"},{\"name\":\"False\",\"type\":\"any\"}],"
"\"properties\":[{\"name\":\"Operation\",\"type\":\"list\",\"values\":[\"=\",\">\",\"<\"]},{\"name\":\"Test\",\"type\":\"any\"}]"
"}"
",\"Delay\": "
"{"
"\"name\":\"Delay\","
"\"icon\":\"pause\","
"\"inports\":[{\"name\":\"Data\",\"type\":\"any\"},{\"name\":\"Reset\",\"type\":\"any\"}],"
"\"outports\":[{\"name\":\"Data\",\"type\":\"any\"}],"
"\"properties\":[{\"name\":\"Seconds\",\"type\":\"int\"}, {\"name\":\"Last only\",\"type\":\"bool\"}]"
"}"
",\"Timer\": "
"{"
"\"name\":\"Timer\","
"\"icon\":\"clock-o\","
"\"inports\":[],"
"\"outports\":[{\"name\":\"Data\",\"type\":\"bool\"}],"
"\"properties\":[{\"name\":\"Seconds\",\"type\":\"int\"},{\"name\":\"Value\",\"type\":\"any\"}]"
"}"
",\"Gate\": "
"{"
"\"name\":\"Gate\","
"\"icon\":\"unlock\","
"\"inports\":[{\"name\":\"Data\",\"type\":\"any\"}, {\"name\":\"State\",\"type\":\"bool\"}],"
"\"outports\":[{\"name\":\"Open\",\"type\":\"any\"}, {\"name\":\"Close\",\"type\":\"any\"}],"
"\"properties\":[]"
"}"
",\"Hysteresis\": "
"{"
"\"name\":\"Hysteresis\","
"\"icon\":\"line-chart\","
"\"inports\":[{\"name\":\"Value\",\"type\":\"double\"}, {\"name\":\"Min\",\"type\":\"double\"}, {\"name\":\"Max\",\"type\":\"double\"}],"
"\"outports\":[{\"name\":\"Rise\",\"type\":\"double\"}, {\"name\":\"Fall\",\"type\":\"double\"}],"
"\"properties\":[{\"name\":\"Min\",\"type\":\"double\"}, {\"name\":\"Max\",\"type\":\"double\"}]"
"}"
",\"Save setting\": "
"{"
"\"name\":\"Save setting\","
"\"icon\":\"save\","
"\"inports\":[{\"name\":\"Value\",\"type\":\"string\"}],"
"\"outports\":[],"
"\"properties\":[{\"name\":\"Name\",\"type\":\"string\"}]"
"}"
",\"Load setting\": "
"{"
"\"name\":\"Load setting\","
"\"icon\":\"database\","
"\"inports\":[{\"name\":\"Name\",\"type\":\"string\"}],"
"\"outports\":[{\"name\":\"Value\",\"type\":\"string\"}],"
"\"properties\":[{\"name\":\"Default\",\"type\":\"string\"}]"
"}"
#if TERMINAL_SUPPORT
",\"Terminal\": "
"{"
"\"name\":\"Terminal\","
"\"icon\":\"terminal\","
"\"inports\":[{\"name\":\"Run\",\"type\":\"any\"}, {\"name\":\"Command\",\"type\":\"string\"}],"
"\"outports\":[],"
"\"properties\":[{\"name\":\"Command\",\"type\":\"string\"}]"
"}"
#endif
#if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
",\"Light\": "
"{"
"\"name\":\"Light\","
"\"icon\":\"sun-o\","
"\"inports\":[{\"name\":\"Color\",\"type\":\"string\"}, {\"name\":\"Brightness\",\"type\":\"int\"}],"
"\"outports\":[],"
"\"properties\":[]"
"}"
#endif
",\"Relay\": "
"{"
"\"name\":\"Relay\","
"\"icon\":\"lightbulb-o\","
"\"inports\":[{\"name\":\"State\",\"type\":\"bool\"}, {\"name\":\"Toggle\",\"type\":\"any\"}],"
"\"outports\":[],"
"\"properties\":[{\"name\":\"Relay\",\"type\":\"list\",\"values\":[%RELAY_VALUES%]}]"
"}"
#if BUTTON_SUPPORT
",\"Button\": "
"{"
"\"name\":\"Button\","
"\"icon\":\"toggle-on\","
"\"inports\":[],"
"\"outports\":[{\"name\":\"Data\",\"type\":\"int\"}],"
"\"properties\":[{\"name\":\"Button\",\"type\":\"list\",\"values\":[%BUTTON_VALUES%]}]"
"}"
#endif
#if MQTT_SUPPORT
",\"MQTT subscribe\": "
"{"
"\"name\":\"MQTT subscribe\","
"\"icon\":\"sign-out\","
"\"inports\":[],"
"\"outports\":[{\"name\":\"Data\",\"type\":\"string\"}],"
"\"properties\":[{\"name\":\"Topic\",\"type\":\"string\"}]"
"}"
",\"MQTT publish\": "
"{"
"\"name\":\"MQTT publish\","
"\"icon\":\"sign-in\","
"\"inports\":[{\"name\":\"Data\",\"type\":\"string\"}],"
"\"outports\":[],"
"\"properties\":[{\"name\":\"Topic\",\"type\":\"string\"}, {\"name\":\"Retain\",\"type\":\"bool\"}]"
"}"
#endif
#if SENSOR_SUPPORT
",\"Sensor\": "
"{"
"\"name\":\"Sensor\","
"\"icon\":\"thermometer-3\","
"\"inports\":[],"
"\"outports\":[{\"name\":\"Data\",\"type\":\"double\"}],"
"\"properties\":[{\"name\":\"Sensor\",\"type\":\"list\",\"values\":[%SENSOR_VALUES%]}]"
"}"
#endif
#if SCHEDULER_SUPPORT
",\"Schedule\": "
"{"
"\"name\":\"Schedule\","
"\"icon\":\"calendar\","
"\"inports\":[],"
"\"outports\":[{\"name\":\"Data\",\"type\":\"bool\"}],"
"\"properties\":[{\"name\":\"Time\",\"type\":\"time\"}, {\"name\":\"Weekdays\",\"type\":\"weekdays\"},{\"name\":\"Value\",\"type\":\"any\"}]"
"}"
#endif
"}";

#endif
12 changes: 12 additions & 0 deletions code/espurna/config/prototypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,15 @@ bool wifiConnected();
#define thermostat_callback_f void *
#endif

// -----------------------------------------------------------------------------
// FLOW
// -----------------------------------------------------------------------------

#if FLOW_SUPPORT
#include "flow.h"
typedef std::function<FlowComponent* (JsonObject&)> flow_component_factory_f;
void flowRegisterComponentValues(String placeholder, std::vector<String>* values);
#else
#define FlowConnections void
#define flow_component_factory_f void *
#endif
4 changes: 4 additions & 0 deletions code/espurna/espurna.ino
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ void setup() {
#if THERMOSTAT_DISPLAY_SUPPORT
displaySetup();
#endif
#if FLOW_SUPPORT
// after all other components are set up
flowSetup();
#endif


// 3rd party code hook
Expand Down
Loading