All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 8m10s
403 lines
8.9 KiB
Go
403 lines
8.9 KiB
Go
package device
|
||
|
||
import (
|
||
"context"
|
||
|
||
"github.com/break/junhong_cmp_fiber/internal/gateway"
|
||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||
"github.com/break/junhong_cmp_fiber/internal/model/dto"
|
||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
||
)
|
||
|
||
// getGatewayDevice 通过标识符获取设备并验证 IMEI 存在
|
||
// 提供统一的设备查找 + IMEI 校验逻辑,供所有 Gateway 代理方法复用
|
||
func (s *Service) getGatewayDevice(ctx context.Context, identifier string) (*model.Device, string, error) {
|
||
device, err := s.deviceStore.GetByIdentifier(ctx, identifier)
|
||
if err != nil {
|
||
return nil, "", errors.New(errors.CodeNotFound, "设备不存在或无权限访问")
|
||
}
|
||
if device.IMEI == "" {
|
||
return nil, "", errors.New(errors.CodeInvalidParam, "该设备未配置 IMEI,无法调用网关接口")
|
||
}
|
||
return device, device.IMEI, nil
|
||
}
|
||
|
||
// GatewayGetDeviceInfo 通过标识符查询设备网关信息
|
||
func (s *Service) GatewayGetDeviceInfo(ctx context.Context, identifier string) (*gateway.DeviceInfoResp, error) {
|
||
_, imei, err := s.getGatewayDevice(ctx, identifier)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
return s.gatewayClient.GetDeviceInfo(ctx, &gateway.DeviceInfoReq{
|
||
DeviceID: imei,
|
||
})
|
||
}
|
||
|
||
// GatewayGetSlotInfo 通过标识符查询设备卡槽信息
|
||
func (s *Service) GatewayGetSlotInfo(ctx context.Context, identifier string) (*gateway.SlotInfoResp, error) {
|
||
_, imei, err := s.getGatewayDevice(ctx, identifier)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
return s.gatewayClient.GetSlotInfo(ctx, &gateway.DeviceInfoReq{
|
||
DeviceID: imei,
|
||
})
|
||
}
|
||
|
||
// GatewaySetSpeedLimit 通过标识符设置设备限速
|
||
func (s *Service) GatewaySetSpeedLimit(ctx context.Context, identifier string, req *dto.SetSpeedLimitRequest) error {
|
||
device, imei, err := s.getGatewayDevice(ctx, identifier)
|
||
if err != nil {
|
||
s.logDeviceOperation(
|
||
ctx,
|
||
constants.AssetAuditOpDeviceSpeedLimit,
|
||
"设备限速失败",
|
||
constants.AssetAuditResultFailed,
|
||
nil,
|
||
nil,
|
||
map[string]any{
|
||
"identifier": identifier,
|
||
"speed_limit": req.SpeedLimit,
|
||
},
|
||
0,
|
||
0,
|
||
0,
|
||
err,
|
||
)
|
||
return err
|
||
}
|
||
if err = s.gatewayClient.SetSpeedLimit(ctx, &gateway.SpeedLimitReq{
|
||
DeviceID: imei,
|
||
SpeedLimit: req.SpeedLimit,
|
||
}); err != nil {
|
||
s.logDeviceOperation(
|
||
ctx,
|
||
constants.AssetAuditOpDeviceSpeedLimit,
|
||
"设备限速失败",
|
||
constants.AssetAuditResultFailed,
|
||
device,
|
||
map[string]any{"device": deviceSnapshot(device)},
|
||
map[string]any{"speed_limit": req.SpeedLimit},
|
||
0,
|
||
0,
|
||
0,
|
||
err,
|
||
)
|
||
return err
|
||
}
|
||
|
||
s.logDeviceOperation(
|
||
ctx,
|
||
constants.AssetAuditOpDeviceSpeedLimit,
|
||
"设备限速",
|
||
constants.AssetAuditResultSuccess,
|
||
device,
|
||
map[string]any{"device": deviceSnapshot(device)},
|
||
map[string]any{"speed_limit": req.SpeedLimit},
|
||
0,
|
||
0,
|
||
0,
|
||
nil,
|
||
)
|
||
return nil
|
||
}
|
||
|
||
// GatewaySetWiFi 通过标识符设置设备 WiFi
|
||
func (s *Service) GatewaySetWiFi(ctx context.Context, identifier string, req *dto.SetWiFiRequest) error {
|
||
device, imei, err := s.getGatewayDevice(ctx, identifier)
|
||
if err != nil {
|
||
s.logDeviceOperation(
|
||
ctx,
|
||
constants.AssetAuditOpDeviceSetWiFi,
|
||
"设备设置WiFi失败",
|
||
constants.AssetAuditResultFailed,
|
||
nil,
|
||
nil,
|
||
map[string]any{
|
||
"identifier": identifier,
|
||
"card_no": req.CardNo,
|
||
"ssid": req.SSID,
|
||
"enabled": req.Enabled,
|
||
"password": req.Password,
|
||
},
|
||
0,
|
||
0,
|
||
0,
|
||
err,
|
||
)
|
||
return err
|
||
}
|
||
if err = s.gatewayClient.SetWiFi(ctx, &gateway.WiFiReq{
|
||
CardNo: req.CardNo,
|
||
DeviceID: imei,
|
||
SSID: req.SSID,
|
||
Password: req.Password,
|
||
Enabled: req.Enabled,
|
||
}); err != nil {
|
||
s.logDeviceOperation(
|
||
ctx,
|
||
constants.AssetAuditOpDeviceSetWiFi,
|
||
"设备设置WiFi失败",
|
||
constants.AssetAuditResultFailed,
|
||
device,
|
||
map[string]any{"device": deviceSnapshot(device)},
|
||
map[string]any{
|
||
"card_no": req.CardNo,
|
||
"ssid": req.SSID,
|
||
"enabled": req.Enabled,
|
||
"password": req.Password,
|
||
},
|
||
0,
|
||
0,
|
||
0,
|
||
err,
|
||
)
|
||
return err
|
||
}
|
||
|
||
s.logDeviceOperation(
|
||
ctx,
|
||
constants.AssetAuditOpDeviceSetWiFi,
|
||
"设备设置WiFi",
|
||
constants.AssetAuditResultSuccess,
|
||
device,
|
||
map[string]any{"device": deviceSnapshot(device)},
|
||
map[string]any{
|
||
"card_no": req.CardNo,
|
||
"ssid": req.SSID,
|
||
"enabled": req.Enabled,
|
||
"password": req.Password,
|
||
},
|
||
0,
|
||
0,
|
||
0,
|
||
nil,
|
||
)
|
||
return nil
|
||
}
|
||
|
||
// GatewaySwitchCard 通过标识符切换设备使用的卡
|
||
func (s *Service) GatewaySwitchCard(ctx context.Context, identifier string, req *dto.SwitchCardRequest) error {
|
||
device, imei, err := s.getGatewayDevice(ctx, identifier)
|
||
if err != nil {
|
||
s.logDeviceOperation(
|
||
ctx,
|
||
constants.AssetAuditOpDeviceSwitchCard,
|
||
"设备切卡失败",
|
||
constants.AssetAuditResultFailed,
|
||
nil,
|
||
nil,
|
||
map[string]any{
|
||
"identifier": identifier,
|
||
"target_iccid": req.TargetICCID,
|
||
},
|
||
0,
|
||
0,
|
||
0,
|
||
err,
|
||
)
|
||
return err
|
||
}
|
||
if err = s.gatewayClient.SwitchCard(ctx, &gateway.SwitchCardReq{
|
||
CardNo: imei,
|
||
ICCID: req.TargetICCID,
|
||
}); err != nil {
|
||
s.logDeviceOperation(
|
||
ctx,
|
||
constants.AssetAuditOpDeviceSwitchCard,
|
||
"设备切卡失败",
|
||
constants.AssetAuditResultFailed,
|
||
device,
|
||
map[string]any{"device": deviceSnapshot(device)},
|
||
map[string]any{"target_iccid": req.TargetICCID},
|
||
0,
|
||
0,
|
||
0,
|
||
err,
|
||
)
|
||
return err
|
||
}
|
||
|
||
s.logDeviceOperation(
|
||
ctx,
|
||
constants.AssetAuditOpDeviceSwitchCard,
|
||
"设备切卡",
|
||
constants.AssetAuditResultSuccess,
|
||
device,
|
||
map[string]any{"device": deviceSnapshot(device)},
|
||
map[string]any{"target_iccid": req.TargetICCID},
|
||
0,
|
||
0,
|
||
0,
|
||
nil,
|
||
)
|
||
return nil
|
||
}
|
||
|
||
// GatewayRebootDevice 通过标识符重启设备
|
||
func (s *Service) GatewayRebootDevice(ctx context.Context, identifier string) error {
|
||
device, imei, err := s.getGatewayDevice(ctx, identifier)
|
||
if err != nil {
|
||
s.logDeviceOperation(
|
||
ctx,
|
||
constants.AssetAuditOpDeviceReboot,
|
||
"设备重启失败",
|
||
constants.AssetAuditResultFailed,
|
||
nil,
|
||
nil,
|
||
map[string]any{"identifier": identifier},
|
||
0,
|
||
0,
|
||
0,
|
||
err,
|
||
)
|
||
return err
|
||
}
|
||
if err = s.gatewayClient.RebootDevice(ctx, &gateway.DeviceOperationReq{
|
||
DeviceID: imei,
|
||
}); err != nil {
|
||
s.logDeviceOperation(
|
||
ctx,
|
||
constants.AssetAuditOpDeviceReboot,
|
||
"设备重启失败",
|
||
constants.AssetAuditResultFailed,
|
||
device,
|
||
map[string]any{"device": deviceSnapshot(device)},
|
||
nil,
|
||
0,
|
||
0,
|
||
0,
|
||
err,
|
||
)
|
||
return err
|
||
}
|
||
|
||
s.logDeviceOperation(
|
||
ctx,
|
||
constants.AssetAuditOpDeviceReboot,
|
||
"设备重启",
|
||
constants.AssetAuditResultSuccess,
|
||
device,
|
||
map[string]any{"device": deviceSnapshot(device)},
|
||
nil,
|
||
0,
|
||
0,
|
||
0,
|
||
nil,
|
||
)
|
||
return nil
|
||
}
|
||
|
||
// GatewayResetDevice 通过标识符恢复设备出厂设置
|
||
func (s *Service) GatewayResetDevice(ctx context.Context, identifier string) error {
|
||
device, imei, err := s.getGatewayDevice(ctx, identifier)
|
||
if err != nil {
|
||
s.logDeviceOperation(
|
||
ctx,
|
||
constants.AssetAuditOpDeviceReset,
|
||
"设备恢复出厂失败",
|
||
constants.AssetAuditResultFailed,
|
||
nil,
|
||
nil,
|
||
map[string]any{"identifier": identifier},
|
||
0,
|
||
0,
|
||
0,
|
||
err,
|
||
)
|
||
return err
|
||
}
|
||
if err = s.gatewayClient.ResetDevice(ctx, &gateway.DeviceOperationReq{
|
||
DeviceID: imei,
|
||
}); err != nil {
|
||
s.logDeviceOperation(
|
||
ctx,
|
||
constants.AssetAuditOpDeviceReset,
|
||
"设备恢复出厂失败",
|
||
constants.AssetAuditResultFailed,
|
||
device,
|
||
map[string]any{"device": deviceSnapshot(device)},
|
||
nil,
|
||
0,
|
||
0,
|
||
0,
|
||
err,
|
||
)
|
||
return err
|
||
}
|
||
|
||
s.logDeviceOperation(
|
||
ctx,
|
||
constants.AssetAuditOpDeviceReset,
|
||
"设备恢复出厂",
|
||
constants.AssetAuditResultSuccess,
|
||
device,
|
||
map[string]any{"device": deviceSnapshot(device)},
|
||
nil,
|
||
0,
|
||
0,
|
||
0,
|
||
nil,
|
||
)
|
||
return nil
|
||
}
|
||
|
||
// GatewaySwitchMode 通过标识符设置设备切卡模式
|
||
func (s *Service) GatewaySwitchMode(ctx context.Context, identifier string, req *dto.SetSwitchModeRequest) error {
|
||
device, imei, err := s.getGatewayDevice(ctx, identifier)
|
||
if err != nil {
|
||
s.logDeviceOperation(
|
||
ctx,
|
||
constants.AssetAuditOpDeviceSwitchMode,
|
||
"设备切卡模式切换失败",
|
||
constants.AssetAuditResultFailed,
|
||
nil,
|
||
nil,
|
||
map[string]any{
|
||
"identifier": identifier,
|
||
"switch_mode": req.SwitchMode,
|
||
},
|
||
0,
|
||
0,
|
||
0,
|
||
err,
|
||
)
|
||
return err
|
||
}
|
||
if err = s.gatewayClient.SwitchMode(ctx, &gateway.SwitchModeReq{
|
||
CardNo: imei,
|
||
SwitchMode: req.SwitchMode,
|
||
}); err != nil {
|
||
s.logDeviceOperation(
|
||
ctx,
|
||
constants.AssetAuditOpDeviceSwitchMode,
|
||
"设备切卡模式切换失败",
|
||
constants.AssetAuditResultFailed,
|
||
device,
|
||
map[string]any{"device": deviceSnapshot(device)},
|
||
map[string]any{"switch_mode": req.SwitchMode},
|
||
0,
|
||
0,
|
||
0,
|
||
err,
|
||
)
|
||
return err
|
||
}
|
||
|
||
s.logDeviceOperation(
|
||
ctx,
|
||
constants.AssetAuditOpDeviceSwitchMode,
|
||
"设备切卡模式切换",
|
||
constants.AssetAuditResultSuccess,
|
||
device,
|
||
map[string]any{"device": deviceSnapshot(device)},
|
||
map[string]any{"switch_mode": req.SwitchMode},
|
||
0,
|
||
0,
|
||
0,
|
||
nil,
|
||
)
|
||
return nil
|
||
}
|