From 6281a308819091897a798e74f1d0c84ec8a99189 Mon Sep 17 00:00:00 2001 From: lixxxww <941403820@qq.com> Date: Mon, 22 Jan 2024 07:55:01 +0000 Subject: [PATCH] =?UTF-8?q?=E8=AE=BE=E5=A4=87/=E5=BD=B1=E5=AD=90=E8=AE=BE?= =?UTF-8?q?=E5=A4=87=E9=80=BB=E8=BE=91=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lixxxww <941403820@qq.com> --- apps/device/api/device.go | 19 ++-- iothub/hook_message_work/hook_message_work.go | 1 - pkg/shadow/shadow.go | 90 +++++++++---------- 3 files changed, 51 insertions(+), 59 deletions(-) diff --git a/apps/device/api/device.go b/apps/device/api/device.go index 7998994..5b6335e 100644 --- a/apps/device/api/device.go +++ b/apps/device/api/device.go @@ -1,16 +1,11 @@ package api -// ========================================================================== -// 生成日期:2023-06-30 09:19:43 +0800 CST -// 生成路径: apps/device/api/devices.go -// 生成人:panda -// ========================================================================== import ( "fmt" + "pandax/apps/device/util" "github.com/PandaXGO/PandaKit/biz" "github.com/PandaXGO/PandaKit/model" "github.com/PandaXGO/PandaKit/restfulx" - "pandax/apps/device/util" "pandax/pkg/global" model2 "pandax/pkg/global/model" "pandax/pkg/shadow" @@ -26,6 +21,7 @@ type DeviceApi struct { DeviceAlarmApp services.DeviceAlarmModel ProductApp services.ProductModel ProductTemplateApp services.ProductTemplateModel + DeviceShadow shadow.DeviceShadow // 添加设备影子 } func (p *DeviceApi) GetDevicePanel(rc *restfulx.ReqCtx) { @@ -96,13 +92,12 @@ func (p *DeviceApi) GetDeviceStatus(rc *restfulx.ReqCtx) { biz.ErrIsNil(err, "查询设备模板失败") // 从设备影子中读取 res := make([]entity.DeviceStatusVo, 0) - getDevice := shadow.InitDeviceShadow(device.Name, device.Pid) rs := make(map[string]shadow.DevicePoint) if classify == global.TslAttributesType { - rs = getDevice.AttributesPoints + rs, _ = p.DeviceShadow.GetDevicePoints(device.Name, global.TslAttributesType) // 使用GetDevicePoints()函数 } if classify == global.TslTelemetryType { - rs = getDevice.TelemetryPoints + rs, _ = p.DeviceShadow.GetDevicePoints(device.Name, global.TslTelemetryType) // 使用GetDevicePoints()函数 } for _, tel := range *template { sdv := entity.DeviceStatusVo{ @@ -112,10 +107,10 @@ func (p *DeviceApi) GetDeviceStatus(rc *restfulx.ReqCtx) { Define: tel.Define, } // 有直接从设备影子中查询,没有查询时序数据库最后一条记录 - if _, ok := rs[tel.Key]; ok { + if point, ok := rs[tel.Key]; ok { if classify == global.TslTelemetryType { - value := rs[tel.Key].Value - sdv.Time = rs[tel.Key].UpdatedAt + value := point.Value + sdv.Time = point.UpdatedAt sdv.Value = value } if classify == global.TslAttributesType { diff --git a/iothub/hook_message_work/hook_message_work.go b/iothub/hook_message_work/hook_message_work.go index 87f56c3..643c86d 100644 --- a/iothub/hook_message_work/hook_message_work.go +++ b/iothub/hook_message_work/hook_message_work.go @@ -69,7 +69,6 @@ func (s *HookService) handleOne(msg *netbase.DeviceEventInfo) { case message.DisConnectMes, message.ConnectMes: //检测设备影子并修改设备影子状态 if msg.Type == message.ConnectMes { - shadow.InitDeviceShadow(msg.DeviceAuth.Name, msg.DeviceAuth.ProductId) shadow.DeviceShadowInstance.SetOnline(msg.DeviceAuth.Name) } else { shadow.DeviceShadowInstance.SetOffline(msg.DeviceAuth.Name) diff --git a/pkg/shadow/shadow.go b/pkg/shadow/shadow.go index ea98ab1..d46dce0 100644 --- a/pkg/shadow/shadow.go +++ b/pkg/shadow/shadow.go @@ -57,18 +57,6 @@ func init() { DeviceShadowInstance = shadow } -func InitDeviceShadow(deviceName, ProductId string) Device { - device, err := DeviceShadowInstance.GetDevice(deviceName) - if err == UnknownDeviceErr { - attributes := make(map[string]DevicePoint) - telemetry := make(map[string]DevicePoint) - device = NewDevice(deviceName, ProductId, attributes, telemetry) - DeviceShadowInstance.AddDevice(device) - //shadow.DeviceShadowInstance.SetDeviceTTL() - } - return device -} - func (d *deviceShadow) AddDevice(device Device) (err error) { if _, ok := d.m.Load(device.Name); ok { return DeviceRepeatErr @@ -82,6 +70,7 @@ func (d *deviceShadow) AddDevice(device Device) (err error) { func (d *deviceShadow) SetDeviceTTL(ttl int) { d.ttl = ttl } + func (d *deviceShadow) GetDevice(deviceName string) (device Device, err error) { if deviceAny, ok := d.m.Load(deviceName); ok { return deviceAny.(Device), nil @@ -99,29 +88,33 @@ func (d *deviceShadow) SetDevicePoint(deviceName, pointType, pointName string, v // update point value device.updatedAt = time.Now() - if pointType == global.TslAttributesType { + switch pointType { + case global.TslAttributesType: device.AttributesPoints[pointName] = NewDevicePoint(pointName, value) - } else if pointType == global.TslTelemetryType { + case global.TslTelemetryType: device.TelemetryPoints[pointName] = NewDevicePoint(pointName, value) - } else { + default: return errors.New("设备属性类型错误") } + // update d.m.Store(deviceName, device) return } + func (d *deviceShadow) GetDevicePoint(deviceName, pointType, pointName string) (value DevicePoint, err error) { if deviceAny, ok := d.m.Load(deviceName); ok { device := deviceAny.(Device) - if device.online == false || time.Now().Sub(device.updatedAt) > time.Duration(d.ttl)*time.Second { + if !device.online || time.Since(device.updatedAt) > time.Duration(d.ttl)*time.Second { return } - if pointType == global.TslAttributesType { + switch pointType { + case global.TslAttributesType: return device.AttributesPoints[pointName], nil - } else if pointType == global.TslTelemetryType { + case global.TslTelemetryType: return device.TelemetryPoints[pointName], nil - } else { + default: return value, errors.New("设备属性类型错误") } } else { @@ -131,11 +124,13 @@ func (d *deviceShadow) GetDevicePoint(deviceName, pointType, pointName string) ( func (d *deviceShadow) GetDevicePoints(deviceName, pointType string) (points map[string]DevicePoint, err error) { if deviceAny, ok := d.m.Load(deviceName); ok { - if pointType == global.TslAttributesType { - return deviceAny.(Device).AttributesPoints, nil - } else if pointType == global.TslTelemetryType { - return deviceAny.(Device).TelemetryPoints, nil - } else { + device := deviceAny.(Device) + switch pointType { + case global.TslAttributesType: + return device.AttributesPoints, nil + case global.TslTelemetryType: + return device.TelemetryPoints, nil + default: return points, errors.New("设备属性类型错误") } } else { @@ -152,17 +147,17 @@ func (d *deviceShadow) GetDeviceUpdateAt(deviceName string) (time.Time, error) { } func (d *deviceShadow) changeOnOff(deviceName string, online bool) (err error) { - if deviceAny, ok := d.m.Load(deviceName); ok { - device := deviceAny.(Device) - if device.online != online { - device.online = online - device.updatedAt = time.Now() - d.m.Store(deviceName, device) - d.handlerCallback(deviceName, online) - } - } else { + deviceAny, ok := d.m.Load(deviceName) + if !ok { return UnknownDeviceErr } + device := deviceAny.(Device) + if device.online != online { + device.online = online + device.updatedAt = time.Now() + d.m.Store(deviceName, device) + d.handlerCallback(deviceName, online) + } return } @@ -193,16 +188,19 @@ func (d *deviceShadow) StopStatusListener() { func (d *deviceShadow) checkOnOff() { for range d.ticker.C { - d.m.Range(func(key, value any) bool { - if device, ok := value.(Device); ok { - // fix: when ttl == 0, device always offline - if d.ttl == 0 { - return true - } + d.m.Range(func(key, value interface{}) bool { + device, ok := value.(Device) + if !ok { + return true + } - if device.online && time.Now().Sub(device.updatedAt) > time.Duration(d.ttl)*time.Second { - _ = d.SetOffline(device.Name) - } + // fix: when ttl == 0, device always offline + if d.ttl == 0 { + return true + } + + if device.online && time.Since(device.updatedAt) > time.Duration(d.ttl)*time.Second { + _ = d.SetOffline(device.Name) } return true }) @@ -239,13 +237,13 @@ func parseOnlineBindPV(pv interface{}) (online bool, err error) { func parseStrOnlineBindPV(pv string) (online bool, err error) { onlineList := []string{"on", "online", "1", "true", "yes"} offlineList := []string{"off", "offline", "0", "false", "no"} - for i, _ := range onlineList { - if pv == onlineList[i] { + for _, v := range onlineList { + if pv == v { return true, nil } } - for i, _ := range offlineList { - if pv == offlineList[i] { + for _, v := range offlineList { + if pv == v { return false, nil } }