实现 GeneralDeviceService

This commit is contained in:
2025-09-16 17:12:27 +08:00
parent 2ac3dcda84
commit c9df4fd6f4
5 changed files with 137 additions and 14 deletions

View File

@@ -39,6 +39,18 @@ const (
SubTypeWaterCurtain DeviceSubType = "water_curtain"
)
// 设备属性名大全
var (
// 普通开关式设备
BusNumber = "bus_number" // 总线号
BusAddress = "bus_address" // 总线地址
RelayChannel = "relay_channel" // 继电器通道号
// 区域主控
LoRaAddress = "lora_address" // 区域主控 LoRa 地址, 如果使用LoRa网关也可能是LoRa网关记录的设备ID
)
// --- Properties 结构体定义 ---
// LoraProperties 定义了区域主控的特有属性
@@ -95,3 +107,31 @@ func (d *Device) ParseProperties(v interface{}) error {
}
return json.Unmarshal(d.Properties, v)
}
// SelfCheck 进行参数自检, 返回检测结果
// 方法会根据自身类型进行参数检查, 参数不全时返回false
// TODO 没写单测
func (d *Device) SelfCheck() bool {
properties := make(map[string]interface{})
if err := d.ParseProperties(&properties); err != nil {
return false
}
has := func(key string) bool {
_, ok := properties[key]
return ok
}
switch d.SubType {
case SubTypeFan:
if !has(BusNumber) || !has(BusAddress) || !has(RelayChannel) {
return false
}
default:
// 不应该有类型未知的设备
return false
}
return true
}