调整 device和模板

This commit is contained in:
2025-09-29 23:46:28 +08:00
parent bc97c6bfed
commit 35c2d03602
3 changed files with 52 additions and 312 deletions

View File

@@ -9,30 +9,12 @@ import (
"gorm.io/gorm"
)
// 设备属性名大全
var (
// 普通开关式设备
BusNumber = "bus_number" // 总线号
BusAddress = "bus_address" // 总线地址
RelayChannel = "relay_channel" // 继电器通道号
// 区域主控
LoRaAddress = "lora_address" // 区域主控 LoRa 地址, 如果使用LoRa网关也可能是LoRa网关记录的设备ID
)
// --- Properties 结构体定义 ---
// LoraProperties 定义了区域主控的特有属性
type LoraProperties struct {
LoraAddress string `json:"lora_address"` // LoRa 地址
}
// BusProperties 定义了总线设备的特有属性
type BusProperties struct {
BusNumber int `json:"bus_number"` // 485 总线号
BusAddress int `json:"bus_address"` // 485 总线地址
RelayChannel int `json:"relay_channel"` // 继电器通道号
// Bus485Properties 定义了总线设备的特有属性
type Bus485Properties struct {
BusNumber uint8 `json:"bus_number"` // 485 总线号
BusAddress uint8 `json:"bus_address"` // 485 总线地址
}
// AreaController 是一个LoRa转总线(如485)的通信网关
@@ -93,7 +75,7 @@ type Device struct {
Location string `gorm:"index" json:"location"`
// Properties 用于存储特定类型设备的独有属性采用JSON格式。
// 建议在应用层为不同子类型的设备定义专用的属性结构体(如 LoraProperties, BusProperties,以保证数据一致性。
// 建议在应用层为不同子类型的设备定义专用的属性结构体,以保证数据一致性。
Properties datatypes.JSON `json:"properties"`
}
@@ -111,12 +93,12 @@ func (d *Device) SelfCheck() error {
return errors.New("设备属性 (Properties) 不能为空")
}
var props map[string]interface{}
var props Bus485Properties
if err := json.Unmarshal(d.Properties, &props); err != nil {
return errors.New("无法解析设备属性 (Properties)")
}
if _, ok := props[BusAddress]; !ok {
if props.BusAddress == 0 {
return errors.New("设备属性 (Properties) 中缺少总线地址 (bus_address)")
}