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.

116 lines
3.4 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. // -----------------------------------------------------------------------------
  31. bool idbSend(const char * topic, const char * payload) {
  32. if (!_idb_enabled) return true;
  33. if (!wifiConnected() || (WiFi.getMode() != WIFI_STA)) return true;
  34. String h = getSetting("idbHost", INFLUXDB_HOST);
  35. #if MDNS_CLIENT_SUPPORT
  36. h = mdnsResolve(h);
  37. #endif
  38. char * host = strdup(h.c_str());
  39. unsigned int port = getSetting("idbPort", INFLUXDB_PORT).toInt();
  40. DEBUG_MSG_P(PSTR("[INFLUXDB] Sending to %s:%u\n"), host, port);
  41. bool success = false;
  42. _idb_client.setTimeout(2);
  43. if (_idb_client.connect((const char *) host, port)) {
  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_P(PSTR("[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:%u\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. if (_idb_client.printf(request) > 0) {
  53. while (_idb_client.connected() && _idb_client.available() == 0) delay(1);
  54. while (_idb_client.available()) _idb_client.read();
  55. if (_idb_client.connected()) _idb_client.stop();
  56. success = true;
  57. } else {
  58. DEBUG_MSG_P(PSTR("[INFLUXDB] Sent failed\n"));
  59. }
  60. _idb_client.stop();
  61. while (_idb_client.connected()) yield();
  62. } else {
  63. DEBUG_MSG_P(PSTR("[INFLUXDB] Connection failed\n"));
  64. }
  65. free(host);
  66. return success;
  67. }
  68. bool idbSend(const char * topic, unsigned char id, const char * payload) {
  69. char measurement[64];
  70. snprintf(measurement, sizeof(measurement), "%s,id=%d", topic, id);
  71. return idbSend(measurement, payload);
  72. }
  73. bool idbEnabled() {
  74. return _idb_enabled;
  75. }
  76. void idbSetup() {
  77. _idbConfigure();
  78. #if WEB_SUPPORT
  79. wsOnSendRegister(_idbWebSocketOnSend);
  80. wsOnReceiveRegister(_idbWebSocketOnReceive);
  81. #endif
  82. // Main callbacks
  83. espurnaRegisterReload(_idbConfigure);
  84. }
  85. #endif