From d0ded5cd9bf5070a120bad58b5be21fe2ac6a4ff Mon Sep 17 00:00:00 2001
From: C3032 <C3032@BC3032>
Date: 星期六, 20 十二月 2025 16:41:09 +0800
Subject: [PATCH] test

---
 IDViewer_2D/ProductManager.xaml.cs |  216 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 216 insertions(+), 0 deletions(-)

diff --git a/IDViewer_2D/ProductManager.xaml.cs b/IDViewer_2D/ProductManager.xaml.cs
new file mode 100644
index 0000000..9c7abc7
--- /dev/null
+++ b/IDViewer_2D/ProductManager.xaml.cs
@@ -0,0 +1,216 @@
+锘縰sing System;
+using System.Collections.Generic;
+using System.Collections.ObjectModel;
+using System.IO;
+using System.Linq;
+using System.Text;
+using System.Text.RegularExpressions;
+using System.Threading.Tasks;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Data;
+using System.Windows.Documents;
+using System.Windows.Input;
+using System.Windows.Media;
+using System.Windows.Media.Imaging;
+using System.Windows.Shapes;
+using SmartScanner.OperateLog;
+using Newtonsoft.Json;
+using static SmartScanner.IDViewerDefines;
+using static SmartScanner.IDViewerSDK;
+namespace SmartScanner
+{
+    /// <summary>
+    /// ProductManager.xaml 鐨勪氦浜掗�昏緫
+    /// </summary>
+    public partial class ProductManager : Window
+    {
+        private readonly ObservableCollection<KeyValuePair<int, string>> _ipMapping = new ObservableCollection<KeyValuePair<int, string>>();
+        private ObservableCollection<string> _availableIPs = new ObservableCollection<string>();
+        private const string ConfigFile = "connection_order.json";
+        IDDeviceInfo[] deviceInfo = new IDDeviceInfo[100];
+
+        public ProductManager()
+        {
+            InitializeComponent();
+            long result = IDVIEWER_Init_();
+            dgMapping.ItemsSource = _ipMapping;
+            cmbIP.ItemsSource = _availableIPs;
+            RefreshDeviceList();
+        }
+        public void SetControlsEnabled(bool isEnabled)
+        {
+            // 绂佺敤鎴栧惎鐢ㄦ墍鏈夊彲缂栬緫鐨勬帶浠�
+            //LoadConfig_RJ.IsEnabled = isEnabled;
+            cmbOrder.IsEnabled = isEnabled;
+            cmbIP.IsEnabled = isEnabled;
+            DeviceRefresh_PM.IsEnabled = isEnabled;
+            AddMapping_PM.IsEnabled = isEnabled;
+            SaveConfig_PM.IsEnabled = isEnabled;
+            //LoadConfig_PM.IsEnabled = isEnabled;
+            // ... 鍏朵粬闇�瑕佹帶鍒剁殑鎺т欢
+
+        }
+
+        private void RefreshDeviceList()
+        {
+            try
+            {
+                //鑾峰彇璁惧鍒楄〃鍙ユ焺
+                IntPtr devicesListHandle = IDVIEWER_DiscoveryDevices_(500);
+
+                // 鑾峰彇璁惧鎬绘暟
+                int deviceCount = (int)IDVIEWER_GetDevicesLength_(devicesListHandle);
+
+                // 鐢熸垚椤哄簭鍙烽�夐」锛�1~N锛�
+                cmbOrder.ItemsSource = Enumerable.Range(1, deviceCount).ToList();
+                cmbOrder.SelectedIndex = 0;
+                List<string> ips = new List<string>();
+                for (int i = 0; i < deviceCount; i++)
+                {
+                    //鑾峰彇鐩告満IP
+                    IDViewerSDK.IDVIEWER_SelectIDDeviceInfo_(devicesListHandle, ref deviceInfo[i], (uint)i);
+                    string deviceIP = deviceInfo[i].cameraIP.Trim();
+                    ips.Add(deviceIP);
+                }
+                // 鑾峰彇鍙敤IP鍒楄〃
+                ips.Where(IsValidIP).ToList();
+                _availableIPs.Clear();
+                foreach (var ip in ips)
+                {
+                    _availableIPs.Add(ip);
+                }
+                tbStatus.Text = $"妫�娴嬪埌 {deviceCount} 鍙拌澶� | 鍙敤IP锛歿ips.Count} 涓�";
+            }
+            catch (Exception ex)
+            {
+                MessageBox.Show($"璁惧妫�娴嬪け璐ワ細{ex.Message}");
+            }
+        }
+
+        private void BtnAddMapping_Click(object sender, RoutedEventArgs e)
+        {
+            if (cmbOrder.SelectedItem == null || cmbIP.SelectedItem == null)
+            {
+                MessageBox.Show("璇烽�夋嫨椤哄簭鍜孖P鍦板潃");
+                return;
+            }
+
+            int selectedOrder = (int)cmbOrder.SelectedItem;
+            string selectedIP = cmbIP.SelectedItem.ToString();
+            // 妫�鏌ラ『搴忓啿绐�
+            if (_ipMapping.Any(x => x.Key == selectedOrder))
+            {
+                MessageBox.Show("璇ラ『搴忓凡鍒嗛厤锛岃鍏堢Щ闄ゅ師鏈夊垎閰�");
+                return;
+            }
+
+            // 妫�鏌P閲嶅
+            if (_ipMapping.Any(x => x.Value == selectedIP))
+            {
+                MessageBox.Show("璇P宸插垎閰嶉『搴�");
+                return;
+            }
+
+            _ipMapping.Add(new KeyValuePair<int, string>(selectedOrder, selectedIP));
+            SortMapping();
+        }
+
+        private void DeleteMapping_Click(object sender, RoutedEventArgs e)
+        {
+            if (dgMapping.SelectedItem is KeyValuePair<int, string> item)
+            {
+                _ipMapping.Remove(item);
+                SortMapping();
+            }
+        }
+
+        private void SortMapping()
+        {
+            var sorted = _ipMapping
+                .OrderBy(x => x.Key)
+                .ToList();
+
+            _ipMapping.Clear();
+            foreach (var item in sorted)
+            {
+                _ipMapping.Add(item);
+            }
+        }
+
+        private void BtnRefreshDevices_Click(object sender, RoutedEventArgs e) => RefreshDeviceList();
+
+        // 淇濆瓨/鍔犺浇鏂规硶
+        private void BtnSave_Click(object sender, RoutedEventArgs e)
+        {
+            try
+            {
+                var config = new
+                {
+                    LastModified = DateTime.Now,
+                    Mapping = _ipMapping.ToList()
+                };
+
+                File.WriteAllText(ConfigFile, JsonConvert.SerializeObject(config, Formatting.Indented));
+                MessageBox.Show("閰嶇疆淇濆瓨鎴愬姛");
+                // 璁板綍鏃ュ織
+                OperateLogService.LogOperation(
+                    "鏄犲皠琛ㄤ慨鏀�",
+                    $"鏄犲皠琛ㄤ慨鏀逛负: {ConfigFile}",
+                    null);
+            }
+            catch (Exception ex)
+            {
+                MessageBox.Show($"淇濆瓨澶辫触锛歿ex.Message}");
+            }
+        }
+        private void BtnLoad_Click(object sender, RoutedEventArgs e)
+        {
+          var loadedMapping = LoadConfig();
+            if (loadedMapping.Count > 0)
+            {
+                _ipMapping.Clear(); // 娓呯┖褰撳墠鏄犲皠
+                foreach (var item in loadedMapping)
+                {
+                    _ipMapping.Add(item); // 娣诲姞鍔犺浇鐨勯厤缃�
+                }
+                SortMapping(); // 閲嶆柊鎺掑簭
+                RefreshMappingDisplay(); // 鍒锋柊鐣岄潰鏄剧ず
+                MessageBox.Show("閰嶇疆鍔犺浇鎴愬姛");
+            }
+        }
+        private void RefreshMappingDisplay()
+        {
+            // 缁戝畾鏁版嵁鍒� DataGrid
+            dgMapping.ItemsSource = null; // 鍏堟竻绌虹粦瀹�
+            dgMapping.ItemsSource = _ipMapping.ToList(); // 閲嶆柊缁戝畾
+        }
+
+        public List<KeyValuePair<int, string>> LoadConfig()
+        {
+            try
+            {
+                if (File.Exists(ConfigFile))
+                {
+                    var json = File.ReadAllText(ConfigFile);
+                    var config = JsonConvert.DeserializeObject<dynamic>(json);
+                    var mapping = new List<KeyValuePair<int, string>>();
+                    foreach (var item in config.Mapping)
+                    {
+                        mapping.Add(new KeyValuePair<int, string>(
+                            (int)item.Key,
+                            (string)item.Value));
+                    }
+                    return mapping;
+                }
+            }
+            catch (Exception ex)
+            {
+                MessageBox.Show($"鍔犺浇鏄犲皠琛ㄥけ璐ワ細{ex.Message}", "閿欒", MessageBoxButton.OK, MessageBoxImage.Error);
+            }
+            return new List<KeyValuePair<int, string>>();
+        }
+        private static bool IsValidIP(string ip) => Regex.IsMatch(ip,
+        @"^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$");
+    }
+}

--
Gitblit v1.9.3