eebus-go icon indicating copy to clipboard operation
eebus-go copied to clipboard

🐛 fix: ensure events only fire when public methods return valid data

Open DerAndereAndi opened this issue 6 months ago • 1 comments

Summary

  • Implement robust data validation for events to ensure consistency between event notifications and public API data availability
  • Fix critical logic bug in DeviceConfiguration event filtering
  • Ensure events only fire when corresponding public methods will return valid data

Changes Made

Core Logic Fix

  • Fixed DeviceConfiguration OR/AND logic bug: Corrected operator precedence in CheckEventPayloadDataForFilter that was causing false positives
  • Changed *item.KeyId == *desc.KeyId || to *item.KeyId == *desc.KeyId && for proper field validation

Event Handler Validation

  • Updated 18 event handlers across three use cases to validate data with public methods before firing events:
    • EG LPC: 3 event handlers (ConsumptionLimit, FailsafeConsumptionActivePowerLimit, FailsafeDurationMinimum)
    • MA MPC: 7 measurement event handlers (Power, PowerPerPhase, EnergyConsumed, EnergyProduced, CurrentPerPhase, VoltagePerPhase, Frequency)
    • MA MGCP: 7 event handlers (PowerLimitationFactor + 6 measurement types)

Test Coverage Improvements

  • Enhanced event tests to provide complete, valid data that passes both event filters and public method validation
  • Added proper electrical connection setup for measurement validation
  • Ensured tests cover both success and failure scenarios
  • Added missing required fields (ValueType, ValueSource, MeasurementType, CommodityType, Duration values)

Problem Solved

Previously, events could fire for data that would then fail when accessed through public methods due to validation failures. This created inconsistency where users would receive event notifications but then get errors when trying to retrieve the announced data.

Solution

Events now perform the same validation as public methods before firing, ensuring that:

  • Events only fire when data is actually retrievable via public API
  • No more misleading event notifications for invalid/incomplete data
  • Consistent behavior between event system and public method access

Test Coverage

  • All existing tests pass
  • Enhanced negative test coverage for scenarios that should not trigger events
  • Comprehensive validation of both event filtering and public method validation logic

Breaking Changes

None - this is a bug fix that improves reliability without changing public APIs.

DerAndereAndi avatar Jul 12 '25 20:07 DerAndereAndi

@sthelen-enqs this should also help for the scenario in your PR https://github.com/enbility/spine-go/pull/52

DerAndereAndi avatar Jul 12 '25 20:07 DerAndereAndi