Skip to content

Commit 1185fee

Browse files
committed
feat: add validation for online timeout and heartbeat configuration in device settings
1 parent 35eacd8 commit 1185fee

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

‎configs/messages.yaml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,11 @@ messages:
307307
zh_CN: "同一类型脚本只能有一个启用"
308308
en_US: "Only one enabled script of the same type is allowed"
309309

310+
# 设备配置相关错误码 (210xxx)
311+
210001:
312+
zh_CN: "超时时间和心跳时间不能同时设置"
313+
en_US: "Online timeout and heartbeat time cannot be set at the same time"
314+
310315
# Device Connect Field Keys (500xxx)
311316
500001:
312317
zh_CN: "接入地址"

‎internal/service/device_config.go‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package service
22

33
import (
44
"context"
5+
"encoding/json"
56
"time"
67

78
"project/initialize"
@@ -70,6 +71,19 @@ func (*DeviceConfig) CreateDeviceConfig(req *model.CreateDeviceConfigReq, claims
7071
}
7172

7273
func (*DeviceConfig) UpdateDeviceConfig(req model.UpdateDeviceConfigReq) (any, error) {
74+
// 判断other_config是否变更,如果变更要对online_timeout和heartbeat进行校验,如果online_timeout和heartbeat都不为0,则组织修改,提示只允许设置其中一个
75+
if req.OtherConfig != nil {
76+
var otherConfig model.DeviceConfigOtherConfig
77+
err := json.Unmarshal([]byte(*req.OtherConfig), &otherConfig)
78+
if err != nil {
79+
return nil, errcode.WithData(errcode.CodeParamError, map[string]interface{}{
80+
"err": err.Error(),
81+
})
82+
}
83+
if otherConfig.OnlineTimeout != 0 && otherConfig.Heartbeat != 0 {
84+
return nil, errcode.New(210001)
85+
}
86+
}
7387
// 对修改设备模板id进行特殊处理
7488
if req.DeviceTemplateId != nil && *req.DeviceTemplateId == "" {
7589
err := dal.UpdateDeviceConfigTemplateID(req.Id, nil)
@@ -142,6 +156,7 @@ func (*DeviceConfig) UpdateDeviceConfig(req model.UpdateDeviceConfigReq) (any, e
142156
}
143157
}
144158
}
159+
145160
return data, nil
146161
}
147162

0 commit comments

Comments
 (0)