@enyo-energy/sunspec-sdk 0.0.105 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +90 -0
- package/dist/cjs/sunspec-devices.cjs +104 -9
- package/dist/cjs/sunspec-devices.d.cts +32 -3
- package/dist/cjs/sunspec-interfaces.d.cts +62 -0
- package/dist/cjs/sunspec-modbus-client.cjs +288 -107
- package/dist/cjs/sunspec-modbus-client.d.cts +89 -5
- package/dist/cjs/version.cjs +1 -1
- package/dist/cjs/version.d.cts +1 -1
- package/dist/sunspec-devices.d.ts +32 -3
- package/dist/sunspec-devices.js +104 -9
- package/dist/sunspec-interfaces.d.ts +62 -0
- package/dist/sunspec-modbus-client.d.ts +89 -5
- package/dist/sunspec-modbus-client.js +285 -106
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +2 -2
|
@@ -35,6 +35,63 @@ export class ModbusOperationTimeoutError extends Error {
|
|
|
35
35
|
this.timeoutMs = timeoutMs;
|
|
36
36
|
this.name = 'ModbusOperationTimeoutError';
|
|
37
37
|
}
|
|
38
|
+
/**
|
|
39
|
+
* `Error` has no enumerable own properties, so a consumer that logs `{ err }` through a
|
|
40
|
+
* JSON serializer emits `{}` and loses the message entirely. Give every error this client
|
|
41
|
+
* rejects with an explicit JSON shape so it survives `JSON.stringify` with a usable message.
|
|
42
|
+
*/
|
|
43
|
+
toJSON() {
|
|
44
|
+
return {
|
|
45
|
+
name: this.name,
|
|
46
|
+
message: this.message,
|
|
47
|
+
operation: this.operation,
|
|
48
|
+
unitId: this.unitId,
|
|
49
|
+
timeoutMs: this.timeoutMs
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Thrown when the client refuses a transaction because the unit is **already known to be down**,
|
|
55
|
+
* as opposed to a transaction that was actually attempted and failed.
|
|
56
|
+
*
|
|
57
|
+
* The distinction matters for logging. A poisoned socket produces exactly one genuine event (the
|
|
58
|
+
* timeout that poisoned it), but every read that lands during the outage would otherwise throw an
|
|
59
|
+
* error indistinguishable from real I/O failure — so a consumer catching per read has no choice but
|
|
60
|
+
* to log each one at ERROR. Callers should log the first {@link SunspecUnavailableError} of an
|
|
61
|
+
* outage (or nothing at all: the SDK already logs the transition) and stay quiet on the rest.
|
|
62
|
+
*
|
|
63
|
+
* {@link sinceMs} is how long the unit has been in this state, so a consumer that does want one
|
|
64
|
+
* line can say how stale the link is without tracking the outage itself.
|
|
65
|
+
*/
|
|
66
|
+
export class SunspecUnavailableError extends Error {
|
|
67
|
+
reason;
|
|
68
|
+
unitId;
|
|
69
|
+
sinceMs;
|
|
70
|
+
constructor(reason, unitId, sinceMs) {
|
|
71
|
+
super(`Modbus unit ${unitId} is unavailable (${reason}, for ${sinceMs}ms) — ` +
|
|
72
|
+
`transaction refused without attempting I/O`);
|
|
73
|
+
this.reason = reason;
|
|
74
|
+
this.unitId = unitId;
|
|
75
|
+
this.sinceMs = sinceMs;
|
|
76
|
+
this.name = 'SunspecUnavailableError';
|
|
77
|
+
}
|
|
78
|
+
/** See {@link ModbusOperationTimeoutError.toJSON}. */
|
|
79
|
+
toJSON() {
|
|
80
|
+
return {
|
|
81
|
+
name: this.name,
|
|
82
|
+
message: this.message,
|
|
83
|
+
reason: this.reason,
|
|
84
|
+
unitId: this.unitId,
|
|
85
|
+
sinceMs: this.sinceMs
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Type guard for {@link SunspecUnavailableError}. Prefer this over `instanceof` at package
|
|
91
|
+
* boundaries, where a duplicated copy of the SDK in the module graph would break identity.
|
|
92
|
+
*/
|
|
93
|
+
export function isSunspecUnavailableError(error) {
|
|
94
|
+
return error instanceof Error && error.name === 'SunspecUnavailableError';
|
|
38
95
|
}
|
|
39
96
|
/**
|
|
40
97
|
* Module-level registry: one SunspecModbusClient per network device (host:port).
|
|
@@ -46,7 +103,7 @@ const registryKey = (host, port) => `${host}:${port}`;
|
|
|
46
103
|
* Get (or create) the singleton SunspecModbusClient for this network device.
|
|
47
104
|
* Increments the refcount; pair with `releaseSunspecClient` on teardown.
|
|
48
105
|
*
|
|
49
|
-
* NOTE: `
|
|
106
|
+
* NOTE: `transport`, `resilience` and `modbusFactory` are only applied when the client is FIRST
|
|
50
107
|
* created for a given host:port. Because the client is cached and refcounted, a later caller
|
|
51
108
|
* passing different config receives the already-created instance and its config is ignored.
|
|
52
109
|
*
|
|
@@ -54,11 +111,11 @@ const registryKey = (host, port) => `${host}:${port}`;
|
|
|
54
111
|
* opens a per-unit connection) instead of the one from `energyApp.useModbus()` — e.g. a mock for
|
|
55
112
|
* tests, or an alternative transport. When provided, `energyApp` may be passed as `undefined`.
|
|
56
113
|
*/
|
|
57
|
-
export function getOrCreateSunspecClient(energyApp, host, port = 502,
|
|
114
|
+
export function getOrCreateSunspecClient(energyApp, host, port = 502, transport, resilience, modbusFactory) {
|
|
58
115
|
const key = registryKey(host, port);
|
|
59
116
|
let entry = CLIENT_REGISTRY.get(key);
|
|
60
117
|
if (!entry) {
|
|
61
|
-
entry = { client: new SunspecModbusClient(energyApp,
|
|
118
|
+
entry = { client: new SunspecModbusClient(energyApp, transport, resilience, modbusFactory), refcount: 0 };
|
|
62
119
|
CLIENT_REGISTRY.set(key, entry);
|
|
63
120
|
}
|
|
64
121
|
entry.refcount++;
|
|
@@ -123,7 +180,7 @@ export async function retryAsync(fn, opts) {
|
|
|
123
180
|
}
|
|
124
181
|
export class SunspecModbusClient {
|
|
125
182
|
energyApp;
|
|
126
|
-
|
|
183
|
+
transport;
|
|
127
184
|
modbusFactory;
|
|
128
185
|
// Per-unit Modbus instance/reader/discovered-models maps. One client = one network device,
|
|
129
186
|
// many unit IDs share that client (each unit ID is one EnergyAppModbusInstance underneath).
|
|
@@ -164,18 +221,29 @@ export class SunspecModbusClient {
|
|
|
164
221
|
// transaction's framing. So a poisoned unit refuses further I/O until reconnectUnit() tears
|
|
165
222
|
// the socket down and reopens it (which clears the flag).
|
|
166
223
|
poisonedUnits = new Set();
|
|
224
|
+
// Wall-clock of the transition INTO the poisoned state, per unit. Survives the close half of a
|
|
225
|
+
// recycle (`_closeUnit` clears the poison flag, not this) so the reopen can report how long the
|
|
226
|
+
// outage lasted. Cleared on the recovery log, which is the one place the outage is considered
|
|
227
|
+
// over. Presence here is "was poisoned at some point since the last successful open".
|
|
228
|
+
poisonedAtByUnit = new Map();
|
|
167
229
|
// Wall-clock of each unit's last SUCCESSFUL transaction. Drives half-open detection: a socket
|
|
168
230
|
// the transport still reports connected but that has gone quiet past staleSocketMs is presumed
|
|
169
231
|
// dead. Absent entry = no successful op yet (never treated as stale — give it a chance).
|
|
170
232
|
lastOkAtByUnit = new Map();
|
|
233
|
+
// Consecutive failed (re)connect attempts, keyed by attempt target. Drives transition-only
|
|
234
|
+
// logging: the first failure of a run is the news, every repeat while the peer stays down is
|
|
235
|
+
// the same news again. Cleared on the first success for that key.
|
|
236
|
+
consecutiveConnectFailures = new Map();
|
|
171
237
|
constructor(
|
|
172
238
|
// The host application. Only used to obtain the default Modbus backend via
|
|
173
239
|
// `energyApp.useModbus()`. May be omitted (`undefined`) when a `modbusFactory` is supplied.
|
|
174
240
|
energyApp,
|
|
175
|
-
// Opt-in
|
|
176
|
-
// serializes frames (`noParallelRequests`)
|
|
177
|
-
//
|
|
178
|
-
|
|
241
|
+
// Opt-in transport options (all default off). Passed straight through to
|
|
242
|
+
// `useModbus().connect()`, which serializes frames (`noParallelRequests`), spaces them
|
|
243
|
+
// (`waitBetweenMessagesMs`) and decides whether the socket comes from the host's shared pool
|
|
244
|
+
// (`pooledConnection`). An empty object leaves them all undefined so the SDK applies its own
|
|
245
|
+
// defaults. Typed as the superset so a plain IModbusPacingConfig still assigns.
|
|
246
|
+
transport = {},
|
|
179
247
|
// Per-operation timeouts + half-open detection. Merged with DEFAULT_MODBUS_RESILIENCE_CONFIG
|
|
180
248
|
// so omitted fields keep their defaults.
|
|
181
249
|
resilience = {},
|
|
@@ -187,7 +255,7 @@ export class SunspecModbusClient {
|
|
|
187
255
|
// connections on its own schedule and needs a factory to call.
|
|
188
256
|
modbusFactory) {
|
|
189
257
|
this.energyApp = energyApp;
|
|
190
|
-
this.
|
|
258
|
+
this.transport = transport;
|
|
191
259
|
this.modbusFactory = modbusFactory;
|
|
192
260
|
if (!energyApp && !modbusFactory) {
|
|
193
261
|
throw new Error("SunspecModbusClient requires either an EnergyApp or a modbusFactory");
|
|
@@ -358,21 +426,29 @@ export class SunspecModbusClient {
|
|
|
358
426
|
}
|
|
359
427
|
const { primaryHost, secondaryHost, port } = this.connectionParams;
|
|
360
428
|
const units = [...this.knownUnits];
|
|
361
|
-
|
|
429
|
+
const failureKey = 'reconnect:all';
|
|
430
|
+
const retrying = this.isRetryingAfterFailure(failureKey);
|
|
431
|
+
const logAttempt = (message) => { if (retrying)
|
|
432
|
+
console.debug(message);
|
|
433
|
+
else
|
|
434
|
+
console.log(message); };
|
|
435
|
+
logAttempt(`Attempting to reconnect to primary host ${primaryHost}:${port} units [${units.join(',')}]...`);
|
|
362
436
|
const primarySuccess = await this.attemptConnection(primaryHost, port, units);
|
|
363
437
|
if (primarySuccess) {
|
|
438
|
+
this.clearConnectFailures(failureKey);
|
|
364
439
|
console.log(`Successfully reconnected to primary host ${primaryHost}:${port} units [${units.join(',')}]`);
|
|
365
440
|
return true;
|
|
366
441
|
}
|
|
367
442
|
if (secondaryHost && secondaryHost !== primaryHost) {
|
|
368
|
-
|
|
443
|
+
logAttempt(`Primary host failed, attempting secondary host ${secondaryHost}:${port} units [${units.join(',')}]...`);
|
|
369
444
|
const secondarySuccess = await this.attemptConnection(secondaryHost, port, units);
|
|
370
445
|
if (secondarySuccess) {
|
|
446
|
+
this.clearConnectFailures(failureKey);
|
|
371
447
|
console.log(`Successfully reconnected to secondary host ${secondaryHost}:${port} units [${units.join(',')}]`);
|
|
372
448
|
return true;
|
|
373
449
|
}
|
|
374
450
|
}
|
|
375
|
-
|
|
451
|
+
this.logConnectFailure(failureKey, `Reconnection failed to all available hosts`);
|
|
376
452
|
return false;
|
|
377
453
|
});
|
|
378
454
|
}
|
|
@@ -390,21 +466,29 @@ export class SunspecModbusClient {
|
|
|
390
466
|
return false;
|
|
391
467
|
}
|
|
392
468
|
const { primaryHost, secondaryHost, port } = this.connectionParams;
|
|
393
|
-
|
|
469
|
+
const failureKey = `reconnect:unit${unitId}`;
|
|
470
|
+
const retrying = this.isRetryingAfterFailure(failureKey);
|
|
471
|
+
const logAttempt = (message) => { if (retrying)
|
|
472
|
+
console.debug(message);
|
|
473
|
+
else
|
|
474
|
+
console.log(message); };
|
|
475
|
+
logAttempt(`Attempting to reconnect unit ${unitId} on primary host ${primaryHost}:${port}...`);
|
|
394
476
|
const primarySuccess = await this.attemptConnection(primaryHost, port, [unitId]);
|
|
395
477
|
if (primarySuccess) {
|
|
478
|
+
this.clearConnectFailures(failureKey);
|
|
396
479
|
console.log(`Successfully reconnected unit ${unitId} on primary host ${primaryHost}:${port}`);
|
|
397
480
|
return true;
|
|
398
481
|
}
|
|
399
482
|
if (secondaryHost && secondaryHost !== primaryHost) {
|
|
400
|
-
|
|
483
|
+
logAttempt(`Primary host failed for unit ${unitId}, attempting secondary host ${secondaryHost}:${port}...`);
|
|
401
484
|
const secondarySuccess = await this.attemptConnection(secondaryHost, port, [unitId]);
|
|
402
485
|
if (secondarySuccess) {
|
|
486
|
+
this.clearConnectFailures(failureKey);
|
|
403
487
|
console.log(`Successfully reconnected unit ${unitId} on secondary host ${secondaryHost}:${port}`);
|
|
404
488
|
return true;
|
|
405
489
|
}
|
|
406
490
|
}
|
|
407
|
-
|
|
491
|
+
this.logConnectFailure(failureKey, `Reconnection failed for unit ${unitId} on all available hosts`);
|
|
408
492
|
return false;
|
|
409
493
|
});
|
|
410
494
|
}
|
|
@@ -419,15 +503,17 @@ export class SunspecModbusClient {
|
|
|
419
503
|
await this._closeUnit(unitId);
|
|
420
504
|
}
|
|
421
505
|
}
|
|
506
|
+
const failureKey = `connect:${host}:${port}:[${units.join(',')}]`;
|
|
422
507
|
try {
|
|
423
508
|
for (const unitId of units) {
|
|
424
509
|
await this._openUnit(host, port, unitId);
|
|
425
510
|
}
|
|
511
|
+
this.clearConnectFailures(failureKey);
|
|
426
512
|
console.log(`Connection attempt to ${host}:${port} units [${units.join(',')}] succeeded (opens=${this.openCount}, closes=${this.closeCount}, openUnits=${this.modbusInstances.size})`);
|
|
427
513
|
return true;
|
|
428
514
|
}
|
|
429
515
|
catch (error) {
|
|
430
|
-
|
|
516
|
+
this.logConnectFailure(failureKey, `Connection attempt to ${host}:${port} failed: ${error}`);
|
|
431
517
|
return false;
|
|
432
518
|
}
|
|
433
519
|
}
|
|
@@ -509,8 +595,9 @@ export class SunspecModbusClient {
|
|
|
509
595
|
key: this.connectionParams?.key,
|
|
510
596
|
ca: this.connectionParams?.ca,
|
|
511
597
|
rejectUnauthorized: this.connectionParams?.rejectUnauthorized,
|
|
512
|
-
noParallelRequests: this.
|
|
513
|
-
waitBetweenMessagesMs: this.
|
|
598
|
+
noParallelRequests: this.transport.noParallelRequests,
|
|
599
|
+
waitBetweenMessagesMs: this.transport.waitBetweenMessagesMs,
|
|
600
|
+
pooledConnection: this.transport.pooledConnection
|
|
514
601
|
}));
|
|
515
602
|
if (!candidate) {
|
|
516
603
|
throw new Error(`useModbus().connect returned null for ${host}:${port} unit ${unitId}`);
|
|
@@ -521,6 +608,7 @@ export class SunspecModbusClient {
|
|
|
521
608
|
// Fresh socket: clear any prior poison and start the freshness clock so a just-opened
|
|
522
609
|
// unit isn't immediately judged stale.
|
|
523
610
|
this.poisonedUnits.delete(unitId);
|
|
611
|
+
this.logPoisonRecovery(unitId);
|
|
524
612
|
this.lastOkAtByUnit.set(unitId, Date.now());
|
|
525
613
|
this.connectionHealth.recordSuccess();
|
|
526
614
|
this.recordOpen();
|
|
@@ -545,7 +633,9 @@ export class SunspecModbusClient {
|
|
|
545
633
|
this.faultTolerantReaders.delete(unitId);
|
|
546
634
|
this.discoveredModelsByUnit.delete(unitId);
|
|
547
635
|
this.discoveredModelListByUnit.delete(unitId);
|
|
548
|
-
// Drop per-unit resilience state so a later reopen starts clean.
|
|
636
|
+
// Drop per-unit resilience state so a later reopen starts clean. `poisonedAtByUnit` is
|
|
637
|
+
// deliberately NOT dropped: a recycle closes before it reopens, and the reopen is where the
|
|
638
|
+
// recovery line reports how long the unit was down.
|
|
549
639
|
this.poisonedUnits.delete(unitId);
|
|
550
640
|
this.lastOkAtByUnit.delete(unitId);
|
|
551
641
|
if (instance) {
|
|
@@ -605,10 +695,26 @@ export class SunspecModbusClient {
|
|
|
605
695
|
*/
|
|
606
696
|
async guardTransaction(unitId, fn) {
|
|
607
697
|
if (this.poisonedUnits.has(unitId)) {
|
|
608
|
-
|
|
698
|
+
// Known-down, not newly-failed: a typed rejection so callers can stay quiet for the
|
|
699
|
+
// duration of an outage the SDK has already logged once. See SunspecUnavailableError.
|
|
700
|
+
const since = this.poisonedAtByUnit.get(unitId);
|
|
701
|
+
throw new SunspecUnavailableError('socket-poisoned', unitId, since === undefined ? 0 : Date.now() - since);
|
|
702
|
+
}
|
|
703
|
+
let op;
|
|
704
|
+
try {
|
|
705
|
+
op = fn();
|
|
706
|
+
}
|
|
707
|
+
catch (error) {
|
|
708
|
+
// A transport throws synchronously (rather than rejecting) when it refuses the
|
|
709
|
+
// transaction before it ever reaches the wire — the socket was closed underneath us,
|
|
710
|
+
// e.g. a poll in flight racing a shutdown. The link is dead, so poison the unit the
|
|
711
|
+
// same way a timeout does; otherwise the client keeps believing the socket is open and
|
|
712
|
+
// every later read repeats the throw with nothing triggering a reconnect.
|
|
713
|
+
this.invalidateUnit(unitId, `transaction threw synchronously (${error})`);
|
|
714
|
+
throw error;
|
|
609
715
|
}
|
|
610
716
|
try {
|
|
611
|
-
const result = await this.withTimeout('transaction', unitId, this.resilience.readTimeoutMs,
|
|
717
|
+
const result = await this.withTimeout('transaction', unitId, this.resilience.readTimeoutMs, () => op);
|
|
612
718
|
this.lastOkAtByUnit.set(unitId, Date.now());
|
|
613
719
|
return result;
|
|
614
720
|
}
|
|
@@ -623,28 +729,100 @@ export class SunspecModbusClient {
|
|
|
623
729
|
* Race `fn` against a timeout. If the timeout wins, reject with a {@link ModbusOperationTimeoutError};
|
|
624
730
|
* the abandoned `fn` promise keeps running (JS can't cancel it) but is no longer awaited. The
|
|
625
731
|
* timer is always cleared so a fast `fn` doesn't keep the event loop alive.
|
|
732
|
+
*
|
|
733
|
+
* `fn()` is invoked inside a try/catch rather than inline in the Promise.race argument list. A
|
|
734
|
+
* transport can throw synchronously (a transaction issued against an already-closed socket), and
|
|
735
|
+
* evaluating it while the array literal is built means the throw escapes before Promise.race
|
|
736
|
+
* exists: nothing ever consumes `timeout`, `.finally()` never runs, and the armed timer rejects
|
|
737
|
+
* `timeoutMs` later into an empty stack — an unhandled rejection that kills the process, blaming
|
|
738
|
+
* a timeout for a failure that actually happened a full timeout window earlier.
|
|
626
739
|
*/
|
|
627
740
|
withTimeout(operation, unitId, timeoutMs, fn) {
|
|
628
741
|
let timer;
|
|
629
742
|
const timeout = new Promise((_, reject) => {
|
|
630
743
|
timer = setTimeout(() => reject(new ModbusOperationTimeoutError(operation, unitId, timeoutMs)), timeoutMs);
|
|
631
744
|
});
|
|
632
|
-
|
|
745
|
+
let op;
|
|
746
|
+
try {
|
|
747
|
+
op = fn();
|
|
748
|
+
}
|
|
749
|
+
catch (err) {
|
|
750
|
+
// Nothing will race `timeout`; disarm it so it can't reject unobserved.
|
|
751
|
+
if (timer !== undefined)
|
|
752
|
+
clearTimeout(timer);
|
|
753
|
+
return Promise.reject(err);
|
|
754
|
+
}
|
|
755
|
+
return Promise.race([op, timeout]).finally(() => {
|
|
633
756
|
if (timer !== undefined)
|
|
634
757
|
clearTimeout(timer);
|
|
635
758
|
});
|
|
636
759
|
}
|
|
760
|
+
/**
|
|
761
|
+
* Log a failed Modbus transaction from a reader/writer catch block.
|
|
762
|
+
*
|
|
763
|
+
* A {@link SunspecUnavailableError} is not a failure — it is this client refusing I/O on a link
|
|
764
|
+
* it has already reported dead once (one WARN on the transition, one INFO on recovery). Logging
|
|
765
|
+
* it at ERROR once per read, per read path, per poll cycle, for the whole outage, is what buries
|
|
766
|
+
* the errors that are actually actionable. So it drops to debug; everything else stays ERROR.
|
|
767
|
+
*/
|
|
768
|
+
logConnectFailure(key, message) {
|
|
769
|
+
const count = (this.consecutiveConnectFailures.get(key) ?? 0) + 1;
|
|
770
|
+
this.consecutiveConnectFailures.set(key, count);
|
|
771
|
+
if (count === 1) {
|
|
772
|
+
console.error(message);
|
|
773
|
+
}
|
|
774
|
+
else {
|
|
775
|
+
console.debug(`${message} (consecutive failure #${count})`);
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
/** True while `key`'s last attempt failed — used to keep repeat attempt chatter at debug. */
|
|
779
|
+
isRetryingAfterFailure(key) {
|
|
780
|
+
return (this.consecutiveConnectFailures.get(key) ?? 0) > 0;
|
|
781
|
+
}
|
|
782
|
+
/** Forget `key`'s failure run on a success, so the next outage logs its first failure again. */
|
|
783
|
+
clearConnectFailures(key) {
|
|
784
|
+
this.consecutiveConnectFailures.delete(key);
|
|
785
|
+
}
|
|
786
|
+
logTransactionError(context, error) {
|
|
787
|
+
if (isSunspecUnavailableError(error)) {
|
|
788
|
+
console.debug(`${context}: ${error.message}`);
|
|
789
|
+
return;
|
|
790
|
+
}
|
|
791
|
+
console.error(`${context}: ${error}`);
|
|
792
|
+
}
|
|
637
793
|
/**
|
|
638
794
|
* Mark a unit's socket as poisoned after a timeout and reflect the hang in the shared health
|
|
639
795
|
* gate immediately, so isHealthy() flips false this cycle instead of after the coarse
|
|
640
796
|
* 5-failure / 5-minute-staleness thresholds. Idempotent; only logs on the first poison.
|
|
641
797
|
*/
|
|
642
798
|
poisonUnit(unitId, error) {
|
|
799
|
+
this.enterPoisoned(unitId, `${error.message} — poisoning unit ${unitId} socket; reconnect path will recycle it`);
|
|
800
|
+
this.connectionHealth.recordFailure(error);
|
|
801
|
+
}
|
|
802
|
+
/**
|
|
803
|
+
* Enter the poisoned state for a unit, logging the TRANSITION only. Everything downstream of
|
|
804
|
+
* this (every refused read, every poll cycle) is the same known state and must stay silent —
|
|
805
|
+
* one WARN in, one INFO out (see {@link logPoisonRecovery}), nothing in between.
|
|
806
|
+
*/
|
|
807
|
+
enterPoisoned(unitId, reason) {
|
|
643
808
|
if (!this.poisonedUnits.has(unitId)) {
|
|
644
|
-
console.warn(`SunspecModbusClient: ${
|
|
809
|
+
console.warn(`SunspecModbusClient: ${reason}`);
|
|
810
|
+
this.poisonedAtByUnit.set(unitId, Date.now());
|
|
645
811
|
}
|
|
646
812
|
this.poisonedUnits.add(unitId);
|
|
647
|
-
|
|
813
|
+
}
|
|
814
|
+
/**
|
|
815
|
+
* Close out a poisoned episode on a successful (re)open: one INFO carrying how long the unit
|
|
816
|
+
* was refusing I/O. No-op for a unit that was never poisoned, so a normal first connect stays
|
|
817
|
+
* quiet.
|
|
818
|
+
*/
|
|
819
|
+
logPoisonRecovery(unitId) {
|
|
820
|
+
const since = this.poisonedAtByUnit.get(unitId);
|
|
821
|
+
if (since === undefined)
|
|
822
|
+
return;
|
|
823
|
+
this.poisonedAtByUnit.delete(unitId);
|
|
824
|
+
console.info(`SunspecModbusClient: unit ${unitId} socket recovered after reconnect ` +
|
|
825
|
+
`(poisonedForMs=${Date.now() - since})`);
|
|
648
826
|
}
|
|
649
827
|
/**
|
|
650
828
|
* Publicly invalidate a unit's socket so {@link isHealthy} returns false and the next
|
|
@@ -659,10 +837,7 @@ export class SunspecModbusClient {
|
|
|
659
837
|
* schedule firing until a real discovery lands. Idempotent; only logs on the first call.
|
|
660
838
|
*/
|
|
661
839
|
invalidateUnit(unitId, reason) {
|
|
662
|
-
|
|
663
|
-
console.warn(`SunspecModbusClient: invalidating unit ${unitId} socket — ${reason}; reconnect path will recycle it`);
|
|
664
|
-
}
|
|
665
|
-
this.poisonedUnits.add(unitId);
|
|
840
|
+
this.enterPoisoned(unitId, `invalidating unit ${unitId} socket — ${reason}; reconnect path will recycle it`);
|
|
666
841
|
this.connectionHealth.recordFailure(new Error(reason));
|
|
667
842
|
}
|
|
668
843
|
/**
|
|
@@ -921,7 +1096,7 @@ export class SunspecModbusClient {
|
|
|
921
1096
|
}
|
|
922
1097
|
}
|
|
923
1098
|
catch (error) {
|
|
924
|
-
|
|
1099
|
+
this.logTransactionError(`Error during model discovery at address ${currentAddress} (unit ${unitId})`, error);
|
|
925
1100
|
}
|
|
926
1101
|
if (discovered.length > 0) {
|
|
927
1102
|
// Commit the fresh discovery atomically. Keep the FIRST (lowest-address) instance per
|
|
@@ -1223,7 +1398,7 @@ export class SunspecModbusClient {
|
|
|
1223
1398
|
}
|
|
1224
1399
|
catch (error) {
|
|
1225
1400
|
this.connectionHealth.recordFailure(error);
|
|
1226
|
-
|
|
1401
|
+
this.logTransactionError(`Error writing register at address ${address}`, error);
|
|
1227
1402
|
throw error;
|
|
1228
1403
|
}
|
|
1229
1404
|
}
|
|
@@ -1369,7 +1544,7 @@ export class SunspecModbusClient {
|
|
|
1369
1544
|
return data;
|
|
1370
1545
|
}
|
|
1371
1546
|
catch (error) {
|
|
1372
|
-
|
|
1547
|
+
this.logTransactionError(`Error reading inverter data`, error);
|
|
1373
1548
|
return null;
|
|
1374
1549
|
}
|
|
1375
1550
|
}
|
|
@@ -1433,7 +1608,7 @@ export class SunspecModbusClient {
|
|
|
1433
1608
|
return data;
|
|
1434
1609
|
}
|
|
1435
1610
|
catch (error) {
|
|
1436
|
-
|
|
1611
|
+
this.logTransactionError(`Error reading single phase inverter data`, error);
|
|
1437
1612
|
return null;
|
|
1438
1613
|
}
|
|
1439
1614
|
}
|
|
@@ -1497,7 +1672,7 @@ export class SunspecModbusClient {
|
|
|
1497
1672
|
return data;
|
|
1498
1673
|
}
|
|
1499
1674
|
catch (error) {
|
|
1500
|
-
|
|
1675
|
+
this.logTransactionError(`Error reading float inverter data (Model ${blockNumber})`, error);
|
|
1501
1676
|
return null;
|
|
1502
1677
|
}
|
|
1503
1678
|
}
|
|
@@ -1616,7 +1791,7 @@ export class SunspecModbusClient {
|
|
|
1616
1791
|
return this.extractMPPTScaleFactors(buffer);
|
|
1617
1792
|
}
|
|
1618
1793
|
catch (error) {
|
|
1619
|
-
|
|
1794
|
+
this.logTransactionError(`Error reading MPPT scale factors (unit ${unitId})`, error);
|
|
1620
1795
|
return null;
|
|
1621
1796
|
}
|
|
1622
1797
|
}
|
|
@@ -1690,7 +1865,7 @@ export class SunspecModbusClient {
|
|
|
1690
1865
|
return this.extractMPPTModuleData(buffer, model, moduleId, scaleFactors);
|
|
1691
1866
|
}
|
|
1692
1867
|
catch (error) {
|
|
1693
|
-
|
|
1868
|
+
this.logTransactionError(`Error reading MPPT data for module ${moduleId} (unit ${unitId})`, error);
|
|
1694
1869
|
return null;
|
|
1695
1870
|
}
|
|
1696
1871
|
}
|
|
@@ -1740,7 +1915,7 @@ export class SunspecModbusClient {
|
|
|
1740
1915
|
}
|
|
1741
1916
|
}
|
|
1742
1917
|
catch (error) {
|
|
1743
|
-
|
|
1918
|
+
this.logTransactionError(`Error reading all MPPT data`, error);
|
|
1744
1919
|
}
|
|
1745
1920
|
return mpptData;
|
|
1746
1921
|
}
|
|
@@ -2002,7 +2177,7 @@ export class SunspecModbusClient {
|
|
|
2002
2177
|
return data;
|
|
2003
2178
|
}
|
|
2004
2179
|
catch (error) {
|
|
2005
|
-
|
|
2180
|
+
this.logTransactionError(`Error reading battery base data`, error);
|
|
2006
2181
|
return null;
|
|
2007
2182
|
}
|
|
2008
2183
|
}
|
|
@@ -2180,7 +2355,7 @@ export class SunspecModbusClient {
|
|
|
2180
2355
|
}
|
|
2181
2356
|
}
|
|
2182
2357
|
catch (error) {
|
|
2183
|
-
|
|
2358
|
+
this.logTransactionError(`Error reading battery data`, error);
|
|
2184
2359
|
return null;
|
|
2185
2360
|
}
|
|
2186
2361
|
}
|
|
@@ -2262,7 +2437,7 @@ export class SunspecModbusClient {
|
|
|
2262
2437
|
return true;
|
|
2263
2438
|
}
|
|
2264
2439
|
catch (error) {
|
|
2265
|
-
|
|
2440
|
+
this.logTransactionError(`Error writing battery controls`, error);
|
|
2266
2441
|
return false;
|
|
2267
2442
|
}
|
|
2268
2443
|
}
|
|
@@ -2362,7 +2537,7 @@ export class SunspecModbusClient {
|
|
|
2362
2537
|
return controls;
|
|
2363
2538
|
}
|
|
2364
2539
|
catch (error) {
|
|
2365
|
-
|
|
2540
|
+
this.logTransactionError(`Error reading battery controls`, error);
|
|
2366
2541
|
return null;
|
|
2367
2542
|
}
|
|
2368
2543
|
}
|
|
@@ -2494,7 +2669,7 @@ export class SunspecModbusClient {
|
|
|
2494
2669
|
return data;
|
|
2495
2670
|
}
|
|
2496
2671
|
catch (error) {
|
|
2497
|
-
|
|
2672
|
+
this.logTransactionError(`Error reading meter data`, error);
|
|
2498
2673
|
return null;
|
|
2499
2674
|
}
|
|
2500
2675
|
}
|
|
@@ -2536,7 +2711,7 @@ export class SunspecModbusClient {
|
|
|
2536
2711
|
return data;
|
|
2537
2712
|
}
|
|
2538
2713
|
catch (error) {
|
|
2539
|
-
|
|
2714
|
+
this.logTransactionError(`Error reading float meter data (Model ${blockNumber})`, error);
|
|
2540
2715
|
return null;
|
|
2541
2716
|
}
|
|
2542
2717
|
}
|
|
@@ -2614,7 +2789,7 @@ export class SunspecModbusClient {
|
|
|
2614
2789
|
return data;
|
|
2615
2790
|
}
|
|
2616
2791
|
catch (error) {
|
|
2617
|
-
|
|
2792
|
+
this.logTransactionError(`Error reading common block`, error);
|
|
2618
2793
|
return null;
|
|
2619
2794
|
}
|
|
2620
2795
|
}
|
|
@@ -2740,7 +2915,7 @@ export class SunspecModbusClient {
|
|
|
2740
2915
|
return settings;
|
|
2741
2916
|
}
|
|
2742
2917
|
catch (error) {
|
|
2743
|
-
|
|
2918
|
+
this.logTransactionError(`Error reading inverter settings`, error);
|
|
2744
2919
|
return null;
|
|
2745
2920
|
}
|
|
2746
2921
|
}
|
|
@@ -2757,74 +2932,76 @@ export class SunspecModbusClient {
|
|
|
2757
2932
|
try {
|
|
2758
2933
|
// Read entire model block in a single Modbus call
|
|
2759
2934
|
const buffer = await this.readModelBlock(unitId, model);
|
|
2760
|
-
//
|
|
2935
|
+
// Buffer offsets are SunSpec point offsets + 2: readModelBlock() returns the
|
|
2936
|
+
// 2-register model header (ID + length) ahead of the data registers. Scale
|
|
2937
|
+
// factors are points 21-23 -> buffer offsets 23-25.
|
|
2761
2938
|
const scaleFactors = {
|
|
2762
|
-
WMaxLimPct_SF: this.extractValue(buffer,
|
|
2763
|
-
OutPFSet_SF: this.extractValue(buffer,
|
|
2764
|
-
VArPct_SF: this.extractValue(buffer,
|
|
2939
|
+
WMaxLimPct_SF: this.extractValue(buffer, 23, 'int16'),
|
|
2940
|
+
OutPFSet_SF: this.extractValue(buffer, 24, 'int16'),
|
|
2941
|
+
VArPct_SF: this.extractValue(buffer, 25, 'int16')
|
|
2765
2942
|
};
|
|
2766
|
-
this.logRegisterRead(123,
|
|
2767
|
-
this.logRegisterRead(123,
|
|
2768
|
-
this.logRegisterRead(123,
|
|
2943
|
+
this.logRegisterRead(123, 23, 'WMaxLimPct_SF', scaleFactors.WMaxLimPct_SF, 'int16');
|
|
2944
|
+
this.logRegisterRead(123, 24, 'OutPFSet_SF', scaleFactors.OutPFSet_SF, 'int16');
|
|
2945
|
+
this.logRegisterRead(123, 25, 'VArPct_SF', scaleFactors.VArPct_SF, 'int16');
|
|
2769
2946
|
// Extract non-scaled fields from buffer
|
|
2770
|
-
const connWinTmsRaw = this.extractValue(buffer,
|
|
2771
|
-
const connRvrtTmsRaw = this.extractValue(buffer,
|
|
2772
|
-
const connRaw = this.extractValue(buffer,
|
|
2773
|
-
const wMaxLimPctWinTmsRaw = this.extractValue(buffer,
|
|
2774
|
-
const wMaxLimPctRvrtTmsRaw = this.extractValue(buffer,
|
|
2775
|
-
const wMaxLimPctRmpTmsRaw = this.extractValue(buffer,
|
|
2776
|
-
const wMaxLimEnaRaw = this.extractValue(buffer,
|
|
2777
|
-
const outPFSetWinTmsRaw = this.extractValue(buffer,
|
|
2778
|
-
const outPFSetRvrtTmsRaw = this.extractValue(buffer,
|
|
2779
|
-
const outPFSetRmpTmsRaw = this.extractValue(buffer,
|
|
2780
|
-
const outPFSetEnaRaw = this.extractValue(buffer,
|
|
2781
|
-
const vArPctWinTmsRaw = this.extractValue(buffer,
|
|
2782
|
-
const vArPctRvrtTmsRaw = this.extractValue(buffer,
|
|
2783
|
-
const vArPctRmpTmsRaw = this.extractValue(buffer,
|
|
2784
|
-
const vArPctModRaw = this.extractValue(buffer,
|
|
2785
|
-
const vArPctEnaRaw = this.extractValue(buffer,
|
|
2786
|
-
this.logRegisterRead(123,
|
|
2787
|
-
this.logRegisterRead(123,
|
|
2788
|
-
this.logRegisterRead(123,
|
|
2789
|
-
this.logRegisterRead(123,
|
|
2790
|
-
this.logRegisterRead(123,
|
|
2791
|
-
this.logRegisterRead(123,
|
|
2792
|
-
this.logRegisterRead(123,
|
|
2793
|
-
this.logRegisterRead(123,
|
|
2794
|
-
this.logRegisterRead(123,
|
|
2795
|
-
this.logRegisterRead(123,
|
|
2796
|
-
this.logRegisterRead(123,
|
|
2797
|
-
this.logRegisterRead(123,
|
|
2798
|
-
this.logRegisterRead(123,
|
|
2799
|
-
this.logRegisterRead(123,
|
|
2800
|
-
this.logRegisterRead(123,
|
|
2801
|
-
this.logRegisterRead(123,
|
|
2947
|
+
const connWinTmsRaw = this.extractValue(buffer, 2, 'uint16');
|
|
2948
|
+
const connRvrtTmsRaw = this.extractValue(buffer, 3, 'uint16');
|
|
2949
|
+
const connRaw = this.extractValue(buffer, 4, 'uint16');
|
|
2950
|
+
const wMaxLimPctWinTmsRaw = this.extractValue(buffer, 6, 'uint16');
|
|
2951
|
+
const wMaxLimPctRvrtTmsRaw = this.extractValue(buffer, 7, 'uint16');
|
|
2952
|
+
const wMaxLimPctRmpTmsRaw = this.extractValue(buffer, 8, 'uint16');
|
|
2953
|
+
const wMaxLimEnaRaw = this.extractValue(buffer, 9, 'uint16');
|
|
2954
|
+
const outPFSetWinTmsRaw = this.extractValue(buffer, 11, 'uint16');
|
|
2955
|
+
const outPFSetRvrtTmsRaw = this.extractValue(buffer, 12, 'uint16');
|
|
2956
|
+
const outPFSetRmpTmsRaw = this.extractValue(buffer, 13, 'uint16');
|
|
2957
|
+
const outPFSetEnaRaw = this.extractValue(buffer, 14, 'uint16');
|
|
2958
|
+
const vArPctWinTmsRaw = this.extractValue(buffer, 18, 'uint16');
|
|
2959
|
+
const vArPctRvrtTmsRaw = this.extractValue(buffer, 19, 'uint16');
|
|
2960
|
+
const vArPctRmpTmsRaw = this.extractValue(buffer, 20, 'uint16');
|
|
2961
|
+
const vArPctModRaw = this.extractValue(buffer, 21, 'uint16');
|
|
2962
|
+
const vArPctEnaRaw = this.extractValue(buffer, 22, 'uint16');
|
|
2963
|
+
this.logRegisterRead(123, 2, 'Conn_WinTms', connWinTmsRaw, 'uint16');
|
|
2964
|
+
this.logRegisterRead(123, 3, 'Conn_RvrtTms', connRvrtTmsRaw, 'uint16');
|
|
2965
|
+
this.logRegisterRead(123, 4, 'Conn', connRaw, 'enum16');
|
|
2966
|
+
this.logRegisterRead(123, 6, 'WMaxLimPct_WinTms', wMaxLimPctWinTmsRaw, 'uint16');
|
|
2967
|
+
this.logRegisterRead(123, 7, 'WMaxLimPct_RvrtTms', wMaxLimPctRvrtTmsRaw, 'uint16');
|
|
2968
|
+
this.logRegisterRead(123, 8, 'WMaxLimPct_RmpTms', wMaxLimPctRmpTmsRaw, 'uint16');
|
|
2969
|
+
this.logRegisterRead(123, 9, 'WMaxLim_Ena', wMaxLimEnaRaw, 'enum16');
|
|
2970
|
+
this.logRegisterRead(123, 11, 'OutPFSet_WinTms', outPFSetWinTmsRaw, 'uint16');
|
|
2971
|
+
this.logRegisterRead(123, 12, 'OutPFSet_RvrtTms', outPFSetRvrtTmsRaw, 'uint16');
|
|
2972
|
+
this.logRegisterRead(123, 13, 'OutPFSet_RmpTms', outPFSetRmpTmsRaw, 'uint16');
|
|
2973
|
+
this.logRegisterRead(123, 14, 'OutPFSet_Ena', outPFSetEnaRaw, 'enum16');
|
|
2974
|
+
this.logRegisterRead(123, 18, 'VArPct_WinTms', vArPctWinTmsRaw, 'uint16');
|
|
2975
|
+
this.logRegisterRead(123, 19, 'VArPct_RvrtTms', vArPctRvrtTmsRaw, 'uint16');
|
|
2976
|
+
this.logRegisterRead(123, 20, 'VArPct_RmpTms', vArPctRmpTmsRaw, 'uint16');
|
|
2977
|
+
this.logRegisterRead(123, 21, 'VArPct_Mod', vArPctModRaw, 'enum16');
|
|
2978
|
+
this.logRegisterRead(123, 22, 'VArPct_Ena', vArPctEnaRaw, 'enum16');
|
|
2802
2979
|
const controls = {
|
|
2803
2980
|
blockNumber: 123,
|
|
2804
2981
|
blockAddress: model.address,
|
|
2805
2982
|
blockLength: model.length,
|
|
2806
|
-
// Connection control -
|
|
2983
|
+
// Connection control - points 0-2 (buffer 2-4)
|
|
2807
2984
|
Conn_WinTms: connWinTmsRaw,
|
|
2808
2985
|
Conn_RvrtTms: connRvrtTmsRaw,
|
|
2809
2986
|
Conn: connRaw,
|
|
2810
|
-
// Power limit control -
|
|
2811
|
-
WMaxLimPct: this.applyScaleFactor(this.extractValue(buffer,
|
|
2987
|
+
// Power limit control - points 3-7 (buffer 5-9)
|
|
2988
|
+
WMaxLimPct: this.applyScaleFactor(this.extractValue(buffer, 5, 'uint16'), scaleFactors.WMaxLimPct_SF, 'uint16', 'Power Limit Percentage', 5, 123),
|
|
2812
2989
|
WMaxLimPct_SF: scaleFactors.WMaxLimPct_SF,
|
|
2813
2990
|
WMaxLimPct_WinTms: wMaxLimPctWinTmsRaw,
|
|
2814
2991
|
WMaxLimPct_RvrtTms: wMaxLimPctRvrtTmsRaw,
|
|
2815
2992
|
WMaxLimPct_RmpTms: wMaxLimPctRmpTmsRaw,
|
|
2816
2993
|
WMaxLim_Ena: wMaxLimEnaRaw,
|
|
2817
|
-
// Power factor control -
|
|
2818
|
-
OutPFSet: this.applyScaleFactor(this.extractValue(buffer,
|
|
2994
|
+
// Power factor control - points 8-12 (buffer 10-14)
|
|
2995
|
+
OutPFSet: this.applyScaleFactor(this.extractValue(buffer, 10, 'int16'), scaleFactors.OutPFSet_SF, 'int16', 'Output Power Factor Set', 10, 123),
|
|
2819
2996
|
OutPFSet_SF: scaleFactors.OutPFSet_SF,
|
|
2820
2997
|
OutPFSet_WinTms: outPFSetWinTmsRaw,
|
|
2821
2998
|
OutPFSet_RvrtTms: outPFSetRvrtTmsRaw,
|
|
2822
2999
|
OutPFSet_RmpTms: outPFSetRmpTmsRaw,
|
|
2823
3000
|
OutPFSet_Ena: outPFSetEnaRaw,
|
|
2824
|
-
// Reactive power control -
|
|
2825
|
-
VArWMaxPct: this.applyScaleFactor(this.extractValue(buffer,
|
|
2826
|
-
VArMaxPct: this.applyScaleFactor(this.extractValue(buffer,
|
|
2827
|
-
VArAvalPct: this.applyScaleFactor(this.extractValue(buffer,
|
|
3001
|
+
// Reactive power control - points 13-20 (buffer 15-22)
|
|
3002
|
+
VArWMaxPct: this.applyScaleFactor(this.extractValue(buffer, 15, 'int16'), scaleFactors.VArPct_SF, 'int16', 'Reactive Power at Max Power %', 15, 123),
|
|
3003
|
+
VArMaxPct: this.applyScaleFactor(this.extractValue(buffer, 16, 'int16'), scaleFactors.VArPct_SF, 'int16', 'Max Reactive Power %', 16, 123),
|
|
3004
|
+
VArAvalPct: this.applyScaleFactor(this.extractValue(buffer, 17, 'int16'), scaleFactors.VArPct_SF, 'int16', 'Available Reactive Power %', 17, 123),
|
|
2828
3005
|
VArPct_SF: scaleFactors.VArPct_SF,
|
|
2829
3006
|
VArPct_WinTms: vArPctWinTmsRaw,
|
|
2830
3007
|
VArPct_RvrtTms: vArPctRvrtTmsRaw,
|
|
@@ -2836,7 +3013,7 @@ export class SunspecModbusClient {
|
|
|
2836
3013
|
return controls;
|
|
2837
3014
|
}
|
|
2838
3015
|
catch (error) {
|
|
2839
|
-
|
|
3016
|
+
this.logTransactionError(`Error reading inverter controls`, error);
|
|
2840
3017
|
return null;
|
|
2841
3018
|
}
|
|
2842
3019
|
}
|
|
@@ -2874,7 +3051,7 @@ export class SunspecModbusClient {
|
|
|
2874
3051
|
return true;
|
|
2875
3052
|
}
|
|
2876
3053
|
catch (error) {
|
|
2877
|
-
|
|
3054
|
+
this.logTransactionError(`Error writing inverter settings`, error);
|
|
2878
3055
|
return false;
|
|
2879
3056
|
}
|
|
2880
3057
|
}
|
|
@@ -2887,43 +3064,45 @@ export class SunspecModbusClient {
|
|
|
2887
3064
|
console.error(`Controls model 123 not found on unit ${unitId}`);
|
|
2888
3065
|
return false;
|
|
2889
3066
|
}
|
|
3067
|
+
// model.address points at the 2-register model header (ID + length), so a
|
|
3068
|
+
// SunSpec point at offset N lives at baseAddr + 2 + N.
|
|
2890
3069
|
const baseAddr = model.address;
|
|
2891
3070
|
try {
|
|
2892
3071
|
// Per-field inverter-control writes are debug-only; the high-level callers
|
|
2893
3072
|
// (`setFeedInLimit`, etc.) still log the action and outcome at info level.
|
|
2894
3073
|
if (controls.Conn !== undefined) {
|
|
2895
|
-
await this.writeRegisterValue(unitId, baseAddr +
|
|
3074
|
+
await this.writeRegisterValue(unitId, baseAddr + 4, controls.Conn, 'uint16');
|
|
2896
3075
|
console.debug(`Set connection control to ${controls.Conn}`);
|
|
2897
3076
|
}
|
|
2898
|
-
// Power limit control (
|
|
3077
|
+
// Power limit control (point 3 -> baseAddr + 5) - needs scale factor
|
|
2899
3078
|
if (controls.WMaxLimPct !== undefined) {
|
|
2900
|
-
const scaleFactor = await this.readRegisterValue(unitId, baseAddr +
|
|
3079
|
+
const scaleFactor = await this.readRegisterValue(unitId, baseAddr + 23, 1, 'int16');
|
|
2901
3080
|
const scaledValue = Math.round(controls.WMaxLimPct / Math.pow(10, scaleFactor));
|
|
2902
|
-
await this.writeRegisterValue(unitId, baseAddr +
|
|
3081
|
+
await this.writeRegisterValue(unitId, baseAddr + 5, scaledValue, 'uint16');
|
|
2903
3082
|
console.debug(`Set power limit to ${controls.WMaxLimPct}% (scaled: ${scaledValue})`);
|
|
2904
3083
|
}
|
|
2905
|
-
// Throttle enable/disable (
|
|
3084
|
+
// Throttle enable/disable (point 7 -> baseAddr + 9)
|
|
2906
3085
|
if (controls.WMaxLim_Ena !== undefined) {
|
|
2907
|
-
await this.writeRegisterValue(unitId, baseAddr +
|
|
3086
|
+
await this.writeRegisterValue(unitId, baseAddr + 9, controls.WMaxLim_Ena, 'uint16');
|
|
2908
3087
|
console.debug(`Set throttle enable to ${controls.WMaxLim_Ena}`);
|
|
2909
3088
|
}
|
|
2910
|
-
// Power factor control (
|
|
3089
|
+
// Power factor control (point 8 -> baseAddr + 10) - needs scale factor
|
|
2911
3090
|
if (controls.OutPFSet !== undefined) {
|
|
2912
|
-
const scaleFactor = await this.readRegisterValue(unitId, baseAddr +
|
|
3091
|
+
const scaleFactor = await this.readRegisterValue(unitId, baseAddr + 24, 1, 'int16');
|
|
2913
3092
|
const scaledValue = Math.round(controls.OutPFSet / Math.pow(10, scaleFactor));
|
|
2914
|
-
await this.writeRegisterValue(unitId, baseAddr +
|
|
3093
|
+
await this.writeRegisterValue(unitId, baseAddr + 10, scaledValue, 'int16');
|
|
2915
3094
|
console.debug(`Set power factor to ${controls.OutPFSet} (scaled: ${scaledValue})`);
|
|
2916
3095
|
}
|
|
2917
|
-
// Power factor enable/disable (
|
|
3096
|
+
// Power factor enable/disable (point 12 -> baseAddr + 14)
|
|
2918
3097
|
if (controls.OutPFSet_Ena !== undefined) {
|
|
2919
|
-
await this.writeRegisterValue(unitId, baseAddr +
|
|
3098
|
+
await this.writeRegisterValue(unitId, baseAddr + 14, controls.OutPFSet_Ena, 'uint16');
|
|
2920
3099
|
console.debug(`Set PF enable to ${controls.OutPFSet_Ena}`);
|
|
2921
3100
|
}
|
|
2922
3101
|
console.debug('Inverter controls written successfully');
|
|
2923
3102
|
return true;
|
|
2924
3103
|
}
|
|
2925
3104
|
catch (error) {
|
|
2926
|
-
|
|
3105
|
+
this.logTransactionError(`Error writing inverter controls`, error);
|
|
2927
3106
|
return false;
|
|
2928
3107
|
}
|
|
2929
3108
|
}
|