| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- 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"
- }
|