修改设备模型
This commit is contained in:
75
internal/infra/models/device_template.go
Normal file
75
internal/infra/models/device_template.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"gorm.io/datatypes"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// DeviceCategory 定义了设备模板的宽泛类别
|
||||
type DeviceCategory string
|
||||
|
||||
const (
|
||||
// CategoryActuator 代表一个执行器,可以被控制(例如:风机、阀门)
|
||||
CategoryActuator DeviceCategory = "actuator"
|
||||
// CategorySensor 代表一个传感器,用于报告测量值(例如:温度计)
|
||||
CategorySensor DeviceCategory = "sensor"
|
||||
// CategoryCompound 代表一个复合设备,既是执行器也是传感器
|
||||
CategoryCompound DeviceCategory = "compound"
|
||||
)
|
||||
|
||||
// ValueDescriptor 描述了传感器可以报告的单个数值。
|
||||
// 它提供了必要的元数据,以便应用程序能够正确解释从设备读取的原始数据。
|
||||
type ValueDescriptor struct {
|
||||
Name string `json:"name"` // 值的业务名称, 例如 "temperature", "humidity"
|
||||
Unit string `json:"unit"` // 测量单位, 例如 "°C", "%RH", "ppm"
|
||||
DataType string `json:"data_type"` // 期望的数据类型, 例如 "float", "int", "boolean"
|
||||
Multiplier float64 `json:"multiplier"` // 乘以原始值的系数 (例如 0.1)
|
||||
Offset float64 `json:"offset"` // 乘法之后再增加的偏移量 (最终值 = 原始值 * multiplier + offset)
|
||||
}
|
||||
|
||||
// DeviceCommands 定义了设备模板支持的指令集合。
|
||||
// 使用指针类型来表示指令的可选性,如果一个模板不支持某个指令,则该字段为 nil。
|
||||
// json tag 中的 "omitempty" 确保了在序列化回 JSON 时,nil 字段会被省略,保持数据库数据的整洁。
|
||||
type DeviceCommands struct {
|
||||
On *string `json:"on,omitempty"` // 开指令
|
||||
Off *string `json:"off,omitempty"` // 关指令
|
||||
Read *string `json:"read,omitempty"` // 读取传感器数值的指令
|
||||
|
||||
// 为了未来的扩展性,可以预留一些通用指令
|
||||
SetSpeed *string `json:"set_speed,omitempty"` // 设置速度/档位
|
||||
SetValue *string `json:"set_value,omitempty"` // 设置某个具体值(例如设定温度)
|
||||
}
|
||||
|
||||
// DeviceTemplate 代表一种物理设备的类型。
|
||||
// 它作为一个蓝图,定义了设备的通用属性、操作指令和数据解释规则。
|
||||
type DeviceTemplate struct {
|
||||
gorm.Model
|
||||
|
||||
// Name 是此模板的唯一名称, 例如 "FanModel-XYZ-2000" 或 "TempSensor-T1"
|
||||
Name string `gorm:"not null;unique" json:"name"`
|
||||
|
||||
// Manufacturer 是设备的制造商。
|
||||
Manufacturer string `json:"manufacturer"`
|
||||
|
||||
// Description 提供了关于此设备类型的更多详细信息。
|
||||
Description string `json:"description"`
|
||||
|
||||
// Category 将模板分类为传感器、执行器或复合设备。
|
||||
Category DeviceCategory `gorm:"not null;index" json:"category"`
|
||||
|
||||
// Commands 存储了一个从“动作名称”到“原始指令”的映射。
|
||||
// 使用 JSON 格式,具有良好的可扩展性。
|
||||
// 例如,对于风机: {"ON": "01050000FF008C3A", "OFF": "010500000000CDCA"}
|
||||
// 例如,对于传感器: {"READ": "010300000001840A"}
|
||||
Commands datatypes.JSON `json:"commands"`
|
||||
|
||||
// Values 描述了传感器模板所能提供的数据点。
|
||||
// 当 Category 是 "sensor" 或 "compound" 时,此字段尤为重要。
|
||||
// 它是一个 ValueDescriptor 对象的 JSON 数组。
|
||||
Values datatypes.JSON `json:"values"`
|
||||
}
|
||||
|
||||
// TableName 自定义 GORM 使用的数据库表名
|
||||
func (DeviceTemplate) TableName() string {
|
||||
return "device_templates"
|
||||
}
|
||||
Reference in New Issue
Block a user