65 lines
2.4 KiB
Go
65 lines
2.4 KiB
Go
package models
|
||
|
||
import (
|
||
"time"
|
||
|
||
"gorm.io/datatypes"
|
||
)
|
||
|
||
// SensorDataType 定义了 SensorData 记录中 Data 字段的整体类型
|
||
|
||
// DeviceSubTypeToSensorDataTypeMap 定义了设备子类型到其产生的传感器数据类型的静态映射.
|
||
// 这个公开的 map 是连接设备定义和数据记录的桥梁, 供其他包直接查询.
|
||
|
||
// SignalMetrics 存储信号强度数据
|
||
type SignalMetrics struct {
|
||
RssiDbm int `json:"rssi_dbm"` // 绝对信号强度(dBm),受距离、障碍物影响
|
||
SnrDb float64 `json:"snr_db"` // 信号与噪声的相对比率(dB),由 RSSI 减去噪声地板(Noise Floor)
|
||
SensitivityDbm int `json:"sensitivity_dbm"` // 网关的最低检测阈值(dBm)
|
||
MarginDb int `json:"margin_db"` // SNR 相对于接收器灵敏度的余量, Margin = SNR - Sensitivity
|
||
}
|
||
|
||
// BatteryLevel 存储电池电量数据
|
||
type BatteryLevel struct {
|
||
BatteryLevelRatio float32 `json:"battery_level_ratio"` // 电量剩余百分比(%)
|
||
BatteryLevelUnavailable bool `json:"battery_level_unavailable"` // 电量数据不可用
|
||
ExternalPower bool `json:"external_power"` // 是否使用外部电源
|
||
}
|
||
|
||
// TemperatureData 存储温度数据
|
||
type TemperatureData struct {
|
||
TemperatureCelsius float64 `json:"temperature_celsius"` // 温度值 (摄氏度)
|
||
}
|
||
|
||
// HumidityData 存储湿度数据
|
||
type HumidityData struct {
|
||
HumidityPercent float64 `json:"humidity_percent"` // 湿度值 (%)
|
||
}
|
||
|
||
// WeightData 存储重量数据
|
||
type WeightData struct {
|
||
WeightKilograms float64 `json:"weight_kilograms"` // 重量值 (公斤)
|
||
}
|
||
|
||
// SensorData 存储所有类型的传感器数据,对应数据库中的 'sensor_data' 表。
|
||
type SensorData struct {
|
||
// Time 是数据记录的时间戳,作为复合主键的一部分。
|
||
Time time.Time `gorm:"primaryKey" json:"time"`
|
||
|
||
// DeviceID 是传感器的唯一标识符,作为复合主键的另一部分。
|
||
DeviceID uint `gorm:"primaryKey" json:"device_id"`
|
||
|
||
// RegionalControllerID 是上报此数据的区域主控的ID。
|
||
RegionalControllerID uint `json:"regional_controller_id"`
|
||
|
||
// SensorDataType 是传感数据的类型
|
||
SensorDataType string `gorm:"not null;index" json:"sensor_data_type"`
|
||
|
||
// Data 存储一个或多个传感器读数,格式为 JSON。
|
||
Data datatypes.JSON `gorm:"type:jsonb" json:"data"`
|
||
}
|
||
|
||
func (SensorData) TableName() string {
|
||
return "sensor_data"
|
||
}
|