Skip to content

Commit 170bd56

Browse files
author
xiejingru
authored
bugfix of event process (#231)
1 parent c5e8c07 commit 170bd56

File tree

4 files changed

+91
-17
lines changed

4 files changed

+91
-17
lines changed

dmcontext/context.go

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const (
2323
OnlineStatus = "online"
2424
OfflineStatus = "offline"
2525
TypeReportEvent = "report"
26+
KeySysExtConf = "BAETYL_SYSTEM_EXT_CONF"
2627
)
2728

2829
var (
@@ -31,11 +32,36 @@ var (
3132
ErrResponseChannelNotExist = errors.New("response channel not exist")
3233
)
3334

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"`
3740
}
3841

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+
3965
type Event struct {
4066
Type string `yaml:"type,omitempty" json:"type,omitempty"`
4167
Payload interface{} `yaml:"payload,omitempty" json:"payload,omitempty"`
@@ -62,7 +88,7 @@ type Context interface {
6288
Online(info *DeviceInfo) error
6389
Offline(info *DeviceInfo) error
6490
GetDeviceAccessConfig() (string, error)
65-
GetDevicePropConfigs() (map[string]DevicePropConfigs, error)
91+
GetDevicePropConfigs() (map[string][]DeviceProperty, error)
6692
Start()
6793
io.Closer
6894
}
@@ -75,7 +101,7 @@ type DmCtx struct {
75101
eventCb EventCallback
76102
deltaCb DeltaCallback
77103
response sync.Map
78-
msgs map[string]chan *v1.Message
104+
msgChs map[string]chan *v1.Message
79105
}
80106

81107
func NewContext(confFile string) Context {
@@ -98,7 +124,7 @@ func NewContext(confFile string) Context {
98124
c.log.Error("failed to load system config, to use default config", log.Error(err))
99125
utils.UnmarshalYAML(nil, sc)
100126
}
101-
c.Store(context.KeySysConf, sc)
127+
c.Store(KeySysExtConf, sc)
102128

103129
var subs []mqtt2.QOSTopic
104130
for _, dev := range sc.Devices {
@@ -109,8 +135,8 @@ func NewContext(confFile string) Context {
109135
c.log.Warn("fail to create system broker client", log.Any("error", err))
110136
}
111137
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 {
114140
c.log.Warn("fail to start mqtt client", log.Any("error", err))
115141
}
116142
return c
@@ -119,8 +145,8 @@ func NewContext(confFile string) Context {
119145
func (c *DmCtx) Start() {
120146
devices := c.SystemConfigExt().Devices
121147
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])
124150
}
125151
}
126152

@@ -222,7 +248,7 @@ func (c *DmCtx) processing(ch chan *v1.Message) {
222248
}
223249

224250
func (c *DmCtx) SystemConfigExt() *SystemConfig {
225-
v, ok := c.Load(context.KeySysConf)
251+
v, ok := c.Load(KeySysExtConf)
226252
if !ok {
227253
return nil
228254
}
@@ -343,8 +369,8 @@ func (c *DmCtx) GetDeviceAccessConfig() (string, error) {
343369
return string(res), nil
344370
}
345371

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
348374
if err := c.LoadCustomConfig(&res, DefaultPropsConf); err != nil {
349375
return nil, errors.Trace(err)
350376
}

dmcontext/observer.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ const (
1818

1919
type observer struct {
2020
log *log.Logger
21-
msgs map[string]chan *v1.Message
21+
msgChs map[string]chan *v1.Message
2222
}
2323

24-
func newObserver(msgs map[string]chan *v1.Message, log *log.Logger) mqtt.Observer {
25-
return &observer{msgs: msgs, log: log}
24+
func newObserver(msgChs map[string]chan *v1.Message, log *log.Logger) mqtt.Observer {
25+
return &observer{msgChs: msgChs, log: log}
2626
}
2727

2828
func ParseTopic(topic string) (string, error) {
@@ -49,7 +49,7 @@ func (o *observer) OnPublish(pkt *packet.Publish) error {
4949
log.Any("payload", string(pkt.Message.Payload)))
5050
return nil
5151
}
52-
if ch, ok := o.msgs[device]; ok {
52+
if ch, ok := o.msgChs[device]; ok {
5353
select {
5454
case ch <- &msg:
5555
default:

spec/v1/node.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,49 @@ func patch(doc, delta map[string]interface{}) (map[string]interface{}, error) {
154154
return newDoc, nil
155155
}
156156

157+
func getDeviceInfos(data map[string]interface{}) []DeviceInfo {
158+
if data == nil {
159+
return nil
160+
}
161+
devs, ok := data[KeyDevices]
162+
if !ok || devs == nil {
163+
return nil
164+
}
165+
res, ok := devs.([]DeviceInfo)
166+
if ok {
167+
return res
168+
}
169+
res = []DeviceInfo{}
170+
dis, ok := devs.([]interface{})
171+
if !ok {
172+
return nil
173+
}
174+
for _, di := range dis {
175+
dim := di.(map[string]interface{})
176+
if dim == nil {
177+
return nil
178+
}
179+
res = append(res, DeviceInfo{Name: dim["name"].(string), Version: dim["version"].(string)})
180+
}
181+
return res
182+
}
183+
184+
func (r Report) DeviceInfos() []DeviceInfo {
185+
return getDeviceInfos(r)
186+
}
187+
188+
func (r Report) SetDeviceInfos(devs []DeviceInfo) {
189+
r[KeyDevices] = devs
190+
}
191+
192+
func (d Desire) DeviceInfos() []DeviceInfo {
193+
return getDeviceInfos(d)
194+
}
195+
196+
func (d Desire) SetDeviceInfos(devs []DeviceInfo) {
197+
d[KeyDevices] = devs
198+
}
199+
157200
func (r Report) AppInfos(isSys bool) []AppInfo {
158201
if isSys {
159202
return getAppInfos(KeySysApps, r)

spec/v1/sync_report.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ type NodeStats struct {
4141
Extension interface{} `yaml:"extension,omitempty" json:"extension,omitempty"`
4242
}
4343

44+
type DeviceInfo struct {
45+
Name string `yaml:"name,omitempty" json:"name,omitempty"`
46+
Version string `yaml:"version,omitempty" json:"version,omitempty"`
47+
}
48+
4449
// AppInfo app info
4550
type AppInfo struct {
4651
Name string `yaml:"name,omitempty" json:"name,omitempty"`

0 commit comments

Comments
 (0)