【feat】添加设备影子

This commit is contained in:
XM-GO
2023-09-22 15:13:57 +08:00
parent 11d7459271
commit 661246609d
5 changed files with 52 additions and 35 deletions

View File

@@ -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
}

View File

@@ -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("设备属性类型错误")