|
| 1 | +/* |
| 2 | + * capacity_peak.c — 15-minute quarter-peak averaging for EU capacity tariffs |
| 3 | + * |
| 4 | + * Pure C implementation — no platform dependencies. |
| 5 | + * Tracks rolling 15-minute average power and monthly peaks for the Belgian |
| 6 | + * capaciteitstarief model. |
| 7 | + */ |
| 8 | + |
| 9 | +#include "capacity_peak.h" |
| 10 | +#include <string.h> |
| 11 | +#include <stdio.h> |
| 12 | +#include <limits.h> |
| 13 | + |
| 14 | +void capacity_init(capacity_state_t *state) { |
| 15 | + if (!state) return; |
| 16 | + memset(state, 0, sizeof(*state)); |
| 17 | +} |
| 18 | + |
| 19 | +void capacity_set_limit(capacity_state_t *state, int32_t limit_w) { |
| 20 | + if (!state) return; |
| 21 | + state->limit_w = limit_w; |
| 22 | +} |
| 23 | + |
| 24 | +void capacity_tick_1s(capacity_state_t *state, int32_t total_power_w, |
| 25 | + uint8_t month, uint8_t year_offset) { |
| 26 | + if (!state) return; |
| 27 | + |
| 28 | + /* Detect month rollover — reset monthly peak */ |
| 29 | + if (month != state->monthly.peak_month || |
| 30 | + year_offset != state->monthly.peak_year_offset) { |
| 31 | + state->monthly.monthly_peak_w = 0; |
| 32 | + state->monthly.peak_month = month; |
| 33 | + state->monthly.peak_year_offset = year_offset; |
| 34 | + } |
| 35 | + |
| 36 | + /* Accumulate watt-seconds */ |
| 37 | + state->window.accumulated_ws += total_power_w; |
| 38 | + state->window.window_elapsed_s++; |
| 39 | + |
| 40 | + /* Window complete? */ |
| 41 | + if (state->window.window_elapsed_s >= CAPACITY_WINDOW_SECONDS) { |
| 42 | + state->window.window_avg_w = |
| 43 | + (int32_t)(state->window.accumulated_ws / CAPACITY_WINDOW_SECONDS); |
| 44 | + state->window.window_valid = 1; |
| 45 | + |
| 46 | + /* Update monthly peak if this window is a new high */ |
| 47 | + if (state->window.window_avg_w > state->monthly.monthly_peak_w) { |
| 48 | + state->monthly.monthly_peak_w = state->window.window_avg_w; |
| 49 | + } |
| 50 | + |
| 51 | + /* Reset window for next 15-minute period */ |
| 52 | + state->window.accumulated_ws = 0; |
| 53 | + state->window.window_elapsed_s = 0; |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +int32_t capacity_get_headroom_w(const capacity_state_t *state) { |
| 58 | + if (!state) return 0; |
| 59 | + if (state->limit_w == 0) return INT32_MAX; |
| 60 | + |
| 61 | + /* |
| 62 | + * Calculate how much additional power can be consumed in the remainder |
| 63 | + * of this window without the window average exceeding the limit. |
| 64 | + * |
| 65 | + * The window average at completion will be: |
| 66 | + * avg = (accumulated_ws + future_ws) / CAPACITY_WINDOW_SECONDS |
| 67 | + * |
| 68 | + * For avg <= limit_w: |
| 69 | + * accumulated_ws + future_ws <= limit_w * CAPACITY_WINDOW_SECONDS |
| 70 | + * |
| 71 | + * remaining_seconds = CAPACITY_WINDOW_SECONDS - window_elapsed_s |
| 72 | + * If we add P watts for all remaining seconds: |
| 73 | + * future_ws = P * remaining_seconds |
| 74 | + * |
| 75 | + * So the max P that keeps avg <= limit_w: |
| 76 | + * P = (limit_w * CAPACITY_WINDOW_SECONDS - accumulated_ws) / remaining_seconds |
| 77 | + * |
| 78 | + * This gives the instantaneous headroom: how much power can be drawn |
| 79 | + * for the rest of this window without exceeding the limit. |
| 80 | + */ |
| 81 | + uint16_t elapsed = state->window.window_elapsed_s; |
| 82 | + if (elapsed == 0) { |
| 83 | + /* Window just started — full headroom available */ |
| 84 | + return state->limit_w; |
| 85 | + } |
| 86 | + |
| 87 | + int64_t budget_ws = (int64_t)state->limit_w * CAPACITY_WINDOW_SECONDS; |
| 88 | + int64_t remaining_ws = budget_ws - state->window.accumulated_ws; |
| 89 | + uint16_t remaining_s = CAPACITY_WINDOW_SECONDS - elapsed; |
| 90 | + |
| 91 | + if (remaining_s == 0) { |
| 92 | + /* |
| 93 | + * Window is about to complete (elapsed == 900 shouldn't happen since |
| 94 | + * tick resets it, but guard against it). Return based on current avg. |
| 95 | + */ |
| 96 | + int32_t current_avg = |
| 97 | + (int32_t)(state->window.accumulated_ws / CAPACITY_WINDOW_SECONDS); |
| 98 | + return state->limit_w - current_avg; |
| 99 | + } |
| 100 | + |
| 101 | + int32_t headroom = (int32_t)(remaining_ws / remaining_s); |
| 102 | + return headroom; |
| 103 | +} |
| 104 | + |
| 105 | +int32_t capacity_get_window_avg_w(const capacity_state_t *state) { |
| 106 | + if (!state) return 0; |
| 107 | + return state->window.window_avg_w; |
| 108 | +} |
| 109 | + |
| 110 | +int32_t capacity_get_monthly_peak_w(const capacity_state_t *state) { |
| 111 | + if (!state) return 0; |
| 112 | + return state->monthly.monthly_peak_w; |
| 113 | +} |
| 114 | + |
| 115 | +int16_t capacity_headroom_to_da(int32_t headroom_w, uint8_t phases) { |
| 116 | + if (phases == 0) return 0; |
| 117 | + /* Convert watts to deciamps: P / (V * phases) * 10 = P * 10 / (230 * phases) */ |
| 118 | + return (int16_t)(headroom_w * 10 / (230 * phases)); |
| 119 | +} |
| 120 | + |
| 121 | +int capacity_to_json(const capacity_state_t *state, char *buf, size_t bufsz) { |
| 122 | + if (!state || !buf || bufsz == 0) { |
| 123 | + return -1; |
| 124 | + } |
| 125 | + |
| 126 | + /* Calculate running average for display */ |
| 127 | + int32_t running_avg = 0; |
| 128 | + if (state->window.window_elapsed_s > 0) { |
| 129 | + running_avg = (int32_t)(state->window.accumulated_ws / |
| 130 | + state->window.window_elapsed_s); |
| 131 | + } |
| 132 | + |
| 133 | + int32_t headroom = capacity_get_headroom_w(state); |
| 134 | + |
| 135 | + int n = snprintf(buf, bufsz, |
| 136 | + "{\"limit_w\":%d," |
| 137 | + "\"window_avg_w\":%d," |
| 138 | + "\"running_avg_w\":%d," |
| 139 | + "\"window_elapsed_s\":%u," |
| 140 | + "\"window_valid\":%u," |
| 141 | + "\"monthly_peak_w\":%d," |
| 142 | + "\"headroom_w\":%d}", |
| 143 | + (int)state->limit_w, |
| 144 | + (int)state->window.window_avg_w, |
| 145 | + (int)running_avg, |
| 146 | + (unsigned)state->window.window_elapsed_s, |
| 147 | + (unsigned)state->window.window_valid, |
| 148 | + (int)state->monthly.monthly_peak_w, |
| 149 | + (int)headroom); |
| 150 | + |
| 151 | + if (n < 0 || (size_t)n >= bufsz) { |
| 152 | + return -1; |
| 153 | + } |
| 154 | + |
| 155 | + return n; |
| 156 | +} |
0 commit comments