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.

91 lines
2.8 KiB

  1. /*
  2. I2C MODULE
  3. Copyright (C) 2017 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. char host[64];
  32. getSetting("idbHost", INFLUXDB_HOST).toCharArray(host, sizeof(host));
  33. int port = getSetting("idbPort", INFLUXDB_PORT).toInt();
  34. DEBUG_MSG("[INFLUXDB] Sending to %s:%d\n", host, port);
  35. _idb_client.setTimeout(2);
  36. if (!_idb_client.connect(host, port)) {
  37. DEBUG_MSG("[INFLUXDB] Connection failed\n");
  38. return false;
  39. }
  40. char data[128];
  41. snprintf(data, sizeof(data), "%s,device=%s value=%s", topic, getSetting("hostname").c_str(), String(payload).c_str());
  42. DEBUG_MSG("[INFLUXDB] Data: %s\n", data);
  43. char request[256];
  44. 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",
  45. getSetting("idbDatabase", INFLUXDB_DATABASE).c_str(),
  46. getSetting("idbUsername", INFLUXDB_USERNAME).c_str(), getSetting("idbPassword", INFLUXDB_PASSWORD).c_str(),
  47. host, port, strlen(data), data);
  48. if (_idb_client.printf(request) > 0) {
  49. while (_idb_client.connected() && _idb_client.available() == 0) delay(1);
  50. while (_idb_client.available()) _idb_client.read();
  51. if (_idb_client.connected()) _idb_client.stop();
  52. return true;
  53. }
  54. _idb_client.stop();
  55. DEBUG_MSG("[INFLUXDB] Sent failed\n");
  56. while (_idb_client.connected()) delay(0);
  57. return false;
  58. }
  59. bool idbEnabled() {
  60. return _idb_enabled;
  61. }
  62. void idbSetup() {
  63. _idbConfigure();
  64. #if WEB_SUPPORT
  65. wsOnSendRegister(_idbWebSocketOnSend);
  66. wsOnAfterParseRegister(_idbConfigure);
  67. #endif
  68. }
  69. #endif