diff --git a/internal/module/m9zTtyApi/read.go b/internal/module/m9zTtyApi/read.go index 486de44..8777029 100644 --- a/internal/module/m9zTtyApi/read.go +++ b/internal/module/m9zTtyApi/read.go @@ -155,11 +155,12 @@ func getDeviceStatus2Real(rpc towgo.JsonRpcConnection) { } d := new(DeviceStatus) d.DeviceStatus = *read - loopCount := configuredLoopCount() - d.ScanDeviceCount = loopCount - loops := make([]DeviceStatusLoop, loopCount) + scanLoopCount := configuredLoopCount() + displayLoopCount := monitorDisplayLoopCount() + d.ScanDeviceCount = scanLoopCount + loops := make([]DeviceStatusLoop, displayLoopCount) var wg sync.WaitGroup - for i := 0; i < loopCount; i++ { + for i := 0; i < displayLoopCount; i++ { idx := i wg.Add(1) go func() { @@ -175,10 +176,12 @@ func getDeviceStatus2Real(rpc towgo.JsonRpcConnection) { loop.A = parametersRead.DCCurrent kw := float64(loop.V*loop.A) / 1000 loop.Kw = float32(math.Round(kw*1000) / 1000) - loop.Faults = m9z.BuildLoopFaults(uint(idx+1), parametersRead.AlarmStatus, parametersRead.AlarmCode) - loop.HasFault = len(loop.Faults) > 0 - loop.FaultMsg = joinLoopFaultMessages(loop.Faults) - loop.FaultCode = joinLoopFaultCodes(loop.Faults) + if idx < scanLoopCount { + loop.Faults = m9z.BuildLoopFaults(uint(idx+1), parametersRead.AlarmStatus, parametersRead.AlarmCode) + loop.HasFault = len(loop.Faults) > 0 + loop.FaultMsg = joinLoopFaultMessages(loop.Faults) + loop.FaultCode = joinLoopFaultCodes(loop.Faults) + } } loops[idx] = loop }() @@ -191,7 +194,7 @@ func getDeviceStatus2Real(rpc towgo.JsonRpcConnection) { return d.Loops[i].Index < d.Loops[j].Index }) for _, loop := range d.Loops { - if loop.HasFault { + if loop.HasFault && loop.Index < scanLoopCount { recordLoopFaults(cuid, uint(loop.Index+1), loop.V, loop.Faults) } } @@ -200,14 +203,15 @@ func getDeviceStatus2Real(rpc towgo.JsonRpcConnection) { } func buildDemoDeviceStatus2() *DeviceStatus { - const loopCount = 10 + displayLoopCount := monitorDisplayLoopCount() + scanLoopCount := configuredLoopCount() d := &DeviceStatus{ - ScanDeviceCount: loopCount, - Loops: make([]DeviceStatusLoop, loopCount), + ScanDeviceCount: scanLoopCount, + Loops: make([]DeviceStatusLoop, displayLoopCount), } d.Mode = 0 d.DeviceTime = time.Now().UTC() - for i := 0; i < loopCount; i++ { + for i := 0; i < displayLoopCount; i++ { relayOpen := i%2 == 0 pwm := uint8(35 + i*5) d.Relays[i] = relayOpen @@ -226,17 +230,19 @@ func buildDemoDeviceStatus2() *DeviceStatus { kw := float64(loop.V*loop.A) / 1000 loop.Kw = float32(math.Round(kw*1000) / 1000) - switch i { - case 0: - loop.Faults = m9z.BuildLoopFaults(uint(i+1), 0x01, "01 00") - case 1: - loop.Faults = m9z.BuildLoopFaults(uint(i+1), 0x02, "02 00") - case 6: - loop.Faults = m9z.BuildLoopFaults(uint(i+1), 0x04, "04 03") - case 8: - loop.Faults = m9z.BuildLoopFaults(uint(i+1), 0x03, "03 00") - default: - loop.Faults = m9z.BuildLoopFaults(uint(i+1), 0x02, "02 00") + if i < scanLoopCount { + switch i { + case 0: + loop.Faults = m9z.BuildLoopFaults(uint(i+1), 0x01, "01 00") + case 1: + loop.Faults = m9z.BuildLoopFaults(uint(i+1), 0x02, "02 00") + case 6: + loop.Faults = m9z.BuildLoopFaults(uint(i+1), 0x04, "04 03") + case 8: + loop.Faults = m9z.BuildLoopFaults(uint(i+1), 0x03, "03 00") + default: + loop.Faults = m9z.BuildLoopFaults(uint(i+1), 0x02, "02 00") + } } loop.HasFault = len(loop.Faults) > 0 loop.FaultMsg = joinLoopFaultMessages(loop.Faults) diff --git a/internal/module/m9zTtyApi/scan.go b/internal/module/m9zTtyApi/scan.go index 26077a6..91c94a0 100644 --- a/internal/module/m9zTtyApi/scan.go +++ b/internal/module/m9zTtyApi/scan.go @@ -9,6 +9,10 @@ func configuredLoopCount() int { return scanConfigManager.GetLoopCount() } +func monitorDisplayLoopCount() int { + return maxLoopCount +} + func normalizeLoopCount(loopCount int) int { if loopCount <= 0 { return defaultLoopCount diff --git a/internal/module/m9zTtyApi/scan_test.go b/internal/module/m9zTtyApi/scan_test.go index 58992d3..c32c8ba 100644 --- a/internal/module/m9zTtyApi/scan_test.go +++ b/internal/module/m9zTtyApi/scan_test.go @@ -22,3 +22,35 @@ func TestNormalizeLoopCount(t *testing.T) { }) } } + +func TestMonitorDisplayLoopCountAlwaysUsesMaxLoopCount(t *testing.T) { + if got := monitorDisplayLoopCount(); got != maxLoopCount { + t.Fatalf("monitorDisplayLoopCount() = %d, want %d", got, maxLoopCount) + } +} + +func TestBuildDemoDeviceStatus2KeepsDisplayLoopsIndependentFromScanCount(t *testing.T) { + scanConfigManager.mu.Lock() + oldLoopCount := scanConfigManager.loopCount + scanConfigManager.loopCount = 6 + scanConfigManager.mu.Unlock() + t.Cleanup(func() { + scanConfigManager.mu.Lock() + scanConfigManager.loopCount = oldLoopCount + scanConfigManager.mu.Unlock() + }) + + status := buildDemoDeviceStatus2() + if got := len(status.Loops); got != monitorDisplayLoopCount() { + t.Fatalf("len(status.Loops) = %d, want %d", got, monitorDisplayLoopCount()) + } + if got := status.ScanDeviceCount; got != 6 { + t.Fatalf("status.ScanDeviceCount = %d, want 6", got) + } + if !status.Loops[5].HasFault { + t.Fatal("status.Loops[5].HasFault = false, want true for the last scanned loop") + } + if status.Loops[6].HasFault { + t.Fatal("status.Loops[6].HasFault = true, want false outside the scan loop count") + } +}