From e3868d4d61ded0e76fc8fe3fdacdd9e6d9113cf1 Mon Sep 17 00:00:00 2001
From: alielie <alielie@git.com>
Date: 星期一, 13 十月 2025 08:50:15 +0800
Subject: [PATCH] 优化HMI;

---
 Plugin.DOControl/DOControl.xaml.cs |   82 ++++++++++++++++++++++++++---------------
 1 files changed, 52 insertions(+), 30 deletions(-)

diff --git a/Plugin.DOControl/DOControl.xaml.cs b/Plugin.DOControl/DOControl.xaml.cs
index 4ec40cc..405c4ff 100644
--- a/Plugin.DOControl/DOControl.xaml.cs
+++ b/Plugin.DOControl/DOControl.xaml.cs
@@ -1,9 +1,11 @@
-锘縰sing HMI.Base;
+锘縰sing ConnectionUtil;
+using HMI.Base;
 using HMI.Core.Setting;
 using HMI.Core.Util;
 using HMI.UI;
 using HMI.UI.Util;
 using log4net;
+using OPCConnection;
 using Panuon.WPF.UI;
 using Plugin.DOControl.Entity;
 using System;
@@ -13,7 +15,6 @@
 using System.Linq;
 using System.Windows;
 using System.Windows.Controls;
-using System.Windows.Documents;
 using System.Windows.Media;
 
 namespace Plugin.DOControl
@@ -26,7 +27,8 @@
         /// <summary>
         /// 瀹炰緥鍖栨湇鍔″櫒鎺ュ彛鍑芥暟
         /// </summary>
-        private OpcInterface.OpcInterface lTSCADAApi = new OpcInterface.OpcInterface();
+        private SCADAApi lTSCADAApi = new OPCClient();
+        //private PipeClient pipeClient = new PipeClient("DAService");
 
         private ObservableCollection<string> groups;
         public ObservableCollection<string> Groups
@@ -98,8 +100,14 @@
 
         public override void DoClose(object sender, EventArgs e)
         {
-            motorMonitorTimer.Enabled = false;
-            base.DoClose(sender, e);
+            try
+            {
+
+                motorMonitorTimer.Enabled = false;
+                base.DoClose(sender, e);
+            }
+            catch { 
+            }
         }
 
         public static string FilePath { get; private set; }
@@ -124,15 +132,14 @@
 
         private void LoadData()
         {
+            ObservableCollection<DOControlData> datas = new ObservableCollection<DOControlData>();
             try
             {
                 motorMonitorTimer.Enabled = false;
-                ObservableCollection<DOControlData> datas = new ObservableCollection<DOControlData>();
                 if (File.Exists(FilePath))
                 {
                     datas = XmlHelper.DeserializeFromXml<ObservableCollection<DOControlData>>(FilePath);
                 }
-                Datasource = datas;
                 GroupMap = datas.GroupBy(x => x.Group)
                    .ToDictionary(
                        group => group.Key,
@@ -157,6 +164,7 @@
             {
                 motorMonitorTimer.Enabled = true;
             }
+            Datasource = datas;
         }
 
         private void MotorMonitorTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
@@ -174,7 +182,7 @@
                         string value = GetPlcValue(data.Address);
                         switch (data.ControlType)
                         {
-                            case ControlType.Button:
+                            case ControlType.BOOL:
                                 var color = Application.Current.FindResource("VerBar") as Brush;
                                 if (value == data.PressDownValue)
                                 {
@@ -183,12 +191,12 @@
                             ((Button)ui).Background = color;
                                 ((Button)ui).Content = value;
                                 break;
-                            case ControlType.Int16:
-                            case ControlType.Int32:
-                            case ControlType.Real:
-                            case ControlType.LReal:
-                            case ControlType.String:
-                            case ControlType.WString:
+                            case ControlType.INT:
+                            case ControlType.WORD:
+                            case ControlType.REAL:
+                            case ControlType.LREAL:
+                            case ControlType.STRING:
+                            case ControlType.WSTRING:
                                 ((Button)ui).Content = value;
                                 break;
                             default:
@@ -235,19 +243,14 @@
                         FrameworkElement ui = null;
                         switch (data.ControlType)
                         {
-                            case ControlType.Button:
-                            case ControlType.ClickButton:
+                            case ControlType.BOOL:
+                            case ControlType.PULSE:
                                 ui = CreateButton(data);
                                 break;
-                            case ControlType.Int16:
-                            case ControlType.Int32:
-                            case ControlType.Real:
-                            case ControlType.LReal:
-                            case ControlType.String:
-                            case ControlType.WString:
-                                ui = CreateNumberInput(data);
+                            case ControlType.TIME:
                                 break;
                             default:
+                                ui = CreateInput(data);
                                 break;
                         }
                         if (ui != null)
@@ -294,11 +297,11 @@
                 MinWidth = 180,
                 MinHeight = 40
             };
-            if (data.ControlType == ControlType.Button)
+            if (data.ControlType == ControlType.BOOL)
             {
                 btn.Click += ControlButton_Click;
             }
-            else if (data.ControlType == ControlType.ClickButton)
+            else if (data.ControlType == ControlType.PULSE)
             {
                 btn.Content = data.Address;
                 btn.PreviewMouseLeftButtonDown += Btn_MouseLeftButtonDown;
@@ -341,7 +344,7 @@
             }
         }
 
-        private Button CreateNumberInput(DOControlData data)
+        private Button CreateInput(DOControlData data)
         {
             Style btnStyle = Application.Current.FindResource("PrimaryButton") as Style;
             var tb = new Button
@@ -361,7 +364,7 @@
                 return tb;
             }
 
-            if (data.ControlType == ControlType.String || data.ControlType == ControlType.WString)
+            if (data.ControlType == ControlType.STRING || data.ControlType == ControlType.WSTRING)
             {
                 tb.Click += StringInput_Click;
             }
@@ -382,8 +385,10 @@
         {
             if (!lTSCADAApi.DoFctWriteSingle(key, value, out string errMsg))
             {
-                throw new Exception(errMsg);
+                ToastHelper.Error(this, errMsg);
             }
+
+            //lTSCADAApi.SendMessage("asdsadasda", out _);
         }
 
         private string GetPlcValue(string key)
@@ -396,6 +401,7 @@
             {
                 return null;
             }
+            return null;
         }
 
         private void ControlButton_Click(object sender, RoutedEventArgs args)
@@ -437,7 +443,8 @@
             NumberPanel np = new NumberPanel
             {
                 Title = data.Name,
-                Owner = Window.GetWindow(this)
+                Owner = Window.GetWindow(this),
+                DotEnabled = data.ControlType == ControlType.REAL || data.ControlType == ControlType.LREAL
             };
             if (np.Open(tb.Content?.ToString()) == true)
             {
@@ -450,7 +457,7 @@
             LoadData();
             if (SystemSetting.Instance.PlcEnabled)
             {
-                if (lTSCADAApi.Connect2("127.0.0.1", 6543, 18888, out string errMsg))
+                if (lTSCADAApi.Connect2("127.0.0.1", 6543, 28888, out string errMsg))
                 {
                     motorMonitorTimer.Enabled = true;
                     RefreshControl();
@@ -459,10 +466,25 @@
                 {
                     MsgBoxHelper2.Error(errMsg);
                 }
+                //try
+                //{
+                //    pipeClient.OnConnected += PipeClient_OnConnected;
+
+                //    pipeClient.Connect();
+                //}
+                //catch (Exception ex)
+                //{
+                //    MsgBoxHelper.Error(this, ex.Message);
+                //}
             }
             Loaded -= UserControl_Loaded;
         }
 
+        private void PipeClient_OnConnected(object sender, EventArgs e)
+        {
+
+        }
+
         private void Button_Click(object sender, RoutedEventArgs e)
         {
             Button btn = sender as Button;

--
Gitblit v1.9.1