@@ -23,6 +23,7 @@ const (
23
23
OnlineStatus = "online"
24
24
OfflineStatus = "offline"
25
25
TypeReportEvent = "report"
26
+ KeySysExtConf = "BAETYL_SYSTEM_EXT_CONF"
26
27
)
27
28
28
29
var (
@@ -31,11 +32,36 @@ var (
31
32
ErrResponseChannelNotExist = errors .New ("response channel not exist" )
32
33
)
33
34
34
- type DevicePropConfigs struct {
35
- Name string `yaml:"name,omitempty" json:"name,omitempty"`
36
- PropConfigs map [string ]string `yaml:"propConfigs,omitempty" json:"propConfigs,omitempty"`
35
+ type DeviceProperty struct {
36
+ Name string `json:"name,omitempty"`
37
+ Type string `json:"type,omitempty" validate:"regexp=^(int16|int32|int64|float32|float64|string|bool)?$"`
38
+ Mode string `json:"mode,omitempty" validate:"regexp=^(ro|rw)?$"`
39
+ Visitor PropertyVisitor `json:"visitor,omitempty"`
37
40
}
38
41
42
+ type PropertyVisitor struct {
43
+ Modbus * ModbusVisitor `json:"modbus,omitempty"`
44
+ Opcua * OpcuaVisitor `json:"opcua,omitempty"`
45
+ Custom * CustomVisitor `json:"custom,omitempty"`
46
+ }
47
+
48
+ type ModbusVisitor struct {
49
+ Function byte `json:"function" validate:"min=1,max=4"`
50
+ Address string `json:"address"`
51
+ Quantity uint16 `json:"quantity"`
52
+ Type string `json:"type,omitempty" validate:"regexp=^(int16|int32|int64|float32|float64|string|bool)?$"`
53
+ Scale float64 `json:"scale"`
54
+ SwapByte bool `json:"swapByte"`
55
+ SwapRegister bool `json:"swapRegister"`
56
+ }
57
+
58
+ type OpcuaVisitor struct {
59
+ NodeID string `json:"nodeid,omitempty"`
60
+ Type string `json:"type,omitempty" validate:"regexp=^(int16|int32|int64|float32|float64|string|bool)?$"`
61
+ }
62
+
63
+ type CustomVisitor string
64
+
39
65
type Event struct {
40
66
Type string `yaml:"type,omitempty" json:"type,omitempty"`
41
67
Payload interface {} `yaml:"payload,omitempty" json:"payload,omitempty"`
@@ -62,7 +88,7 @@ type Context interface {
62
88
Online (info * DeviceInfo ) error
63
89
Offline (info * DeviceInfo ) error
64
90
GetDeviceAccessConfig () (string , error )
65
- GetDevicePropConfigs () (map [string ]DevicePropConfigs , error )
91
+ GetDevicePropConfigs () (map [string ][] DeviceProperty , error )
66
92
Start ()
67
93
io.Closer
68
94
}
@@ -75,7 +101,7 @@ type DmCtx struct {
75
101
eventCb EventCallback
76
102
deltaCb DeltaCallback
77
103
response sync.Map
78
- msgs map [string ]chan * v1.Message
104
+ msgChs map [string ]chan * v1.Message
79
105
}
80
106
81
107
func NewContext (confFile string ) Context {
@@ -98,7 +124,7 @@ func NewContext(confFile string) Context {
98
124
c .log .Error ("failed to load system config, to use default config" , log .Error (err ))
99
125
utils .UnmarshalYAML (nil , sc )
100
126
}
101
- c .Store (context . KeySysConf , sc )
127
+ c .Store (KeySysExtConf , sc )
102
128
103
129
var subs []mqtt2.QOSTopic
104
130
for _ , dev := range sc .Devices {
@@ -109,8 +135,8 @@ func NewContext(confFile string) Context {
109
135
c .log .Warn ("fail to create system broker client" , log .Any ("error" , err ))
110
136
}
111
137
c .mqtt = mqtt
112
- c .msgs = make (map [string ]chan * v1.Message , 1024 )
113
- if err := c .mqtt .Start (newObserver (c .msgs , c .log )); err != nil {
138
+ c .msgChs = make (map [string ]chan * v1.Message )
139
+ if err := c .mqtt .Start (newObserver (c .msgChs , c .log )); err != nil {
114
140
c .log .Warn ("fail to start mqtt client" , log .Any ("error" , err ))
115
141
}
116
142
return c
@@ -119,8 +145,8 @@ func NewContext(confFile string) Context {
119
145
func (c * DmCtx ) Start () {
120
146
devices := c .SystemConfigExt ().Devices
121
147
for _ , dev := range devices {
122
- c .msgs [dev .Name ] = make (chan * v1.Message )
123
- go c .processing (c .msgs [dev .Name ])
148
+ c .msgChs [dev .Name ] = make (chan * v1.Message , 1024 )
149
+ go c .processing (c .msgChs [dev .Name ])
124
150
}
125
151
}
126
152
@@ -222,7 +248,7 @@ func (c *DmCtx) processing(ch chan *v1.Message) {
222
248
}
223
249
224
250
func (c * DmCtx ) SystemConfigExt () * SystemConfig {
225
- v , ok := c .Load (context . KeySysConf )
251
+ v , ok := c .Load (KeySysExtConf )
226
252
if ! ok {
227
253
return nil
228
254
}
@@ -343,8 +369,8 @@ func (c *DmCtx) GetDeviceAccessConfig() (string, error) {
343
369
return string (res ), nil
344
370
}
345
371
346
- func (c * DmCtx ) GetDevicePropConfigs () (map [string ]DevicePropConfigs , error ) {
347
- var res map [string ]DevicePropConfigs
372
+ func (c * DmCtx ) GetDevicePropConfigs () (map [string ][] DeviceProperty , error ) {
373
+ var res map [string ][] DeviceProperty
348
374
if err := c .LoadCustomConfig (& res , DefaultPropsConf ); err != nil {
349
375
return nil , errors .Trace (err )
350
376
}
0 commit comments