调整 ValueDescriptor
This commit is contained in:
@@ -21,11 +21,9 @@ const (
|
||||
// ValueDescriptor 描述了传感器可以报告的单个数值。
|
||||
// 它提供了必要的元数据,以便应用程序能够正确解释从设备读取的原始数据。
|
||||
type ValueDescriptor struct {
|
||||
Name string `json:"name"`
|
||||
Unit string `json:"unit"`
|
||||
DataType string `json:"data_type"`
|
||||
Multiplier float64 `json:"multiplier"`
|
||||
Offset float64 `json:"offset"`
|
||||
Type SensorType `json:"type"`
|
||||
Multiplier float64 `json:"multiplier"`
|
||||
Offset float64 `json:"offset"`
|
||||
}
|
||||
|
||||
// --- 指令结构体 (Command Structs) ---
|
||||
@@ -137,15 +135,10 @@ func (dt *DeviceTemplate) SelfCheck() error {
|
||||
if dt.Values == nil {
|
||||
return errors.New("传感器类型的设备模板缺少 Values 定义")
|
||||
}
|
||||
var values []*ValueDescriptor
|
||||
var values *ValueDescriptor
|
||||
if err := dt.ParseValues(&values); err != nil {
|
||||
return errors.New("无法解析传感器模板的 Values 属性")
|
||||
}
|
||||
// 黄金准则: 一个传感器模板只能定义一种数值的解析方式
|
||||
if len(values) != 1 {
|
||||
return errors.New("传感器模板的 Values 定义必须且只能包含一个描述符")
|
||||
}
|
||||
|
||||
default:
|
||||
return errors.New("未知的设备模板类别")
|
||||
}
|
||||
|
||||
@@ -6,10 +6,16 @@ import (
|
||||
"gorm.io/datatypes"
|
||||
)
|
||||
|
||||
// SensorDataType 定义了 SensorData 记录中 Data 字段的整体类型
|
||||
// SensorType 定义了 SensorData 记录中 Data 字段的整体类型
|
||||
type SensorType string
|
||||
|
||||
// DeviceSubTypeToSensorDataTypeMap 定义了设备子类型到其产生的传感器数据类型的静态映射.
|
||||
// 这个公开的 map 是连接设备定义和数据记录的桥梁, 供其他包直接查询.
|
||||
const (
|
||||
SensorTypeSignalMetrics SensorType = "signal_metrics" // 信号强度
|
||||
SensorTypeBatteryLevel SensorType = "battery_level" // 电池电量
|
||||
SensorTypeTemperature SensorType = "temperature" // 温度
|
||||
SensorTypeHumidity SensorType = "humidity" // 湿度
|
||||
SensorTypeWeight SensorType = "weight" // 重量
|
||||
)
|
||||
|
||||
// SignalMetrics 存储信号强度数据
|
||||
type SignalMetrics struct {
|
||||
@@ -53,7 +59,7 @@ type SensorData struct {
|
||||
RegionalControllerID uint `json:"regional_controller_id"`
|
||||
|
||||
// SensorDataType 是传感数据的类型
|
||||
SensorDataType string `gorm:"not null;index" json:"sensor_data_type"`
|
||||
SensorDataType SensorType `gorm:"not null;index" json:"sensor_data_type"`
|
||||
|
||||
// Data 存储一个或多个传感器读数,格式为 JSON。
|
||||
Data datatypes.JSON `gorm:"type:jsonb" json:"data"`
|
||||
|
||||
Reference in New Issue
Block a user