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.

128 lines
3.7 KiB

  1. /*
  2. INFLUXDB MODULE
  3. Copyright (C) 2017-2018 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #if INFLUXDB_SUPPORT
  6. #include "ESPAsyncTCP.h"
  7. #include "SyncClient.h"
  8. bool _idb_enabled = false;
  9. SyncClient _idb_client;
  10. // -----------------------------------------------------------------------------
  11. bool _idbWebSocketOnReceive(const char * key, JsonVariant& value) {
  12. return (strncmp(key, "idb", 3) == 0);
  13. }
  14. void _idbWebSocketOnSend(JsonObject& root) {
  15. root["idbVisible"] = 1;
  16. root["idbEnabled"] = getSetting("idbEnabled", INFLUXDB_ENABLED).toInt() == 1;
  17. root["idbHost"] = getSetting("idbHost", INFLUXDB_HOST);
  18. root["idbPort"] = getSetting("idbPort", INFLUXDB_PORT).toInt();
  19. root["idbDatabase"] = getSetting("idbDatabase", INFLUXDB_DATABASE);
  20. root["idbUsername"] = getSetting("idbUsername", INFLUXDB_USERNAME);
  21. root["idbPassword"] = getSetting("idbPassword", INFLUXDB_PASSWORD);
  22. }
  23. void _idbConfigure() {
  24. _idb_enabled = getSetting("idbEnabled", INFLUXDB_ENABLED).toInt() == 1;
  25. if (_idb_enabled && (getSetting("idbHost", INFLUXDB_HOST).length() == 0)) {
  26. _idb_enabled = false;
  27. setSetting("idbEnabled", 0);
  28. }
  29. }
  30. #if BROKER_SUPPORT
  31. void _idbBrokerCallback(const char * topic, unsigned char id, const char * payload) {
  32. if (strcmp(MQTT_TOPIC_RELAY, topic) == 0) {
  33. idbSend(topic, id, (char *) payload);
  34. }
  35. }
  36. #endif // BROKER_SUPPORT
  37. // -----------------------------------------------------------------------------
  38. bool idbSend(const char * topic, const char * payload) {
  39. if (!_idb_enabled) return true;
  40. if (!wifiConnected() || (WiFi.getMode() != WIFI_STA)) return true;
  41. String h = getSetting("idbHost", INFLUXDB_HOST);
  42. #if MDNS_CLIENT_SUPPORT
  43. h = mdnsResolve(h);
  44. #endif
  45. char * host = strdup(h.c_str());
  46. unsigned int port = getSetting("idbPort", INFLUXDB_PORT).toInt();
  47. DEBUG_MSG_P(PSTR("[INFLUXDB] Sending to %s:%u\n"), host, port);
  48. bool success = false;
  49. _idb_client.setTimeout(2);
  50. if (_idb_client.connect((const char *) host, port)) {
  51. char data[128];
  52. snprintf(data, sizeof(data), "%s,device=%s value=%s", topic, getSetting("hostname").c_str(), String(payload).c_str());
  53. DEBUG_MSG_P(PSTR("[INFLUXDB] Data: %s\n"), data);
  54. char request[256];
  55. snprintf(request, sizeof(request), "POST /write?db=%s&u=%s&p=%s HTTP/1.1\r\nHost: %s:%u\r\nContent-Length: %d\r\n\r\n%s",
  56. getSetting("idbDatabase", INFLUXDB_DATABASE).c_str(),
  57. getSetting("idbUsername", INFLUXDB_USERNAME).c_str(), getSetting("idbPassword", INFLUXDB_PASSWORD).c_str(),
  58. host, port, strlen(data), data);
  59. if (_idb_client.printf(request) > 0) {
  60. while (_idb_client.connected() && _idb_client.available() == 0) delay(1);
  61. while (_idb_client.available()) _idb_client.read();
  62. if (_idb_client.connected()) _idb_client.stop();
  63. success = true;
  64. } else {
  65. DEBUG_MSG_P(PSTR("[INFLUXDB] Sent failed\n"));
  66. }
  67. _idb_client.stop();
  68. while (_idb_client.connected()) yield();
  69. } else {
  70. DEBUG_MSG_P(PSTR("[INFLUXDB] Connection failed\n"));
  71. }
  72. free(host);
  73. return success;
  74. }
  75. bool idbSend(const char * topic, unsigned char id, const char * payload) {
  76. char measurement[64];
  77. snprintf(measurement, sizeof(measurement), "%s,id=%d", topic, id);
  78. return idbSend(measurement, payload);
  79. }
  80. bool idbEnabled() {
  81. return _idb_enabled;
  82. }
  83. void idbSetup() {
  84. _idbConfigure();
  85. #if WEB_SUPPORT
  86. wsOnSendRegister(_idbWebSocketOnSend);
  87. wsOnReceiveRegister(_idbWebSocketOnReceive);
  88. #endif
  89. #if BROKER_SUPPORT
  90. brokerRegister(_idbBrokerCallback);
  91. #endif
  92. // Main callbacks
  93. espurnaRegisterReload(_idbConfigure);
  94. }
  95. #endif