【feat】添加设备影子

This commit is contained in:
XM-GO
2023-09-21 15:57:47 +08:00
parent 672f6361f1
commit 3ac9354573
2 changed files with 60 additions and 24 deletions

View File

@@ -2,21 +2,41 @@ package shadow
import "time"
const (
PointAttributesType = "Attributes"
PointTelemetryType = "Telemetry"
)
// Device 设备结构
type Device struct {
Name string // 设备名称
ProductName string // 设备模型名称
Points map[string]any // 设备点位列表 key参数名 value 值
online bool // 在线状态
disconnectTimes int // 断开连接次数60秒内超过3次判定离线
updatedAt time.Time // 更新时间
Name string // 设备名称
ProductName string // 设备模型名称
AttributesPoints map[string]DevicePoint // 设备属性点位列表 key 作为属性
TelemetryPoints map[string]DevicePoint // 设备遥测点位列表 key 作为属性
online bool // 在线状态
disconnectTimes int // 断开连接次数60秒内超过3次判定离线
updatedAt time.Time // 更新时间
}
func NewDevice(deviceName string, productName string, points map[string]any) Device {
// DevicePoint 设备点位结构
type DevicePoint struct {
Name string // 点位名称
Value interface{} // 点位值
}
func NewDevice(deviceName string, productName string, attributes, telemetry map[string]DevicePoint) Device {
return Device{
Name: deviceName,
ProductName: productName,
Points: points,
online: true,
Name: deviceName,
ProductName: productName,
AttributesPoints: attributes,
TelemetryPoints: telemetry,
online: true,
}
}
func NewDevicePoint(pointName string, value interface{}) DevicePoint {
return DevicePoint{
Name: pointName,
Value: value,
}
}

View File

@@ -18,9 +18,9 @@ type DeviceShadow interface {
AddDevice(device Device) (err error)
GetDevice(deviceName string) (device Device, err error)
SetDevicePoint(deviceName, pointName string, value interface{}) (err error)
GetDevicePoint(deviceName, pointName string) (value interface{}, err error)
GetDevicePoints(deviceName string) (points map[string]any, err error)
SetDevicePoint(deviceName, pointType, pointName string, value interface{}) (err error)
GetDevicePoint(deviceName, pointType, pointName string) (value DevicePoint, err error)
GetDevicePoints(deviceName, pointType string) (points map[string]DevicePoint, err error)
GetDeviceUpdateAt(deviceName string) (time.Time, error)
@@ -78,39 +78,55 @@ func (d *deviceShadow) GetDevice(deviceName string) (device Device, err error) {
}
}
func (d *deviceShadow) SetDevicePoint(deviceName, pointName string, value interface{}) (err error) {
func (d *deviceShadow) SetDevicePoint(deviceName, pointType, pointName string, value interface{}) (err error) {
deviceAny, ok := d.m.Load(deviceName)
if !ok {
return UnknownDeviceErr
}
device := deviceAny.(Device)
if device.Points == nil {
device.Points = make(map[string]any)
}
// update point value
device.updatedAt = time.Now()
device.disconnectTimes = 0
device.Points[pointName] = value
if pointType == PointAttributesType {
device.AttributesPoints[pointName] = NewDevicePoint(pointName, value)
} else if pointType == PointTelemetryType {
device.TelemetryPoints[pointName] = NewDevicePoint(pointName, value)
} else {
return errors.New("设备属性类型错误")
}
// update
d.m.Store(deviceName, device)
return
}
func (d *deviceShadow) GetDevicePoint(deviceName, pointName string) (value interface{}, err error) {
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 {
return
}
return device.Points[pointName], nil
if pointType == PointAttributesType {
return device.AttributesPoints[pointName], nil
} else if pointType == PointTelemetryType {
return device.TelemetryPoints[pointName], nil
} else {
return value, errors.New("设备属性类型错误")
}
} else {
return nil, UnknownDeviceErr
return value, UnknownDeviceErr
}
}
func (d *deviceShadow) GetDevicePoints(deviceName string) (points map[string]any, err error) {
func (d *deviceShadow) GetDevicePoints(deviceName, pointType string) (points map[string]DevicePoint, err error) {
if deviceAny, ok := d.m.Load(deviceName); ok {
return deviceAny.(Device).Points, nil
if pointType == PointAttributesType {
return deviceAny.(Device).AttributesPoints, nil
} else if pointType == PointTelemetryType {
return deviceAny.(Device).TelemetryPoints, nil
} else {
return points, errors.New("设备属性类型错误")
}
} else {
return nil, UnknownDeviceErr
}