1818
1919#pragma once
2020
21- #include " app/clusters/energy-evse-server/energy-evse-server.h"
2221#include < EVSECallbacks.h>
2322#include < EnergyEvseTargetsStore.h>
2423#include < EvseTargetsConfig.h>
24+ #include < app/clusters/energy-evse-server/CodegenIntegration.h>
25+ #include < app/clusters/energy-evse-server/EnergyEvseCluster.h>
2526
2627#include < app/util/config.h>
2728#include < cstring>
@@ -48,58 +49,56 @@ enum EVSEStateMachineEvent
4849};
4950
5051/* *
51- * Helper class to handle all of the session related info
52+ * Helper class to handle session timing and energy meter deltas.
53+ * Session attribute values are stored in the cluster (EnergyEvseCluster) and
54+ * updated through Instance setters. This class only tracks the internal
55+ * computation state (start time, energy meter baselines).
5256 */
5357class EvseSession
5458{
5559public:
5660 EvseSession () {}
61+
5762 /* *
58- * @brief This function records the start time and provided energy meter values as part of the new session .
63+ * @brief Start a new session: assigns a session ID, resets duration/ energy counters .
5964 *
60- * @param endpointId - The endpoint to report the update on
65+ * @param instance - The cluster Instance to update attributes on
6166 * @param chargingMeterValue - The current value of the energy meter (charging) in mWh
6267 * @param dischargingMeterValue - The current value of the energy meter (discharging) in mWh
6368 */
64- void StartSession (EndpointId endpointId , int64_t chargingMeterValue, int64_t dischargingMeterValue);
69+ void StartSession (Instance * instance , int64_t chargingMeterValue, int64_t dischargingMeterValue);
6570
6671 /* *
67- * @brief This function updates the session information at the unplugged event
72+ * @brief Stop the current session: recalculates duration and energy values.
6873 *
69- * @param endpointId - The endpoint to report the update on
74+ * @param instance - The cluster Instance to update attributes on
7075 * @param chargingMeterValue - The current value of the energy meter (charging) in mWh
7176 * @param dischargingMeterValue - The current value of the energy meter (discharging) in mWh
7277 */
73- void StopSession (EndpointId endpointId , int64_t chargingMeterValue, int64_t dischargingMeterValue);
78+ void StopSession (Instance * instance , int64_t chargingMeterValue, int64_t dischargingMeterValue);
7479
7580 /* *
76- * @brief This function updates the session Duration to allow read attributes to return latest values
81+ * @brief Recalculate session duration from start time to now.
7782 *
78- * @param endpointId - The endpoint to report the update on
83+ * @param instance - The cluster Instance to update attributes on
7984 */
80- void RecalculateSessionDuration (EndpointId endpointId );
85+ void RecalculateSessionDuration (Instance * instance );
8186
8287 /* *
83- * @brief This function updates the EnergyCharged meter value
88+ * @brief Update the session's charged energy delta.
8489 *
85- * @param endpointId - The endpoint to report the update on
86- * @param chargingMeterValue - The value of the energy meter (charging) in mWh
90+ * @param instance - The cluster Instance to update attributes on
91+ * @param chargingMeterValue - The current value of the energy meter (charging) in mWh
8792 */
88- void UpdateEnergyCharged (EndpointId endpointId , int64_t chargingMeterValue);
93+ void UpdateEnergyCharged (Instance * instance , int64_t chargingMeterValue);
8994
9095 /* *
91- * @brief This function updates the EnergyDischarged meter value
96+ * @brief Update the session's discharged energy delta.
9297 *
93- * @param endpointId - The endpoint to report the update on
94- * @param dischargingMeterValue - The value of the energy meter (discharging) in mWh
98+ * @param instance - The cluster Instance to update attributes on
99+ * @param dischargingMeterValue - The current value of the energy meter (discharging) in mWh
95100 */
96- void UpdateEnergyDischarged (EndpointId endpointId, int64_t dischargingMeterValue);
97-
98- /* Public members - represent attributes in the cluster */
99- DataModel::Nullable<uint32_t > mSessionID ;
100- DataModel::Nullable<uint32_t > mSessionDuration ;
101- DataModel::Nullable<int64_t > mSessionEnergyCharged ;
102- DataModel::Nullable<int64_t > mSessionEnergyDischarged ;
101+ void UpdateEnergyDischarged (Instance * instance, int64_t dischargingMeterValue);
103102
104103private:
105104 uint32_t mStartTime = 0 ; // Epoch_s - 0 means it hasn't started yet
@@ -239,69 +238,61 @@ class EnergyEvseDelegate : public EnergyEvse::Delegate
239238 Status SendFaultEvent (FaultStateEnum newFaultState);
240239
241240 // ------------------------------------------------------------------
242- // Get attribute methods
243- StateEnum GetState () override ;
244- CHIP_ERROR SetState (StateEnum);
245-
246- SupplyStateEnum GetSupplyState () override ;
247- CHIP_ERROR SetSupplyState (SupplyStateEnum);
248-
249- FaultStateEnum GetFaultState () override ;
250- CHIP_ERROR SetFaultState (FaultStateEnum);
251-
252- DataModel::Nullable<uint32_t > GetChargingEnabledUntil () override ;
253- CHIP_ERROR SetChargingEnabledUntil (DataModel::Nullable<uint32_t >);
254-
255- DataModel::Nullable<uint32_t > GetDischargingEnabledUntil () override ;
256- CHIP_ERROR SetDischargingEnabledUntil (DataModel::Nullable<uint32_t >);
257-
258- int64_t GetCircuitCapacity () override ;
259- CHIP_ERROR SetCircuitCapacity (int64_t );
260-
261- int64_t GetMinimumChargeCurrent () override ;
262- CHIP_ERROR SetMinimumChargeCurrent (int64_t );
263-
264- int64_t GetMaximumChargeCurrent () override ;
265- CHIP_ERROR SetMaximumChargeCurrent (int64_t );
266-
267- int64_t GetMaximumDischargeCurrent () override ;
268- CHIP_ERROR SetMaximumDischargeCurrent (int64_t );
269-
270- int64_t GetUserMaximumChargeCurrent () override ;
271- CHIP_ERROR SetUserMaximumChargeCurrent (int64_t ) override ;
241+ // Attribute methods - called by cluster to propagate attribute changes to the delegate
242+ void OnStateChanged (StateEnum newValue) override ;
243+ void OnSupplyStateChanged (SupplyStateEnum newValue) override ;
244+ void OnFaultStateChanged (FaultStateEnum newValue) override ;
245+ void OnChargingEnabledUntilChanged (DataModel::Nullable<uint32_t > newValue) override ;
246+ void OnDischargingEnabledUntilChanged (DataModel::Nullable<uint32_t > newValue) override ;
247+ void OnCircuitCapacityChanged (int64_t newValue) override ;
248+ void OnMinimumChargeCurrentChanged (int64_t newValue) override ;
249+ void OnMaximumChargeCurrentChanged (int64_t newValue) override ;
250+ void OnMaximumDischargeCurrentChanged (int64_t newValue) override ;
251+ void OnUserMaximumChargeCurrentChanged (int64_t newValue) override ;
252+ void OnRandomizationDelayWindowChanged (uint32_t newValue) override ;
253+ void OnNextChargeStartTimeChanged (DataModel::Nullable<uint32_t > newValue) override ;
254+ void OnNextChargeTargetTimeChanged (DataModel::Nullable<uint32_t > newValue) override ;
255+ void OnNextChargeRequiredEnergyChanged (DataModel::Nullable<int64_t > newValue) override ;
256+ void OnNextChargeTargetSoCChanged (DataModel::Nullable<Percent> newValue) override ;
257+ void OnApproximateEVEfficiencyChanged (DataModel::Nullable<uint16_t > newValue) override ;
258+ void OnStateOfChargeChanged (DataModel::Nullable<Percent> newValue) override ;
259+ void OnBatteryCapacityChanged (DataModel::Nullable<int64_t > newValue) override ;
260+ void OnVehicleIDChanged (DataModel::Nullable<CharSpan> newValue) override ;
261+ void OnSessionIDChanged (DataModel::Nullable<uint32_t > newValue) override ;
262+ void OnSessionDurationChanged (DataModel::Nullable<uint32_t > newValue) override ;
263+ void OnSessionEnergyChargedChanged (DataModel::Nullable<int64_t > newValue) override ;
264+ void OnSessionEnergyDischargedChanged (DataModel::Nullable<int64_t > newValue) override ;
272265
273- uint32_t GetRandomizationDelayWindow () override ;
274- CHIP_ERROR SetRandomizationDelayWindow (uint32_t ) override ;
275-
276- /* PREF attributes */
277- DataModel::Nullable<uint32_t > GetNextChargeStartTime () override ;
278- CHIP_ERROR SetNextChargeStartTime (DataModel::Nullable<uint32_t > newNextChargeStartTimeUtc);
279-
280- DataModel::Nullable<uint32_t > GetNextChargeTargetTime () override ;
281- CHIP_ERROR SetNextChargeTargetTime (DataModel::Nullable<uint32_t > newNextChargeTargetTimeUtc);
282-
283- DataModel::Nullable<int64_t > GetNextChargeRequiredEnergy () override ;
284- CHIP_ERROR SetNextChargeRequiredEnergy (DataModel::Nullable<int64_t > newNextChargeRequiredEnergyMilliWattH);
285-
286- DataModel::Nullable<Percent> GetNextChargeTargetSoC () override ;
287- CHIP_ERROR SetNextChargeTargetSoC (DataModel::Nullable<Percent> newValue);
288-
289- DataModel::Nullable<uint16_t > GetApproximateEVEfficiency () override ;
290- CHIP_ERROR SetApproximateEVEfficiency (DataModel::Nullable<uint16_t >) override ;
291-
292- /* SOC attributes */
293- DataModel::Nullable<Percent> GetStateOfCharge () override ;
294- CHIP_ERROR SetStateOfCharge (DataModel::Nullable<Percent>);
295- DataModel::Nullable<int64_t > GetBatteryCapacity () override ;
296- CHIP_ERROR SetBatteryCapacity (DataModel::Nullable<int64_t >);
266+ // ------------------------------------------------------------------
267+ // Instance management for codegen integration
268+ void SetInstance (Instance * aInstance) { mInstance = aInstance; }
269+ Instance * GetInstance () { return mInstance ; }
297270
298- /* PNC attributes*/
299- DataModel::Nullable<CharSpan> GetVehicleID () override ;
300- /* Session SESS attributes */
301- DataModel::Nullable<uint32_t > GetSessionID () override ;
302- DataModel::Nullable<uint32_t > GetSessionDuration () override ;
303- DataModel::Nullable<int64_t > GetSessionEnergyCharged () override ;
304- DataModel::Nullable<int64_t > GetSessionEnergyDischarged () override ;
271+ // ------------------------------------------------------------------
272+ // Local getters for internal delegate use - delegates to cluster instance
273+ StateEnum GetState () const ;
274+ SupplyStateEnum GetSupplyState () const ;
275+ FaultStateEnum GetFaultState () const ;
276+ DataModel::Nullable<uint32_t > GetChargingEnabledUntil () const ;
277+ DataModel::Nullable<uint32_t > GetDischargingEnabledUntil () const ;
278+ int64_t GetCircuitCapacity () const ;
279+ int64_t GetMinimumChargeCurrent () const ;
280+ int64_t GetMaximumChargeCurrent () const ;
281+ int64_t GetMaximumDischargeCurrent () const ;
282+ int64_t GetUserMaximumChargeCurrent () const ;
283+ uint32_t GetRandomizationDelayWindow () const ;
284+ DataModel::Nullable<uint32_t > GetNextChargeStartTime () const ;
285+ DataModel::Nullable<uint32_t > GetNextChargeTargetTime () const ;
286+ DataModel::Nullable<int64_t > GetNextChargeRequiredEnergy () const ;
287+ DataModel::Nullable<Percent> GetNextChargeTargetSoC () const ;
288+ DataModel::Nullable<uint16_t > GetApproximateEVEfficiency () const ;
289+ DataModel::Nullable<Percent> GetStateOfCharge () const ;
290+ DataModel::Nullable<int64_t > GetBatteryCapacity () const ;
291+ DataModel::Nullable<CharSpan> GetVehicleID () const ;
292+ DataModel::Nullable<uint32_t > GetSessionID () const ;
293+ DataModel::Nullable<uint32_t > GetSessionDuration () const ;
294+ DataModel::Nullable<int64_t > GetSessionEnergyCharged () const ;
295+ DataModel::Nullable<int64_t > GetSessionEnergyDischarged () const ;
305296
306297private:
307298 /* Constants */
@@ -362,39 +353,18 @@ class EnergyEvseDelegate : public EnergyEvse::Delegate
362353 */
363354 static void EvseCheckTimerExpiry (System::Layer * systemLayer, void * delegate);
364355
365- /* Attributes */
366- StateEnum mState = StateEnum::kNotPluggedIn ;
367- SupplyStateEnum mSupplyState = SupplyStateEnum::kDisabled ;
368- FaultStateEnum mFaultState = FaultStateEnum::kNoError ;
369- DataModel::Nullable<uint32_t > mChargingEnabledUntil ; // TODO Default to 0 to indicate disabled
370- DataModel::Nullable<uint32_t > mDischargingEnabledUntil ; // TODO Default to 0 to indicate disabled
371- int64_t mCircuitCapacity = 0 ;
372- int64_t mMinimumChargeCurrent = kDefaultMinChargeCurrent_mA ;
373- int64_t mMaximumChargeCurrent = 0 ;
374- int64_t mMaximumDischargeCurrent = 0 ;
375- int64_t mUserMaximumChargeCurrent = kDefaultUserMaximumChargeCurrent_mA ; // TODO update spec
376- uint32_t mRandomizationDelayWindow = kDefaultRandomizationDelayWindow_sec ;
377- /* PREF attributes */
378- DataModel::Nullable<uint32_t > mNextChargeStartTime ;
379- DataModel::Nullable<uint32_t > mNextChargeTargetTime ;
380- DataModel::Nullable<int64_t > mNextChargeRequiredEnergy ;
381- DataModel::Nullable<Percent> mNextChargeTargetSoC ;
382- DataModel::Nullable<uint16_t > mApproximateEVEfficiency ;
383-
384- /* SOC attributes */
385- DataModel::Nullable<Percent> mStateOfCharge ;
386- DataModel::Nullable<int64_t > mBatteryCapacity ;
387-
388- /* PNC attributes*/
389- DataModel::Nullable<CharSpan> mVehicleID ;
390- char mVehicleIDBuf [kMaxVehicleIDBufSize ];
356+ /* Instance pointer for accessing cluster */
357+ Instance * mInstance = nullptr ;
391358
392- /* Session Object */
359+ /* Session Object - delegate owns session state management */
393360 EvseSession mSession = EvseSession();
394361
395362 /* Helper variables to hold meter val since last EnergyTransferStarted event */
396- int64_t mImportedMeterValueAtEnergyTransferStart ; // Charging
397- int64_t mExportedMeterValueAtEnergyTransferStart ; // Discharging
363+ int64_t mImportedMeterValueAtEnergyTransferStart ;
364+ int64_t mExportedMeterValueAtEnergyTransferStart ;
365+
366+ /* VehicleID buffer for delegate use */
367+ char mVehicleIDBuf [kMaxVehicleIDBufSize ];
398368
399369 /* Targets Delegate */
400370 EvseTargetsDelegate * mEvseTargetsDelegate = nullptr ;
0 commit comments