2025-09-24 21:53:18 +08:00
|
|
|
|
package models
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2025-11-09 21:37:35 +08:00
|
|
|
|
"encoding/json"
|
|
|
|
|
|
"errors"
|
2025-09-24 21:53:18 +08:00
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
"gorm.io/datatypes"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2025-09-29 22:23:35 +08:00
|
|
|
|
// SensorType 定义了 SensorData 记录中 Data 字段的整体类型
|
|
|
|
|
|
type SensorType string
|
2025-09-24 21:53:18 +08:00
|
|
|
|
|
2025-09-29 22:23:35 +08:00
|
|
|
|
const (
|
2025-10-01 20:39:59 +08:00
|
|
|
|
SensorTypeSignalMetrics SensorType = "信号强度" // 信号强度
|
|
|
|
|
|
SensorTypeBatteryLevel SensorType = "电池电量" // 电池电量
|
|
|
|
|
|
SensorTypeTemperature SensorType = "温度" // 温度
|
|
|
|
|
|
SensorTypeHumidity SensorType = "湿度" // 湿度
|
|
|
|
|
|
SensorTypeWeight SensorType = "重量" // 重量
|
2025-09-29 22:23:35 +08:00
|
|
|
|
)
|
2025-09-26 15:26:21 +08:00
|
|
|
|
|
2025-09-24 21:53:18 +08:00
|
|
|
|
// SignalMetrics 存储信号强度数据
|
|
|
|
|
|
type SignalMetrics struct {
|
2025-09-24 22:34:11 +08:00
|
|
|
|
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
|
2025-09-24 21:53:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// BatteryLevel 存储电池电量数据
|
|
|
|
|
|
type BatteryLevel struct {
|
2025-09-24 22:34:11 +08:00
|
|
|
|
BatteryLevelRatio float32 `json:"battery_level_ratio"` // 电量剩余百分比(%)
|
2025-09-24 21:53:18 +08:00
|
|
|
|
BatteryLevelUnavailable bool `json:"battery_level_unavailable"` // 电量数据不可用
|
|
|
|
|
|
ExternalPower bool `json:"external_power"` // 是否使用外部电源
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-24 22:34:11 +08:00
|
|
|
|
// 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"` // 重量值 (公斤)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-24 21:53:18 +08:00
|
|
|
|
// 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"`
|
|
|
|
|
|
|
2025-09-29 22:26:58 +08:00
|
|
|
|
// SensorType 是传感数据的类型
|
|
|
|
|
|
SensorType SensorType `gorm:"not null;index" json:"sensor_type"`
|
2025-09-24 21:53:18 +08:00
|
|
|
|
|
|
|
|
|
|
// Data 存储一个或多个传感器读数,格式为 JSON。
|
|
|
|
|
|
Data datatypes.JSON `gorm:"type:jsonb" json:"data"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (SensorData) TableName() string {
|
|
|
|
|
|
return "sensor_data"
|
|
|
|
|
|
}
|
2025-11-09 21:37:35 +08:00
|
|
|
|
|
|
|
|
|
|
// ParseData 解析 JSON 数据到一个具体的结构体中。
|
|
|
|
|
|
// 调用方需要传入一个指向目标结构体实例的指针。
|
|
|
|
|
|
func (s *SensorData) ParseData(v interface{}) error {
|
|
|
|
|
|
if s.Data == nil {
|
|
|
|
|
|
return errors.New("传感器数据为空,无法解析")
|
|
|
|
|
|
}
|
|
|
|
|
|
return json.Unmarshal(s.Data, v)
|
|
|
|
|
|
}
|