840 lines
42 KiB
C
840 lines
42 KiB
C
/* 宿主示例整数电能源。闭式查询只依赖恢复基线和指定单调时刻,
|
||
* FLOW补发游标与当前值查询相互独立;持久化由EnergyFreeze核心负责。 */
|
||
#include "iec104_example_energy_meter.h"
|
||
|
||
#include <float.h>
|
||
#include <fenv.h>
|
||
#include <limits.h>
|
||
#include <math.h>
|
||
#include <stdlib.h>
|
||
#include <string.h>
|
||
|
||
#ifdef _WIN32
|
||
#ifndef WIN32_LEAN_AND_MEAN
|
||
#define WIN32_LEAN_AND_MEAN
|
||
#endif
|
||
#include <windows.h>
|
||
#endif
|
||
|
||
#define METER_SCALE UINT64_C(3600000)
|
||
#define METER_EXACT_INTEGER UINT64_C(9007199254740991)
|
||
#define METER_RECOVERY_CAPACITY (IEC60870_ENERGY_FREEZE_DAYS * \
|
||
(IEC60870_ENERGY_FREEZE_SLOTS + 1U) + IEC60870_ENERGY_FREEZE_FLOW_CAPACITY)
|
||
|
||
#ifdef IEC104_EXAMPLE_DTU_PROFILE
|
||
/* 保留DTU原四点P+/Q+/P-/Q-,四象限补在其后;读数与持久恢复共用这张映射。 */
|
||
enum { METER_P_PLUS = 0, METER_Q_PLUS = 1, METER_P_MINUS = 2, METER_Q_MINUS = 3,
|
||
METER_Q1 = 4, METER_Q4 = 5, METER_Q2 = 6, METER_Q3 = 7 };
|
||
#define METER_ACTIVE_WATTS 66U
|
||
#define METER_REACTIVE_VARS 22U
|
||
#else
|
||
enum { METER_P_PLUS = 0, METER_Q_PLUS = 1, METER_P_MINUS = 4, METER_Q_MINUS = 5,
|
||
METER_Q1 = 2, METER_Q4 = 3, METER_Q2 = 6, METER_Q3 = 7 };
|
||
#define METER_ACTIVE_WATTS 400000U
|
||
#define METER_REACTIVE_VARS 150000U
|
||
#endif
|
||
|
||
static const uint8_t meter_independent_positions[EXAMPLE_ENERGY_METER_INDEPENDENT] = {
|
||
METER_P_PLUS, METER_P_MINUS, METER_Q1, METER_Q2, METER_Q3, METER_Q4
|
||
};
|
||
|
||
typedef struct { float values[EXAMPLE_ENERGY_METER_POINTS]; } MeterVector;
|
||
typedef struct { uint64_t low, high, chosen; } MeterInterval;
|
||
|
||
/**
|
||
* 功能:核对binary32、double精度、求值模式及最近舍入条件,保证编码与整数原像搜索使用同一口径。
|
||
* 输入参数:无。
|
||
* 输出参数:无独立输出参数。
|
||
* 返回值:满足电能编码要求的数值环境时返回true,否则返回false。
|
||
* 说明:无。
|
||
*/
|
||
static bool Numeric_Mode(void)
|
||
{
|
||
return sizeof(float) == 4U && FLT_RADIX == 2 && FLT_MANT_DIG == 24 &&
|
||
DBL_MANT_DIG >= 53 && FLT_EVAL_METHOD == 0 && fegetround() == FE_TONEAREST;
|
||
}
|
||
|
||
/**
|
||
* 功能:检查UTC毫秒是否位于冻结核心支持的日历范围,不读取或校准实际墙钟。
|
||
* 输入参数:utc_ms——UTC毫秒时刻。
|
||
* 输出参数:无独立输出参数。
|
||
* 返回值:UTC毫秒能落在支持的CP56日历范围内时返回true,否则返回false。
|
||
* 说明:无。
|
||
*/
|
||
static bool Valid_Time(uint64_t utc_ms)
|
||
{
|
||
return utc_ms >= IEC60870_ENERGY_FREEZE_UTC_MIN && utc_ms <= IEC60870_ENERGY_FREEZE_UTC_MAX;
|
||
}
|
||
|
||
/**
|
||
* 功能:整数Wh/varh先经double除以1000,再舍入为float的kWh/kvarh;恢复必须复用此编码顺序。
|
||
* 输入参数:value——本次待处理的数值。
|
||
* 输出参数:无独立输出参数。
|
||
* 返回值:由整数电能量按实现比例编码的binary32值。
|
||
* 说明:无。
|
||
*/
|
||
static float Encode(uint64_t value)
|
||
{
|
||
return (float)((double)value / 1000.0);
|
||
}
|
||
|
||
/**
|
||
* 功能:按原始字节比较float,恢复验收要求编码完全一致,不能仅用数值相等替代。
|
||
* 输入参数:first——第一个浮点值;second——第二个浮点值。
|
||
* 输出参数:无独立输出参数。
|
||
* 返回值:两个浮点值的binary32编码完全相同时返回true,否则返回false。
|
||
* 说明:无。
|
||
*/
|
||
static bool Same_Float(float first, float second)
|
||
{
|
||
return memcmp(&first, &second, sizeof(first)) == 0;
|
||
}
|
||
|
||
/**
|
||
* 功能:取较大端点,用于收紧整数原像或合计约束的下界。
|
||
* 输入参数:first——第一个无符号整数;second——第二个无符号整数。
|
||
* 输出参数:无独立输出参数。
|
||
* 返回值:两个无符号整数中的较大者。
|
||
* 说明:无。
|
||
*/
|
||
static uint64_t Maximum(uint64_t first, uint64_t second)
|
||
{
|
||
return first > second ? first : second;
|
||
}
|
||
|
||
/**
|
||
* 功能:取较小值,用于收紧可行上界或限制当前相位的有效导通时间。
|
||
* 输入参数:first——第一个无符号整数;second——第二个无符号整数。
|
||
* 输出参数:无独立输出参数。
|
||
* 返回值:两个无符号整数中的较小者。
|
||
* 说明:无。
|
||
*/
|
||
static uint64_t Minimum(uint64_t first, uint64_t second)
|
||
{
|
||
return first < second ? first : second;
|
||
}
|
||
|
||
/**
|
||
* 功能:将候选整数限制到闭区间内,调用方须先保证low不大于high。
|
||
* 输入参数:value——本次待处理的数值;low——允许区间下界;high——允许区间上界。
|
||
* 输出参数:无独立输出参数。
|
||
* 返回值:限制在闭区间[low, high]内的值。
|
||
* 说明:无。
|
||
*/
|
||
static uint64_t Clamp(uint64_t value, uint64_t low, uint64_t high)
|
||
{
|
||
return value < low ? low : value > high ? high : value;
|
||
}
|
||
|
||
/**
|
||
* 功能:执行受检无符号加法,防止时间或积分量回绕;溢出返回false且不修改result。
|
||
* 输入参数:first——第一个无符号整数;second——第二个无符号整数;result——计算结果回写位置。
|
||
* 输出参数:result——计算结果回写位置。
|
||
* 返回值:加法不溢出且结果已写回时返回true,否则返回false。
|
||
* 说明:无。
|
||
*/
|
||
static bool Add_Checked(uint64_t first, uint64_t second, uint64_t *result)
|
||
{
|
||
if (first > UINT64_MAX - second) { return false; }
|
||
*result = first + second;
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 功能:执行受检无符号乘法,允许零因子;溢出返回false且不修改result。
|
||
* 输入参数:first——第一个无符号整数;second——第二个无符号整数;result——计算结果回写位置。
|
||
* 输出参数:result——计算结果回写位置。
|
||
* 返回值:乘法不溢出且结果已写回时返回true,否则返回false。
|
||
* 说明:无。
|
||
*/
|
||
static bool Multiply_Checked(uint64_t first, uint64_t second, uint64_t *result)
|
||
{
|
||
if (second != 0U && first > UINT64_MAX / second) { return false; }
|
||
*result = first * second;
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 功能:将P+、P-、Q1、Q2、Q3、Q4六个独立量排成P+、Q+、Q1、Q4、P-、Q-、Q2、Q3八值。
|
||
* 输入参数:independent——六路独立电能寄存器;registers——八路整数电能寄存器回写数组。
|
||
* 输出参数:registers——八路整数电能寄存器回写数组。
|
||
* 返回值:无。
|
||
* 说明:DTU使用P+/Q+/P-/Q-/Q1/Q4/Q2/Q3;Q+/Q-分别由对应象限整数相加,调用方已保证求和不越界。
|
||
*/
|
||
static void Arrange(const uint64_t independent[EXAMPLE_ENERGY_METER_INDEPENDENT],
|
||
uint64_t registers[EXAMPLE_ENERGY_METER_POINTS])
|
||
{
|
||
registers[METER_P_PLUS] = independent[0];
|
||
registers[METER_Q_PLUS] = independent[2] + independent[3];
|
||
registers[METER_Q1] = independent[2];
|
||
registers[METER_Q4] = independent[5];
|
||
registers[METER_P_MINUS] = independent[1];
|
||
registers[METER_Q_MINUS] = independent[4] + independent[5];
|
||
registers[METER_Q2] = independent[3];
|
||
registers[METER_Q3] = independent[4];
|
||
}
|
||
|
||
/**
|
||
* 功能:建立零积分基线、波形起点和首个单调时刻/UTC映射,首条反转从第1个半周期开始。
|
||
* 输入参数:self——调用方持有的模块实例;period_ms——功率方波周期,单位毫秒;mono_ms——绝对单调毫秒时刻;utc_ms——UTC毫秒时刻。
|
||
* 输出参数:self——本次操作更新的实例或工作区。
|
||
* 返回值:true表示本次操作成功,false表示校验拒绝或处理失败。
|
||
* 说明:周期须为2000..86400000ms内的4倍数;参数或数值环境无效时返回false,不改写实例。
|
||
*/
|
||
bool Example_Energy_Meter_Init(ExampleEnergyMeter *self, uint32_t period_ms,
|
||
uint64_t mono_ms, uint64_t utc_ms)
|
||
{
|
||
if (self == NULL || !Numeric_Mode() || !Valid_Time(utc_ms) ||
|
||
period_ms < 2000U || period_ms > 86400000U || period_ms % 4U != 0U) { return false; }
|
||
memset(self, 0, sizeof(*self));
|
||
self->origin_ms = self->last_step_ms = mono_ms;
|
||
self->period_ms = period_ms;
|
||
self->next_flow_index = 1U;
|
||
self->epoch_count = 1U;
|
||
self->epochs[0].monotonic_ms = mono_ms;
|
||
self->epochs[0].utc_ms = utc_ms;
|
||
self->last_result = IEC60870_ENERGY_FREEZE_OK;
|
||
self->initialized = true;
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 功能:对每个独立量计算它在完整周期和当前相位中的导通毫秒。
|
||
* 输入参数:self——调用方持有的模块实例;mono_ms——绝对单调毫秒时刻;registers——八路整数电能寄存器回写数组。
|
||
* 输出参数:registers——八路整数电能寄存器回写数组。
|
||
* 返回值:true表示计算或反解成功并发布输出数组;false表示输入、范围或数值约束不满足。
|
||
* 说明:不保存“上次输出”或推进积分,先读当前值再补旧FLOW仍得到旧时刻的值。
|
||
*/
|
||
static bool Conducting_Time(const ExampleEnergyMeter *self, uint64_t mono_ms,
|
||
size_t index, uint64_t *duration)
|
||
{
|
||
static const uint8_t quarter[4] = {0U, 3U, 2U, 1U}; /* Q1,Q2,Q3,Q4起始quarter。 */
|
||
uint64_t elapsed = mono_ms - self->origin_ms;
|
||
uint64_t cycles = elapsed / self->period_ms, phase = elapsed % self->period_ms;
|
||
uint64_t width = self->period_ms / (index < 2U ? 2U : 4U);
|
||
uint64_t start = index < 2U ? index * width : quarter[index - 2U] * width;
|
||
uint64_t overlap = phase <= start ? 0U : Minimum(phase - start, width);
|
||
return Multiply_Checked(cycles, width, duration) && Add_Checked(*duration, overlap, duration);
|
||
}
|
||
|
||
/* 返回不含持久恢复基线的 Wms/varms;默认增益仍走原受检整数乘法。 */
|
||
static bool Integral_At(const ExampleEnergyMeter *self, uint64_t mono_ms,
|
||
uint64_t accumulated[EXAMPLE_ENERGY_METER_INDEPENDENT],
|
||
double remainder[EXAMPLE_ENERGY_METER_INDEPENDENT])
|
||
{
|
||
static const uint8_t reversed[EXAMPLE_ENERGY_METER_INDEPENDENT] = {1U, 0U, 4U, 5U, 2U, 3U};
|
||
const ExampleEnergyMeterGain *segment = NULL;
|
||
size_t index;
|
||
if (self == NULL || !self->initialized || self->faulted || self->period_ms == 0U ||
|
||
self->gain_count > EXAMPLE_ENERGY_METER_GAIN_SEGMENTS || mono_ms < self->origin_ms) { return false; }
|
||
for (index = self->gain_count; index > 0U; --index) {
|
||
if (self->gains[index - 1U].monotonic_ms <= mono_ms) { segment = &self->gains[index - 1U]; break; }
|
||
}
|
||
for (index = 0U; index < EXAMPLE_ENERGY_METER_INDEPENDENT; ++index) {
|
||
uint64_t duration, start_duration = 0U, increment;
|
||
uint64_t power = index < 2U ? METER_ACTIVE_WATTS : METER_REACTIVE_VARS;
|
||
size_t source = segment != NULL && segment->reverse ? reversed[index] : index;
|
||
double gain = segment == NULL ? 1.0 : index < 2U ? segment->active_gain : segment->reactive_gain;
|
||
accumulated[index] = segment == NULL ? 0U : segment->accumulated[index];
|
||
remainder[index] = segment == NULL ? 0.0 : segment->remainder[index];
|
||
if (!Conducting_Time(self, mono_ms, source, &duration) ||
|
||
(segment != NULL && !Conducting_Time(self, segment->monotonic_ms, source, &start_duration)) ||
|
||
duration < start_duration) { return false; }
|
||
duration -= start_duration;
|
||
if (gain == 1.0) {
|
||
if (!Multiply_Checked(duration, power, &increment)) { return false; }
|
||
} else {
|
||
long double value = (long double)duration * (long double)power * (long double)gain +
|
||
(long double)remainder[index];
|
||
long double whole;
|
||
/* 先在浮点域排除 2^63,避免 INT64_MAX 转 double 后向上舍入的边界。 */
|
||
if (!isfinite(value) || value < 0.0L || value >= 9223372036854775808.0L) { return false; }
|
||
whole = floorl(value);
|
||
increment = (uint64_t)whole;
|
||
remainder[index] = (double)(value - whole);
|
||
if (remainder[index] >= 1.0) { ++increment; remainder[index] = 0.0; }
|
||
}
|
||
if (accumulated[index] > (uint64_t)INT64_MAX ||
|
||
increment > (uint64_t)INT64_MAX - accumulated[index]) { return false; }
|
||
accumulated[index] += increment;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
bool Example_Energy_Meter_Evaluate_At(const ExampleEnergyMeter *self,
|
||
uint64_t mono_ms, uint64_t registers[EXAMPLE_ENERGY_METER_POINTS])
|
||
{
|
||
uint64_t accumulated[EXAMPLE_ENERGY_METER_INDEPENDENT];
|
||
uint64_t independent[EXAMPLE_ENERGY_METER_INDEPENDENT], result[EXAMPLE_ENERGY_METER_POINTS];
|
||
double remainder[EXAMPLE_ENERGY_METER_INDEPENDENT];
|
||
size_t index;
|
||
if (registers == NULL || !Integral_At(self, mono_ms, accumulated, remainder)) { return false; }
|
||
for (index = 0U; index < EXAMPLE_ENERGY_METER_INDEPENDENT; ++index) {
|
||
if (self->base_accumulators[index] < 0 ||
|
||
accumulated[index] > (uint64_t)INT64_MAX - (uint64_t)self->base_accumulators[index]) { return false; }
|
||
independent[index] = ((uint64_t)self->base_accumulators[index] + accumulated[index]) / METER_SCALE;
|
||
}
|
||
if (independent[2] > METER_EXACT_INTEGER - independent[3] ||
|
||
independent[4] > METER_EXACT_INTEGER - independent[5]) { return false; }
|
||
Arrange(independent, result);
|
||
memcpy(registers, result, sizeof(result));
|
||
return true;
|
||
}
|
||
|
||
/* 纯计算候选;Can/Set 共用,失败不修改游标、历史分段或已确认的增益。 */
|
||
static bool Prepare_Gain(const ExampleEnergyMeter *self, uint64_t mono_ms,
|
||
double active_gain, double reactive_gain, double energy_factor, bool reverse,
|
||
ExampleEnergyMeterGain *candidate, size_t *position, bool *change)
|
||
{
|
||
const ExampleEnergyMeterGain *last;
|
||
double active, reactive;
|
||
bool previous_reverse;
|
||
size_t index;
|
||
if (self == NULL || !self->initialized || self->faulted || !Numeric_Mode() ||
|
||
self->gain_count > EXAMPLE_ENERGY_METER_GAIN_SEGMENTS || mono_ms < self->last_step_ms ||
|
||
!isfinite(active_gain) || active_gain <= 0.0 || !isfinite(reactive_gain) || reactive_gain <= 0.0 ||
|
||
!isfinite(energy_factor) || energy_factor <= 0.0) { return false; }
|
||
active = active_gain * energy_factor; reactive = reactive_gain * energy_factor;
|
||
if (!isfinite(active) || active <= 0.0 || !isfinite(reactive) || reactive <= 0.0 ||
|
||
!isfinite(active * METER_ACTIVE_WATTS) || !isfinite(reactive * METER_REACTIVE_VARS)) { return false; }
|
||
last = self->gain_count == 0U ? NULL : &self->gains[self->gain_count - 1U];
|
||
if (mono_ms < self->origin_ms || (last != NULL && mono_ms < last->monotonic_ms)) { return false; }
|
||
*change = active != (last == NULL ? 1.0 : last->active_gain) ||
|
||
reactive != (last == NULL ? 1.0 : last->reactive_gain) || reverse != (last != NULL && last->reverse);
|
||
if (!*change) { return true; }
|
||
*position = last != NULL && mono_ms == last->monotonic_ms ? self->gain_count - 1U : self->gain_count;
|
||
if (*position == EXAMPLE_ENERGY_METER_GAIN_SEGMENTS) { return false; }
|
||
memset(candidate, 0, sizeof(*candidate));
|
||
if (!Integral_At(self, mono_ms, candidate->accumulated, candidate->remainder)) { return false; }
|
||
for (index = 0U; index < EXAMPLE_ENERGY_METER_INDEPENDENT; ++index) {
|
||
if (self->base_accumulators[index] < 0 || candidate->accumulated[index] >
|
||
(uint64_t)INT64_MAX - (uint64_t)self->base_accumulators[index]) { return false; }
|
||
}
|
||
candidate->monotonic_ms = mono_ms;
|
||
candidate->active_gain = active; candidate->reactive_gain = reactive; candidate->reverse = reverse;
|
||
previous_reverse = *position == 0U ? false : self->gains[*position - 1U].reverse;
|
||
candidate->reverse_transition = mono_ms > self->origin_ms && reverse != previous_reverse;
|
||
candidate->flow_done = *position < self->gain_count && self->gains[*position].flow_done;
|
||
return true;
|
||
}
|
||
|
||
bool Example_Energy_Meter_Can_Set_Gains_At(const ExampleEnergyMeter *self, uint64_t mono_ms,
|
||
double active_gain, double reactive_gain, double energy_factor, bool reverse)
|
||
{
|
||
ExampleEnergyMeterGain candidate;
|
||
size_t position = 0U;
|
||
bool change;
|
||
return Prepare_Gain(self, mono_ms, active_gain, reactive_gain, energy_factor, reverse,
|
||
&candidate, &position, &change);
|
||
}
|
||
|
||
bool Example_Energy_Meter_Set_Gains_At(ExampleEnergyMeter *self, uint64_t mono_ms,
|
||
double active_gain, double reactive_gain, double energy_factor, bool reverse)
|
||
{
|
||
ExampleEnergyMeterGain candidate;
|
||
size_t position = 0U;
|
||
bool change;
|
||
if (!Prepare_Gain(self, mono_ms, active_gain, reactive_gain, energy_factor, reverse,
|
||
&candidate, &position, &change)) { return false; }
|
||
if (change) {
|
||
self->gains[position] = candidate;
|
||
if (position == self->gain_count) { ++self->gain_count; }
|
||
}
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 功能:查询指定单调时刻的八值并编码为浮点示值,成功时统一填零品质。
|
||
* 输入参数:self——调用方持有的模块实例;mono_ms——绝对单调毫秒时刻;values——八路浮点电能值数组;quality——八路品质回写数组。
|
||
* 输出参数:values——八路浮点电能值数组;quality——八路品质回写数组。
|
||
* 返回值:true表示计算或反解成功并发布输出数组;false表示输入、范围或数值约束不满足。
|
||
* 说明:不推进积分或事件游标;数值环境或求值失败时返回false,保持两个输出数组原内容。
|
||
*/
|
||
bool Example_Energy_Meter_Snapshot(const ExampleEnergyMeter *self, uint64_t mono_ms,
|
||
float values[EXAMPLE_ENERGY_METER_POINTS], uint8_t quality[EXAMPLE_ENERGY_METER_POINTS])
|
||
{
|
||
uint64_t registers[EXAMPLE_ENERGY_METER_POINTS];
|
||
size_t index;
|
||
if (values == NULL || quality == NULL || !Numeric_Mode() ||
|
||
!Example_Energy_Meter_Evaluate_At(self, mono_ms, registers)) { return false; }
|
||
for (index = 0U; index < EXAMPLE_ENERGY_METER_POINTS; ++index) { values[index] = Encode(registers[index]); }
|
||
memset(quality, 0, EXAMPLE_ENERGY_METER_POINTS);
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 功能:单调encode的两个下界搜索得到完整整数原像,不能用ceil(v*1000)替代。
|
||
* 输入参数:value——本次待处理的数值;out——反解区间回写位置。
|
||
* 输出参数:out——反解区间回写位置。
|
||
* 返回值:true表示本次操作成功,false表示校验拒绝或处理失败。
|
||
* 说明:后者会把0.001f恢复成2Wh,或在binary32乘法时把较大值恢复得更小。
|
||
*/
|
||
static bool Inverse(float value, MeterInterval *out)
|
||
{
|
||
uint64_t low = 0U, high = METER_EXACT_INTEGER + 1U, first;
|
||
if (!isfinite(value) || value < 0.0f) { return false; }
|
||
while (low < high) {
|
||
uint64_t middle = low + (high - low) / 2U;
|
||
if (Encode(middle) >= value) { high = middle; } else { low = middle + 1U; }
|
||
}
|
||
first = low;
|
||
if (first > METER_EXACT_INTEGER || !Same_Float(Encode(first), value)) { return false; }
|
||
high = METER_EXACT_INTEGER + 1U;
|
||
while (low < high) {
|
||
uint64_t middle = low + (high - low) / 2U;
|
||
if (Encode(middle) > value) { high = middle; } else { low = middle + 1U; }
|
||
}
|
||
out->low = first;
|
||
out->high = low - 1U;
|
||
out->chosen = first;
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 功能:在已确定的整数原像区间内选择最接近double(value)*1000的整数,同距取较小值。
|
||
* 输入参数:value——本次待处理的数值;low——允许区间下界;high——允许区间上界。
|
||
* 输出参数:无独立输出参数。
|
||
* 返回值:允许区间内最接近目标电能编码的整数候选。
|
||
* 说明:超出区间时取最近端点,避免选择不可重新编码为原float的候选。
|
||
*/
|
||
static uint64_t Nearest(float value, uint64_t low, uint64_t high)
|
||
{
|
||
double wanted = (double)value * 1000.0;
|
||
uint64_t below;
|
||
if (wanted <= (double)low) { return low; }
|
||
if (wanted >= (double)high) { return high; }
|
||
below = (uint64_t)wanted;
|
||
return wanted - (double)below > 0.5 ? below + 1U : below;
|
||
}
|
||
|
||
/**
|
||
* 功能:从两个独立整数原像区间中选出满足合计约束的电能寄存器。
|
||
* 输入参数:first——第一个电能反解区间;second——第二个电能反解区间;sum——两路电能的总量区间。
|
||
* 输出参数:first——本次操作更新的实例或工作区;second——本次操作更新的实例或工作区。
|
||
* 返回值:true表示本次操作成功,false表示校验拒绝或处理失败。
|
||
* 说明:先求合计可行区间,再在两个独立原像中拆分;不把合计舍入误差
|
||
* 直接加给某个象限,避免超出该象限原像或模拟累加器范围。
|
||
*/
|
||
static bool Restore_Pair(MeterInterval *first, MeterInterval *second, const MeterInterval *sum)
|
||
{
|
||
uint64_t low = Maximum(first->low + second->low, sum->low);
|
||
uint64_t high = Minimum(first->high + second->high, sum->high);
|
||
uint64_t selected, first_low, first_high;
|
||
if (low > high) { return false; }
|
||
selected = Clamp(first->chosen + second->chosen, low, high);
|
||
first_low = Maximum(first->low, selected > second->high ? selected - second->high : 0U);
|
||
if (selected < second->low) { return false; }
|
||
first_high = Minimum(first->high, selected - second->low);
|
||
if (first_low > first_high) { return false; }
|
||
first->chosen = Clamp(first->chosen, first_low, first_high);
|
||
second->chosen = selected - first->chosen;
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 功能:从八个浮点示值恢复可行的整数Wh/varh,同时满足六个独立量和两个无功合计的编码原像。
|
||
* 输入参数:values——八路浮点电能值数组;registers——八路整数电能寄存器回写数组。
|
||
* 输出参数:registers——八路整数电能寄存器回写数组。
|
||
* 返回值:true表示计算或反解成功并发布输出数组;false表示输入、范围或数值约束不满足。
|
||
* 说明:独立量受累加器容量约束;最终八值重新编码须逐字节匹配,成功才写回registers。
|
||
* 无可行原像、数值环境不符或超限时返回false,不以浮点和或简单乘1000替代逆像恢复。
|
||
*/
|
||
bool Example_Energy_Meter_Restore_From_Values(const float values[EXAMPLE_ENERGY_METER_POINTS],
|
||
uint64_t registers[EXAMPLE_ENERGY_METER_POINTS])
|
||
{
|
||
MeterInterval independent[EXAMPLE_ENERGY_METER_INDEPENDENT], positive, negative;
|
||
uint64_t restored[EXAMPLE_ENERGY_METER_INDEPENDENT], result[EXAMPLE_ENERGY_METER_POINTS];
|
||
size_t index;
|
||
if (values == NULL || registers == NULL || !Numeric_Mode()) { return false; }
|
||
for (index = 0U; index < EXAMPLE_ENERGY_METER_INDEPENDENT; ++index) {
|
||
float value = values[meter_independent_positions[index]];
|
||
if (!Inverse(value, &independent[index])) { return false; }
|
||
independent[index].high = Minimum(independent[index].high, EXAMPLE_ENERGY_METER_REGISTER_MAX);
|
||
if (independent[index].low > independent[index].high) { return false; }
|
||
independent[index].chosen = Nearest(value, independent[index].low, independent[index].high);
|
||
}
|
||
if (!Inverse(values[METER_Q_PLUS], &positive) || !Inverse(values[METER_Q_MINUS], &negative) ||
|
||
!Restore_Pair(&independent[2], &independent[3], &positive) ||
|
||
!Restore_Pair(&independent[4], &independent[5], &negative)) { return false; }
|
||
for (index = 0U; index < EXAMPLE_ENERGY_METER_INDEPENDENT; ++index) {
|
||
restored[index] = independent[index].chosen;
|
||
if (restored[index] > (uint64_t)INT64_MAX / METER_SCALE) { return false; }
|
||
}
|
||
Arrange(restored, result);
|
||
for (index = 0U; index < EXAMPLE_ENERGY_METER_POINTS; ++index) {
|
||
if (!Same_Float(Encode(result[index]), values[index])) { return false; }
|
||
}
|
||
memcpy(registers, result, sizeof(result));
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 功能:按全部八值字典序供qsort排序;输入已排除非有限值,分量是否逐项不下降由恢复流程另行检查。
|
||
* 输入参数:left——排序左侧向量;right——排序右侧向量。
|
||
* 输出参数:无独立输出参数。
|
||
* 返回值:左向量小于、等于或大于右向量时分别返回负数、0或正数。
|
||
* 说明:无。
|
||
*/
|
||
static int Compare_Vectors(const void *left, const void *right)
|
||
{
|
||
const MeterVector *first = left, *second = right;
|
||
size_t index;
|
||
for (index = 0U; index < EXAMPLE_ENERGY_METER_POINTS; ++index) {
|
||
if (first->values[index] < second->values[index]) { return -1; }
|
||
if (first->values[index] > second->values[index]) { return 1; }
|
||
}
|
||
return 0;
|
||
}
|
||
|
||
/**
|
||
* 功能:将完整的八个非负有限示值追加到有界恢复候选区,成功才增加count。
|
||
* 输入参数:vectors——恢复候选向量数组;count——已收集向量数量的读写位置;values——八路浮点电能值数组。
|
||
* 输出参数:vectors——恢复候选向量数组;count——已收集向量数量的读写位置。
|
||
* 返回值:true表示本次操作成功,false表示校验拒绝或处理失败。
|
||
* 说明:容量已满或任一值非法时返回false,不保存部分向量。
|
||
*/
|
||
static bool Save_Vector(MeterVector *vectors, size_t *count, const float *values)
|
||
{
|
||
size_t index;
|
||
if (*count >= METER_RECOVERY_CAPACITY) { return false; }
|
||
for (index = 0U; index < EXAMPLE_ENERGY_METER_POINTS; ++index) {
|
||
if (!isfinite(values[index]) || values[index] < 0.0f) { return false; }
|
||
}
|
||
memcpy(vectors[*count].values, values, sizeof(vectors[*count].values));
|
||
++*count;
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 功能:收集保留窗口的INTERVAL/DAY及完整有效的八点FLOW,核心两类点数均须为8。
|
||
* 输入参数:core——已初始化的冻结电能核心;vectors——恢复候选向量数组;count——已收集向量数量的读写位置。
|
||
* 输出参数:vectors——恢复候选向量数组;count——已收集向量数量的读写位置。
|
||
* 返回值:true表示本次操作成功,false表示校验拒绝或处理失败。
|
||
* 说明:不完整或坏品质FLOW跳过,定时记录损坏、负值或容量不足则失败;失败后的候选不可使用。
|
||
* 扫描复用核心读取工作区,不提交记录,由调用方提供METER_RECOVERY_CAPACITY容量。
|
||
*/
|
||
static bool Read_Vectors(IEC60870_EnergyFreeze_t *core, MeterVector *vectors, size_t *count)
|
||
{
|
||
IEC60870_EnergyFreezeStatus_t status;
|
||
union {
|
||
IEC60870_EnergyFreezeRecord_t record;
|
||
IEC60870_EnergyFreezeFlowReverse_t flow;
|
||
} scratch;
|
||
uint32_t offset;
|
||
size_t index, point;
|
||
if (!IEC60870_EnergyFreeze_Get_Status(core, &status) || status.faulted ||
|
||
core->config.point_count != EXAMPLE_ENERGY_METER_POINTS ||
|
||
core->config.flowrev_count != EXAMPLE_ENERGY_METER_POINTS) { return false; }
|
||
*count = 0U;
|
||
if (status.retained_valid) {
|
||
for (offset = 0U; offset < IEC60870_ENERGY_FREEZE_DAYS; ++offset) {
|
||
uint32_t day = status.retained_from + offset;
|
||
uint64_t window_start;
|
||
uint16_t slot;
|
||
/* 首次保存接近2099末日时,未用满的32槽尾部可能越过日历上界。 */
|
||
if (!IEC60870_EnergyFreeze_Window_Start(day, core->config.day_start_minutes, &window_start)) { continue; }
|
||
for (slot = 0U; slot <= IEC60870_ENERGY_FREEZE_SLOTS; ++slot) {
|
||
IEC60870_EnergyFreezeResult_t result = slot == 0U ?
|
||
IEC60870_EnergyFreeze_Read_Day(core, day, &scratch.record) :
|
||
IEC60870_EnergyFreeze_Read_Interval(core, day, slot, &scratch.record);
|
||
if (result == IEC60870_ENERGY_FREEZE_EMPTY) { continue; }
|
||
if (result != IEC60870_ENERGY_FREEZE_OK || scratch.record.count != EXAMPLE_ENERGY_METER_POINTS) { return false; }
|
||
for (point = 0U; point < EXAMPLE_ENERGY_METER_POINTS; ++point) {
|
||
if (scratch.record.quality[point] != 0U) { return false; }
|
||
}
|
||
if (!Save_Vector(vectors, count, scratch.record.values)) { return false; }
|
||
}
|
||
}
|
||
}
|
||
for (index = 0U; index < status.flow_count; ++index) {
|
||
if (IEC60870_EnergyFreeze_Get_FlowReverse(core, index, &scratch.flow) != IEC60870_ENERGY_FREEZE_OK) { return false; }
|
||
if (scratch.flow.count != EXAMPLE_ENERGY_METER_POINTS) { continue; }
|
||
for (point = 0U; point < EXAMPLE_ENERGY_METER_POINTS; ++point) {
|
||
if (scratch.flow.indices[point] != point || scratch.flow.quality[point] != 0U ||
|
||
!isfinite(scratch.flow.values[point])) { break; }
|
||
}
|
||
if (point != EXAMPLE_ENERGY_METER_POINTS) { continue; }
|
||
if (!Save_Vector(vectors, count, scratch.flow.values)) { return false; }
|
||
}
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 功能:仅启动时恢复:排序全部八值候选,验证相邻向量逐分量不下降,再由最大向量建立积分基线。
|
||
* 输入参数:self——调用方持有的模块实例;core——已初始化的冻结电能核心。
|
||
* 输出参数:self——本次操作更新的实例或工作区。
|
||
* 返回值:true表示恢复检查成功;空库保留原整数基线,有候选时验证支配链后恢复基线。false表示读取、数值或内存申请失败。
|
||
* 说明:Windows临时堆保存候选并在返回前释放;扫描或恢复失败锁存faulted,空库不改变现有基线。
|
||
* 参数无效、分配失败或非Windows后端直接返回false;调用方均须停止启动,不使用部分恢复值。
|
||
*/
|
||
bool Example_Energy_Meter_Restore(ExampleEnergyMeter *self, IEC60870_EnergyFreeze_t *core)
|
||
{
|
||
#ifdef _WIN32
|
||
MeterVector *vectors;
|
||
uint64_t restored[EXAMPLE_ENERGY_METER_POINTS];
|
||
size_t count, index, point;
|
||
bool success = false;
|
||
if (self == NULL || core == NULL || !self->initialized || self->faulted ||
|
||
self->next_flow_index != 1U || self->last_step_ms != self->origin_ms || !Numeric_Mode()) { return false; }
|
||
vectors = HeapAlloc(GetProcessHeap(), 0U, METER_RECOVERY_CAPACITY * sizeof(*vectors));
|
||
if (vectors == NULL) { return false; }
|
||
if (!Read_Vectors(core, vectors, &count)) { goto complete; }
|
||
if (count == 0U) { success = true; goto complete; }
|
||
qsort(vectors, count, sizeof(*vectors), Compare_Vectors);
|
||
for (index = 1U; index < count; ++index) {
|
||
for (point = 0U; point < EXAMPLE_ENERGY_METER_POINTS; ++point) {
|
||
if (vectors[index].values[point] < vectors[index - 1U].values[point]) { goto complete; }
|
||
}
|
||
}
|
||
if (!Example_Energy_Meter_Restore_From_Values(vectors[count - 1U].values, restored)) { goto complete; }
|
||
for (index = 0U; index < EXAMPLE_ENERGY_METER_INDEPENDENT; ++index) {
|
||
self->base_accumulators[index] = (int64_t)(restored[meter_independent_positions[index]] * METER_SCALE);
|
||
}
|
||
success = true;
|
||
complete:
|
||
(void)HeapFree(GetProcessHeap(), 0U, vectors);
|
||
if (!success) { self->last_result = IEC60870_ENERGY_FREEZE_ERROR; self->faulted = true; }
|
||
return success;
|
||
#else
|
||
(void)self;
|
||
(void)core;
|
||
return false;
|
||
#endif
|
||
}
|
||
|
||
/**
|
||
* 功能:按波形起点加index个半周期求反转的单调时刻;受检乘加溢出返回false,保持输出不变。
|
||
* 输入参数:self——调用方持有的模块实例;index——本次条目、点或逻辑槽的下标;mono_ms——绝对单调毫秒回写位置。
|
||
* 输出参数:mono_ms——绝对单调毫秒回写位置。
|
||
* 返回值:true表示本次操作成功,false表示校验拒绝或处理失败。
|
||
* 说明:无。
|
||
*/
|
||
static bool Event_Time(const ExampleEnergyMeter *self, uint64_t index, uint64_t *mono_ms)
|
||
{
|
||
uint64_t elapsed;
|
||
return Multiply_Checked(index, self->period_ms / 2U, &elapsed) &&
|
||
Add_Checked(self->origin_ms, elapsed, mono_ms);
|
||
}
|
||
|
||
static size_t Next_Gain_Flow(const ExampleEnergyMeter *self)
|
||
{
|
||
size_t index;
|
||
for (index = 0U; index < self->gain_count; ++index) {
|
||
if (self->gains[index].reverse_transition && !self->gains[index].flow_done) { break; }
|
||
}
|
||
return index;
|
||
}
|
||
|
||
/**
|
||
* 功能:移除最老墙钟分段并紧凑剩余数组;调用方保证队列非空,并先处理该段尚需映射的事件。
|
||
* 输入参数:self——调用方持有的模块实例。
|
||
* 输出参数:self——移除最旧时段并压紧epochs,更新epoch_count。
|
||
* 返回值:无。
|
||
* 说明:无。
|
||
*/
|
||
static void Remove_Oldest_Epoch(ExampleEnergyMeter *self)
|
||
{
|
||
--self->epoch_count;
|
||
memmove(self->epochs, self->epochs + 1U, self->epoch_count * sizeof(self->epochs[0]));
|
||
}
|
||
|
||
/**
|
||
* 功能:按下一条待生成FLOW的时刻淘汰不再需要的旧分段,保留能映射该事件的最近基准。
|
||
* 输入参数:self——调用方持有的模块实例。
|
||
* 输出参数:self——按待处理事件游标清理已不再需要的时段,更新epochs和epoch_count。
|
||
* 返回值:无。
|
||
* 说明:下一事件时刻溢出时暂不清理,不推进事件游标或丢弃事件。
|
||
*/
|
||
static void Trim_Epochs(ExampleEnergyMeter *self)
|
||
{
|
||
uint64_t next;
|
||
size_t gain = Next_Gain_Flow(self);
|
||
if (!Event_Time(self, self->next_flow_index, &next)) { return; }
|
||
if (gain < self->gain_count && self->gains[gain].monotonic_ms < next) {
|
||
next = self->gains[gain].monotonic_ms;
|
||
}
|
||
while (self->epoch_count > 1U && self->epochs[1].monotonic_ms <= next) { Remove_Oldest_Epoch(self); }
|
||
}
|
||
|
||
/**
|
||
* 功能:加入校时成功后的逻辑切换点,单调时刻不能倒退;相同切点覆盖UTC,已提交事件不重标。
|
||
* 输入参数:self——调用方持有的模块实例;mono_ms——绝对单调毫秒时刻;utc_ms——UTC毫秒时刻。
|
||
* 输出参数:self——本次操作更新的实例或工作区。
|
||
* 返回值:true表示新的逻辑时钟基准已登记;false表示实例、时刻或顺序校验失败。
|
||
* 说明:分段队列满时丢弃最老时段尚未生成的事件并累计flowrev_dropped,再腾出一个分段。
|
||
* 不改变积分起点和示值;此映射由应用显式维护,不自动跟随系统墙钟刷新。
|
||
*/
|
||
bool Example_Energy_Meter_Set_Epoch(ExampleEnergyMeter *self, uint64_t mono_ms, uint64_t utc_ms)
|
||
{
|
||
ExampleEnergyMeterEpoch *last;
|
||
if (self == NULL || !self->initialized || self->faulted || !Valid_Time(utc_ms) ||
|
||
self->epoch_count == 0U || mono_ms < self->origin_ms) { return false; }
|
||
last = &self->epochs[self->epoch_count - 1U];
|
||
if (mono_ms < last->monotonic_ms) { return false; }
|
||
if (mono_ms == last->monotonic_ms) { last->utc_ms = utc_ms; return true; }
|
||
Trim_Epochs(self);
|
||
if (self->epoch_count == EXAMPLE_ENERGY_METER_EPOCHS) {
|
||
size_t gain;
|
||
uint64_t elapsed = self->epochs[1].monotonic_ms - self->origin_ms;
|
||
uint64_t half = self->period_ms / 2U;
|
||
uint64_t first = elapsed / half + (elapsed % half != 0U ? 1U : 0U);
|
||
if (first > self->next_flow_index) {
|
||
self->flowrev_dropped += first - self->next_flow_index;
|
||
self->next_flow_index = first;
|
||
}
|
||
for (gain = 0U; gain < self->gain_count; ++gain) {
|
||
ExampleEnergyMeterGain *segment = &self->gains[gain];
|
||
if (segment->reverse_transition && !segment->flow_done &&
|
||
segment->monotonic_ms < self->epochs[1].monotonic_ms) {
|
||
segment->flow_done = true;
|
||
++self->flowrev_dropped;
|
||
}
|
||
}
|
||
Remove_Oldest_Epoch(self);
|
||
}
|
||
self->epochs[self->epoch_count].monotonic_ms = mono_ms;
|
||
self->epochs[self->epoch_count].utc_ms = utc_ms;
|
||
++self->epoch_count;
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 功能:使用最后一个不晚于查询时刻的分段,将单调毫秒映射为UTC;旧事件保留其所属分段的时间口径。
|
||
* 输入参数:self——调用方持有的模块实例;mono_ms——绝对单调毫秒时刻;utc_ms——UTC毫秒回写位置。
|
||
* 输出参数:utc_ms——UTC毫秒回写位置。
|
||
* 返回值:true表示找到适用时段并写回UTC毫秒;false表示时段或时刻不可映射。
|
||
* 说明:找不到基准、算术溢出或超出日历范围时返回false,保持utc_ms原值,不修改分段队列。
|
||
*/
|
||
bool Example_Energy_Meter_Time_At(const ExampleEnergyMeter *self, uint64_t mono_ms, uint64_t *utc_ms)
|
||
{
|
||
size_t index;
|
||
uint64_t value;
|
||
if (self == NULL || utc_ms == NULL || !self->initialized || self->faulted) { return false; }
|
||
for (index = self->epoch_count; index > 0U; --index) {
|
||
const ExampleEnergyMeterEpoch *epoch = &self->epochs[index - 1U];
|
||
if (mono_ms < epoch->monotonic_ms) { continue; }
|
||
if (!Add_Checked(epoch->utc_ms, mono_ms - epoch->monotonic_ms, &value) || !Valid_Time(value)) { return false; }
|
||
*utc_ms = value;
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
/**
|
||
* 功能:保存失败原因并锁存计量源故障,统一返回false,阻止后续Step重放结果不确定的持久提交。
|
||
* 输入参数:self——调用方持有的模块实例;result——待判定的处理结果。
|
||
* 输出参数:self——本次操作更新的实例或工作区。
|
||
* 返回值:始终返回false,供调用方直接传播已锁存的故障。
|
||
* 说明:无。
|
||
*/
|
||
static bool Failed(ExampleEnergyMeter *self, IEC60870_EnergyFreezeResult_t result)
|
||
{
|
||
self->last_result = result;
|
||
self->faulted = true;
|
||
return false;
|
||
}
|
||
|
||
/**
|
||
* 功能:判断冻结电能提交结果是否可视为已接受。
|
||
* 输入参数:result——待判定的处理结果。
|
||
* 输出参数:无独立输出参数。
|
||
* 返回值:结果为OK或DUPLICATE时返回true,其余结果返回false。
|
||
* 说明:只有成功或完全相同的重复提交视为已接受;EMPTY/SKIPPED由各调用位置单独处理。
|
||
*/
|
||
static bool Accepted(IEC60870_EnergyFreezeResult_t result)
|
||
{
|
||
return result == IEC60870_ENERGY_FREEZE_OK || result == IEC60870_ENERGY_FREEZE_DUPLICATE;
|
||
}
|
||
|
||
/**
|
||
* 功能:每轮先提交至多4条按发生时刻求值的FLOW,超过256条的积压只保留最新部分并计数。
|
||
* 输入参数:self——调用方持有的模块实例;core——已初始化的冻结电能核心;mono_ms——绝对单调毫秒时刻。
|
||
* 输出参数:self——推进FLOW游标、时段及故障状态;core——通过记录和封存接口推进持久冻结状态。
|
||
* 返回值:true表示本轮推进无错误,仍可能保留待办或等待;false表示推进失败。
|
||
* 说明:随后推进封存、查询Due并冻结当前快照;写第1节后再次Due,以便同轮从该节派生DAY。
|
||
* UTC回拨只暂停定时冻结,已处理的FLOW保留;持久失败或时间/数值异常锁存故障,禁止重放。
|
||
*/
|
||
bool Example_Energy_Meter_Step(ExampleEnergyMeter *self, IEC60870_EnergyFreeze_t *core, uint64_t mono_ms)
|
||
{
|
||
static const uint8_t indices[EXAMPLE_ENERGY_METER_POINTS] = {0U, 1U, 2U, 3U, 4U, 5U, 6U, 7U};
|
||
float values[EXAMPLE_ENERGY_METER_POINTS];
|
||
uint8_t quality[EXAMPLE_ENERGY_METER_POINTS];
|
||
IEC60870_EnergyFreezeDue_t due;
|
||
IEC60870_EnergyFreezeResult_t result;
|
||
uint64_t utc_ms, final_index;
|
||
unsigned budget;
|
||
if (self == NULL || core == NULL || !self->initialized || self->faulted) { return false; }
|
||
if (mono_ms < self->last_step_ms || self->epoch_count == 0U ||
|
||
mono_ms < self->epochs[self->epoch_count - 1U].monotonic_ms ||
|
||
(self->gain_count != 0U && mono_ms < self->gains[self->gain_count - 1U].monotonic_ms) ||
|
||
!Example_Energy_Meter_Time_At(self, mono_ms, &utc_ms)) { return Failed(self, IEC60870_ENERGY_FREEZE_INVALID); }
|
||
self->last_step_ms = mono_ms;
|
||
final_index = (mono_ms - self->origin_ms) / (self->period_ms / 2U);
|
||
if (final_index >= self->next_flow_index &&
|
||
final_index - self->next_flow_index >= EXAMPLE_ENERGY_METER_FLOW_BACKLOG) {
|
||
uint64_t first = final_index - EXAMPLE_ENERGY_METER_FLOW_BACKLOG + 1U;
|
||
self->flowrev_dropped += first - self->next_flow_index;
|
||
self->next_flow_index = first;
|
||
}
|
||
for (budget = 0U; budget < EXAMPLE_ENERGY_METER_FLOW_BUDGET; ++budget) {
|
||
uint64_t event_mono, event_utc, periodic_mono = 0U;
|
||
size_t gain = Next_Gain_Flow(self);
|
||
bool periodic = self->next_flow_index <= final_index;
|
||
bool gain_due = gain < self->gain_count && self->gains[gain].monotonic_ms <= mono_ms;
|
||
if (!periodic && !gain_due) { break; }
|
||
if (periodic && !Event_Time(self, self->next_flow_index, &periodic_mono)) {
|
||
return Failed(self, IEC60870_ENERGY_FREEZE_INVALID);
|
||
}
|
||
if (gain_due && (!periodic || self->gains[gain].monotonic_ms < periodic_mono)) {
|
||
periodic = false;
|
||
event_mono = self->gains[gain].monotonic_ms;
|
||
} else {
|
||
event_mono = periodic_mono;
|
||
gain_due = gain_due && self->gains[gain].monotonic_ms == event_mono;
|
||
}
|
||
/* 方波换向与配置极性在同一毫秒同时翻转,实际 P 符号不变。 */
|
||
if (periodic && gain_due) {
|
||
++self->next_flow_index;
|
||
self->gains[gain].flow_done = true;
|
||
continue;
|
||
}
|
||
if (!Example_Energy_Meter_Time_At(self, event_mono, &event_utc) ||
|
||
!Example_Energy_Meter_Snapshot(self, event_mono, values, quality)) {
|
||
return Failed(self, IEC60870_ENERGY_FREEZE_INVALID);
|
||
}
|
||
result = IEC60870_EnergyFreeze_Append_FlowReverse(core, event_utc, indices, values, quality,
|
||
EXAMPLE_ENERGY_METER_POINTS);
|
||
if (result == IEC60870_ENERGY_FREEZE_SKIPPED) { break; }
|
||
if (!Accepted(result)) { return Failed(self, result); }
|
||
if (periodic) { ++self->next_flow_index; }
|
||
if (gain_due) { self->gains[gain].flow_done = true; }
|
||
}
|
||
Trim_Epochs(self);
|
||
result = IEC60870_EnergyFreeze_Advance(core, utc_ms);
|
||
self->last_result = result;
|
||
if (result == IEC60870_ENERGY_FREEZE_TIME_REVERSED) { return true; }
|
||
if (result == IEC60870_ENERGY_FREEZE_SKIPPED) { return true; }
|
||
if (!Accepted(result)) { return Failed(self, result); }
|
||
result = IEC60870_EnergyFreeze_Due(core, utc_ms, &due);
|
||
if (result == IEC60870_ENERGY_FREEZE_EMPTY || result == IEC60870_ENERGY_FREEZE_SKIPPED) { return true; }
|
||
if (!Accepted(result)) { return Failed(self, result); }
|
||
if (due.interval) {
|
||
if (!Example_Energy_Meter_Snapshot(self, mono_ms, values, quality)) {
|
||
return Failed(self, IEC60870_ENERGY_FREEZE_INVALID);
|
||
}
|
||
result = IEC60870_EnergyFreeze_Record_Interval(core, utc_ms, utc_ms, values, quality,
|
||
EXAMPLE_ENERGY_METER_POINTS);
|
||
if (result == IEC60870_ENERGY_FREEZE_SKIPPED) { return true; }
|
||
if (!Accepted(result)) { return Failed(self, result); }
|
||
result = IEC60870_EnergyFreeze_Due(core, utc_ms, &due);
|
||
if (result == IEC60870_ENERGY_FREEZE_EMPTY || result == IEC60870_ENERGY_FREEZE_SKIPPED) { return true; }
|
||
if (!Accepted(result)) { return Failed(self, result); }
|
||
}
|
||
if (due.derive_day) {
|
||
result = IEC60870_EnergyFreeze_Derive_Day(core, utc_ms, due.day);
|
||
if (result != IEC60870_ENERGY_FREEZE_SKIPPED && !Accepted(result)) { return Failed(self, result); }
|
||
}
|
||
self->last_result = IEC60870_ENERGY_FREEZE_OK;
|
||
return true;
|
||
}
|