From fffa4605cc0b7d6c9363d0c8f0019827248f4bf6 Mon Sep 17 00:00:00 2001 From: xiaocai <1643013940@qq.com> Date: Tue, 4 Jun 2024 11:15:52 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E5=88=9D=E5=A7=8B=E5=8C=96):=20?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=8F=92=E4=BB=B6=E5=90=AF=E5=8A=A8=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 优化插件启动逻辑,考虑重启时设备影子数据留存 --- internal/bootstrap/plugin.go | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/internal/bootstrap/plugin.go b/internal/bootstrap/plugin.go index 85bb365..4f6a93d 100644 --- a/internal/bootstrap/plugin.go +++ b/internal/bootstrap/plugin.go @@ -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 { @@ -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) }