|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "sort" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/daniel-sullivan/srne-solar-controller/modbus" |
| 9 | + "github.com/daniel-sullivan/srne-solar-controller/register" |
| 10 | + "github.com/spf13/cobra" |
| 11 | +) |
| 12 | + |
| 13 | +var dumpCmd = &cobra.Command{ |
| 14 | + Use: "dump <group>", |
| 15 | + Short: "Dump a register group with human-readable output", |
| 16 | + Long: func() string { |
| 17 | + groups := register.AllGroups() |
| 18 | + names := make([]string, 0, len(groups)) |
| 19 | + for name := range groups { |
| 20 | + names = append(names, name) |
| 21 | + } |
| 22 | + sort.Strings(names) |
| 23 | + return fmt.Sprintf("Dump a named register group.\n\nAvailable groups: all, faults, %s", strings.Join(names, ", ")) |
| 24 | + }(), |
| 25 | + Args: cobra.ExactArgs(1), |
| 26 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 27 | + client, err := newClient() |
| 28 | + if err != nil { |
| 29 | + return err |
| 30 | + } |
| 31 | + defer client.Close() |
| 32 | + |
| 33 | + session := modbus.NewSession(client) |
| 34 | + groupName := args[0] |
| 35 | + |
| 36 | + if groupName == "faults" { |
| 37 | + return dumpFaultHistory(session) |
| 38 | + } |
| 39 | + |
| 40 | + if groupName == "all" { |
| 41 | + for _, entry := range register.OrderedGroups() { |
| 42 | + if err := dumpGroup(session, entry.Group); err != nil { |
| 43 | + fmt.Printf(" ERROR: %v\n", err) |
| 44 | + } |
| 45 | + fmt.Println() |
| 46 | + } |
| 47 | + if err := dumpFaultHistory(session); err != nil { |
| 48 | + fmt.Printf(" ERROR: %v\n", err) |
| 49 | + } |
| 50 | + return nil |
| 51 | + } |
| 52 | + |
| 53 | + groups := register.AllGroups() |
| 54 | + group, ok := groups[groupName] |
| 55 | + if !ok { |
| 56 | + names := make([]string, 0, len(groups)) |
| 57 | + for name := range groups { |
| 58 | + names = append(names, name) |
| 59 | + } |
| 60 | + sort.Strings(names) |
| 61 | + return fmt.Errorf("unknown group %q, available: all, faults, %s", groupName, strings.Join(names, ", ")) |
| 62 | + } |
| 63 | + |
| 64 | + return dumpGroup(session, group) |
| 65 | + }, |
| 66 | +} |
| 67 | + |
| 68 | +func init() { |
| 69 | + rootCmd.AddCommand(dumpCmd) |
| 70 | +} |
| 71 | + |
| 72 | +func dumpGroup(session *modbus.Session, group register.Group) error { |
| 73 | + fmt.Printf("=== %s ===\n", group.Name) |
| 74 | + if len(group.Registers) == 0 { |
| 75 | + return nil |
| 76 | + } |
| 77 | + |
| 78 | + const maxRegsPerRead = 32 |
| 79 | + if err := bulkRead(session, group, maxRegsPerRead); err != nil { |
| 80 | + return err |
| 81 | + } |
| 82 | + |
| 83 | + for _, reg := range group.Registers { |
| 84 | + regCount := int(reg.RegCount()) |
| 85 | + regValues := make([]uint16, regCount) |
| 86 | + ok := true |
| 87 | + for i := 0; i < regCount; i++ { |
| 88 | + v, err := session.Lookup(reg.Address + uint16(i)) |
| 89 | + if err != nil { |
| 90 | + ok = false |
| 91 | + break |
| 92 | + } |
| 93 | + regValues[i] = v |
| 94 | + } |
| 95 | + |
| 96 | + if !ok { |
| 97 | + if reg.Optional { |
| 98 | + continue // silently skip unavailable optional registers |
| 99 | + } |
| 100 | + fmt.Printf(" 0x%04X %-35s <read error>\n", reg.Address, reg.Name) |
| 101 | + continue |
| 102 | + } |
| 103 | + |
| 104 | + formatted := register.FormatValue(reg, regValues, session.Lookup) |
| 105 | + |
| 106 | + extra := enumLabel(reg, regValues) |
| 107 | + if extra != "" { |
| 108 | + formatted = fmt.Sprintf("%s (%s)", formatted, extra) |
| 109 | + } |
| 110 | + |
| 111 | + fmt.Printf(" 0x%04X %-35s %s\n", reg.Address, reg.Name, formatted) |
| 112 | + } |
| 113 | + return nil |
| 114 | +} |
| 115 | + |
| 116 | +// bulkRead reads all registers in a group in batched reads and stores them in the session cache. |
| 117 | +// Optional registers are read in their own spans so failures don't affect required registers. |
| 118 | +func bulkRead(session *modbus.Session, group register.Group, maxPerRead int) error { |
| 119 | + type span struct { |
| 120 | + start, end uint16 |
| 121 | + optional bool |
| 122 | + } |
| 123 | + var spans []span |
| 124 | + |
| 125 | + for _, reg := range group.Registers { |
| 126 | + end := reg.Address + reg.RegCount() |
| 127 | + // Optional registers always get their own span to isolate failures |
| 128 | + if reg.Optional { |
| 129 | + spans = append(spans, span{reg.Address, end, true}) |
| 130 | + continue |
| 131 | + } |
| 132 | + if len(spans) > 0 { |
| 133 | + last := &spans[len(spans)-1] |
| 134 | + gap := int(reg.Address) - int(last.end) |
| 135 | + if !last.optional && gap >= 0 && gap <= 4 && int(end-last.start) <= maxPerRead { |
| 136 | + last.end = end |
| 137 | + continue |
| 138 | + } |
| 139 | + } |
| 140 | + spans = append(spans, span{reg.Address, end, false}) |
| 141 | + } |
| 142 | + |
| 143 | + for _, s := range spans { |
| 144 | + count := s.end - s.start |
| 145 | + values, err := session.ReadRegisters(s.start, count) |
| 146 | + if err != nil { |
| 147 | + if !s.optional { |
| 148 | + return fmt.Errorf("read 0x%04X-0x%04X: %w", s.start, s.end-1, err) |
| 149 | + } |
| 150 | + // Optional register not available on this device — skip silently |
| 151 | + continue |
| 152 | + } |
| 153 | + session.Store(s.start, values) |
| 154 | + } |
| 155 | + return nil |
| 156 | +} |
| 157 | + |
| 158 | +func dumpFaultHistory(session *modbus.Session) error { |
| 159 | + fmt.Println("=== Fault History ===") |
| 160 | + |
| 161 | + count := 0 |
| 162 | + for i := 0; i < register.FaultRecordCount; i++ { |
| 163 | + addr := uint16(register.FaultHistoryBase) + uint16(i*register.FaultRecordSize) |
| 164 | + values, err := session.ReadRegisters(addr, uint16(register.FaultRecordSize)) |
| 165 | + if err != nil { |
| 166 | + return fmt.Errorf("read fault record %d at 0x%04X: %w", i, addr, err) |
| 167 | + } |
| 168 | + |
| 169 | + record := register.ParseFaultRecord(i, values) |
| 170 | + if record.IsEmpty() { |
| 171 | + continue |
| 172 | + } |
| 173 | + |
| 174 | + fmt.Println(register.FormatFaultRecord(record)) |
| 175 | + count++ |
| 176 | + } |
| 177 | + |
| 178 | + if count == 0 { |
| 179 | + fmt.Println(" (no faults recorded)") |
| 180 | + } |
| 181 | + return nil |
| 182 | +} |
| 183 | + |
| 184 | +func enumLabel(reg register.Register, values []uint16) string { |
| 185 | + if len(values) == 0 { |
| 186 | + return "" |
| 187 | + } |
| 188 | + switch reg.Address { |
| 189 | + case 0x0210: |
| 190 | + return register.MachineState(values[0]) |
| 191 | + case 0x010B: |
| 192 | + return register.ChargeStatus(values[0]) |
| 193 | + case 0xE004: |
| 194 | + return register.BatteryType(values[0]) |
| 195 | + case 0xE204: |
| 196 | + return register.OutputPriority(values[0]) |
| 197 | + case 0xE20F: |
| 198 | + return register.ChargerPriority(values[0]) |
| 199 | + } |
| 200 | + return "" |
| 201 | +} |
0 commit comments