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.

131 lines
3.8 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. #if BROKER_SUPPORT
  31. void _idbBrokerCallback(const unsigned char type, const char * topic, unsigned char id, const char * payload) {
  32. // Only process status & senssor messages
  33. if ((BROKER_MSG_TYPE_STATUS == type) || (BROKER_MSG_TYPE_SENSOR == type)) {
  34. idbSend(topic, id, (char *) payload);
  35. }
  36. }
  37. #endif // BROKER_SUPPORT
  38. // -----------------------------------------------------------------------------
  39. bool idbSend(const char * topic, const char * payload) {
  40. if (!_idb_enabled) return true;
  41. if (!wifiConnected() || (WiFi.getMode() != WIFI_STA)) return true;
  42. String h = getSetting("idbHost", INFLUXDB_HOST);
  43. #if MDNS_CLIENT_SUPPORT
  44. h = mdnsResolve(h);
  45. #endif
  46. char * host = strdup(h.c_str());
  47. unsigned int port = getSetting("idbPort", INFLUXDB_PORT).toInt();
  48. DEBUG_MSG_P(PSTR("[INFLUXDB] Sending to %s:%u\n"), host, port);
  49. bool success = false;
  50. _idb_client.setTimeout(2);
  51. if (_idb_client.connect((const char *) host, port)) {
  52. char data[128];
  53. snprintf(data, sizeof(data), "%s,device=%s value=%s", topic, getSetting("hostname").c_str(), String(payload).c_str());
  54. DEBUG_MSG_P(PSTR("[INFLUXDB] Data: %s\n"), data);
  55. char request[256];
  56. 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",
  57. getSetting("idbDatabase", INFLUXDB_DATABASE).c_str(),
  58. getSetting("idbUsername", INFLUXDB_USERNAME).c_str(), getSetting("idbPassword", INFLUXDB_PASSWORD).c_str(),
  59. host, port, strlen(data), data);
  60. if (_idb_client.printf(request) > 0) {
  61. while (_idb_client.connected() && _idb_client.available() == 0) delay(1);
  62. while (_idb_client.available()) _idb_client.read();
  63. if (_idb_client.connected()) _idb_client.stop();
  64. success = true;
  65. } else {
  66. DEBUG_MSG_P(PSTR("[INFLUXDB] Sent failed\n"));
  67. }
  68. _idb_client.stop();
  69. while (_idb_client.connected()) yield();
  70. } else {
  71. DEBUG_MSG_P(PSTR("[INFLUXDB] Connection failed\n"));
  72. }
  73. free(host);
  74. return success;
  75. }
  76. bool idbSend(const char * topic, unsigned char id, const char * payload) {
  77. char measurement[64];
  78. snprintf(measurement, sizeof(measurement), "%s,id=%d", topic, id);
  79. return idbSend(measurement, payload);
  80. }
  81. bool idbEnabled() {
  82. return _idb_enabled;
  83. }
  84. void idbSetup() {
  85. _idbConfigure();
  86. #if WEB_SUPPORT
  87. wsOnSendRegister(_idbWebSocketOnSend);
  88. wsOnReceiveRegister(_idbWebSocketOnReceive);
  89. #endif
  90. #if BROKER_SUPPORT
  91. brokerRegister(_idbBrokerCallback);
  92. #endif
  93. // Main callbacks
  94. espurnaRegisterReload(_idbConfigure);
  95. }
  96. #endif