fix: Gateway 响应类型兼容 — 新增 FlexBool/FlexInt 处理设备返回的字符串类型字段
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:
2026-03-28 18:24:17 +08:00
parent 02b10f87cf
commit f5dd2ce4ab
2 changed files with 51 additions and 23 deletions

View File

@@ -363,15 +363,15 @@ func (s *Service) fetchDeviceGatewayInfo(ctx context.Context, deviceID uint) *dt
// 使用指针字段,未赋值的字段保持 nilomitempty
func mapSyncRespToDeviceGatewayInfo(r *gateway.SyncDeviceInfoResp) *dto.DeviceGatewayInfo {
info := &dto.DeviceGatewayInfo{
OnlineStatus: &r.OnlineStatus,
OnlineStatus: flexIntPtr(r.OnlineStatus),
RunTime: strPtr(r.RunTime),
ConnectTime: strPtr(r.ConnectTime),
LastOnlineTime: strPtr(r.LastOnlineTime),
LastUpdateTime: strPtr(r.LastUpdateTime),
Rsrp: &r.Rsrp,
Rsrq: &r.Rsrq,
Rsrp: flexIntPtr(r.Rsrp),
Rsrq: flexIntPtr(r.Rsrq),
Rssi: strPtr(r.RSSI),
Sinr: &r.Sinr,
Sinr: flexIntPtr(r.Sinr),
SSID: strPtr(r.SSID),
WifiEnabled: flexBoolPtr(r.WifiEnabled),
WifiPassword: strPtr(r.WifiPassword),
@@ -382,21 +382,22 @@ func mapSyncRespToDeviceGatewayInfo(r *gateway.SyncDeviceInfoResp) *dto.DeviceGa
DailyUsage: strPtr(r.DailyUsage),
DLStats: strPtr(r.DLStats),
ULStats: strPtr(r.ULStats),
LimitSpeed: &r.LimitSpeed,
LimitSpeed: flexIntPtr(r.LimitSpeed),
CurrentIccid: strPtr(r.CurrentIccid),
MaxClients: &r.MaxClients,
MaxClients: flexIntPtr(r.MaxClients),
SoftwareVersion: strPtr(r.SoftwareVersion),
SwitchMode: strPtr(r.SwitchMode),
SyncInterval: &r.SyncInterval,
SyncInterval: flexIntPtr(r.SyncInterval),
DeviceID: strPtr(r.DeviceID),
DeviceName: strPtr(r.DeviceName),
DeviceType: strPtr(r.DeviceType),
IMEI: strPtr(r.IMEI),
IMSI: strPtr(r.IMSI),
Status: &r.Status,
Status: flexIntPtr(r.Status),
}
if r.BatteryLevel != nil {
info.BatteryLevel = r.BatteryLevel
n := int(*r.BatteryLevel)
info.BatteryLevel = &n
}
return info
}
@@ -415,6 +416,11 @@ func flexBoolPtr(fb gateway.FlexBool) *bool {
return &b
}
func flexIntPtr(fi gateway.FlexInt) *int {
n := int(fi)
return &n
}
// Refresh 刷新资产数据(调网关同步)
func (s *Service) Refresh(ctx context.Context, assetType string, id uint) (*dto.AssetRealtimeStatusResponse, error) {
switch assetType {
@@ -505,7 +511,7 @@ func (s *Service) updateDeviceFromSyncInfo(ctx context.Context, deviceID uint, r
// 更新设备 5 个存储字段
updates := map[string]any{
"online_status": resp.OnlineStatus,
"online_status": int(resp.OnlineStatus),
"software_version": resp.SoftwareVersion,
"switch_mode": resp.SwitchMode,
"last_gateway_sync_at": now,