@@ -26,6 +26,14 @@ export class CollectDataService {
2626 private readonly maxBuildDateRetries = 3 ;
2727 private readonly buildDateRetryDelayMs = 2000 ;
2828 // ---------------------------------------
29+ // --- Added coefficients tracing state ---
30+ private lastCoefficientsRequest : Date | null = null ;
31+ private coefficientsRetryAttempts = 0 ;
32+ private coefficientsReceived = false ;
33+ private pendingCoefficients : BoardCoefficientsModel | null = null ;
34+ private readonly maxCoefficientsRetries = 3 ;
35+ private readonly coefficientsRetryDelayMs = 2000 ;
36+ // ----------------------------------------
2937
3038 private sketchBuildDateSubject = new Subject < VersionModel > ( ) ;
3139
@@ -105,15 +113,45 @@ export class CollectDataService {
105113 this . serialPortService . write ( 'i\n' ) ;
106114 }
107115
108- public setBoardCoefficients ( coefficients : BoardCoefficientsModel ) {
116+ public setBoardCoefficients ( coefficients : BoardCoefficientsModel , isRetry = false ) {
117+ if ( ! isRetry ) {
118+ this . coefficientsRetryAttempts = 0 ;
119+ this . coefficientsReceived = false ;
120+ this . pendingCoefficients = coefficients ;
121+ }
122+ this . lastCoefficientsRequest = new Date ( ) ;
109123 this . logger . info (
110124 `[${ CollectDataService . name } ].${ this . setBoardCoefficients . name } => ` +
111- `New board coefficients: '${ JSON . stringify ( coefficients ) } '` ,
125+ `Sending board coefficients (attempt ${ this . coefficientsRetryAttempts + 1 } / ${ this . maxCoefficientsRetries } ) : '${ JSON . stringify ( coefficients ) } '` ,
112126 ) ;
113127 this . serialPortService . write (
114128 `s${ coefficients . voltageCalibration } :${ coefficients . currentCalibration } :` +
115129 `${ coefficients . powerFactorCalibration } \n` ,
116130 ) ;
131+ this . scheduleCoefficientsRetry ( ) ;
132+ }
133+
134+ private scheduleCoefficientsRetry ( ) {
135+ if ( this . coefficientsReceived ) {
136+ return ;
137+ }
138+ if ( this . coefficientsRetryAttempts >= this . maxCoefficientsRetries ) {
139+ this . logger . error (
140+ `[${ CollectDataService . name } ].${ this . scheduleCoefficientsRetry . name } => ` +
141+ `Coefficients not confirmed after ${ this . maxCoefficientsRetries } attempts` ,
142+ ) ;
143+ return ;
144+ }
145+ this . coefficientsRetryAttempts ++ ;
146+ setTimeout ( ( ) => {
147+ if ( ! this . coefficientsReceived && this . pendingCoefficients ) {
148+ this . logger . warn (
149+ `[${ CollectDataService . name } ].${ this . scheduleCoefficientsRetry . name } => ` +
150+ `No coefficients confirmation yet, retrying (attempt ${ this . coefficientsRetryAttempts + 1 } )` ,
151+ ) ;
152+ this . setBoardCoefficients ( this . pendingCoefficients , true ) ;
153+ }
154+ } , this . coefficientsRetryDelayMs ) ;
117155 }
118156
119157 private scheduleBuildDateRetry ( ) {
@@ -247,19 +285,46 @@ export class CollectDataService {
247285 newCoefficients . voltageCalibration = this . config . voltageCalibration ;
248286 newCoefficients . currentCalibration = this . config . currentCalibration ;
249287 newCoefficients . powerFactorCalibration = this . config . powerFactorCalibration ;
250- if (
288+
289+ // Check if received coefficients match the pending (expected) coefficients
290+ const matchesPending =
291+ this . pendingCoefficients &&
292+ coefficients . voltageCalibration === this . pendingCoefficients . voltageCalibration &&
293+ coefficients . currentCalibration === this . pendingCoefficients . currentCalibration &&
294+ coefficients . powerFactorCalibration === this . pendingCoefficients . powerFactorCalibration ;
295+
296+ if ( matchesPending ) {
297+ this . coefficientsReceived = true ;
298+ const latencyMs = this . lastCoefficientsRequest
299+ ? Date . now ( ) - this . lastCoefficientsRequest . getTime ( )
300+ : - 1 ;
301+ this . logger . info (
302+ `[${ CollectDataService . name } ].${ this . processCalibrationCoefficientsData . name } => ` +
303+ `Coefficients confirmed (latency=${ latencyMs } ms)` ,
304+ ) ;
305+ await this . dataService . processCalibrationCoefficientsData ( coefficients ) ;
306+ if ( this . coefficientsSubject ) {
307+ this . coefficientsSubject . next ( coefficients ) ;
308+ }
309+ } else if (
251310 newCoefficients . voltageCalibration === coefficients . voltageCalibration &&
252311 newCoefficients . currentCalibration === coefficients . currentCalibration &&
253312 newCoefficients . powerFactorCalibration === coefficients . powerFactorCalibration
254313 ) {
314+ // Coefficients match config but we weren't expecting them (no pending request)
315+ this . logger . info (
316+ `[${ CollectDataService . name } ].${ this . processCalibrationCoefficientsData . name } => ` +
317+ `Coefficients match config (no pending request)` ,
318+ ) ;
255319 await this . dataService . processCalibrationCoefficientsData ( coefficients ) ;
256320 if ( this . coefficientsSubject ) {
257321 this . coefficientsSubject . next ( coefficients ) ;
258322 }
259323 } else {
260324 this . logger . error (
261325 `[${ CollectDataService . name } ].${ this . processCalibrationCoefficientsData . name } ` +
262- `=> The error happens for setting board coefficients` ,
326+ `=> Coefficients mismatch - Expected: ${ JSON . stringify ( this . pendingCoefficients || newCoefficients ) } , ` +
327+ `Received: ${ JSON . stringify ( coefficients ) } ` ,
263328 ) ;
264329 this . setBoardCoefficients ( newCoefficients ) ;
265330 }
0 commit comments