Files
pig-farm-controller/internal/model/device.go

139 lines
3.8 KiB
Go
Raw Normal View History

// Package model 提供数据模型定义
// 包含设备控制等相关数据结构
package model
import (
"fmt"
"strconv"
"strings"
"time"
"gorm.io/gorm"
)
// DeviceType 设备类型枚举
type DeviceType string
const (
// DeviceTypeFan 风机
DeviceTypeFan DeviceType = "fan"
// DeviceTypeWaterCurtain 水帘
DeviceTypeWaterCurtain DeviceType = "water_curtain"
// DeviceTypePigPenController 猪舍主控
DeviceTypePigPenController DeviceType = "pig_pen_controller"
// DeviceTypeFeedMillController 做料车间主控
DeviceTypeFeedMillController DeviceType = "feed_mill_controller"
// DeviceTypeRelay 中继设备
DeviceTypeRelay DeviceType = "relay"
)
// Device 代表设备信息
type Device struct {
// ID 设备ID
ID uint `gorm:"primaryKey;column:id" json:"id"`
// Name 设备名称
Name string `gorm:"not null;column:name" json:"name"`
// Type 设备类型
Type DeviceType `gorm:"not null;column:type" json:"type"`
// ParentID 上级设备ID(用于设备层级关系,指向区域主控设备)
ParentID *uint `gorm:"column:parent_id;index" json:"parent_id"`
// Address 设备地址普通设备的485总线地址或区域主控的Lora地址中继设备不需要
// 格式:对于普通设备,可以是"bus_number:device_address"或"device_address"
// 对于区域主控是Lora地址
Address *string `gorm:"column:address" json:"address,omitempty"`
// CreatedAt 创建时间
CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
// UpdatedAt 更新时间
UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
// DeletedAt 删除时间(用于软删除)
DeletedAt gorm.DeletedAt `gorm:"index;column:deleted_at" json:"-"`
}
// TableName 指定Device模型对应的数据库表名
func (Device) TableName() string {
return "devices"
}
// Set485Address 设置普通设备的485总线地址和设备地址
func (d *Device) Set485Address(busNumber int, deviceAddress string) {
if d.Type != DeviceTypeFan && d.Type != DeviceTypeWaterCurtain {
return
}
address := fmt.Sprintf("%d:%s", busNumber, deviceAddress)
d.Address = &address
}
// Get485Address 获取普通设备的总线号和设备地址
func (d *Device) Get485Address() (busNumber int, deviceAddress string, err error) {
if d.Address == nil {
return 0, "", fmt.Errorf("address is nil")
}
parts := strings.Split(*d.Address, ":")
if len(parts) != 2 {
// 如果没有总线号默认为总线0
return 0, *d.Address, nil
}
busNumber, err = strconv.Atoi(parts[0])
if err != nil {
return 0, "", fmt.Errorf("invalid bus number: %v", err)
}
deviceAddress = parts[1]
return busNumber, deviceAddress, nil
}
// DeviceControl 代表设备控制记录
type DeviceControl struct {
// ID 记录ID
ID uint `gorm:"primaryKey;column:id" json:"id"`
// UserID 用户ID
UserID uint `gorm:"not null;column:user_id;index" json:"user_id"`
// Location 设备安装位置描述
Location string `gorm:"not null;column:location" json:"location"`
// DeviceType 设备类型
DeviceType DeviceType `gorm:"not null;column:device_type" json:"device_type"`
// DeviceID 设备编号
DeviceID string `gorm:"not null;column:device_id" json:"device_id"`
// Action 控制动作(开/关)
Action string `gorm:"not null;column:action" json:"action"`
// Status 控制状态(成功/失败)
Status string `gorm:"not null;column:status" json:"status"`
// Result 控制结果详情(可选)
Result string `gorm:"column:result" json:"result"`
// CreatedAt 创建时间
CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
// UpdatedAt 更新时间
UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
// DeletedAt 删除时间(用于软删除)
DeletedAt gorm.DeletedAt `gorm:"index;column:deleted_at" json:"-"`
}
// TableName 指定DeviceControl模型对应的数据库表名
func (DeviceControl) TableName() string {
return "device_controls"
}