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.

103 lines
3.1 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. void _idbWebSocketOnSend(JsonObject& root) {
  12. root["idbVisible"] = 1;
  13. root["idbEnabled"] = getSetting("idbEnabled", INFLUXDB_ENABLED).toInt() == 1;
  14. root["idbHost"] = getSetting("idbHost", INFLUXDB_HOST);
  15. root["idbPort"] = getSetting("idbPort", INFLUXDB_PORT).toInt();
  16. root["idbDatabase"] = getSetting("idbDatabase", INFLUXDB_DATABASE);
  17. root["idbUsername"] = getSetting("idbUsername", INFLUXDB_USERNAME);
  18. root["idbPassword"] = getSetting("idbPassword", INFLUXDB_PASSWORD);
  19. }
  20. void _idbConfigure() {
  21. _idb_enabled = getSetting("idbEnabled", INFLUXDB_ENABLED).toInt() == 1;
  22. if (_idb_enabled && (getSetting("idbHost", INFLUXDB_HOST).length() == 0)) {
  23. _idb_enabled = false;
  24. setSetting("idbEnabled", 0);
  25. }
  26. }
  27. // -----------------------------------------------------------------------------
  28. template<typename T> bool idbSend(const char * topic, T payload) {
  29. if (!_idb_enabled) return true;
  30. if (!wifiConnected() || (WiFi.getMode() != WIFI_STA)) return true;
  31. String h = getSetting("idbHost", INFLUXDB_HOST);
  32. #if MDNS_CLIENT_SUPPORT
  33. h = mdnsResolve(h);
  34. #endif
  35. char * host = strdup(h.c_str());
  36. int port = getSetting("idbPort", INFLUXDB_PORT).toInt();
  37. DEBUG_MSG("[INFLUXDB] Sending to %s:%u\n", host, port);
  38. _idb_client.setTimeout(2);
  39. if (!_idb_client.connect(host, port)) {
  40. DEBUG_MSG("[INFLUXDB] Connection failed\n");
  41. free(host);
  42. return false;
  43. }
  44. char data[128];
  45. snprintf(data, sizeof(data), "%s,device=%s value=%s", topic, getSetting("hostname").c_str(), String(payload).c_str());
  46. DEBUG_MSG("[INFLUXDB] Data: %s\n", data);
  47. char request[256];
  48. snprintf(request, sizeof(request), "POST /write?db=%s&u=%s&p=%s HTTP/1.1\r\nHost: %s:%d\r\nContent-Length: %d\r\n\r\n%s",
  49. getSetting("idbDatabase", INFLUXDB_DATABASE).c_str(),
  50. getSetting("idbUsername", INFLUXDB_USERNAME).c_str(), getSetting("idbPassword", INFLUXDB_PASSWORD).c_str(),
  51. host, port, strlen(data), data);
  52. free(host);
  53. if (_idb_client.printf(request) > 0) {
  54. while (_idb_client.connected() && _idb_client.available() == 0) delay(1);
  55. while (_idb_client.available()) _idb_client.read();
  56. if (_idb_client.connected()) _idb_client.stop();
  57. return true;
  58. }
  59. _idb_client.stop();
  60. DEBUG_MSG("[INFLUXDB] Sent failed\n");
  61. while (_idb_client.connected()) delay(0);
  62. return false;
  63. }
  64. template<typename T> bool idbSend(const char * topic, unsigned char id, T payload) {
  65. char measurement[64];
  66. snprintf(measurement, sizeof(measurement), "%s,id=%d", topic, id);
  67. return idbSend(topic, payload);
  68. }
  69. bool idbEnabled() {
  70. return _idb_enabled;
  71. }
  72. void idbSetup() {
  73. _idbConfigure();
  74. #if WEB_SUPPORT
  75. wsOnSendRegister(_idbWebSocketOnSend);
  76. wsOnAfterParseRegister(_idbConfigure);
  77. #endif
  78. }
  79. #endif