Browse Source

Runtime heartbeat configuration (#1406)

refactor-terminal
Max Prokhorov 6 years ago
committed by GitHub
parent
commit
59685150b4
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 153 additions and 49 deletions
  1. +52
    -5
      code/espurna/config/general.h
  2. +101
    -44
      code/espurna/utils.ino

+ 52
- 5
code/espurna/config/general.h View File

@ -185,23 +185,74 @@
#define UPTIME_OVERFLOW 4294967295 // Uptime overflow value #define UPTIME_OVERFLOW 4294967295 // Uptime overflow value
// Topics that will be reported in heartbeat
// Values that will be reported in heartbeat
#ifndef HEARTBEAT_REPORT_STATUS
#define HEARTBEAT_REPORT_STATUS 1 #define HEARTBEAT_REPORT_STATUS 1
#endif
#ifndef HEARTBEAT_REPORT_SSID
#define HEARTBEAT_REPORT_SSID 1 #define HEARTBEAT_REPORT_SSID 1
#endif
#ifndef HEARTBEAT_REPORT_IP
#define HEARTBEAT_REPORT_IP 1 #define HEARTBEAT_REPORT_IP 1
#endif
#ifndef HEARTBEAT_REPORT_MAC
#define HEARTBEAT_REPORT_MAC 1 #define HEARTBEAT_REPORT_MAC 1
#endif
#ifndef HEARTBEAT_REPORT_RSSI
#define HEARTBEAT_REPORT_RSSI 1 #define HEARTBEAT_REPORT_RSSI 1
#endif
#ifndef HEARTBEAT_REPORT_UPTIME
#define HEARTBEAT_REPORT_UPTIME 1 #define HEARTBEAT_REPORT_UPTIME 1
#endif
#ifndef HEARTBEAT_REPORT_DATETIME
#define HEARTBEAT_REPORT_DATETIME 1 #define HEARTBEAT_REPORT_DATETIME 1
#endif
#ifndef HEARTBEAT_REPORT_FREEHEAP
#define HEARTBEAT_REPORT_FREEHEAP 1 #define HEARTBEAT_REPORT_FREEHEAP 1
#endif
#ifndef HEARTBEAT_REPORT_VCC
#define HEARTBEAT_REPORT_VCC 1 #define HEARTBEAT_REPORT_VCC 1
#endif
#ifndef HEARTBEAT_REPORT_RELAY
#define HEARTBEAT_REPORT_RELAY 1 #define HEARTBEAT_REPORT_RELAY 1
#endif
#ifndef HEARTBEAT_REPORT_LIGHT
#define HEARTBEAT_REPORT_LIGHT 1 #define HEARTBEAT_REPORT_LIGHT 1
#endif
#ifndef HEARTBEAT_REPORT_HOSTNAME
#define HEARTBEAT_REPORT_HOSTNAME 1 #define HEARTBEAT_REPORT_HOSTNAME 1
#endif
#ifndef HEARTBEAT_REPORT_APP
#define HEARTBEAT_REPORT_APP 1 #define HEARTBEAT_REPORT_APP 1
#endif
#ifndef HEARTBEAT_REPORT_VERSION
#define HEARTBEAT_REPORT_VERSION 1 #define HEARTBEAT_REPORT_VERSION 1
#endif
#ifndef HEARTBEAT_REPORT_BOARD
#define HEARTBEAT_REPORT_BOARD 1 #define HEARTBEAT_REPORT_BOARD 1
#endif
#ifndef HEARTBEAT_REPORT_LOADAVG
#define HEARTBEAT_REPORT_LOADAVG 1
#endif
#ifndef HEARTBEAT_REPORT_INTERVAL
#define HEARTBEAT_REPORT_INTERVAL 0 #define HEARTBEAT_REPORT_INTERVAL 0
#endif
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Load average // Load average
@ -211,10 +262,6 @@
#define LOADAVG_INTERVAL 30000 // Interval between calculating load average (in ms) #define LOADAVG_INTERVAL 30000 // Interval between calculating load average (in ms)
#endif #endif
#ifndef LOADAVG_REPORT
#define LOADAVG_REPORT 1 // Should we report Load average over MQTT?
#endif
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// BUTTON // BUTTON
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------


+ 101
- 44
code/espurna/utils.ino View File

@ -137,6 +137,59 @@ unsigned long getUptime() {
#if HEARTBEAT_MODE != HEARTBEAT_NONE #if HEARTBEAT_MODE != HEARTBEAT_NONE
// -----------------------------------------------------------------------------
// Heartbeat helper
// -----------------------------------------------------------------------------
namespace Heartbeat {
enum Report : uint32_t {
Status = 1 << 1,
Ssid = 1 << 2,
Ip = 1 << 3,
Mac = 1 << 4,
Rssi = 1 << 5,
Uptime = 1 << 6,
Datetime = 1 << 7,
Freeheap = 1 << 8,
Vcc = 1 << 9,
Relay = 1 << 10,
Light = 1 << 11,
Hostname = 1 << 12,
App = 1 << 13,
Version = 1 << 14,
Board = 1 << 15,
Loadavg = 1 << 16,
Interval = 1 << 17
};
constexpr uint32_t defaultValue() {
return (Status * (HEARTBEAT_REPORT_STATUS)) | \
(Ssid * (HEARTBEAT_REPORT_SSID)) | \
(Ip * (HEARTBEAT_REPORT_IP)) | \
(Mac * (HEARTBEAT_REPORT_MAC)) | \
(Rssi * (HEARTBEAT_REPORT_RSSI)) | \
(Uptime * (HEARTBEAT_REPORT_UPTIME)) | \
(Datetime * (HEARTBEAT_REPORT_DATETIME)) | \
(Freeheap * (HEARTBEAT_REPORT_FREEHEAP)) | \
(Vcc * (HEARTBEAT_REPORT_VCC)) | \
(Relay * (HEARTBEAT_REPORT_RELAY)) | \
(Light * (HEARTBEAT_REPORT_LIGHT)) | \
(Hostname * (HEARTBEAT_REPORT_HOSTNAME)) | \
(App * (HEARTBEAT_REPORT_APP)) | \
(Version * (HEARTBEAT_REPORT_VERSION)) | \
(Board * (HEARTBEAT_REPORT_BOARD)) | \
(Loadavg * (HEARTBEAT_REPORT_LOADAVG)) | \
(Interval * (HEARTBEAT_REPORT_INTERVAL));
}
uint32_t currentValue() {
const String cfg = getSetting("hbReport");
if (!cfg.length()) return defaultValue();
return strtoul(cfg.c_str(), NULL, 10);
}
}
void heartbeat() { void heartbeat() {
unsigned long uptime_seconds = getUptime(); unsigned long uptime_seconds = getUptime();
@ -163,65 +216,70 @@ void heartbeat() {
#endif #endif
} }
const uint32_t hb_cfg = Heartbeat::currentValue();
if (!hb_cfg) return;
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
// MQTT // MQTT
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
#if MQTT_SUPPORT #if MQTT_SUPPORT
if (!serial) { if (!serial) {
#if (HEARTBEAT_REPORT_INTERVAL)
mqttSend(MQTT_TOPIC_INTERVAL, HEARTBEAT_INTERVAL / 1000);
#endif
#if (HEARTBEAT_REPORT_APP)
if (hb_cfg & Heartbeat::Interval)
mqttSend(MQTT_TOPIC_INTERVAL, String(HEARTBEAT_INTERVAL / 1000).c_str());
if (hb_cfg & Heartbeat::App)
mqttSend(MQTT_TOPIC_APP, APP_NAME); mqttSend(MQTT_TOPIC_APP, APP_NAME);
#endif
#if (HEARTBEAT_REPORT_VERSION)
if (hb_cfg & Heartbeat::Version)
mqttSend(MQTT_TOPIC_VERSION, APP_VERSION); mqttSend(MQTT_TOPIC_VERSION, APP_VERSION);
#endif
#if (HEARTBEAT_REPORT_BOARD)
if (hb_cfg & Heartbeat::Board)
mqttSend(MQTT_TOPIC_BOARD, getBoardName().c_str()); mqttSend(MQTT_TOPIC_BOARD, getBoardName().c_str());
#endif
#if (HEARTBEAT_REPORT_HOSTNAME)
mqttSend(MQTT_TOPIC_HOSTNAME, getSetting("hostname").c_str());
#endif
#if (HEARTBEAT_REPORT_SSID)
if (hb_cfg & Heartbeat::Hostname)
mqttSend(MQTT_TOPIC_HOSTNAME, getSetting("hostname", getIdentifier()).c_str());
if (hb_cfg & Heartbeat::Ssid)
mqttSend(MQTT_TOPIC_SSID, WiFi.SSID().c_str()); mqttSend(MQTT_TOPIC_SSID, WiFi.SSID().c_str());
#endif
#if (HEARTBEAT_REPORT_IP)
if (hb_cfg & Heartbeat::Ip)
mqttSend(MQTT_TOPIC_IP, getIP().c_str()); mqttSend(MQTT_TOPIC_IP, getIP().c_str());
#endif
#if (HEARTBEAT_REPORT_MAC)
if (hb_cfg & Heartbeat::Mac)
mqttSend(MQTT_TOPIC_MAC, WiFi.macAddress().c_str()); mqttSend(MQTT_TOPIC_MAC, WiFi.macAddress().c_str());
#endif
#if (HEARTBEAT_REPORT_RSSI)
if (hb_cfg & Heartbeat::Rssi)
mqttSend(MQTT_TOPIC_RSSI, String(WiFi.RSSI()).c_str()); mqttSend(MQTT_TOPIC_RSSI, String(WiFi.RSSI()).c_str());
#endif
#if (HEARTBEAT_REPORT_UPTIME)
if (hb_cfg & Heartbeat::Uptime)
mqttSend(MQTT_TOPIC_UPTIME, String(uptime_seconds).c_str()); mqttSend(MQTT_TOPIC_UPTIME, String(uptime_seconds).c_str());
#if NTP_SUPPORT
if ((hb_cfg & Heartbeat::Datetime) && (ntpSynced()))
mqttSend(MQTT_TOPIC_DATETIME, ntpDateTime().c_str());
#endif #endif
#if (HEARTBEAT_REPORT_DATETIME) && (NTP_SUPPORT)
if (ntpSynced()) mqttSend(MQTT_TOPIC_DATETIME, ntpDateTime().c_str());
#endif
#if (HEARTBEAT_REPORT_FREEHEAP)
if (hb_cfg & Heartbeat::Freeheap)
mqttSend(MQTT_TOPIC_FREEHEAP, String(free_heap).c_str()); mqttSend(MQTT_TOPIC_FREEHEAP, String(free_heap).c_str());
#endif
#if (HEARTBEAT_REPORT_RELAY)
if (hb_cfg & Heartbeat::Relay)
relayMQTT(); relayMQTT();
#if (LIGHT_PROVIDER != LIGHT_PROVIDER_NONE)
if (hb_cfg & Heartbeat::Light)
lightMQTT();
#endif #endif
#if (LIGHT_PROVIDER != LIGHT_PROVIDER_NONE) & (HEARTBEAT_REPORT_LIGHT)
lightMQTT();
#endif
#if (HEARTBEAT_REPORT_VCC)
#if ADC_MODE_VALUE == ADC_VCC
if ((hb_cfg & Heartbeat::Vcc) && (ADC_MODE_VALUE == ADC_VCC))
mqttSend(MQTT_TOPIC_VCC, String(ESP.getVcc()).c_str()); mqttSend(MQTT_TOPIC_VCC, String(ESP.getVcc()).c_str());
#endif
#endif
#if (HEARTBEAT_REPORT_STATUS)
if (hb_cfg & Heartbeat::Status)
mqttSend(MQTT_TOPIC_STATUS, MQTT_STATUS_ONLINE, true); mqttSend(MQTT_TOPIC_STATUS, MQTT_STATUS_ONLINE, true);
#endif
#if (LOADAVG_REPORT)
if (hb_cfg & Heartbeat::Loadavg)
mqttSend(MQTT_TOPIC_LOADAVG, String(systemLoadAverage()).c_str()); mqttSend(MQTT_TOPIC_LOADAVG, String(systemLoadAverage()).c_str());
#endif
} }
#endif #endif
@ -230,15 +288,14 @@ void heartbeat() {
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
#if INFLUXDB_SUPPORT #if INFLUXDB_SUPPORT
#if (HEARTBEAT_REPORT_UPTIME)
if (hb_cfg & Heartbeat::Uptime)
idbSend(MQTT_TOPIC_UPTIME, String(uptime_seconds).c_str()); idbSend(MQTT_TOPIC_UPTIME, String(uptime_seconds).c_str());
#endif
#if (HEARTBEAT_REPORT_FREEHEAP)
if (hb_cfg & Heartbeat::Freeheap)
idbSend(MQTT_TOPIC_FREEHEAP, String(free_heap).c_str()); idbSend(MQTT_TOPIC_FREEHEAP, String(free_heap).c_str());
#endif
#if (HEARTBEAT_REPORT_RSSI)
if (hb_cfg & Heartbeat::Rssi)
idbSend(MQTT_TOPIC_RSSI, String(WiFi.RSSI()).c_str()); idbSend(MQTT_TOPIC_RSSI, String(WiFi.RSSI()).c_str());
#endif
#endif #endif
} }


Loading…
Cancel
Save