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.

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