增加AreaControllerProperties
This commit is contained in:
@@ -3,6 +3,7 @@ package models
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"gorm.io/datatypes"
|
||||
@@ -16,6 +17,11 @@ type Bus485Properties struct {
|
||||
BusAddress uint8 `json:"bus_address"` // 485 总线地址
|
||||
}
|
||||
|
||||
// AreaControllerProperties 定义了区域主控的特有属性
|
||||
type AreaControllerProperties struct {
|
||||
FirmwareVersion string `json:"firmware_version,omitempty"` // 主控程序版本
|
||||
}
|
||||
|
||||
// AreaController 是一个LoRa转总线(如485)的通信网关
|
||||
type AreaController struct {
|
||||
Model
|
||||
@@ -45,6 +51,29 @@ func (ac *AreaController) SelfCheck() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// ParseProperties 解析 JSON 属性到一个具体的结构体中。
|
||||
// 调用方需要传入一个指向目标结构体实例的指针。
|
||||
func (ac *AreaController) ParseProperties(v interface{}) error {
|
||||
if ac.Properties == nil {
|
||||
return errors.New("区域主控属性为空,无法解析")
|
||||
}
|
||||
return json.Unmarshal(ac.Properties, v)
|
||||
}
|
||||
|
||||
// SetProperties 将一个结构体编码为 JSON 并设置到 Properties 字段。
|
||||
func (ac *AreaController) SetProperties(v interface{}) error {
|
||||
if v == nil {
|
||||
ac.Properties = nil
|
||||
return nil
|
||||
}
|
||||
jsonBytes, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
return fmt.Errorf("无法编码区域主控的属性 (Properties): %w", err)
|
||||
}
|
||||
ac.Properties = jsonBytes
|
||||
return nil
|
||||
}
|
||||
|
||||
// TableName 自定义 GORM 使用的数据库表名
|
||||
func (AreaController) TableName() string {
|
||||
return "area_controllers"
|
||||
|
||||
Reference in New Issue
Block a user