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.

142 lines
4.1 KiB

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