Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

317 lines
8.4 KiB

  1. /*
  2. RPN RULES MODULE
  3. Use RPNLib library (https://github.com/xoseperez/rpnlib)
  4. Copyright (C) 2019 by Xose Pérez <xose dot perez at gmail dot com>
  5. */
  6. #if RPN_RULES_SUPPORT
  7. #include "relay.h"
  8. #include <rpnlib.h>
  9. // -----------------------------------------------------------------------------
  10. // Custom commands
  11. // -----------------------------------------------------------------------------
  12. rpn_context _rpn_ctxt;
  13. bool _rpn_run = false;
  14. unsigned long _rpn_delay = RPN_DELAY;
  15. unsigned long _rpn_last = 0;
  16. // -----------------------------------------------------------------------------
  17. bool _rpnWebSocketOnKeyCheck(const char * key, JsonVariant& value) {
  18. return (strncmp(key, "rpn", 3) == 0);
  19. }
  20. void _rpnWebSocketOnConnected(JsonObject& root) {
  21. root["rpnSticky"] = getSetting("rpnSticky", 1).toInt();
  22. root["rpnDelay"] = getSetting("rpnDelay", RPN_DELAY).toInt();
  23. JsonArray& rules = root.createNestedArray("rpnRules");
  24. unsigned char i = 0;
  25. String rule = getSetting("rpnRule", i, "");
  26. while (rule.length()) {
  27. rules.add(rule);
  28. rule = getSetting("rpnRule", ++i, "");
  29. }
  30. #if MQTT_SUPPORT
  31. i=0;
  32. JsonArray& topics = root.createNestedArray("rpnTopics");
  33. JsonArray& names = root.createNestedArray("rpnNames");
  34. String rpn_topic = getSetting("rpnTopic", i, "");
  35. while (rpn_topic.length() > 0) {
  36. String rpn_name = getSetting("rpnName", i, "");
  37. topics.add(rpn_topic);
  38. names.add(rpn_name);
  39. rpn_topic = getSetting("rpnTopic", ++i, "");
  40. }
  41. #endif
  42. }
  43. #if MQTT_SUPPORT
  44. void _rpnMQTTSubscribe() {
  45. unsigned char i = 0;
  46. String rpn_topic = getSetting("rpnTopic", i, "");
  47. while (rpn_topic.length()) {
  48. mqttSubscribeRaw(rpn_topic.c_str());
  49. rpn_topic = getSetting("rpnTopic", ++i, "");
  50. }
  51. }
  52. void _rpnMQTTCallback(unsigned int type, const char * topic, const char * payload) {
  53. if (type == MQTT_CONNECT_EVENT) {
  54. _rpnMQTTSubscribe();
  55. }
  56. if (type == MQTT_MESSAGE_EVENT) {
  57. unsigned char i = 0;
  58. String rpn_topic = getSetting("rpnTopic", i, "");
  59. while (rpn_topic.length()) {
  60. if (rpn_topic.equals(topic)) {
  61. String rpn_name = getSetting("rpnName", i, "");
  62. if (rpn_name.length()) {
  63. rpn_variable_set(_rpn_ctxt, rpn_name.c_str(), atof(payload));
  64. _rpn_last = millis();
  65. _rpn_run = true;
  66. break;
  67. }
  68. }
  69. rpn_topic = getSetting("rpnTopic", ++i, "");
  70. }
  71. }
  72. }
  73. #endif // MQTT_SUPPORT
  74. void _rpnConfigure() {
  75. #if MQTT_SUPPORT
  76. if (mqttConnected()) _rpnMQTTSubscribe();
  77. #endif
  78. _rpn_delay = getSetting("rpnDelay", RPN_DELAY).toInt();
  79. }
  80. void _rpnBrokerCallback(const String& topic, unsigned char id, double value, const char*) {
  81. char name[32] = {0};
  82. snprintf(name, sizeof(name), "%s%u", topic.c_str(), id);
  83. rpn_variable_set(_rpn_ctxt, name, value);
  84. _rpn_last = millis();
  85. _rpn_run = true;
  86. }
  87. void _rpnBrokerStatus(const String& topic, unsigned char id, unsigned int value) {
  88. _rpnBrokerCallback(topic, id, double(value), nullptr);
  89. }
  90. void _rpnInit() {
  91. // Init context
  92. rpn_init(_rpn_ctxt);
  93. // Time functions
  94. rpn_operator_set(_rpn_ctxt, "now", 0, [](rpn_context & ctxt) {
  95. if (!ntpSynced()) return false;
  96. rpn_stack_push(ctxt, now());
  97. return true;
  98. });
  99. rpn_operator_set(_rpn_ctxt, "utc", 0, [](rpn_context & ctxt) {
  100. if (!ntpSynced()) return false;
  101. rpn_stack_push(ctxt, ntpLocal2UTC(now()));
  102. return true;
  103. });
  104. rpn_operator_set(_rpn_ctxt, "dow", 1, [](rpn_context & ctxt) {
  105. float a;
  106. rpn_stack_pop(ctxt, a);
  107. unsigned char dow = (weekday(int(a)) + 5) % 7;
  108. rpn_stack_push(ctxt, dow);
  109. return true;
  110. });
  111. rpn_operator_set(_rpn_ctxt, "hour", 1, [](rpn_context & ctxt) {
  112. float a;
  113. rpn_stack_pop(ctxt, a);
  114. rpn_stack_push(ctxt, hour(int(a)));
  115. return true;
  116. });
  117. rpn_operator_set(_rpn_ctxt, "minute", 1, [](rpn_context & ctxt) {
  118. float a;
  119. rpn_stack_pop(ctxt, a);
  120. rpn_stack_push(ctxt, minute(int(a)));
  121. return true;
  122. });
  123. // Debug
  124. rpn_operator_set(_rpn_ctxt, "debug", 0, [](rpn_context & ctxt) {
  125. _rpnDump();
  126. return true;
  127. });
  128. // Relay operators
  129. rpn_operator_set(_rpn_ctxt, "relay", 2, [](rpn_context & ctxt) {
  130. float a, b;
  131. rpn_stack_pop(ctxt, b); // relay number
  132. rpn_stack_pop(ctxt, a); // new status
  133. if (int(a) == 2) {
  134. relayToggle(int(b));
  135. } else {
  136. relayStatus(int(b), int(a) == 1);
  137. }
  138. return true;
  139. });
  140. // Channel operators
  141. #if RELAY_PROVIDER == RELAY_PROVIDER_LIGHT
  142. rpn_operator_set(_rpn_ctxt, "update", 0, [](rpn_context & ctxt) {
  143. lightUpdate(true, true);
  144. return true;
  145. });
  146. rpn_operator_set(_rpn_ctxt, "black", 0, [](rpn_context & ctxt) {
  147. lightColor((unsigned long) 0);
  148. return true;
  149. });
  150. rpn_operator_set(_rpn_ctxt, "channel", 2, [](rpn_context & ctxt) {
  151. float a, b;
  152. rpn_stack_pop(ctxt, b); // channel number
  153. rpn_stack_pop(ctxt, a); // new value
  154. lightChannel(int(b), int(a));
  155. return true;
  156. });
  157. #endif
  158. }
  159. #if TERMINAL_SUPPORT
  160. void _rpnInitCommands() {
  161. terminalRegisterCommand(F("RPN.VARS"), [](Embedis* e) {
  162. unsigned char num = rpn_variables_size(_rpn_ctxt);
  163. if (0 == num) {
  164. DEBUG_MSG_P(PSTR("[RPN] No variables\n"));
  165. } else {
  166. DEBUG_MSG_P(PSTR("[RPN] Variables:\n"));
  167. for (unsigned char i=0; i<num; i++) {
  168. char * name = rpn_variable_name(_rpn_ctxt, i);
  169. float value;
  170. rpn_variable_get(_rpn_ctxt, name, value);
  171. DEBUG_MSG_P(PSTR(" %s: %s\n"), name, String(value).c_str());
  172. }
  173. }
  174. terminalOK();
  175. });
  176. terminalRegisterCommand(F("RPN.OPS"), [](Embedis* e) {
  177. unsigned char num = _rpn_ctxt.operators.size();
  178. DEBUG_MSG_P(PSTR("[RPN] Operators:\n"));
  179. for (unsigned char i=0; i<num; i++) {
  180. DEBUG_MSG_P(PSTR(" %s (%d)\n"), _rpn_ctxt.operators[i].name, _rpn_ctxt.operators[i].argc);
  181. }
  182. terminalOK();
  183. });
  184. terminalRegisterCommand(F("RPN.TEST"), [](Embedis* e) {
  185. if (e->argc == 2) {
  186. DEBUG_MSG_P(PSTR("[RPN] Running \"%s\"\n"), e->argv[1]);
  187. rpn_process(_rpn_ctxt, e->argv[1], true);
  188. _rpnDump();
  189. rpn_stack_clear(_rpn_ctxt);
  190. terminalOK();
  191. } else {
  192. terminalError(F("Wrong arguments"));
  193. }
  194. });
  195. }
  196. #endif
  197. void _rpnDump() {
  198. float value;
  199. DEBUG_MSG_P(PSTR("[RPN] Stack:\n"));
  200. unsigned char num = rpn_stack_size(_rpn_ctxt);
  201. if (0 == num) {
  202. DEBUG_MSG_P(PSTR(" (empty)\n"));
  203. } else {
  204. unsigned char index = num - 1;
  205. while (rpn_stack_get(_rpn_ctxt, index, value)) {
  206. DEBUG_MSG_P(PSTR(" %02d: %s\n"), index--, String(value).c_str());
  207. }
  208. }
  209. }
  210. void _rpnRun() {
  211. unsigned char i = 0;
  212. String rule = getSetting("rpnRule", i, "");
  213. while (rule.length()) {
  214. //DEBUG_MSG_P(PSTR("[RPN] Running \"%s\"\n"), rule.c_str());
  215. rpn_process(_rpn_ctxt, rule.c_str(), true);
  216. //_rpnDump();
  217. rule = getSetting("rpnRule", ++i, "");
  218. rpn_stack_clear(_rpn_ctxt);
  219. }
  220. if (getSetting("rpnSticky", 1).toInt() == 0) {
  221. rpn_variables_clear(_rpn_ctxt);
  222. }
  223. }
  224. void _rpnLoop() {
  225. if (_rpn_run && (millis() - _rpn_last > _rpn_delay)) {
  226. _rpnRun();
  227. _rpn_run = false;
  228. }
  229. }
  230. void rpnSetup() {
  231. // Init context
  232. _rpnInit();
  233. // Load & cache settings
  234. _rpnConfigure();
  235. // Terminal commands
  236. #if TERMINAL_SUPPORT
  237. _rpnInitCommands();
  238. #endif
  239. // Websockets
  240. #if WEB_SUPPORT
  241. wsRegister()
  242. .onVisible([](JsonObject& root) { root["rpnVisible"] = 1; })
  243. .onConnected(_rpnWebSocketOnConnected)
  244. .onKeyCheck(_rpnWebSocketOnKeyCheck);
  245. #endif
  246. // MQTT
  247. #if MQTT_SUPPORT
  248. mqttRegister(_rpnMQTTCallback);
  249. #endif
  250. StatusBroker::Register(_rpnBrokerStatus);
  251. SensorReadBroker::Register(_rpnBrokerCallback);
  252. espurnaRegisterReload(_rpnConfigure);
  253. espurnaRegisterLoop(_rpnLoop);
  254. }
  255. #endif // RPN_RULES_SUPPORT