diff --git a/apps/device/api/device.go b/apps/device/api/device.go index b35ffa4..2e3f5d9 100644 --- a/apps/device/api/device.go +++ b/apps/device/api/device.go @@ -13,6 +13,7 @@ import ( "github.com/PandaXGO/PandaKit/restfulx" "pandax/pkg/global" "pandax/pkg/mqtt" + "pandax/pkg/shadow" "pandax/pkg/tool" "strings" "time" @@ -80,20 +81,30 @@ func (p *DeviceApi) GetDeviceStatus(rc *restfulx.ReqCtx) { template := p.ProductTemplateApp.FindList(entity.ProductTemplate{Classify: classify, Pid: device.Pid}) res := make([]entity.DeviceStatusVo, 0) - rs, err := global.TdDb.GetOne(fmt.Sprintf(`select * from %s_%s ORDER BY ts DESC LIMIT 1`, device.Name, classify)) - biz.ErrIsNil(err, "查询设备状态信息失败") + getDevice := shadow.InitDeviceShadow(device.Name, device.Pid) + rs := make(map[string]shadow.DevicePoint) + if classify == global.TslAttributesType { + rs = getDevice.AttributesPoints + } + if classify == global.TslTelemetryType { + rs = getDevice.TelemetryPoints + } for _, tel := range *template { sdv := entity.DeviceStatusVo{ Name: tel.Name, Key: tel.Key, Type: tel.Type, Define: tel.Define, - Time: rs["ts"], } if v, ok := rs[strings.ToLower(tel.Key)]; ok { - sdv.Value = v + sdv.Value = v.Value + sdv.Time = v.UpdatedAt } else { - sdv.Value = tel.Define["default_value"] + if classify == global.TslAttributesType { + if value, ok := tel.Define["default_value"]; ok { + sdv.Value = value + } + } } res = append(res, sdv) } diff --git a/iothub/hook_message_work.go b/iothub/hook_message_work.go index b474929..35fadd4 100644 --- a/iothub/hook_message_work.go +++ b/iothub/hook_message_work.go @@ -76,7 +76,7 @@ func (s *HookService) handleOne(msg *DeviceEventInfo) { case message.DisConnectMes, message.ConnectMes: //检测设备影子并修改设备影子状态 if msg.Type == message.ConnectMes { - InitDeviceShadow(etoken) + shadow.InitDeviceShadow(etoken.Name, etoken.ProductId) shadow.DeviceShadowInstance.SetOnline(etoken.Name) } else { shadow.DeviceShadowInstance.SetOffline(etoken.Name) @@ -149,18 +149,6 @@ func SendZtWebsocket(deviceId, message string) { } } -// InitDeviceShadow 初始化设备影子 -func InitDeviceShadow(etoken *tool.DeviceAuth) { - _, err := shadow.DeviceShadowInstance.GetDevice(etoken.Name) - if err == shadow.UnknownDeviceErr { - attributes := make(map[string]shadow.DevicePoint) - telemetry := make(map[string]shadow.DevicePoint) - newDevice := shadow.NewDevice(etoken.Name, etoken.ProductId, attributes, telemetry) - shadow.DeviceShadowInstance.AddDevice(newDevice) - //shadow.DeviceShadowInstance.SetDeviceTTL() - } -} - // SetDeviceShadow 设置设备点 func SetDeviceShadow(etoken *tool.DeviceAuth, msgVals map[string]interface{}, msgType string) { defer func() { @@ -174,11 +162,11 @@ func SetDeviceShadow(etoken *tool.DeviceAuth, msgVals map[string]interface{}, ms continue } if message.AttributesMes == msgType { - err := shadow.DeviceShadowInstance.SetDevicePoint(etoken.Name, shadow.PointAttributesType, tel.Key, msgVals[tel.Key]) + err := shadow.DeviceShadowInstance.SetDevicePoint(etoken.Name, global.TslAttributesType, tel.Key, msgVals[tel.Key]) biz.ErrIsNil(err, "设置设备影子点失败") } if message.TelemetryMes == msgType { - err := shadow.DeviceShadowInstance.SetDevicePoint(etoken.Name, shadow.PointTelemetryType, tel.Key, msgVals[tel.Key]) + err := shadow.DeviceShadowInstance.SetDevicePoint(etoken.Name, global.TslTelemetryType, tel.Key, msgVals[tel.Key]) biz.ErrIsNil(err, "设置设备影子点失败") } } diff --git a/pkg/global/const_device.go b/pkg/global/const_device.go index 499852c..915f629 100644 --- a/pkg/global/const_device.go +++ b/pkg/global/const_device.go @@ -1,5 +1,11 @@ package global +const ( + TslAttributesType = "attributes" + TslTelemetryType = "telemetry" + TslCommandsType = "commands" +) + // 告警等级 const ( CRITICAL = "CRITICAL" // 危险 diff --git a/pkg/shadow/device.go b/pkg/shadow/device.go index 7f291aa..5b4a3c5 100644 --- a/pkg/shadow/device.go +++ b/pkg/shadow/device.go @@ -1,10 +1,7 @@ package shadow -import "time" - -const ( - PointAttributesType = "Attributes" - PointTelemetryType = "Telemetry" +import ( + "time" ) // Device 设备结构 @@ -19,8 +16,9 @@ type Device struct { // DevicePoint 设备点位结构 type DevicePoint struct { - Name string // 点位名称 - Value interface{} // 点位值 + Name string // 点位名称 + Value interface{} // 点位值 + UpdatedAt time.Time } func NewDevice(deviceName string, productName string, attributes, telemetry map[string]DevicePoint) Device { @@ -35,7 +33,20 @@ func NewDevice(deviceName string, productName string, attributes, telemetry map[ func NewDevicePoint(pointName string, value interface{}) DevicePoint { return DevicePoint{ - Name: pointName, - Value: value, + Name: pointName, + Value: value, + UpdatedAt: time.Now(), } } + +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 +} diff --git a/pkg/shadow/shadow.go b/pkg/shadow/shadow.go index b325f98..52ec9ac 100644 --- a/pkg/shadow/shadow.go +++ b/pkg/shadow/shadow.go @@ -3,6 +3,7 @@ package shadow import ( "errors" "fmt" + "pandax/pkg/global" "sync" "time" ) @@ -86,9 +87,9 @@ func (d *deviceShadow) SetDevicePoint(deviceName, pointType, pointName string, v // update point value device.updatedAt = time.Now() - if pointType == PointAttributesType { + if pointType == global.TslAttributesType { device.AttributesPoints[pointName] = NewDevicePoint(pointName, value) - } else if pointType == PointTelemetryType { + } else if pointType == global.TslTelemetryType { device.TelemetryPoints[pointName] = NewDevicePoint(pointName, value) } else { return errors.New("设备属性类型错误") @@ -104,9 +105,9 @@ func (d *deviceShadow) GetDevicePoint(deviceName, pointType, pointName string) ( if device.online == false || time.Now().Sub(device.updatedAt) > time.Duration(d.ttl)*time.Second { return } - if pointType == PointAttributesType { + if pointType == global.TslAttributesType { return device.AttributesPoints[pointName], nil - } else if pointType == PointTelemetryType { + } else if pointType == global.TslTelemetryType { return device.TelemetryPoints[pointName], nil } else { return value, errors.New("设备属性类型错误") @@ -118,9 +119,9 @@ 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 == PointAttributesType { + if pointType == global.TslAttributesType { return deviceAny.(Device).AttributesPoints, nil - } else if pointType == PointTelemetryType { + } else if pointType == global.TslTelemetryType { return deviceAny.(Device).TelemetryPoints, nil } else { return points, errors.New("设备属性类型错误")