Skip to content

Commit

Permalink
refactor(初始化): 优化插件启动逻辑
Browse files Browse the repository at this point in the history
- 优化插件启动逻辑,考虑重启时设备影子数据留存
  • Loading branch information
xcocx committed Jun 4, 2024
1 parent 558e05f commit fffa460
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions internal/bootstrap/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,19 @@ func initDeviceDriver(configMap map[string]config.Config) {
// 初始化影子服务
func initDeviceShadow(configMap map[string]config.Config) {
// 设置影子服务设备生命周期
helper.DeviceShadow = shadow.NewDeviceShadow()
// 设置回调
helper.DeviceShadow.SetOnlineChangeCallback(func(deviceId string, online bool) {
if online {
helper.Logger.Info("device online", zap.String("deviceId", deviceId))
} else {
helper.Logger.Warn("device offline...", zap.String("deviceId", deviceId))
}
//触发设备在离线事件
helper.TriggerEvents(event.EventCodeDeviceStatus, deviceId, online)
})
if helper.DeviceShadow == nil {
helper.DeviceShadow = shadow.NewDeviceShadow()
// 设置回调
helper.DeviceShadow.SetOnlineChangeCallback(func(deviceId string, online bool) {
if online {
helper.Logger.Info("device online", zap.String("deviceId", deviceId))
} else {
helper.Logger.Warn("device offline...", zap.String("deviceId", deviceId))
}
//触发设备在离线事件
helper.TriggerEvents(event.EventCodeDeviceStatus, deviceId, online)
})
}
// 添加设备
for _, c := range configMap {
for _, model := range c.DeviceModels {
Expand All @@ -135,6 +137,12 @@ func initDeviceShadow(configMap map[string]config.Config) {
if c.ProtocolName == "virtual" {
d.Ttl = "8760h"
}
// 设备存在校验
if helper.DeviceShadow.HasDevice(d.ID) {
helper.Logger.Warn("device already exist", zap.String("deviceId", d.ID))
continue
}
// 添加设备
dev := shadow.NewDevice(d, model.Name, nil)
_ = helper.DeviceShadow.AddDevice(dev)
}
Expand Down

0 comments on commit fffa460

Please sign in to comment.