From 9d1660270272180cd71147d6ddd53d1382643d2e Mon Sep 17 00:00:00 2001
From: wenxiangchao <wenxiangchao@ihengxin.com.cn>
Date: 星期一, 02 十一月 2026 18:00:14 +0800
Subject: [PATCH] merge: 并入远程 Initial commit 作为历史根节点

---
 Plugin.CurrentSensorControl/SingleSensor.xaml.cs |  180 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 180 insertions(+), 0 deletions(-)

diff --git a/Plugin.CurrentSensorControl/SingleSensor.xaml.cs b/Plugin.CurrentSensorControl/SingleSensor.xaml.cs
new file mode 100644
index 0000000..76ad6ae
--- /dev/null
+++ b/Plugin.CurrentSensorControl/SingleSensor.xaml.cs
@@ -0,0 +1,180 @@
+锘縰sing CommunityToolkit.Mvvm.ComponentModel;
+using HslCommunication;
+using HslCommunication.ModBus;
+using System.Windows;
+using System.Windows.Controls;
+
+namespace Plugin.CurrentSensorControl
+{
+    /// <summary>
+    /// SingleSensor.xaml 鐨勪氦浜掗�昏緫
+    /// </summary>
+    [ObservableObject]
+    public partial class SingleSensor : UserControl
+    {
+        private static Dictionary<string, ModbusTcpNet> clients = new Dictionary<string, ModbusTcpNet>();
+        private static Dictionary<ModbusTcpNet, bool> status = new Dictionary<ModbusTcpNet, bool>();
+
+        [ObservableProperty]
+        private string sensorName;
+
+        [ObservableProperty]
+        private bool isConnected;
+
+        [ObservableProperty]
+        private string current;
+
+        [ObservableProperty]
+        private string ip;
+
+        [ObservableProperty]
+        private int port;
+
+
+
+        /// <summary>
+        /// 鍦板潃(绔欏彿)
+        /// </summary>
+        [ObservableProperty]
+        private int address;
+
+        [ObservableProperty]
+        private string errMsg;
+
+        /// <summary>
+        /// 閫氶亾
+        /// </summary>
+        [ObservableProperty]
+        private int channel;
+
+        private ModbusTcpNet client;
+
+
+        public SingleSensor()
+        {
+            DataContext = this;
+            InitializeComponent();
+        }
+
+        public SingleSensor(string name, string ip, int port, int address, int channel) : this()
+        {
+            SensorName = name;
+            Ip = ip;
+            Port = port;
+            Address = address;
+            Channel = channel;
+            string key = $"{Ip}:{Port}";
+            if (!clients.ContainsKey(key))
+            {
+                client = new ModbusTcpNet(Ip, Port);
+                clients[key] = client;
+            }
+            else
+            {
+                client = clients[key];
+            }
+        }
+
+        private void controlBtn_Click(object sender, RoutedEventArgs e)
+        {
+            if (IsConnected)
+            {
+                this.Close();
+            }
+            else
+            {
+                Task.Factory.StartNew(() => { this.Connect(); });
+            }
+        }
+
+        public void Connect()
+        {
+            lock (client)
+            {
+                if (!status.ContainsKey(client) || !status[client])
+                {
+                    var res = client.ConnectServer();
+                    IsConnected = res.IsSuccess;
+                    status[client] = IsConnected;
+                }
+                else
+                {
+                    IsConnected = status[client];
+                }
+            }
+        }
+
+        public void ReadCurrent()
+        {
+            //if (status.ContainsKey(client) && status[client])
+            //{
+            //    OperateResult<double[]> res;
+            //    lock (client)
+            //    {
+            //        res = client.ReadDouble(channel.ToString(), 1);
+            //    }
+            //    Dispatcher.Invoke(() =>
+            //    {
+            //        if (res.IsSuccess)
+            //        {
+            //            Current = res.Content[0].ToString();
+            //            ErrMsg = string.Empty;
+            //        }
+            //        else
+            //        {
+            //            ErrMsg = res.Message;
+            //            //Close();
+            //        }
+            //    });
+            //}
+            //else
+            //{
+            //    IsConnected = false;
+            //}
+            var sb = status[client];
+            if (IsConnected)
+            {
+                OperateResult<ushort> res;
+                lock (client)
+                {
+                    res = client.ReadUInt16(channel.ToString());
+                }
+                Dispatcher.Invoke(() =>
+                {
+                    if (res.IsSuccess)
+                    {
+                        Current = (res.Content / 1000d).ToString();
+                        ErrMsg = string.Empty;
+                    }
+                    else
+                    {
+                        ErrMsg = res.Message;
+                        //Close();
+                    }
+                });
+            }
+        }
+
+        public void Close()
+        {
+            lock (client)
+            {
+                var connected = status[client];
+                if (connected)
+                {
+                    var res = client.ConnectClose();
+                    if (res.IsSuccess)
+                    {
+                        client.Dispose();
+                    }
+                    IsConnected = !res.IsSuccess;
+                    status[client] = IsConnected;
+                }
+                else
+                {
+                    IsConnected = connected;
+                }
+            }
+        }
+    }
+}

--
Gitblit v1.9.1