PgAddress.xaml.cs 7.64 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2
using CommunityToolkit.Mvvm.Input;
using FObjBase;
3
using System;
潘栩锋's avatar
潘栩锋 committed
4 5 6
using System.Collections.Generic;
using System.ComponentModel;
using System.Text.RegularExpressions;
7
using System.Windows;
潘栩锋's avatar
潘栩锋 committed
8 9 10 11 12 13 14 15 16 17 18 19 20
using System.Windows.Controls;

namespace FLY.Thick.Base.UI
{
    /// <summary>
    /// PgAddress.xaml 的交互逻辑
    /// </summary>
    public partial class PgAddress : Page
    {
        PgAddressVm viewModel;
        public PgAddress()
        {
            InitializeComponent();
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
            if (System.ComponentModel.LicenseManager.UsageMode != System.ComponentModel.LicenseUsageMode.Runtime)
                return;

            this.Loaded += UcGridGraph_Loaded;
            this.Unloaded += UcGridGraph_Unloaded;
        }

        private void UcGridGraph_Unloaded(object sender, RoutedEventArgs e)
        {
            viewModel.DisposeBinding();
        }

        private void UcGridGraph_Loaded(object sender, RoutedEventArgs e)
        {
            viewModel.SetBinding();
潘栩锋's avatar
潘栩锋 committed
36
        }
37

潘栩锋's avatar
潘栩锋 committed
38
        [Unity.InjectionMethod]
潘栩锋's avatar
潘栩锋 committed
39
        public void Init()
潘栩锋's avatar
潘栩锋 committed
40 41 42 43 44 45 46 47 48 49 50 51
        {
            viewModel = new PgAddressVm();
            viewModel.Init();
            this.DataContext = viewModel;
        }
    }
    public class PgAddressVm : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public List<ConnAddr> ConnAddrs { get; }

潘栩锋's avatar
潘栩锋 committed
52
        public RelayCommand ApplyCmd { get; }
潘栩锋's avatar
潘栩锋 committed
53 54 55

        FObjServiceClientManager serviceClientManager;

56 57 58 59 60 61 62
        Dictionary<object, List<Misc.BindingOperations.PropertyChangedEventContexts>> bindingConexts = new Dictionary<object, List<Misc.BindingOperations.PropertyChangedEventContexts>>();

        /// <summary>
        /// 数据已经绑定了
        /// </summary>
        bool isBinding;

潘栩锋's avatar
潘栩锋 committed
63
        public PgAddressVm()
潘栩锋's avatar
潘栩锋 committed
64 65 66 67 68 69
        {
            ApplyCmd = new RelayCommand(Apply);
            ConnAddrs = new List<ConnAddr>();

        }

潘栩锋's avatar
潘栩锋 committed
70
        public void Init()
潘栩锋's avatar
潘栩锋 committed
71 72 73
        {
            serviceClientManager = FObjServiceClientManager.Instance;

潘栩锋's avatar
潘栩锋 committed
74 75

            foreach (var ca in serviceClientManager.ConnAddrs)
潘栩锋's avatar
潘栩锋 committed
76 77 78
            {
                ConnAddrs.Add(new ConnAddr() { ConnName = ca.ConnName, Address = ca.Addr });
            };
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108

            SetBinding();
        }

        /// <summary>
        /// 参数绑定
        /// </summary>
        public void SetBinding()
        {
            if (isBinding)
                return;
            isBinding = true;

            //下面全部event保存在bindingConexts
            Misc.BindingOperations.StartMarkdownEvents(bindingConexts);
            //备份event完毕, 必须关闭记录
            Misc.BindingOperations.StopMarkdownEvents();
        }
        /// <summary>
        /// 解除绑定
        /// </summary>
        public void DisposeBinding()
        {
            if (!isBinding)//已经解除绑定了
                return;
            isBinding = false;

            //释放全部 event
            Misc.BindingOperations.DisposeEventTargetObject(bindingConexts);

潘栩锋's avatar
潘栩锋 committed
109 110 111 112
        }

        private void Apply()
        {
潘栩锋's avatar
潘栩锋 committed
113 114 115
            if (!WdPassword.Authorize("Address"))
                return;

116 117
            Regex regex_hostname = new Regex(@"(\S+)\:(\d+)");
            Regex regex_ip = new Regex(@"(\d+)\.(\d+)\.(\d+)\.(\d+)\:(\d+)");
潘栩锋's avatar
潘栩锋 committed
118
            //检测地址格式
潘栩锋's avatar
潘栩锋 committed
119
            foreach (var ca in ConnAddrs)
潘栩锋's avatar
潘栩锋 committed
120
            {
121 122 123 124
                //把地址的 : 替换为 :
                ca.Address = ca.Address.Replace(":", ":");
                Match m_hostname = regex_hostname.Match(ca.Address);
                Match m_ip = regex_ip.Match(ca.Address);
潘栩锋's avatar
潘栩锋 committed
125 126

                if (!m_hostname.Success)
潘栩锋's avatar
潘栩锋 committed
127 128
                {
                    //格式错误
129 130
                    MessageBox.Show(
                        $"{ca.ConnName} 地址格式错误! 正确为 IP地址:端口号 或 域名:端口号 , 请检查",
潘栩锋's avatar
潘栩锋 committed
131
                        "格式错误",
132
                        MessageBoxButton.OK, MessageBoxImage.Error);
潘栩锋's avatar
潘栩锋 committed
133 134
                    return;
                }
135 136

                if (m_ip.Success)
潘栩锋's avatar
潘栩锋 committed
137
                {
138 139
                    //这个IP
                    for (int i = 0; i < 4; i++)
潘栩锋's avatar
潘栩锋 committed
140
                    {
141 142 143
                        if (!int.TryParse(m_ip.Groups[1 + i].Value, out int num))
                        {
                            //格式错误
144
                            MessageBox.Show($"{ca.ConnName} 地址格式错误! 第{i + 1} 位IP段 出错",
145
                                "格式错误",
146
                        MessageBoxButton.OK, MessageBoxImage.Error);
147 148 149 150 151
                            return;
                        }
                        if (num < 0 || num > 255)
                        {
                            //格式错误
152 153
                            MessageBox.Show(
                                $"{ca.ConnName} 地址格式错误! 第{i + 1}位IP段应该在 0~255 之间",  
154
                                "格式错误",
155
                        MessageBoxButton.OK, MessageBoxImage.Error); 
156 157
                            return;
                        }
潘栩锋's avatar
潘栩锋 committed
158 159
                    }
                }
潘栩锋's avatar
潘栩锋 committed
160

161 162


潘栩锋's avatar
潘栩锋 committed
163
                {
164
                    if (!int.TryParse(m_hostname.Groups[2].Value, out int port))
潘栩锋's avatar
潘栩锋 committed
165 166
                    {
                        //格式错误
167
                        MessageBox.Show(
潘栩锋's avatar
潘栩锋 committed
168 169 170 171 172 173
                            $"{ca.ConnName} 地址格式错误! 端口出错");
                        return;
                    }
                    if (port < 1 || port > 65535)
                    {
                        //格式错误
174 175 176 177
                        MessageBox.Show(
                            $"{ca.ConnName} 地址格式错误! 端口应该在 1~65535 之间",
                                "格式错误",
                        MessageBoxButton.OK, MessageBoxImage.Error);
潘栩锋's avatar
潘栩锋 committed
178 179 180
                        return;
                    }
                }
181 182

                var ep = Misc.StringConverter.ToIPEndPoint(ca.Address);
潘栩锋's avatar
潘栩锋 committed
183 184
                if (ep == null)
                {
185
                    //不能转为ep
186 187 188 189
                    MessageBox.Show(
                    $"{ca.ConnName} 不能有效转换IPv4",
                                "地址出错",
                        MessageBoxButton.OK, MessageBoxImage.Error);
190 191
                    return;
                }
潘栩锋's avatar
潘栩锋 committed
192 193 194 195 196 197 198 199 200
            }

            int modifyCnt = 0;
            //检查完毕
            foreach (var ca in ConnAddrs)
            {
                var objCa = serviceClientManager.ConnAddrs.Find(_ca => _ca.ConnName == ca.ConnName);
                if (objCa == null)
                    continue;
潘栩锋's avatar
潘栩锋 committed
201
                if (objCa.Addr != ca.Address)
潘栩锋's avatar
潘栩锋 committed
202 203 204 205 206 207 208 209 210 211 212
                {
                    modifyCnt++;
                    //重新连接
                    serviceClientManager.ReConnect(ca.ConnName, ca.Address);
                }
            };

            if (modifyCnt > 0)
            {
                serviceClientManager.Save();

213 214
                string tit = (string)Application.Current.TryFindResource("str.PgAddress.AmendSuccessfully");
                FLY.ControlLibrary.Window_Tip.Show(tit, null, TimeSpan.FromSeconds(2));
潘栩锋's avatar
潘栩锋 committed
215 216
                return;
            }
潘栩锋's avatar
潘栩锋 committed
217
            else
潘栩锋's avatar
潘栩锋 committed
218
            {
219 220
                string tit = (string)Application.Current.TryFindResource("str.PgAddress.AmendFailed");
                FLY.ControlLibrary.Window_Tip.Show(tit, null, TimeSpan.FromSeconds(2));
潘栩锋's avatar
潘栩锋 committed
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
                return;
            }
        }
    }
    public class ConnAddr : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        /// <summary>
        /// 连接器名称
        /// </summary>
        public string ConnName { get; set; }

        /// <summary>
        /// 设备地址
        /// </summary>
        public string Address { get; set; }
潘栩锋's avatar
潘栩锋 committed
237

潘栩锋's avatar
潘栩锋 committed
238 239
    }
}