From 2ea23e2364f742f528fb8b97657212c174d1bbd7 Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期六, 21 十月 2023 14:06:59 +0800
Subject: [PATCH] 增加切换serf状态接口(调试用)

---
 service/device_plc.go |   37 +++++++++++++++++++++++++------------
 1 files changed, 25 insertions(+), 12 deletions(-)

diff --git a/service/device_plc.go b/service/device_plc.go
index 7ec6fee..fc5fadc 100644
--- a/service/device_plc.go
+++ b/service/device_plc.go
@@ -1,13 +1,15 @@
 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{}
@@ -16,19 +18,19 @@
 	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.System.DeviceId).First()
 	if err == gorm.ErrRecordNotFound {
 		return &model.DevicePlc{
-			Brand:     "",
-			Method:    "",
-			PortName:  "",
-			Frequency: 0,
-			Address:   "",
-			Port:      0,
-			IsOpen:    false,
-			Detail:    "",
-			Details:   make([]*model.DevicePlcAddress, 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)

--
Gitblit v1.8.0