Repository navigation
feat(modbus): PR2 — SunSpec Modbus server, register map, e2e tests - #2
Merged
Merged
Conversation
The M101 const block had offsets off by -2 (assumed M101 starts at offset 68 instead of canonical 70). Root cause: the comment 'absolute offset = M1.LENGTH + 0' was wrong - M1 starts at base offset 2 (SunS magic occupies offsets 0-1), so M101 starts at 2 + 68 = 70. This bug did not affect PR2's implementation because sunspec-registers.ts was authored against the canonical SunSpec JSON (github.com/sunspec/models/json/model_101.json) and observation 575, not against this design doc. Also corrected DCV / DCA ordering and added missing DCA, DCA_SF, DCV_SF, TMPSNK, TMPTRNS, TMPOT, TMP_SF, STVND, EVT2, EVT_VND1..4 fields for completeness. Refs: obs 575 (sunspec/register-map).
The first correction commit (1deb513) only fixed the M101 header arithmetic. It left the DC block with off-by-2 typos inherited from the original design. This commit brings the rest of the M101 const block into alignment with the canonical SunSpec JSON and the verified implementation in src/modbus/sunspec-registers.ts: - DCA: 97 (+27) - DCA_SF: 98 (+28) - DCV: 99 (+29) - DCV_SF: 100 (+30) - DCW: 101 (+31, was 99) - DCW_SF: 102 (+32, was missing) - TMPCAB: 103 (+33, was 101 - collision with DCW) - TMPSNK: 104 (+34) - TMPTRNS: 105 (+35) - TMPOT: 106 (+36) - TMP_SF: 107 (+37) - ST: 108 (+38, was 106) - STVND: 109 (+39, was 107) - EVT1: 110 (+40, was 108) - EVT2: 112 (+42, was 110) - EVT_VND1..4 unchanged from previous commit Refs: PR2 fresh review finding #1 (minor, design.md still had DC offset typos after the first correction).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR2 — SunSpec Modbus TCP server + register map + e2e tests
Branches from
mainafter PR1 (the foundation: domain types, scale-factor helpers, state bus, adapter interface) merged. Independent of PR3.What's in this PR
src/modbus/sunspec-registers.ts— canonical SunSpec M1 (L=68) + M101 (L=52) register map with absolute buffer offsets, pluswriteUint16,writeInt32BE, andwriteSunSpecStringhelpers (all big-endian). Module-level constants:SUNS_MAGIC_HI=0x5375,SUNS_MAGIC_LO=0x6e53,EOM_SENTINEL=0xffff,HOLDING_REGISTER_COUNT=124.src/modbus/sunspec-modbus-server.service.ts—SunSpecModbusServerServicewith the double-buffer atomic-swap pattern (bufA / bufB, flipactiveafter write). ImplementsOnApplicationBootstrap(seed + bind TCP socket) andOnApplicationShutdown(close with 5 s grace). TheIServiceVectorexposes Promise-returning read handlers (single-register reads) and a sync-returninggetMultipleHoldingRegisters(modbus-serial v8's FC3 path treats a 3-arg Promise handler as if it returned a sync array — see comment in the source).test/e2e/sunspec-modbus.e2e-spec.ts— full e2e test usingmodbus-serialas a TCP client against a NestJS test app booted with aFakeAdapter. Verifies: SunS magic at registers 40000-40001, M1 ID/L/Mn at 40002-40003/40004-40019, M101 ID/L at 40070-40071, A / W / Hz / St projections + their*_SFregister encodings, WH as int32 BE, the EOM sentinel, and the read-only write rejection. Uses port 5021 (override viaMODBUS_TEST_PORT) to avoid colliding with a dev server bound to 5020.jest-e2e.config.ts+test:e2enpm script..github/workflows/ci.yml— addedTest (e2e)step runningpnpm test:e2eafter the unit-test step.Verification gate
All green locally:
Address convention (important)
modbus-serialv8 calls ourIServiceVectorhandlers withaddras the buffer offset (0-based), where register 40000 = offset 0. So when HA reads register 40002 (M1 ID), the handler receivesaddr=2. The e2e test reads at 40000+ for verification; the register constant values insunspec-registers.tsare absolute buffer offsets.SunSpec register offsets — verified against canonical sources
All offsets are verified against the canonical SunSpec models at https://github.com/sunspec/models (see
model_1.jsonandmodel_101.json) and against Engram obs 575 (sunspec/register-map). The M101 const block is the canonical layout (M101.ID at absolute offset 70, M101.A at 72, M101.W at 84, M101.ST at 108, etc.) — note this differs by +2 from the design.md draft, which assumed M101 starts at offset 68 (pre-SunS-magic). The canonical layout is correct.Workload