-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensor.go
185 lines (162 loc) · 5.29 KB
/
sensor.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
package modbus_tcp
import (
"context"
"errors"
"fmt"
"strings"
"sync"
"time"
"github.com/goburrow/modbus"
"go.viam.com/rdk/components/sensor"
"go.viam.com/rdk/logging"
"go.viam.com/rdk/resource"
)
var errUnimplemented = errors.New("unimplemented")
var Model = resource.NewModel("bill", "advantech-wise-4050", "modbus")
var PrettyName = "WISE-4050 4DI/4DO 2.4G WiFi IoT Wireless I/O Module"
var Description = "WISE-4000 series is an Ethernet-based wired or wireless IoT device, which inte- grated with IoT data acquisition, processing, and publishing functions."
type mySensor struct {
resource.Named
logger logging.Logger
readings string // the value to be returned by the sensor.readings() method
mu sync.RWMutex
cancelCtx context.Context
cancelFunc func()
monitor func()
done chan bool
wg sync.WaitGroup
sensorConfig *CloudConfig
}
func init() {
resource.RegisterComponent(
sensor.API,
Model,
resource.Registration[sensor.Sensor, *CloudConfig]{Constructor: NewSensor})
}
func NewSensor(ctx context.Context, deps resource.Dependencies, conf resource.Config, logger logging.Logger) (sensor.Sensor, error) {
logger.Infof("Starting %s %s", PrettyName)
s := &mySensor{
Named: conf.ResourceName().AsNamed(),
logger: logger,
}
if err := s.Reconfigure(ctx, deps, conf); err != nil {
return nil, err
}
return s, nil
}
func (s *mySensor) Reconfigure(ctx context.Context, deps resource.Dependencies, conf resource.Config) error {
var err error
s.mu.Lock()
defer s.mu.Unlock()
s.sensorConfig, err = resource.NativeConfig[*CloudConfig](conf)
if err != nil {
return err
}
s.logger.Debugf("Reconfiguring %s", PrettyName)
return err
}
// Get sensor reading
func (s *mySensor) Readings(ctx context.Context, _ map[string]interface{}) (map[string]interface{}, error) {
readings, err := s.RunModbusTCP()
if err != nil {
return nil, err
}
return readings, nil
}
// DoCommand can be implemented to extend sensor functionality but returns unimplemented in this example.
func (s *mySensor) DoCommand(ctx context.Context, cmd map[string]interface{}) (map[string]interface{}, error) {
fmt.Printf("DOING!")
// Connect to the Modbus device
handler := modbus.NewTCPClientHandler(fmt.Sprintf("%s:%s", s.sensorConfig.DeviceAddress, s.sensorConfig.Port))
handler.Timeout = 10 * time.Second
handler.SlaveId = 1
err := handler.Connect()
defer handler.Close()
if err != nil {
fmt.Printf("Failed to connect: %v\n", err)
return nil, err
}
fmt.Printf("DOING!")
client := modbus.NewClient(handler)
// Iterate through the command map and write to each specified coil
for i := 1; i <= s.sensorConfig.DO.Length; i++ {
coilKey := fmt.Sprintf("coil%d", i)
if coilValue, ok := cmd[coilKey]; ok {
coilAddr := uint16(s.sensorConfig.DO.BaseAddress + i - 1)
var value uint16
if coilValue.(bool) {
value = 0xFF00 // ON value for Modbus coil
} else {
value = 0x0000 // OFF value for Modbus coil
}
_, err := client.WriteSingleCoil(coilAddr, value)
if err != nil {
return nil, err
}
}
}
return map[string]interface{}{"status": "success"}, nil
}
// The close method is executed when the component is shut down
func (s *mySensor) Close(ctx context.Context) error {
s.logger.Infof("Shutting down %s", PrettyName)
return nil
}
func (s *mySensor) RunModbusTCP() (map[string]interface{}, error) {
// Connect to the Modbus device
handler := modbus.NewTCPClientHandler(fmt.Sprintf("%s:%s", s.sensorConfig.DeviceAddress, s.sensorConfig.Port)) // Replace with your device IP and port
handler.Timeout = 10 * time.Second
handler.SlaveId = 1 // Set the Slave ID
err := handler.Connect()
defer handler.Close()
if err != nil {
fmt.Printf("Failed to connect: %v\n", err)
return nil, err
}
client := modbus.NewClient(handler)
result := make(map[string]interface{})
// Read DI Status from the correct address
diCoilStates, err := readCoilStates(client, s.sensorConfig.DI.BaseAddress, s.sensorConfig.DI.Length) // Adjust starting address and length
if err != nil {
return nil, err
}
fmt.Printf(strings.Join(diCoilStates, ", "))
result["inputCoils"] = strings.Join(diCoilStates, ", ")
// Read DO Status from the correct address
doCoilStates, err := readCoilStates(client, s.sensorConfig.DO.BaseAddress, s.sensorConfig.DO.Length) // Adjust starting address and length
if err != nil {
return nil, err
}
fmt.Printf(strings.Join(doCoilStates, ", "))
result["outputCoils"] = strings.Join(doCoilStates, ", ")
return result, nil
}
func readCoilStates(client modbus.Client, startAddr, numCoils int) ([]string, error) {
uStartAddr := uint16(startAddr)
uNumCoils := uint16(numCoils)
results, err := client.ReadCoils(uStartAddr, uNumCoils)
if err != nil {
fmt.Printf("Failed to read: %v\n", err)
return nil, err
}
return decodeCoilValues(results, numCoils), nil
}
func decodeCoilValues(byteVal []byte, numCoils int) []string {
coilStates := make([]string, 0)
coilsProcessed := 0
for i, val := range byteVal {
for bit := 0; bit < 8; bit++ {
if coilsProcessed >= numCoils {
break // Exit if we've read all the coils we need
}
coilState := val&(1<<bit) != 0
stateStr := "False"
if coilState {
stateStr = "True"
}
coilStates = append(coilStates, fmt.Sprintf("Coil %d: %s", i*8+bit+1, stateStr))
coilsProcessed++
}
}
return coilStates
}