|
| 1 | +/* |
| 2 | + * diag_sampler.cpp - Diagnostic telemetry firmware integration |
| 3 | + * |
| 4 | + * Bridges firmware globals into diag_snapshot_t and pushes them |
| 5 | + * into the static ring buffer. Call diag_sample() from timer1s. |
| 6 | + */ |
| 7 | + |
| 8 | +#if defined(SMARTEVSE_VERSION) /* ESP32 firmware only */ |
| 9 | + |
| 10 | +#include "main.h" |
| 11 | +#include "esp32.h" |
| 12 | +#include "diag_sampler.h" |
| 13 | +#include "diag_modbus.h" |
| 14 | +#include "evse_bridge.h" |
| 15 | +#include "meter.h" |
| 16 | +#include "debug.h" |
| 17 | + |
| 18 | +#include <WiFi.h> |
| 19 | +#include <string.h> |
| 20 | + |
| 21 | +/* Extern globals not exposed via headers */ |
| 22 | +extern uint8_t State; |
| 23 | +extern uint8_t ErrorFlags; |
| 24 | +extern uint8_t ChargeDelay; |
| 25 | +extern uint8_t Mode; |
| 26 | +extern uint16_t ChargeCurrent; |
| 27 | +extern int16_t IsetBalanced; |
| 28 | +extern uint16_t OverrideCurrent; |
| 29 | +extern uint16_t SolarStopTimer; |
| 30 | +extern uint16_t ImportCurrent; |
| 31 | +extern uint16_t StartCurrent; |
| 32 | +extern uint8_t NoCurrent; |
| 33 | +extern uint8_t C1Timer; |
| 34 | +extern uint8_t AccessTimer; |
| 35 | +extern uint8_t Nr_Of_Phases_Charging; |
| 36 | +extern uint8_t LoadBl; |
| 37 | +extern uint16_t Balanced[]; |
| 38 | +extern uint8_t BalancedState[]; |
| 39 | +extern int8_t TempEVSE; |
| 40 | +extern uint8_t RCmon; |
| 41 | +extern uint8_t pilot; |
| 42 | +extern int16_t Isum; |
| 43 | +extern EnableC2_t EnableC2; |
| 44 | +extern AccessStatus_t AccessStatus; |
| 45 | +extern Switch_Phase_t Switching_Phases_C2; |
| 46 | +extern uint32_t serialnr; |
| 47 | + |
| 48 | +#if MQTT |
| 49 | +extern struct MQTTclient_t MQTTclient; |
| 50 | +extern struct MQTTclientSmartEVSE_t MQTTclientSmartEVSE; |
| 51 | +#endif |
| 52 | + |
| 53 | +/* Static ring buffer — 8 KB at default 128 slots */ |
| 54 | +static diag_snapshot_t diag_buffer[DIAG_RING_SIZE_DEFAULT]; |
| 55 | +static diag_ring_t diag_ring; |
| 56 | +static uint32_t diag_uptime_seconds; |
| 57 | + |
| 58 | +void diag_sampler_init(void) |
| 59 | +{ |
| 60 | + diag_ring_init(&diag_ring, diag_buffer, DIAG_RING_SIZE_DEFAULT); |
| 61 | + diag_uptime_seconds = 0; |
| 62 | +} |
| 63 | + |
| 64 | +diag_ring_t *diag_get_ring(void) |
| 65 | +{ |
| 66 | + return &diag_ring; |
| 67 | +} |
| 68 | + |
| 69 | +extern diag_mb_ring_t g_diag_mb_ring; |
| 70 | + |
| 71 | +void diag_start(diag_profile_t profile) |
| 72 | +{ |
| 73 | + diag_ring_reset(&diag_ring); |
| 74 | + diag_set_profile(&diag_ring, profile); |
| 75 | + diag_ring.start_time = diag_uptime_seconds; |
| 76 | + |
| 77 | + /* Enable Modbus event ring for MODBUS/FAST profiles */ |
| 78 | + bool mb_enable = (profile == DIAG_PROFILE_MODBUS || profile == DIAG_PROFILE_FAST); |
| 79 | + diag_mb_enable(&g_diag_mb_ring, mb_enable); |
| 80 | + if (mb_enable) |
| 81 | + diag_mb_reset(&g_diag_mb_ring); |
| 82 | +} |
| 83 | + |
| 84 | +void diag_stop(void) |
| 85 | +{ |
| 86 | + diag_ring_freeze(&diag_ring, true); |
| 87 | + diag_mb_enable(&g_diag_mb_ring, false); |
| 88 | +} |
| 89 | + |
| 90 | +void diag_sample(void) |
| 91 | +{ |
| 92 | + diag_uptime_seconds++; |
| 93 | + |
| 94 | + if (!diag_ring_tick(&diag_ring)) |
| 95 | + return; |
| 96 | + |
| 97 | + diag_snapshot_t snap; |
| 98 | + memset(&snap, 0, sizeof(snap)); |
| 99 | + |
| 100 | + snap.timestamp = diag_uptime_seconds; |
| 101 | + |
| 102 | + /* State machine — read from globals (called within timer1s context, |
| 103 | + * after evse_sync_ctx_to_globals so globals are up-to-date) */ |
| 104 | + snap.state = State; |
| 105 | + snap.error_flags = (uint8_t)(ErrorFlags & 0xFF); |
| 106 | + snap.charge_delay = ChargeDelay; |
| 107 | + snap.access_status = (uint8_t)AccessStatus; |
| 108 | + snap.mode = Mode; |
| 109 | + |
| 110 | + /* Currents */ |
| 111 | + snap.mains_irms[0] = MainsMeter.Irms[0]; |
| 112 | + snap.mains_irms[1] = MainsMeter.Irms[1]; |
| 113 | + snap.mains_irms[2] = MainsMeter.Irms[2]; |
| 114 | + snap.ev_irms[0] = EVMeter.Irms[0]; |
| 115 | + snap.ev_irms[1] = EVMeter.Irms[1]; |
| 116 | + snap.ev_irms[2] = EVMeter.Irms[2]; |
| 117 | + snap.isum = Isum; |
| 118 | + |
| 119 | + /* Power allocation */ |
| 120 | + snap.charge_current = ChargeCurrent; |
| 121 | + snap.iset_balanced = (int16_t)IsetBalanced; |
| 122 | + snap.override_current = OverrideCurrent; |
| 123 | + |
| 124 | + /* Solar */ |
| 125 | + snap.solar_stop_timer = SolarStopTimer; |
| 126 | + snap.import_current = ImportCurrent; |
| 127 | + snap.start_current = StartCurrent; |
| 128 | + |
| 129 | + /* Timers — StateTimer is internal to evse_ctx_t, read via bridge */ |
| 130 | + evse_bridge_lock(); |
| 131 | + snap.state_timer = g_evse_ctx.StateTimer; |
| 132 | + evse_bridge_unlock(); |
| 133 | + snap.c1_timer = C1Timer; |
| 134 | + snap.access_timer = AccessTimer; |
| 135 | + snap.no_current = NoCurrent; |
| 136 | + |
| 137 | + /* Phase switching */ |
| 138 | + snap.nr_phases_charging = Nr_Of_Phases_Charging; |
| 139 | + snap.switching_c2 = (uint8_t)Switching_Phases_C2; |
| 140 | + snap.enable_c2 = (uint8_t)EnableC2; |
| 141 | + |
| 142 | + /* Load balancing */ |
| 143 | + snap.load_bl = LoadBl; |
| 144 | + snap.balanced_state_0 = BalancedState[0]; |
| 145 | + snap.balanced_0 = Balanced[0]; |
| 146 | + |
| 147 | + /* Temperature & safety */ |
| 148 | + snap.temp_evse = TempEVSE; |
| 149 | + snap.rc_mon = RCmon; |
| 150 | + snap.pilot_reading = pilot; |
| 151 | + |
| 152 | + /* Modbus health */ |
| 153 | +#if !defined(SMARTEVSE_VERSION) || SMARTEVSE_VERSION >=30 && SMARTEVSE_VERSION < 40 |
| 154 | + snap.mains_meter_timeout = MainsMeter.Timeout; |
| 155 | + snap.ev_meter_timeout = EVMeter.Timeout; |
| 156 | +#endif |
| 157 | + snap.mains_meter_type = MainsMeter.Type; |
| 158 | + snap.ev_meter_type = EVMeter.Type; |
| 159 | + |
| 160 | + /* Network */ |
| 161 | + snap.wifi_rssi = WiFi.isConnected() ? (int8_t)WiFi.RSSI() : 0; |
| 162 | +#if MQTT |
| 163 | + snap.mqtt_connected = (MQTTclient.connected ? 1 : 0) |
| 164 | + | (MQTTclientSmartEVSE.connected ? 2 : 0); |
| 165 | +#endif |
| 166 | + |
| 167 | + diag_ring_push(&diag_ring, &snap); |
| 168 | + |
| 169 | + /* Push to WebSocket clients if any are connected */ |
| 170 | + diag_ws_push_snapshot(&snap); |
| 171 | +} |
| 172 | + |
| 173 | +int diag_status_json(char *buf, size_t bufsz) |
| 174 | +{ |
| 175 | + if (!buf || bufsz == 0) |
| 176 | + return -1; |
| 177 | + |
| 178 | + const char *profile_names[] = { |
| 179 | + "off", "general", "solar", "loadbal", "modbus", "fast" |
| 180 | + }; |
| 181 | + const char *pname = "off"; |
| 182 | + if (diag_ring.profile >= DIAG_PROFILE_OFF && |
| 183 | + diag_ring.profile <= DIAG_PROFILE_FAST) |
| 184 | + pname = profile_names[diag_ring.profile]; |
| 185 | + |
| 186 | + int n = snprintf(buf, bufsz, |
| 187 | + "{\"profile\":\"%s\",\"count\":%u,\"capacity\":%u," |
| 188 | + "\"frozen\":%s,\"uptime\":%u}", |
| 189 | + pname, |
| 190 | + (unsigned)diag_ring.count, |
| 191 | + (unsigned)diag_ring.capacity, |
| 192 | + diag_ring.frozen ? "true" : "false", |
| 193 | + (unsigned)diag_uptime_seconds); |
| 194 | + |
| 195 | + if (n < 0 || (size_t)n >= bufsz) |
| 196 | + return -1; |
| 197 | + |
| 198 | + return n; |
| 199 | +} |
| 200 | + |
| 201 | +#endif /* SMARTEVSE_VERSION */ |
0 commit comments