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.

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