1. 设备model增加地址字段, 用于保存硬件地址
2. 优化前端界面
This commit is contained in:
@@ -3,6 +3,9 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
@@ -42,6 +45,11 @@ type Device struct {
|
||||
// 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"`
|
||||
|
||||
@@ -57,6 +65,37 @@ 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
|
||||
|
||||
Reference in New Issue
Block a user