device.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package device
  2. import (
  3. "time"
  4. )
  5. // Device 设备模型
  6. type Device struct {
  7. ID string `json:"id" gorm:"primaryKey"`
  8. Name string `json:"name" gorm:"not null"`
  9. Type string `json:"type" gorm:"not null"`
  10. Protocol string `json:"protocol" gorm:"not null"`
  11. Status DeviceStatus `json:"status" gorm:"default:'offline'"`
  12. LastSeen *time.Time `json:"last_seen"`
  13. Metadata map[string]string `json:"metadata" gorm:"serializer:json"`
  14. Config DeviceConfig `json:"config" gorm:"serializer:json"`
  15. Firmware FirmwareInfo `json:"firmware" gorm:"serializer:json"`
  16. Location LocationInfo `json:"location" gorm:"serializer:json"`
  17. CreatedAt time.Time `json:"created_at"`
  18. UpdatedAt time.Time `json:"updated_at"`
  19. DeletedAt *time.Time `json:"-" gorm:"index"`
  20. }
  21. // DeviceStatus 设备状态
  22. type DeviceStatus string
  23. const (
  24. StatusOnline DeviceStatus = "online"
  25. StatusOffline DeviceStatus = "offline"
  26. StatusError DeviceStatus = "error"
  27. StatusUpdating DeviceStatus = "updating"
  28. )
  29. // DeviceConfig 设备配置
  30. type DeviceConfig struct {
  31. ReportInterval int `json:"report_interval"` // 报告间隔(秒)
  32. Threshold map[string]interface{} `json:"threshold"` // 阈值设置
  33. Enabled bool `json:"enabled"` // 是否启用
  34. Custom map[string]interface{} `json:"custom"` // 自定义配置
  35. }
  36. // FirmwareInfo 固件信息
  37. type FirmwareInfo struct {
  38. Version string `json:"version"`
  39. Checksum string `json:"checksum"`
  40. ReleaseDate time.Time `json:"release_date"`
  41. Description string `json:"description"`
  42. URL string `json:"url"`
  43. }
  44. // LocationInfo 位置信息
  45. type LocationInfo struct {
  46. Latitude float64 `json:"latitude"`
  47. Longitude float64 `json:"longitude"`
  48. Altitude float64 `json:"altitude"`
  49. Address string `json:"address"`
  50. Building string `json:"building"`
  51. Floor string `json:"floor"`
  52. Room string `json:"room"`
  53. }
  54. // DeviceData 设备数据
  55. type DeviceData struct {
  56. DeviceID string `json:"device_id"`
  57. Timestamp time.Time `json:"timestamp"`
  58. Metrics map[string]interface{} `json:"metrics"`
  59. Location *LocationInfo `json:"location,omitempty"`
  60. Quality DataQuality `json:"quality"`
  61. }
  62. // DataQuality 数据质量
  63. type DataQuality struct {
  64. Good bool `json:"good"`
  65. Accuracy float64 `json:"accuracy"`
  66. Source string `json:"source"`
  67. }
  68. // DeviceEvent 设备事件
  69. type DeviceEvent struct {
  70. ID string `json:"id"`
  71. DeviceID string `json:"device_id"`
  72. Type EventType `json:"type"`
  73. Severity EventSeverity `json:"severity"`
  74. Message string `json:"message"`
  75. Data map[string]interface{} `json:"data,omitempty"`
  76. Timestamp time.Time `json:"timestamp"`
  77. Acked bool `json:"acked"`
  78. AckedBy string `json:"acked_by,omitempty"`
  79. AckedAt *time.Time `json:"acked_at,omitempty"`
  80. }
  81. // EventType 事件类型
  82. type EventType string
  83. const (
  84. EventConnect EventType = "connect"
  85. EventDisconnect EventType = "disconnect"
  86. EventError EventType = "error"
  87. EventMaintenance EventType = "maintenance"
  88. EventFirmware EventType = "firmware"
  89. EventConfig EventType = "config"
  90. EventAlert EventType = "alert"
  91. )
  92. // EventSeverity 事件严重程度
  93. type EventSeverity string
  94. const (
  95. SeverityInfo EventSeverity = "info"
  96. SeverityWarning EventSeverity = "warning"
  97. SeverityError EventSeverity = "error"
  98. SeverityCritical EventSeverity = "critical"
  99. )
  100. // CreateDeviceRequest 创建设备请求
  101. type CreateDeviceRequest struct {
  102. Name string `json:"name" binding:"required"`
  103. Type string `json:"type" binding:"required"`
  104. Protocol string `json:"protocol" binding:"required"`
  105. Metadata map[string]string `json:"metadata"`
  106. Config DeviceConfig `json:"config"`
  107. Location LocationInfo `json:"location"`
  108. }
  109. // UpdateDeviceRequest 更新设备请求
  110. type UpdateDeviceRequest struct {
  111. Name string `json:"name"`
  112. Metadata map[string]string `json:"metadata"`
  113. Config DeviceConfig `json:"config"`
  114. Location LocationInfo `json:"location"`
  115. }
  116. // UpdateDeviceConfigRequest 更新设备配置请求
  117. type UpdateDeviceConfigRequest struct {
  118. Config DeviceConfig `json:"config" binding:"required"`
  119. }
  120. // UpdateFirmwareRequest 更新固件请求
  121. type UpdateFirmwareRequest struct {
  122. Version string `json:"version" binding:"required"`
  123. URL string `json:"url" binding:"required"`
  124. Description string `json:"description"`
  125. }
  126. // DeviceResponse 设备响应
  127. type DeviceResponse struct {
  128. ID string `json:"id"`
  129. Name string `json:"name"`
  130. Type string `json:"type"`
  131. Protocol string `json:"protocol"`
  132. Status DeviceStatus `json:"status"`
  133. LastSeen *time.Time `json:"last_seen"`
  134. Metadata map[string]string `json:"metadata"`
  135. Config DeviceConfig `json:"config"`
  136. Firmware FirmwareInfo `json:"firmware"`
  137. Location LocationInfo `json:"location"`
  138. CreatedAt time.Time `json:"created_at"`
  139. UpdatedAt time.Time `json:"updated_at"`
  140. }
  141. // ToResponse 转换为响应格式
  142. func (d *Device) ToResponse() DeviceResponse {
  143. return DeviceResponse{
  144. ID: d.ID,
  145. Name: d.Name,
  146. Type: d.Type,
  147. Protocol: d.Protocol,
  148. Status: d.Status,
  149. LastSeen: d.LastSeen,
  150. Metadata: d.Metadata,
  151. Config: d.Config,
  152. Firmware: d.Firmware,
  153. Location: d.Location,
  154. CreatedAt: d.CreatedAt,
  155. UpdatedAt: d.UpdatedAt,
  156. }
  157. }
  158. // TableName 指定表名
  159. func (Device) TableName() string {
  160. return "devices"
  161. }
  162. // TableName 指定表名
  163. func (DeviceEvent) TableName() string {
  164. return "device_events"
  165. }