fix: Gateway 响应类型兼容 — 新增 FlexBool/FlexInt 处理设备返回的字符串类型字段
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 7m4s
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 7m4s
部分设备厂商(如华为)的 Gateway 响应中 bool/int 字段返回为字符串(wifi_enabled: "1"、battery_level: "30"), 导致 sonic 严格类型检查反序列化失败,整个设备实时数据丢失。 - 新增 FlexBool 类型:兼容 true/false、"1"/"0"、"true"/"false" - 新增 FlexInt 类型:兼容 30、"30"、null - SyncDeviceInfoResp 所有 bool/int 字段改用 Flex 类型 - 更新 B 端和 C 端 DTO 映射函数的类型转换
This commit is contained in:
@@ -3,6 +3,7 @@ package gateway
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -24,6 +25,27 @@ func (b *FlexBool) UnmarshalJSON(data []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// FlexInt 灵活整数类型
|
||||
// 与 FlexBool 同理,Gateway 部分设备返回 int 字段时使用字符串(如 "30")而非 JSON number。
|
||||
type FlexInt int
|
||||
|
||||
// UnmarshalJSON 实现 json.Unmarshaler 接口
|
||||
// 支持:30, "30", null
|
||||
func (i *FlexInt) UnmarshalJSON(data []byte) error {
|
||||
raw := strings.Trim(string(data), "\"")
|
||||
if raw == "" || raw == "null" {
|
||||
*i = 0
|
||||
return nil
|
||||
}
|
||||
n, err := strconv.Atoi(raw)
|
||||
if err != nil {
|
||||
*i = 0
|
||||
return nil
|
||||
}
|
||||
*i = FlexInt(n)
|
||||
return nil
|
||||
}
|
||||
|
||||
// GatewayResponse 是 Gateway API 的通用响应结构
|
||||
type GatewayResponse struct {
|
||||
Code int `json:"code" description:"业务状态码(200 = 成功)"`
|
||||
@@ -173,17 +195,17 @@ type SyncDeviceInfoResp struct {
|
||||
WifiEnabled FlexBool `json:"wifi_enabled" description:"WiFi开关状态"`
|
||||
SwitchMode string `json:"switch_mode" description:"切卡模式:0=自动,1=手动"`
|
||||
RSSI string `json:"rssi" description:"接收信号强度"`
|
||||
BatteryLevel *int `json:"battery_level" description:"电池电量百分比(无电池时为null)"`
|
||||
OnlineStatus int `json:"online_status" description:"在线状态:1=在线,2=离线"`
|
||||
BatteryLevel *FlexInt `json:"battery_level" description:"电池电量百分比(无电池时为null)"`
|
||||
OnlineStatus FlexInt `json:"online_status" description:"在线状态:1=在线,2=离线"`
|
||||
LastUpdateTime string `json:"last_update_time" description:"设备信息最后更新时间(ISO 8601)"`
|
||||
LastOnlineTime string `json:"last_online_time" description:"设备最后在线时间(ISO 8601)"`
|
||||
RunTime string `json:"run_time" description:"本次开机运行时间(秒)"`
|
||||
DailyUsage string `json:"daily_usage" description:"日使用流量(字节)"`
|
||||
|
||||
// 信号相关(D-10 补全)
|
||||
Rsrp int `json:"rsrp" description:"参考信号接收功率(dBm)"`
|
||||
Rsrq int `json:"rsrq" description:"参考信号接收质量(dB)"`
|
||||
Sinr int `json:"sinr" description:"信噪比(dB)"`
|
||||
Rsrp FlexInt `json:"rsrp" description:"参考信号接收功率(dBm)"`
|
||||
Rsrq FlexInt `json:"rsrq" description:"参考信号接收质量(dB)"`
|
||||
Sinr FlexInt `json:"sinr" description:"信噪比(dB)"`
|
||||
|
||||
// WiFi 相关(D-10 补全)
|
||||
WifiPassword string `json:"wifi_password" description:"WiFi密码"`
|
||||
@@ -194,14 +216,14 @@ type SyncDeviceInfoResp struct {
|
||||
LANIP string `json:"lan_ip" description:"局域网网关IP地址"`
|
||||
|
||||
// 连接相关(D-10 补全)
|
||||
MaxClients int `json:"max_clients" description:"最大连接客户端数"`
|
||||
ConnectTime string `json:"connect_time" description:"设备本次联网时间(秒)"`
|
||||
MaxClients FlexInt `json:"max_clients" description:"最大连接客户端数"`
|
||||
ConnectTime string `json:"connect_time" description:"设备本次联网时间(秒)"`
|
||||
|
||||
// 设备属性(D-10 补全)
|
||||
Status int `json:"status" description:"设备状态:1=正常,0=禁用"`
|
||||
IMSI string `json:"imsi" description:"IMSI用户标识码"`
|
||||
ULStats string `json:"ul_stats" description:"本次开机上传流量(字节)"`
|
||||
DLStats string `json:"dl_stats" description:"本次开机下载流量(字节)"`
|
||||
LimitSpeed int `json:"limit_speed" description:"限速速率(KB/s)"`
|
||||
SyncInterval int `json:"sync_interval" description:"信息上报周期(秒)"`
|
||||
Status FlexInt `json:"status" description:"设备状态:1=正常,0=禁用"`
|
||||
IMSI string `json:"imsi" description:"IMSI用户标识码"`
|
||||
ULStats string `json:"ul_stats" description:"本次开机上传流量(字节)"`
|
||||
DLStats string `json:"dl_stats" description:"本次开机下载流量(字节)"`
|
||||
LimitSpeed FlexInt `json:"limit_speed" description:"限速速率(KB/s)"`
|
||||
SyncInterval FlexInt `json:"sync_interval" description:"信息上报周期(秒)"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user