package device import ( "time" ) // Device 设备模型 type Device struct { ID string `json:"id" gorm:"primaryKey"` Name string `json:"name" gorm:"not null"` Type string `json:"type" gorm:"not null"` Protocol string `json:"protocol" gorm:"not null"` Status DeviceStatus `json:"status" gorm:"default:'offline'"` LastSeen *time.Time `json:"last_seen"` Metadata map[string]string `json:"metadata" gorm:"serializer:json"` Config DeviceConfig `json:"config" gorm:"serializer:json"` Firmware FirmwareInfo `json:"firmware" gorm:"serializer:json"` Location LocationInfo `json:"location" gorm:"serializer:json"` CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` DeletedAt *time.Time `json:"-" gorm:"index"` } // DeviceStatus 设备状态 type DeviceStatus string const ( StatusOnline DeviceStatus = "online" StatusOffline DeviceStatus = "offline" StatusError DeviceStatus = "error" StatusUpdating DeviceStatus = "updating" ) // DeviceConfig 设备配置 type DeviceConfig struct { ReportInterval int `json:"report_interval"` // 报告间隔(秒) Threshold map[string]interface{} `json:"threshold"` // 阈值设置 Enabled bool `json:"enabled"` // 是否启用 Custom map[string]interface{} `json:"custom"` // 自定义配置 } // FirmwareInfo 固件信息 type FirmwareInfo struct { Version string `json:"version"` Checksum string `json:"checksum"` ReleaseDate time.Time `json:"release_date"` Description string `json:"description"` URL string `json:"url"` } // LocationInfo 位置信息 type LocationInfo struct { Latitude float64 `json:"latitude"` Longitude float64 `json:"longitude"` Altitude float64 `json:"altitude"` Address string `json:"address"` Building string `json:"building"` Floor string `json:"floor"` Room string `json:"room"` } // DeviceData 设备数据 type DeviceData struct { DeviceID string `json:"device_id"` Timestamp time.Time `json:"timestamp"` Metrics map[string]interface{} `json:"metrics"` Location *LocationInfo `json:"location,omitempty"` Quality DataQuality `json:"quality"` } // DataQuality 数据质量 type DataQuality struct { Good bool `json:"good"` Accuracy float64 `json:"accuracy"` Source string `json:"source"` } // DeviceEvent 设备事件 type DeviceEvent struct { ID string `json:"id"` DeviceID string `json:"device_id"` Type EventType `json:"type"` Severity EventSeverity `json:"severity"` Message string `json:"message"` Data map[string]interface{} `json:"data,omitempty"` Timestamp time.Time `json:"timestamp"` Acked bool `json:"acked"` AckedBy string `json:"acked_by,omitempty"` AckedAt *time.Time `json:"acked_at,omitempty"` } // EventType 事件类型 type EventType string const ( EventConnect EventType = "connect" EventDisconnect EventType = "disconnect" EventError EventType = "error" EventMaintenance EventType = "maintenance" EventFirmware EventType = "firmware" EventConfig EventType = "config" EventAlert EventType = "alert" ) // EventSeverity 事件严重程度 type EventSeverity string const ( SeverityInfo EventSeverity = "info" SeverityWarning EventSeverity = "warning" SeverityError EventSeverity = "error" SeverityCritical EventSeverity = "critical" ) // CreateDeviceRequest 创建设备请求 type CreateDeviceRequest struct { Name string `json:"name" binding:"required"` Type string `json:"type" binding:"required"` Protocol string `json:"protocol" binding:"required"` Metadata map[string]string `json:"metadata"` Config DeviceConfig `json:"config"` Location LocationInfo `json:"location"` } // UpdateDeviceRequest 更新设备请求 type UpdateDeviceRequest struct { Name string `json:"name"` Metadata map[string]string `json:"metadata"` Config DeviceConfig `json:"config"` Location LocationInfo `json:"location"` } // UpdateDeviceConfigRequest 更新设备配置请求 type UpdateDeviceConfigRequest struct { Config DeviceConfig `json:"config" binding:"required"` } // UpdateFirmwareRequest 更新固件请求 type UpdateFirmwareRequest struct { Version string `json:"version" binding:"required"` URL string `json:"url" binding:"required"` Description string `json:"description"` } // DeviceResponse 设备响应 type DeviceResponse struct { ID string `json:"id"` Name string `json:"name"` Type string `json:"type"` Protocol string `json:"protocol"` Status DeviceStatus `json:"status"` LastSeen *time.Time `json:"last_seen"` Metadata map[string]string `json:"metadata"` Config DeviceConfig `json:"config"` Firmware FirmwareInfo `json:"firmware"` Location LocationInfo `json:"location"` CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` } // ToResponse 转换为响应格式 func (d *Device) ToResponse() DeviceResponse { return DeviceResponse{ ID: d.ID, Name: d.Name, Type: d.Type, Protocol: d.Protocol, Status: d.Status, LastSeen: d.LastSeen, Metadata: d.Metadata, Config: d.Config, Firmware: d.Firmware, Location: d.Location, CreatedAt: d.CreatedAt, UpdatedAt: d.UpdatedAt, } } // TableName 指定表名 func (Device) TableName() string { return "devices" } // TableName 指定表名 func (DeviceEvent) TableName() string { return "device_events" }