| | |
| | | package service |
| | | |
| | | import ( |
| | | "apsClient/conf" |
| | | "apsClient/model" |
| | | "apsClient/model/request" |
| | | "apsClient/pkg/ecode" |
| | | "apsClient/pkg/logx" |
| | | "apsClient/pkg/structx" |
| | | "encoding/json" |
| | | "gorm.io/gorm" |
| | | "errors" |
| | | "github.com/jinzhu/gorm" |
| | | ) |
| | | |
| | | type DevicePlcService struct{} |
| | |
| | | return DevicePlcService{} |
| | | } |
| | | |
| | | func (DevicePlcService) GetDevicePlc() (*model.DevicePlc, int) { |
| | | DevicePlc, err := model.NewDevicePlcSearch().SetOrder("id desc").First() |
| | | func (slf DevicePlcService) GetDevicePlc() (*model.DevicePlc, int) { |
| | | devicePlc, err := model.NewDevicePlcSearch().SetDeviceId(conf.Conf.CurrentDeviceID).First() |
| | | if err == gorm.ErrRecordNotFound { |
| | | logx.Errorf("GetDevicePlc failed:%v", err) |
| | | return &model.DevicePlc{ |
| | | Id: 0, |
| | | DeviceID: "", |
| | | Brand: "", |
| | | Method: "", |
| | | Address: "", |
| | | Port: 0, |
| | | BaudRate: 0, |
| | | SerialName: "", |
| | | DataBit: 0, |
| | | StopBit: 0, |
| | | Parity: 0, |
| | | IsOpen: false, |
| | | Detail: "", |
| | | Details: make([]*model.DevicePlcAddress, 0), |
| | | }, ecode.OK |
| | | } |
| | | if err != nil { |
| | | logx.Errorf("GetDevicePlc failed:%v", err) |
| | | return nil, ecode.DBErr |
| | | } |
| | | if len(devicePlc.Details) == 0 { |
| | | return nil, ecode.PlcAddressError |
| | | } |
| | | |
| | | return DevicePlc, ecode.OK |
| | | return devicePlc, ecode.OK |
| | | } |
| | | |
| | | func (slf DevicePlcService) GetDeviceChannelAmount() (int, error) { |
| | | device, err := model.NewDeviceSearch().SetDeviceId(conf.Conf.CurrentDeviceID).First() |
| | | if err != nil { |
| | | return 0, errors.New("没有找到当前设备") |
| | | } |
| | | return device.ExtChannelAmount + 1, nil |
| | | } |
| | | |
| | | func (DevicePlcService) UpdateDevicePlc(plc *request.UpdatePlc) int { |
| | |
| | | return ecode.ParamsErr |
| | | } |
| | | record.Detail = string(detail) |
| | | |
| | | if plc.Id == 0 { |
| | | if record.ID == 0 { |
| | | err = model.NewDevicePlcSearch().Create(&record) |
| | | } else { |
| | | err = model.NewDevicePlcSearch().SetId(plc.Id).Save(&record) |
| | | err = model.NewDevicePlcSearch().Save(&record) |
| | | } |
| | | |
| | | if err != nil { |