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.

127 lines
3.6 KiB

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