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.

97 lines
3.0 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. template<typename T> bool idbSend(const char * topic, unsigned char id, T payload) {
  60. char measurement[64];
  61. snprintf_P(measurement, sizeof(measurement), PSTR("%s,id=%d"), topic, id);
  62. return idbSend(topic, payload);
  63. }
  64. bool idbEnabled() {
  65. return _idb_enabled;
  66. }
  67. void idbSetup() {
  68. _idbConfigure();
  69. #if WEB_SUPPORT
  70. wsOnSendRegister(_idbWebSocketOnSend);
  71. wsOnAfterParseRegister(_idbConfigure);
  72. #endif
  73. }
  74. #endif