/
cmd_modbus.go
357 lines (312 loc) · 9.81 KB
/
cmd_modbus.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package cmd
import (
"fmt"
"time"
"github.com/MickMake/GoSungrow/cmdModbus"
"github.com/MickMake/GoUnify/Only"
"github.com/MickMake/GoUnify/cmdHelp"
"github.com/MickMake/GoUnify/cmdLog"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// https://github.com/simonvetter/modbus
// https://github.com/goburrow/modbus
// -> fork -> https://github.com/activeshadow/modbus
// https://github.com/btfak/modbus
// -> fork -> https://github.com/dpapathanasiou/go-modbus
const (
flagModbusUsername = "modbus-user"
flagModbusPassword = "modbus-password"
flagModbusHost = "modbus-host"
flagModbusPort = "modbus-port"
)
//goland:noinspection GoNameStartsWithPackageName
type CmdModbus struct {
CmdDefault
Username string
Password string
Host string
Port string
Client cmdModbus.Modbus
log cmdLog.Log
optionSleepDelay time.Duration
optionFetchSchedule time.Duration
// optionCacheTimeout time.Duration
}
func NewCmdModbus(logLevel string) *CmdModbus {
var ret *CmdModbus
for range Only.Once {
if logLevel == "" {
logLevel = cmdLog.LogLevelInfoStr
}
ret = &CmdModbus{
CmdDefault: CmdDefault{
Error: nil,
cmd: nil,
SelfCmd: nil,
},
log: cmdLog.New(logLevel),
optionSleepDelay: time.Second * 40, // Takes up to 40 seconds for data to come in.
optionFetchSchedule: time.Minute * 5,
// previous: make(map[string]*api.DataEntries, 0),
}
}
return ret
}
func (c *CmdModbus) AttachCommand(cmd *cobra.Command) *cobra.Command {
for range Only.Once {
if cmd == nil {
break
}
c.cmd = cmd
// ******************************************************************************** //
var cmdRoot = &cobra.Command{
Use: "modbus",
Aliases: []string{""},
Annotations: map[string]string{"group": "ModBus"},
Short: "Connect directly to a Sungrow inverter via Modbus.",
Long: "Connect directly to a Sungrow inverter via Modbus.",
DisableFlagParsing: false,
DisableFlagsInUseLine: false,
PreRunE: nil,
RunE: c.CmdModbus,
Args: cobra.MinimumNArgs(1),
}
cmd.AddCommand(cmdRoot)
cmdRoot.Example = cmdHelp.PrintExamples(cmdRoot, "run", "sync")
// ******************************************************************************** //
var cmdRootGet = &cobra.Command{
Use: "get <address> [quantity] [type]",
Aliases: []string{""},
Annotations: map[string]string{"group": "MQTT"},
Short: "Get value from a Modbus register.",
Long: "Get value from a Modbus register.",
DisableFlagParsing: false,
DisableFlagsInUseLine: false,
PreRunE: func(cmd *cobra.Command, args []string) error {
// cmds.Error = cmds.SunGrowArgs(cmd, args)
// if cmds.Error != nil {
// return cmds.Error
// }
cmds.Error = cmds.Modbus.ModbusArgs(cmd, args)
if cmds.Error != nil {
return cmds.Error
}
return nil
},
RunE: cmds.Modbus.CmdModbusGet,
Args: cobra.RangeArgs(1, 3),
}
cmdRoot.AddCommand(cmdRootGet)
cmdRootGet.Example = cmdHelp.PrintExamples(cmdRootGet, "")
// ******************************************************************************** //
var cmdRootScan = &cobra.Command{
Use: "scan <start address> <end address> [type]",
Aliases: []string{""},
Annotations: map[string]string{"group": "MQTT"},
Short: "Scan value from a Modbus register.",
Long: "Scan value from a Modbus register.",
DisableFlagParsing: false,
DisableFlagsInUseLine: false,
PreRunE: func(cmd *cobra.Command, args []string) error {
// cmds.Error = cmds.SunGrowArgs(cmd, args)
// if cmds.Error != nil {
// return cmds.Error
// }
cmds.Error = cmds.Modbus.ModbusArgs(cmd, args)
if cmds.Error != nil {
return cmds.Error
}
return nil
},
RunE: cmds.Modbus.CmdModbusScan,
Args: cobra.RangeArgs(2, 3),
}
cmdRoot.AddCommand(cmdRootScan)
cmdRootScan.Example = cmdHelp.PrintExamples(cmdRootScan, "")
}
return c.SelfCmd
}
func (c *CmdModbus) AttachFlags(cmd *cobra.Command, viper *viper.Viper) {
for range Only.Once {
cmd.PersistentFlags().StringVarP(&c.Username, flagModbusUsername, "", "", "Modbus username.")
viper.SetDefault(flagModbusUsername, "")
cmd.PersistentFlags().StringVarP(&c.Password, flagModbusPassword, "", "", "Modbus password.")
viper.SetDefault(flagModbusPassword, "")
cmd.PersistentFlags().StringVarP(&c.Host, flagModbusHost, "", "", "Modbus host.")
viper.SetDefault(flagModbusHost, "")
cmd.PersistentFlags().StringVarP(&c.Port, flagModbusPort, "", cmdModbus.DefaultPort, "Modbus port.")
viper.SetDefault(flagModbusPort, cmdModbus.DefaultPort)
}
}
func (c *CmdModbus) ModbusArgs(_ *cobra.Command, _ []string) error {
for range Only.Once {
c.Client = cmdModbus.New(cmdModbus.Modbus{
ClientId: DefaultServiceName,
Username: c.Username,
Password: c.Password,
Host: c.Host,
Port: c.Port,
LogLevel: c.log.GetLogLevel(),
})
c.Error = c.Client.GetError()
if c.Error != nil {
break
}
fmt.Println("WARNING! This feature is still in alpha state.")
c.log.Info("Connecting to Modbus...\n")
c.Error = c.Client.Connect()
if c.Error != nil {
break
}
c.Client.SetLog(c.log)
// c.log.Info("Connecting to SunGrow...\n")
// c.Client.SungrowDevices, c.Error = cmds.Api.SunGrow.GetDeviceList()
// if c.Error != nil {
// break
// }
}
return c.Error
}
func (c *CmdModbus) CmdModbus(cmd *cobra.Command, _ []string) error {
return cmd.Help()
}
func (c *CmdModbus) CmdModbusGet(_ *cobra.Command, args []string) error {
for range Only.Once {
args = MinimumArraySize(3, args)
var address cmdModbus.Address
address, c.Error = cmdModbus.StringToAddress(args[0])
if c.Error != nil {
break
}
var quantity cmdModbus.Quantity
quantity, c.Error = cmdModbus.StringToQuantity(args[1])
if c.Error != nil {
quantity = 1
c.Error = nil
}
if quantity == 0 {
quantity = 1
}
c.log.Info("Get Modbus value from %v\n", address)
fmt.Println("################################################################################")
fmt.Println("# Read Input Register #")
retI := c.Client.Read(address, quantity, args[2])
if c.Error != nil {
break
}
fmt.Printf("%s\n", retI)
fmt.Println("################################################################################")
fmt.Println("# Read Holding Register #")
retI = c.Client.ReadHolding(address, quantity, args[2])
if c.Error != nil {
break
}
fmt.Printf("%s\n", retI)
fmt.Println("################################################################################")
fmt.Println("# Read Coil #")
retB := c.Client.ReadBool(address, quantity)
if c.Error != nil {
break
}
fmt.Printf("%s\n", retB)
fmt.Println("################################################################################")
fmt.Println("# Read Discrete Input #")
retB = c.Client.ReadDiscreteInput(address, quantity)
if c.Error != nil {
break
}
fmt.Printf("%s\n", retB)
}
return c.Error
}
func (c *CmdModbus) CmdModbusScan(_ *cobra.Command, args []string) error {
for range Only.Once {
args = MinimumArraySize(3, args)
var start cmdModbus.Address
start, c.Error = cmdModbus.StringToAddress(args[0])
if c.Error != nil {
break
}
var end cmdModbus.Address
end, c.Error = cmdModbus.StringToAddress(args[1])
if c.Error != nil {
c.Error = nil
end = 0
}
if end == 0 {
end = start + 100
}
size := args[2]
if size == "" {
size = cmdModbus.TypeUnsigned8Bit
}
c.log.Info("Scanning Modbus addresses from %v to %v\n", start, end)
limit := cmdModbus.Quantity(100) // 123
var quantity cmdModbus.Quantity
fmt.Println("################################################################################")
fmt.Println("# Read Input Register #")
for address := start; address < end; {
quantity = cmdModbus.Quantity(end - address)
if quantity > limit {
quantity = limit
}
ret := c.Client.Read(address, quantity, size)
if c.Client.IsError() {
address++
continue
}
fmt.Printf("# Address[%d]: #\n%s", address, ret)
address += cmdModbus.Address(quantity)
}
fmt.Println("################################################################################")
fmt.Println("# Read Holding Register #")
for address := start; address < end; {
quantity = cmdModbus.Quantity(end - start)
if quantity > limit {
quantity = limit
}
ret := c.Client.ReadHolding(address, quantity, size)
if c.Client.IsError() {
address++
continue
}
fmt.Printf("# Address[%d]: #\n%s", address, ret)
address += cmdModbus.Address(quantity)
}
// fmt.Println("################################################################################")
// fmt.Println("# Read Coil #")
// for address := start; address < end; {
// quantity = cmdModbus.Quantity(end - start)
// if quantity > limit {
// quantity = limit
// }
//
// ret := c.Client.ReadBool(address, quantity)
// if c.Client.IsError() {
// address++
// continue
// }
// fmt.Printf("# Address[%d]: #\n%s", address, ret)
// address += cmdModbus.Address(quantity)
// }
//
// fmt.Println("################################################################################")
// fmt.Println("# Read Discrete Input #")
// for address := start; address < end; {
// quantity = cmdModbus.Quantity(end - start)
// if quantity > limit {
// quantity = limit
// }
//
// ret := c.Client.ReadDiscreteInput(address, quantity)
// if c.Client.IsError() {
// address++
// continue
// }
// fmt.Printf("# Address[%d]: #\n%s", address, ret)
// address += cmdModbus.Address(quantity)
// }
}
return c.Error
}