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.

306 lines
8.0 KiB

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