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.

106 lines
3.2 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. bool idbSend(const char * topic, const char * 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. unsigned int port = getSetting("idbPort", INFLUXDB_PORT).toInt();
  37. DEBUG_MSG("[INFLUXDB] Sending to %s:%u\n", host, port);
  38. bool success = false;
  39. _idb_client.setTimeout(2);
  40. if (_idb_client.connect((const char *) host, port)) {
  41. char data[128];
  42. snprintf(data, sizeof(data), "%s,device=%s value=%s", topic, getSetting("hostname").c_str(), String(payload).c_str());
  43. DEBUG_MSG("[INFLUXDB] Data: %s\n", data);
  44. char request[256];
  45. 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",
  46. getSetting("idbDatabase", INFLUXDB_DATABASE).c_str(),
  47. getSetting("idbUsername", INFLUXDB_USERNAME).c_str(), getSetting("idbPassword", INFLUXDB_PASSWORD).c_str(),
  48. host, port, strlen(data), data);
  49. if (_idb_client.printf(request) > 0) {
  50. while (_idb_client.connected() && _idb_client.available() == 0) delay(1);
  51. while (_idb_client.available()) _idb_client.read();
  52. if (_idb_client.connected()) _idb_client.stop();
  53. success = true;
  54. } else {
  55. DEBUG_MSG("[INFLUXDB] Sent failed\n");
  56. }
  57. _idb_client.stop();
  58. while (_idb_client.connected()) yield();
  59. } else {
  60. DEBUG_MSG("[INFLUXDB] Connection failed\n");
  61. }
  62. free(host);
  63. return success;
  64. }
  65. bool idbSend(const char * topic, unsigned char id, const char * payload) {
  66. char measurement[64];
  67. snprintf(measurement, sizeof(measurement), "%s,id=%d", topic, id);
  68. return idbSend(topic, payload);
  69. }
  70. bool idbEnabled() {
  71. return _idb_enabled;
  72. }
  73. void idbSetup() {
  74. _idbConfigure();
  75. #if WEB_SUPPORT
  76. wsOnSendRegister(_idbWebSocketOnSend);
  77. wsOnAfterParseRegister(_idbConfigure);
  78. #endif
  79. }
  80. #endif