From 0940f31b7eba1cf6b6a8c1fca72d97cf686e79f2 Mon Sep 17 00:00:00 2001
From: alielie <alielie@git.com>
Date: 星期五, 04 七月 2025 10:43:00 +0800
Subject: [PATCH] 优化插件,完善TreeListView

---
 Plugin.ServoManage/ServoManage.xaml.cs |  114 +++++++++++++++++++++++++++++++++++++++-----------------
 1 files changed, 79 insertions(+), 35 deletions(-)

diff --git a/Plugin.ServoManage/ServoManage.xaml.cs b/Plugin.ServoManage/ServoManage.xaml.cs
index 76a460e..10e593d 100644
--- a/Plugin.ServoManage/ServoManage.xaml.cs
+++ b/Plugin.ServoManage/ServoManage.xaml.cs
@@ -1,9 +1,12 @@
-锘縰sing HMI.Base;
+锘縰sing ConnectionUtil;
+using HMI.Base;
+using HMI.Core.Entity;
+using HMI.Core.Manager;
 using HMI.Core.Setting;
 using HMI.Core.Util;
-using HMI.ParameterManage.Entity;
-using HMI.ParameterManage.Util;
+using HMI.UI;
 using HMI.UI.Util;
+using OPCConnection;
 using Plugin.ServoManage.Entity;
 using System;
 using System.Collections.Generic;
@@ -15,6 +18,7 @@
 using System.Timers;
 using System.Windows;
 using System.Windows.Controls;
+using System.Windows.Threading;
 
 namespace Plugin.ServoManage
 {
@@ -89,10 +93,11 @@
         /// <summary>
         /// 瀹炰緥鍖栨湇鍔″櫒鎺ュ彛鍑芥暟
         /// </summary>
-        private OpcInterface.OpcInterface lTSCADAApi = new OpcInterface.OpcInterface();
+        private SCADAApi lTSCADAApi = new OPCClient();
         private bool isActived = false;
         private System.Timers.Timer motorMonitorTimer;
 
+        #region ServoData
         private string enable;
         [IsStatus]
         public string Enable
@@ -345,28 +350,20 @@
                 Set(ref posCode, value);
             }
         }
+        #endregion
 
         public ServoManage()
         {
             DataContext = this;
             motorMonitorTimer = new System.Timers.Timer();
             motorMonitorTimer.Elapsed += MotorMonitorTimer_Elapsed;
+            motorMonitorTimer.Interval = 100;
             InitializeComponent();
         }
 
         private void MotorMonitorTimer_Elapsed(object sender, ElapsedEventArgs e)
         {
-            this.motorMonitorTimer.Enabled = false;
-            try
-            {
-                RefreshServoStatus();
-                this.motorMonitorTimer.Enabled = true;
-            }
-            catch (Exception ex)
-            {
-                this.motorMonitorTimer.Enabled = true;
-                return;
-            }
+            RefreshServoStatus();
         }
 
         public override void SetBaseFolder(string folder)
@@ -387,8 +384,7 @@
                     {
                         if (lTSCADAApi.Connect2("127.0.0.1", 6543, 18888, out string errMsg))
                         {
-                            this.motorMonitorTimer.Interval = 100;
-                            this.motorMonitorTimer.Start();
+                            motorMonitorTimer.Enabled = true;
                             isActived = true;
                         }
                         else
@@ -408,6 +404,16 @@
                     MsgBoxHelper2.Error(ex.Message);
                 }
             }
+            else
+            {
+                motorMonitorTimer.Enabled = true;
+            }
+        }
+
+        public override void DeActive()
+        {
+            motorMonitorTimer.Enabled = false;
+            base.DeActive();
         }
 
         private void UserControl_Loaded(object sender, RoutedEventArgs e)
@@ -435,7 +441,6 @@
                 case "rfhServoBtn":
                     LoadData();
                     break;
-
                 case "addProductBtn":
                     OpenProductModal(null);
                     break;
@@ -515,12 +520,12 @@
         public ObservableCollection<ServoProductData> ReadCommonProducts()
         {
             ObservableCollection<ServoProductData> datas = new ObservableCollection<ServoProductData>();
-            List<ProductData> products = ParameterManager.GetProducts<List<ProductData>>();
+            List<ProductData> products = ProductManager.GetProducts<List<ProductData>>();
             if (products != null && products.Count > 0)
             {
                 foreach (var product in products)
                 {
-                    var parameter = ParameterManager.GetParametersByProductName(product.Name)
+                    var parameter = ProductManager.GetParametersByProductName(product.Name)
                          ?.FirstOrDefault(p => p.Name == "ProgramCode");
                     if (parameter != null)
                     {
@@ -580,7 +585,8 @@
 
         private void RefreshServoStatus()
         {
-            Dispatcher.Invoke(() =>
+            this.motorMonitorTimer.Enabled = false;
+            try
             {
                 if (SelectedServo == null) return;
                 PropertyInfo[] properties = this.GetType().GetProperties();
@@ -590,19 +596,29 @@
                     {
                         continue;
                     }
-
-                    if (prop.Name == "SelectedProduct")
+                    Dispatcher.InvokeAsync(new Action(() =>
                     {
-                        var data = GetBitValue($"{SelectedServo.Name}_sRecipeCode");
-                        SelectedProduct = data;
-                    }
-                    else
-                    {
-                        var data = GetBitValue($"{SelectedServo.Name}_{prop.Name}");
-                        prop.SetValue(this, data);
-                    }
+                        if (prop.Name == "SelectedProduct")
+                        {
+                            var data = GetBitValue($"{SelectedServo.Name}_sRecipeCode");
+                            SelectedProduct = data;
+                        }
+                        else
+                        {
+                            var data = GetBitValue($"{SelectedServo.Name}_{prop.Name}");
+                            prop.SetValue(this, data);
+                        }
+                    }), DispatcherPriority.SystemIdle);
                 }
-            });
+            }
+            catch (Exception ex)
+            {
+
+            }
+            finally
+            {
+                this.motorMonitorTimer.Enabled = true;
+            }
         }
 
         /// <summary>
@@ -647,7 +663,6 @@
             Button btn = sender as Button;
             try
             {
-                motorMonitorTimer.Enabled = false;
                 switch (btn.Tag)
                 {
                     case "Enable":
@@ -657,7 +672,7 @@
                         EnableDoClick(btn.Tag.ToString());
                         break;
                     default:
-                        DoSignClick(btn.Tag?.ToString());
+                        //DoSignClick(btn.Tag?.ToString());
                         break;
                 }
             }
@@ -668,7 +683,6 @@
             }
             finally
             {
-                motorMonitorTimer.Enabled = true;
             }
         }
 
@@ -720,5 +734,35 @@
                 SetBitValue($"{SelectedServo?.Name}_{tag}", np.Data?.ToString());
             }
         }
+
+        private void Button_PreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
+        {
+            Button btn = sender as Button;
+            var tag = btn.Tag.ToString();
+            try
+            {
+                motorMonitorTimer.Enabled = false;
+                SetBitValue($"{SelectedServo?.Name}_{tag}", "true");
+            }
+            finally
+            {
+                motorMonitorTimer.Enabled = true;
+            }
+        }
+
+        private void Button_PreviewMouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
+        {
+            Button btn = sender as Button;
+            var tag = btn.Tag.ToString();
+            try
+            {
+                motorMonitorTimer.Enabled = false;
+                SetBitValue($"{SelectedServo?.Name}_{tag}", "false");
+            }
+            finally
+            {
+                motorMonitorTimer.Enabled = true;
+            }
+        }
     }
 }

--
Gitblit v1.9.1