@enyo-energy/sunspec-sdk 0.0.101 → 0.0.102
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/dist/cjs/sunspec-devices.cjs +372 -7
- package/dist/cjs/sunspec-devices.d.cts +81 -1
- package/dist/cjs/sunspec-interfaces.d.cts +81 -0
- package/dist/cjs/sunspec-modbus-client.cjs +153 -49
- package/dist/cjs/sunspec-modbus-client.d.cts +30 -1
- package/dist/cjs/version.cjs +1 -1
- package/dist/cjs/version.d.cts +1 -1
- package/dist/sunspec-devices.d.ts +81 -1
- package/dist/sunspec-devices.js +370 -6
- package/dist/sunspec-interfaces.d.ts +81 -0
- package/dist/sunspec-modbus-client.d.ts +30 -1
- package/dist/sunspec-modbus-client.js +153 -49
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +2 -2
|
@@ -708,7 +708,16 @@ class SunspecModbusClient {
|
|
|
708
708
|
*/
|
|
709
709
|
async detectSunspecBaseAddress(unitId, customBaseAddress) {
|
|
710
710
|
const instance = this.getInstance(unitId);
|
|
711
|
-
// If custom base address provided, try it first (
|
|
711
|
+
// If a custom base address is provided, try it first (0-based, then 1-based variant).
|
|
712
|
+
//
|
|
713
|
+
// A custom base is only a real SunSpec anchor for a device whose map genuinely starts
|
|
714
|
+
// there. It is NOT an anchor when it points at a *model block* inside a shared unit —
|
|
715
|
+
// e.g. a SolarEdge meter block (~40188) co-resident with the inverter on unit 1: the
|
|
716
|
+
// "SunS" marker lives ONLY at the unit's SunSpec root (40000/40001), never at the meter
|
|
717
|
+
// block. So a miss here must NOT fail discovery; fall through to the standard root probe
|
|
718
|
+
// below. (This is exactly the reconnect path that used to strand the meter — its
|
|
719
|
+
// rediscovery threw "not SunSpec compliant" at 40188, returned no models, and poisoned
|
|
720
|
+
// the shared socket, dragging the inverter offline too.)
|
|
712
721
|
if (customBaseAddress !== undefined) {
|
|
713
722
|
console.log(`Detect models for custom base address '${customBaseAddress}' ...`);
|
|
714
723
|
// Try 0-based at custom address
|
|
@@ -741,41 +750,44 @@ class SunspecModbusClient {
|
|
|
741
750
|
catch (error) {
|
|
742
751
|
console.debug(`Could not read SunS at ${customBaseAddress + 1}:`, error);
|
|
743
752
|
}
|
|
753
|
+
console.debug(`No "SunS" marker at custom base ${customBaseAddress}/${customBaseAddress + 1} on unit ${unitId} — ` +
|
|
754
|
+
`falling back to the standard SunSpec root (40001/40000). Expected when the custom base points at a ` +
|
|
755
|
+
`model block inside a shared unit (e.g. a co-resident meter), whose anchor is the unit root.`);
|
|
744
756
|
}
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
}
|
|
757
|
-
}
|
|
758
|
-
catch (error) {
|
|
759
|
-
console.debug('Could not read SunS at 40001:', error);
|
|
760
|
-
}
|
|
761
|
-
// Try 0-based addressing
|
|
762
|
-
try {
|
|
763
|
-
const sunspecId = await this.runExclusive(unitId, () => instance.readRegisterStringValue(40000, 2));
|
|
764
|
-
if (sunspecId.includes('SunS')) {
|
|
765
|
-
console.log('Detected 0-based addressing mode (base address: 40000)');
|
|
766
|
-
return {
|
|
767
|
-
baseAddress: 40000,
|
|
768
|
-
isZeroBased: true,
|
|
769
|
-
nextAddress: 40002
|
|
770
|
-
};
|
|
771
|
-
}
|
|
757
|
+
// Standard SunSpec root probe. Runs when no custom base was given, OR as the fallback
|
|
758
|
+
// when the custom base carried no marker. Try 1-based (40001) first (most common), then
|
|
759
|
+
// 0-based (40000).
|
|
760
|
+
try {
|
|
761
|
+
const sunspecId = await this.runExclusive(unitId, () => instance.readRegisterStringValue(40001, 2));
|
|
762
|
+
if (sunspecId.includes('SunS')) {
|
|
763
|
+
console.log('Detected 1-based addressing mode (base address: 40001)');
|
|
764
|
+
return {
|
|
765
|
+
baseAddress: 40001,
|
|
766
|
+
isZeroBased: false,
|
|
767
|
+
nextAddress: 40003
|
|
768
|
+
};
|
|
772
769
|
}
|
|
773
|
-
|
|
774
|
-
|
|
770
|
+
}
|
|
771
|
+
catch (error) {
|
|
772
|
+
console.debug('Could not read SunS at 40001:', error);
|
|
773
|
+
}
|
|
774
|
+
// Try 0-based addressing
|
|
775
|
+
try {
|
|
776
|
+
const sunspecId = await this.runExclusive(unitId, () => instance.readRegisterStringValue(40000, 2));
|
|
777
|
+
if (sunspecId.includes('SunS')) {
|
|
778
|
+
console.log('Detected 0-based addressing mode (base address: 40000)');
|
|
779
|
+
return {
|
|
780
|
+
baseAddress: 40000,
|
|
781
|
+
isZeroBased: true,
|
|
782
|
+
nextAddress: 40002
|
|
783
|
+
};
|
|
775
784
|
}
|
|
776
785
|
}
|
|
786
|
+
catch (error) {
|
|
787
|
+
console.debug('Could not read SunS at 40000:', error);
|
|
788
|
+
}
|
|
777
789
|
const addressesChecked = customBaseAddress !== undefined
|
|
778
|
-
? `${customBaseAddress}, ${customBaseAddress + 1}`
|
|
790
|
+
? `${customBaseAddress}, ${customBaseAddress + 1}, 40001, 40000`
|
|
779
791
|
: '40000 or 40001';
|
|
780
792
|
throw new Error(`Device is not SunSpec compliant - "SunS" identifier not found at addresses ${addressesChecked}`);
|
|
781
793
|
}
|
|
@@ -786,29 +798,35 @@ class SunspecModbusClient {
|
|
|
786
798
|
async discoverModels(unitId, customBaseAddress) {
|
|
787
799
|
const instance = this.getInstance(unitId);
|
|
788
800
|
const models = this.getModelsMap(unitId);
|
|
789
|
-
models.clear();
|
|
790
801
|
const modelList = this.getModelList(unitId);
|
|
791
|
-
modelList.length = 0;
|
|
792
802
|
const maxAddress = 50000; // Safety limit
|
|
793
|
-
let currentAddress = 0;
|
|
794
803
|
console.log(`Starting Sunspec model discovery for unit ${unitId}...`);
|
|
795
804
|
// Probe for the SunS identifier first. A missing identifier means the unit ID isn't a
|
|
796
805
|
// SunSpec device — common during unit-ID scans — and is logged at debug level so probes
|
|
797
806
|
// don't surface as errors. Mid-scan failures below remain at error level since they
|
|
798
807
|
// indicate a partial / malformed SunSpec map.
|
|
808
|
+
//
|
|
809
|
+
// IMPORTANT: we discover into TEMP buffers and only commit into the stored map once the
|
|
810
|
+
// walk yields at least one model. A reconnect probe can transiently fail (e.g. a single-
|
|
811
|
+
// session SolarEdge endpoint drops the socket with ECONNRESET on the first read after
|
|
812
|
+
// accepting the connection). Clearing the stored map up-front — as this used to — turned
|
|
813
|
+
// that blip into "no usable models", which trips tryReconnect()'s gate into invalidating
|
|
814
|
+
// a unit whose model map was perfectly good a moment ago. Preserving the last known-good
|
|
815
|
+
// map keeps a genuinely-connected sibling alive; a device that is really gone is still
|
|
816
|
+
// caught by read-health (isHealthy()).
|
|
799
817
|
let addressInfo;
|
|
800
818
|
try {
|
|
801
819
|
addressInfo = await this.detectSunspecBaseAddress(unitId, customBaseAddress);
|
|
802
820
|
}
|
|
803
821
|
catch (error) {
|
|
804
822
|
console.debug(`No SunSpec device at unit ${unitId}: ${error}`);
|
|
805
|
-
//
|
|
806
|
-
//
|
|
807
|
-
|
|
808
|
-
console.debug(`Discovery complete for unit ${unitId}. Found 0 models: []`);
|
|
823
|
+
// Same outcome as a normal "no device found"; keep at debug. Do NOT wipe the stored
|
|
824
|
+
// map — return it untouched so a transient probe failure can't erase good discovery.
|
|
825
|
+
console.debug(`Discovery incomplete for unit ${unitId}; keeping ${models.size} previously-discovered model(s).`);
|
|
809
826
|
return models;
|
|
810
827
|
}
|
|
811
|
-
|
|
828
|
+
const discovered = [];
|
|
829
|
+
let currentAddress = addressInfo.nextAddress;
|
|
812
830
|
try {
|
|
813
831
|
while (currentAddress < maxAddress) {
|
|
814
832
|
const buffer = await this.runExclusive(unitId, () => instance.readHoldingRegisters(currentAddress, 2));
|
|
@@ -824,19 +842,12 @@ class SunspecModbusClient {
|
|
|
824
842
|
console.debug(`Found end marker at address ${currentAddress}`);
|
|
825
843
|
break;
|
|
826
844
|
}
|
|
827
|
-
// Store discovered model
|
|
828
845
|
const model = {
|
|
829
846
|
id: modelId,
|
|
830
847
|
address: currentAddress,
|
|
831
848
|
length: modelLength
|
|
832
849
|
};
|
|
833
|
-
|
|
834
|
-
// findModel() resolves to the device's own block. Record every instance —
|
|
835
|
-
// including repeats like a meter's Common (id 1) — in the ordered list.
|
|
836
|
-
if (!models.has(modelId)) {
|
|
837
|
-
models.set(modelId, model);
|
|
838
|
-
}
|
|
839
|
-
modelList.push(model);
|
|
850
|
+
discovered.push(model);
|
|
840
851
|
// Per-model discovery step. The end-of-discovery summary below is the
|
|
841
852
|
// info-level outcome; the per-model walk is debug-only.
|
|
842
853
|
console.debug(`Discovered Model ${modelId} at address ${currentAddress} with length ${modelLength} (unit ${unitId})`);
|
|
@@ -847,6 +858,24 @@ class SunspecModbusClient {
|
|
|
847
858
|
catch (error) {
|
|
848
859
|
console.error(`Error during model discovery at address ${currentAddress} (unit ${unitId}): ${error}`);
|
|
849
860
|
}
|
|
861
|
+
if (discovered.length > 0) {
|
|
862
|
+
// Commit the fresh discovery atomically. Keep the FIRST (lowest-address) instance per
|
|
863
|
+
// id in the keyed directory so findModel() resolves to the device's own block; record
|
|
864
|
+
// every instance — including repeats like a meter's Common (id 1) — in the ordered list.
|
|
865
|
+
models.clear();
|
|
866
|
+
modelList.length = 0;
|
|
867
|
+
for (const model of discovered) {
|
|
868
|
+
if (!models.has(model.id)) {
|
|
869
|
+
models.set(model.id, model);
|
|
870
|
+
}
|
|
871
|
+
modelList.push(model);
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
else {
|
|
875
|
+
// The anchor answered but the walk yielded nothing (e.g. the socket dropped right
|
|
876
|
+
// after the SunS probe). Preserve the last known-good map rather than clobbering it.
|
|
877
|
+
console.debug(`Discovery for unit ${unitId} yielded no models; keeping ${models.size} previously-discovered model(s).`);
|
|
878
|
+
}
|
|
850
879
|
// Report every instance in address order so repeated ids (e.g. two Common blocks, or
|
|
851
880
|
// multiple meters) are visible — models.size would hide them behind the unique keys.
|
|
852
881
|
console.log(`Discovery complete for unit ${unitId}. Found ${modelList.length} model instance(s): [${modelList.map(m => m.id).join(', ')}]`);
|
|
@@ -988,12 +1017,87 @@ class SunspecModbusClient {
|
|
|
988
1017
|
return value;
|
|
989
1018
|
}
|
|
990
1019
|
/**
|
|
991
|
-
*
|
|
1020
|
+
* Read a contiguous span of `quantity` registers starting at `address` in a single logical
|
|
1021
|
+
* transaction — chunked at the 125-register Modbus limit and serialized on the unit's socket so no
|
|
1022
|
+
* other frame interleaves between chunks. FC03 holding registers by default; FC04 input registers
|
|
1023
|
+
* when `useInputRegisters` is true. The SunSpec model readers use {@link readModelBlock}; this is
|
|
1024
|
+
* the equivalent for callers reading a hand-defined block (see {@link SunspecCustomDevice}). Decode
|
|
1025
|
+
* individual fields out of the returned buffer with {@link decodeBlockValue}.
|
|
1026
|
+
*/
|
|
1027
|
+
async readRegisterBlock(unitId, address, quantity, useInputRegisters = false) {
|
|
1028
|
+
const reader = this.getReader(unitId);
|
|
1029
|
+
const MAX_REGISTERS_PER_READ = 125;
|
|
1030
|
+
try {
|
|
1031
|
+
const chunks = await this.runExclusive(unitId, async () => {
|
|
1032
|
+
const acc = [];
|
|
1033
|
+
for (let offset = 0; offset < quantity; offset += MAX_REGISTERS_PER_READ) {
|
|
1034
|
+
const count = Math.min(MAX_REGISTERS_PER_READ, quantity - offset);
|
|
1035
|
+
let result;
|
|
1036
|
+
if (useInputRegisters) {
|
|
1037
|
+
if (!reader.readInputRegisters) {
|
|
1038
|
+
throw new Error(`Reader for unit ${unitId} does not support FC04 input registers`);
|
|
1039
|
+
}
|
|
1040
|
+
result = await reader.readInputRegisters(address + offset, count);
|
|
1041
|
+
}
|
|
1042
|
+
else {
|
|
1043
|
+
result = await reader.readHoldingRegisters(address + offset, count);
|
|
1044
|
+
}
|
|
1045
|
+
if (!result.success || !result.value) {
|
|
1046
|
+
throw new Error(`Failed to read register block at address ${address + offset} (unit ${unitId}): ${result.error?.message || 'Unknown error'}`);
|
|
1047
|
+
}
|
|
1048
|
+
acc.push(result.value);
|
|
1049
|
+
}
|
|
1050
|
+
return acc;
|
|
1051
|
+
});
|
|
1052
|
+
this.connectionHealth.recordSuccess();
|
|
1053
|
+
return chunks.length === 1 ? chunks[0] : Buffer.concat(chunks);
|
|
1054
|
+
}
|
|
1055
|
+
catch (error) {
|
|
1056
|
+
this.connectionHealth.recordFailure(error);
|
|
1057
|
+
throw error;
|
|
1058
|
+
}
|
|
1059
|
+
}
|
|
1060
|
+
/**
|
|
1061
|
+
* Decode a typed value out of a buffer returned by {@link readRegisterBlock}, at a register offset
|
|
1062
|
+
* from the block start. A thin public wrapper over the same block-extraction the SunSpec model
|
|
1063
|
+
* readers use internally.
|
|
1064
|
+
*/
|
|
1065
|
+
decodeBlockValue(buffer, registerOffset, dataType, quantity = 1) {
|
|
1066
|
+
return this.extractValue(buffer, registerOffset, dataType, quantity);
|
|
1067
|
+
}
|
|
1068
|
+
/**
|
|
1069
|
+
* Helper to read holding register value(s) (FC03) using the fault-tolerant reader with data type
|
|
1070
|
+
* conversion.
|
|
992
1071
|
*/
|
|
993
1072
|
async readRegisterValue(unitId, address, quantity = 1, dataType) {
|
|
1073
|
+
return this.readTypedRegisters(unitId, address, quantity, dataType, false);
|
|
1074
|
+
}
|
|
1075
|
+
/**
|
|
1076
|
+
* Helper to read input register value(s) (FC04) using the fault-tolerant reader with data type
|
|
1077
|
+
* conversion. SunSpec itself never uses input registers, but plain-Modbus devices behind a
|
|
1078
|
+
* {@link SunspecCustomDevice} may expose live values there. Throws if the transport's reader does
|
|
1079
|
+
* not implement `readInputRegisters`.
|
|
1080
|
+
*/
|
|
1081
|
+
async readInputRegisterValue(unitId, address, quantity = 1, dataType) {
|
|
1082
|
+
return this.readTypedRegisters(unitId, address, quantity, dataType, true);
|
|
1083
|
+
}
|
|
1084
|
+
/**
|
|
1085
|
+
* Shared body of {@link readRegisterValue} / {@link readInputRegisterValue}. Runs the read under
|
|
1086
|
+
* the unit's transaction lock (so it inherits the timeout/poison/staleness resilience) and records
|
|
1087
|
+
* connection health on both success and failure.
|
|
1088
|
+
*/
|
|
1089
|
+
async readTypedRegisters(unitId, address, quantity, dataType, useInputRegisters) {
|
|
994
1090
|
const reader = this.getReader(unitId);
|
|
995
1091
|
try {
|
|
996
|
-
const result = await this.runExclusive(unitId, () =>
|
|
1092
|
+
const result = await this.runExclusive(unitId, () => {
|
|
1093
|
+
if (useInputRegisters) {
|
|
1094
|
+
if (!reader.readInputRegisters) {
|
|
1095
|
+
throw new Error(`Reader for unit ${unitId} does not support FC04 input registers`);
|
|
1096
|
+
}
|
|
1097
|
+
return reader.readInputRegisters(address, quantity);
|
|
1098
|
+
}
|
|
1099
|
+
return reader.readHoldingRegisters(address, quantity);
|
|
1100
|
+
});
|
|
997
1101
|
// Check if the read was successful
|
|
998
1102
|
if (!result.success || !result.value) {
|
|
999
1103
|
throw new Error(`Failed to read register at address ${address}: ${result.error?.message || 'Unknown error'}`);
|
|
@@ -349,9 +349,38 @@ export declare class SunspecModbusClient {
|
|
|
349
349
|
*/
|
|
350
350
|
private extractValue;
|
|
351
351
|
/**
|
|
352
|
-
*
|
|
352
|
+
* Read a contiguous span of `quantity` registers starting at `address` in a single logical
|
|
353
|
+
* transaction — chunked at the 125-register Modbus limit and serialized on the unit's socket so no
|
|
354
|
+
* other frame interleaves between chunks. FC03 holding registers by default; FC04 input registers
|
|
355
|
+
* when `useInputRegisters` is true. The SunSpec model readers use {@link readModelBlock}; this is
|
|
356
|
+
* the equivalent for callers reading a hand-defined block (see {@link SunspecCustomDevice}). Decode
|
|
357
|
+
* individual fields out of the returned buffer with {@link decodeBlockValue}.
|
|
358
|
+
*/
|
|
359
|
+
readRegisterBlock(unitId: number, address: number, quantity: number, useInputRegisters?: boolean): Promise<Buffer>;
|
|
360
|
+
/**
|
|
361
|
+
* Decode a typed value out of a buffer returned by {@link readRegisterBlock}, at a register offset
|
|
362
|
+
* from the block start. A thin public wrapper over the same block-extraction the SunSpec model
|
|
363
|
+
* readers use internally.
|
|
364
|
+
*/
|
|
365
|
+
decodeBlockValue(buffer: Buffer, registerOffset: number, dataType: EnergyAppModbusDataType, quantity?: number): number | string | number[];
|
|
366
|
+
/**
|
|
367
|
+
* Helper to read holding register value(s) (FC03) using the fault-tolerant reader with data type
|
|
368
|
+
* conversion.
|
|
353
369
|
*/
|
|
354
370
|
readRegisterValue(unitId: number, address: number, quantity: number | undefined, dataType: EnergyAppModbusDataType): Promise<number | string | number[]>;
|
|
371
|
+
/**
|
|
372
|
+
* Helper to read input register value(s) (FC04) using the fault-tolerant reader with data type
|
|
373
|
+
* conversion. SunSpec itself never uses input registers, but plain-Modbus devices behind a
|
|
374
|
+
* {@link SunspecCustomDevice} may expose live values there. Throws if the transport's reader does
|
|
375
|
+
* not implement `readInputRegisters`.
|
|
376
|
+
*/
|
|
377
|
+
readInputRegisterValue(unitId: number, address: number, quantity: number | undefined, dataType: EnergyAppModbusDataType): Promise<number | string | number[]>;
|
|
378
|
+
/**
|
|
379
|
+
* Shared body of {@link readRegisterValue} / {@link readInputRegisterValue}. Runs the read under
|
|
380
|
+
* the unit's transaction lock (so it inherits the timeout/poison/staleness resilience) and records
|
|
381
|
+
* connection health on both success and failure.
|
|
382
|
+
*/
|
|
383
|
+
private readTypedRegisters;
|
|
355
384
|
/**
|
|
356
385
|
* Helper to write register value(s)
|
|
357
386
|
*/
|
package/dist/cjs/version.cjs
CHANGED
|
@@ -9,7 +9,7 @@ exports.getSdkVersion = getSdkVersion;
|
|
|
9
9
|
/**
|
|
10
10
|
* Current version of the enyo Energy App SDK.
|
|
11
11
|
*/
|
|
12
|
-
exports.SDK_VERSION = '0.0.
|
|
12
|
+
exports.SDK_VERSION = '0.0.102';
|
|
13
13
|
/**
|
|
14
14
|
* Gets the current SDK version.
|
|
15
15
|
* @returns The semantic version string of the SDK
|
package/dist/cjs/version.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type IRetryConfig, type SunspecBatteryBaseData, SunspecBatteryCapability, type SunspecBatteryControls, type SunspecBatteryData, type SunspecBatteryFeatureMode, type SunspecInverterData, SunspecInverterCapability, SunspecPvPowerSource, SunspecPvPowerOrigin, type SunspecInverterReading, type SunspecBatteryReading, SunspecMeterCapability, type SunspecMPPTData, SunspecStorageMode } from "./sunspec-interfaces.js";
|
|
1
|
+
import { type IRetryConfig, type SunspecBatteryBaseData, SunspecBatteryCapability, type SunspecBatteryControls, type SunspecBatteryData, type SunspecBatteryFeatureMode, type SunspecInverterData, SunspecInverterCapability, SunspecPvPowerSource, SunspecPvPowerOrigin, type SunspecInverterReading, type SunspecBatteryReading, SunspecMeterCapability, type SunspecMPPTData, SunspecStorageMode, type SunspecCustomDeviceConfig } from "./sunspec-interfaces.js";
|
|
2
2
|
import { ApplianceManager, EnergyApp } from "@enyo-energy/energy-app-sdk";
|
|
3
3
|
import { type EnyoAppliance, type EnyoApplianceErrorCode, EnyoApplianceName } from "@enyo-energy/energy-app-sdk/dist/types/enyo-appliance.js";
|
|
4
4
|
import { EnyoNetworkDevice } from "@enyo-energy/energy-app-sdk/dist/types/enyo-network-device.js";
|
|
@@ -68,6 +68,20 @@ export declare abstract class BaseSunspecDevice {
|
|
|
68
68
|
* Ensure the Sunspec client is connected and models are discovered
|
|
69
69
|
*/
|
|
70
70
|
protected ensureConnected(): Promise<void>;
|
|
71
|
+
/**
|
|
72
|
+
* Populate the per-unit model directory after the socket opens. Default: run SunSpec model
|
|
73
|
+
* discovery. Devices that read a hand-defined static register map instead of a discovered SunSpec
|
|
74
|
+
* directory (see {@link SunspecCustomDevice}) override this to a no-op.
|
|
75
|
+
*/
|
|
76
|
+
protected discoverAfterConnect(): Promise<void>;
|
|
77
|
+
/**
|
|
78
|
+
* Validate that a just-completed reconnect actually restored a usable device. Default: re-run
|
|
79
|
+
* SunSpec discovery and check it against {@link getRequiredModelIds} (a socket that opens but comes
|
|
80
|
+
* back with no usable models means the gateway accepted the connection before it was ready to
|
|
81
|
+
* answer the scan). Returns false to treat the reconnect as failed so the backoff schedule keeps
|
|
82
|
+
* running. Discovery-less devices override this to just confirm the socket is open.
|
|
83
|
+
*/
|
|
84
|
+
protected revalidateAfterReconnect(): Promise<boolean>;
|
|
71
85
|
/**
|
|
72
86
|
* Attempt a reconnection if the tiered retry schedule allows it.
|
|
73
87
|
* Called from readData() when the device is disconnected.
|
|
@@ -735,4 +749,70 @@ export declare class SunspecMeter extends BaseSunspecDevice {
|
|
|
735
749
|
*/
|
|
736
750
|
readData(clockId: string, resolution: EnyoDataBusMessageResolution): Promise<EnyoDataBusMessage[]>;
|
|
737
751
|
}
|
|
752
|
+
/**
|
|
753
|
+
* A generic, pre-configured Modbus device that plugs into the exact same resilient lifecycle as the
|
|
754
|
+
* SunSpec devices — the per-device {@link ConnectionRetryManager}, {@link tryReconnect} tiered backoff,
|
|
755
|
+
* {@link markOffline}/{@link reassertConnectedIfRecovered} shared-socket healing, and the client's
|
|
756
|
+
* per-transaction timeout/poison/staleness guards — but reads a **hand-defined static register map**
|
|
757
|
+
* instead of a discovered SunSpec model directory.
|
|
758
|
+
*
|
|
759
|
+
* It emulates a configurable {@link SunspecCustomDeviceConfig.role} (`meter` | `inverter` | `battery`):
|
|
760
|
+
* the role selects the appliance type created on connect and which EnyoDataBus values message
|
|
761
|
+
* {@link readData} publishes, and each {@link SunspecCustomRegister} maps onto one numeric field of
|
|
762
|
+
* that message. Supports FC03 holding registers (default), FC04 input registers, and writes.
|
|
763
|
+
*
|
|
764
|
+
* Discovery is fully bypassed: {@link connect} opens the socket only, and the reconnect path is
|
|
765
|
+
* validated by socket liveness rather than a SunSpec scan.
|
|
766
|
+
*/
|
|
767
|
+
export declare class SunspecCustomDevice extends BaseSunspecDevice {
|
|
768
|
+
private readonly config;
|
|
769
|
+
/** Read plan derived once from the static register map (see {@link planBlocks}). */
|
|
770
|
+
private plannedBlocks?;
|
|
771
|
+
constructor(energyApp: EnergyApp, name: EnyoApplianceName[], networkDevice: EnyoNetworkDevice, sunspecClient: SunspecModbusClient, applianceManager: ApplianceManager, config: SunspecCustomDeviceConfig, unitId?: number, port?: number, baseAddress?: number, retryConfig?: IRetryConfig, appliance?: EnyoAppliance, useTls?: boolean);
|
|
772
|
+
/**
|
|
773
|
+
* No SunSpec model directory to build — this device reads pre-defined static addresses. Overriding
|
|
774
|
+
* the base discovery hook to a no-op is what lets {@link ensureConnected}/{@link connect} open the
|
|
775
|
+
* socket without a SunS scan.
|
|
776
|
+
*/
|
|
777
|
+
protected discoverAfterConnect(): Promise<void>;
|
|
778
|
+
/**
|
|
779
|
+
* A reconnect is usable as soon as the socket is open again — there is no model map to revalidate.
|
|
780
|
+
* Without this override the base default would run SunSpec discovery, find nothing, and treat every
|
|
781
|
+
* reconnect as a permanent failure.
|
|
782
|
+
*/
|
|
783
|
+
protected revalidateAfterReconnect(): Promise<boolean>;
|
|
784
|
+
/**
|
|
785
|
+
* Connect the underlying socket (no discovery) and create/update the appliance for the configured
|
|
786
|
+
* role. Identity metadata comes from {@link SunspecCustomDeviceConfig.identity} since there is no
|
|
787
|
+
* SunSpec Common block to read it from.
|
|
788
|
+
*/
|
|
789
|
+
connect(): Promise<void>;
|
|
790
|
+
disconnect(): Promise<void>;
|
|
791
|
+
readData(clockId: string, resolution: EnyoDataBusMessageResolution): Promise<EnyoDataBusMessage[]>;
|
|
792
|
+
/**
|
|
793
|
+
* Write a value to a pre-configured writable register. Delegates to the client's resilient
|
|
794
|
+
* {@link SunspecModbusClient.writeRegisterValue}.
|
|
795
|
+
*/
|
|
796
|
+
writeCustomRegister(name: string, value: number | number[]): Promise<boolean>;
|
|
797
|
+
/**
|
|
798
|
+
* Read every configured register as a small number of block reads and extract each register from
|
|
799
|
+
* its block buffer by offset (mirroring the SunSpec model-block read). Applies the fixed decimal
|
|
800
|
+
* scaling and returns a field → value map. Non-numeric reads (e.g. a `string` dataType that can't
|
|
801
|
+
* populate a numeric databus field) are skipped with a warning.
|
|
802
|
+
*/
|
|
803
|
+
private readConfiguredRegisters;
|
|
804
|
+
/** Lazily compute and cache the block read plan; the register map is static per device. */
|
|
805
|
+
private getPlannedBlocks;
|
|
806
|
+
/**
|
|
807
|
+
* Group the configured registers (separately per function code) sorted by address, coalescing them
|
|
808
|
+
* — across gaps — into blocks no wider than {@link SunspecCustomDeviceConfig.blockSize}. A new block
|
|
809
|
+
* starts whenever adding the next register would exceed that span. Each block records its members'
|
|
810
|
+
* offsets so a single read can be sliced into all of them.
|
|
811
|
+
*/
|
|
812
|
+
private planBlocks;
|
|
813
|
+
private buildRoleMessage;
|
|
814
|
+
private buildMeterMessage;
|
|
815
|
+
private buildInverterMessage;
|
|
816
|
+
private buildBatteryMessage;
|
|
817
|
+
}
|
|
738
818
|
export {};
|