zhangqian
2023-09-27 b331c9990a0396301e934daffe095f99d62d1c89
service/device_plc.go
@@ -7,6 +7,7 @@
   "apsClient/pkg/logx"
   "apsClient/pkg/structx"
   "encoding/json"
   "errors"
   "gorm.io/gorm"
)
@@ -16,19 +17,20 @@
   return DevicePlcService{}
}
func (DevicePlcService) GetDevicePlc() (*model.DevicePlc, int) {
func (slf DevicePlcService) GetDevicePlc() (*model.DevicePlc, int) {
   DevicePlc, err := model.NewDevicePlcSearch().SetOrder("id desc").First()
   if err == gorm.ErrRecordNotFound {
      return &model.DevicePlc{
         Brand:     "",
         Method:    "",
         PortName:  "",
         Frequency: 0,
         Address:   "",
         Port:      0,
         IsOpen:    false,
         Detail:    "",
         Details:   make([]*model.DevicePlcAddress, 0),
         Id:         0,
         Brand:      "",
         Method:     "",
         Address:    "",
         Port:       0,
         BaudRate:   0,
         SerialName: "",
         IsOpen:     false,
         Detail:     "",
         Details:    make([]*model.DevicePlcAddress, 0),
      }, ecode.OK
   }
   if err != nil {
@@ -38,6 +40,17 @@
   return DevicePlc, ecode.OK
}
func (slf DevicePlcService) GetDeviceChannelAmount() (int, error) {
   plcConfig, code := slf.GetDevicePlc()
   if code != ecode.OK || plcConfig.Id == 0 {
      return 0, errors.New("未获取到PLC地址,请在工艺模型库中上传!")
   }
   if len(plcConfig.Details) == 0 {
      return 0, errors.New("未获取到PLC地址,请在工艺模型库中上传!")
   }
   return len(plcConfig.Details) / 2, nil
}
func (DevicePlcService) UpdateDevicePlc(plc *request.UpdatePlc) int {
   var record model.DevicePlc
   err := structx.AssignTo(plc, &record)