【feat】添加设备影子

This commit is contained in:
XM-GO
2023-09-21 18:00:33 +08:00
parent 3ac9354573
commit 11d7459271
12 changed files with 253 additions and 446 deletions

View File

@@ -26,6 +26,7 @@ type RpcPayload struct {
Params any `json:"params"`
}
// RequestAttributes 下发指令
func (rpc RpcRequest) RequestCmd(rpcPayload RpcPayload) (respPayload string, err error) {
topic := fmt.Sprintf(RpcReqTopic, rpc.RequestId)
payload, err := json.Marshal(rpcPayload)
@@ -66,6 +67,7 @@ func (rpc RpcRequest) RequestCmd(rpcPayload RpcPayload) (respPayload string, err
}
}
// RequestAttributes rpc 下发属性
func (rpc RpcRequest) RequestAttributes(rpcPayload RpcPayload) error {
topic := fmt.Sprintf(RpcReqTopic, rpc.RequestId)
if rpcPayload.Method == "" {
@@ -86,7 +88,8 @@ func (rpc RpcRequest) RequestAttributes(rpcPayload RpcPayload) error {
// RespondTpc 处理设备端请求服务端方法
func (rpc RpcRequest) RespondTpc(reqPayload RpcPayload) error {
topic := fmt.Sprintf(RpcRespTopic, rpc.RequestId)
// 此处处理设备的请求参数逻辑
//TODO 此处处理设备的请求参数逻辑
//自己定义请求逻辑
if reqPayload.Params == "getCurrentTime" {
unix := time.Now().Unix()
msg := fmt.Sprintf("%d", unix)

View File

@@ -14,7 +14,6 @@ type Device struct {
AttributesPoints map[string]DevicePoint // 设备属性点位列表 key 作为属性
TelemetryPoints map[string]DevicePoint // 设备遥测点位列表 key 作为属性
online bool // 在线状态
disconnectTimes int // 断开连接次数60秒内超过3次判定离线
updatedAt time.Time // 更新时间
}

View File

@@ -29,9 +29,6 @@ type DeviceShadow interface {
SetOnline(deviceName string) (err error)
SetOffline(deviceName string) (err error)
// MayBeOffline 可能离线事件60秒内超过3次判定离线
MayBeOffline(deviceName string) (err error)
SetOnlineChangeCallback(handlerFunc OnlineChangeCallback)
// StopStatusListener 停止设备状态监听
@@ -48,13 +45,15 @@ type deviceShadow struct {
ttl int
}
func NewDeviceShadow() DeviceShadow {
var DeviceShadowInstance DeviceShadow
func init() {
shadow := &deviceShadow{
m: &sync.Map{},
ticker: time.NewTicker(time.Second),
}
go shadow.checkOnOff()
return shadow
DeviceShadowInstance = shadow
}
func (d *deviceShadow) AddDevice(device Device) (err error) {
@@ -86,7 +85,6 @@ func (d *deviceShadow) SetDevicePoint(deviceName, pointType, pointName string, v
device := deviceAny.(Device)
// update point value
device.updatedAt = time.Now()
device.disconnectTimes = 0
if pointType == PointAttributesType {
device.AttributesPoints[pointName] = NewDevicePoint(pointName, value)
@@ -146,7 +144,6 @@ func (d *deviceShadow) changeOnOff(deviceName string, online bool) (err error) {
if device.online != online {
device.online = online
device.updatedAt = time.Now()
device.disconnectTimes = 0
d.m.Store(deviceName, device)
d.handlerCallback(deviceName, online)
}
@@ -177,24 +174,6 @@ func (d *deviceShadow) SetOnlineChangeCallback(handlerFunc OnlineChangeCallback)
d.handlerFunc = handlerFunc
}
func (d *deviceShadow) MayBeOffline(deviceName string) (err error) {
if deviceAny, ok := d.m.Load(deviceName); ok {
device := deviceAny.(Device)
if device.online == false {
return
}
device.disconnectTimes++
if time.Now().Sub(device.updatedAt).Seconds() > 60 && device.disconnectTimes >= 3 {
return d.SetOffline(deviceName)
}
// 更新设备信息
d.m.Store(deviceName, device)
return
} else {
return UnknownDeviceErr
}
}
func (d *deviceShadow) StopStatusListener() {
d.ticker.Stop()
}

View File

@@ -1,7 +1,6 @@
package tool
import (
"log"
"reflect"
"regexp"
"strings"
@@ -71,7 +70,6 @@ func CheckInterfaceIsArray(data interface{}) (bool, []map[string]interface{}) {
if valueType.Kind() == reflect.Slice || valueType.Kind() == reflect.Array {
var maps []map[string]interface{}
for _, item := range data.([]interface{}) {
log.Println("item", item)
if m, ok := item.(map[string]interface{}); ok {
maps = append(maps, m)
}
@@ -80,3 +78,8 @@ func CheckInterfaceIsArray(data interface{}) (bool, []map[string]interface{}) {
}
return false, nil
}
func GetInterfaceType(v interface{}) string {
interfaceType := reflect.TypeOf(v)
return interfaceType.String()
}

View File

@@ -11,3 +11,8 @@ func TestGenerateID(t *testing.T) {
id := GenerateID()
t.Log(id)
}
func TestGetInterfaceType(t *testing.T) {
id := GetInterfaceType(`{"aa": 23}`)
t.Log(id)
}