PgAddress.xaml.cs 6.19 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1
using FObjBase;
潘栩锋's avatar
潘栩锋 committed
2
using CommunityToolkit.Mvvm.Input;
潘栩锋's avatar
潘栩锋 committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
using System;
using System.Collections.Generic;
using System.ComponentModel;
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.Navigation;
using System.Windows.Shapes;

namespace FLY.Thick.Base.UI
{
    /// <summary>
    /// PgAddress.xaml 的交互逻辑
    /// </summary>
    public partial class PgAddress : Page
    {
        PgAddressVm viewModel;
        public PgAddress()
        {
            InitializeComponent();
        }
        [Unity.InjectionMethod]
        public void Init() 
        {
            viewModel = new PgAddressVm();
            viewModel.Init();
            this.DataContext = viewModel;
        }
    }
    public class PgAddressVm : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public List<ConnAddr> ConnAddrs { get; }

46
        public RelayCommand ApplyCmd { get; } 
潘栩锋's avatar
潘栩锋 committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

        FObjServiceClientManager serviceClientManager;

        public PgAddressVm() 
        {
            ApplyCmd = new RelayCommand(Apply);
            ConnAddrs = new List<ConnAddr>();

        }

        public void Init() 
        {
            serviceClientManager = FObjServiceClientManager.Instance;
            

            foreach (var ca in serviceClientManager.ConnAddrs) 
            {
                ConnAddrs.Add(new ConnAddr() { ConnName = ca.ConnName, Address = ca.Addr });
            };
        }

        private void Apply()
        {
潘栩锋's avatar
潘栩锋 committed
70 71 72
            if (!WdPassword.Authorize("Address"))
                return;

73 74
            Regex regex_hostname = new Regex(@"(\S+)\:(\d+)");
            Regex regex_ip = new Regex(@"(\d+)\.(\d+)\.(\d+)\.(\d+)\:(\d+)");
潘栩锋's avatar
潘栩锋 committed
75
            //检测地址格式
76
            foreach (var ca in ConnAddrs) 
潘栩锋's avatar
潘栩锋 committed
77
            {
78 79 80 81 82 83
                //把地址的 : 替换为 :
                ca.Address = ca.Address.Replace(":", ":");
                Match m_hostname = regex_hostname.Match(ca.Address);
                Match m_ip = regex_ip.Match(ca.Address);
                
                if (!m_hostname.Success) 
潘栩锋's avatar
潘栩锋 committed
84 85 86 87
                {
                    //格式错误
                    FLY.ControlLibrary.Window_WarningTip.Show(
                        "格式错误",
88
                        $"{ca.ConnName} 地址格式错误! 正确为 IP地址:端口号 或 域名:端口号 , 请检查");
潘栩锋's avatar
潘栩锋 committed
89 90
                    return;
                }
91 92

                if (m_ip.Success)
潘栩锋's avatar
潘栩锋 committed
93
                {
94 95
                    //这个IP
                    for (int i = 0; i < 4; i++)
潘栩锋's avatar
潘栩锋 committed
96
                    {
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
                        if (!int.TryParse(m_ip.Groups[1 + i].Value, out int num))
                        {
                            //格式错误
                            FLY.ControlLibrary.Window_WarningTip.Show(
                                "格式错误",
                                $"{ca.ConnName} 地址格式错误! 第{i + 1} 位IP段 出错");
                            return;
                        }
                        if (num < 0 || num > 255)
                        {
                            //格式错误
                            FLY.ControlLibrary.Window_WarningTip.Show(
                                "格式错误",
                                $"{ca.ConnName} 地址格式错误! 第{i + 1}位IP段应该在 0~255 之间");
                            return;
                        }
潘栩锋's avatar
潘栩锋 committed
113 114
                    }
                }
115 116 117
                


潘栩锋's avatar
潘栩锋 committed
118
                {
119
                    if (!int.TryParse(m_hostname.Groups[2].Value, out int port))
潘栩锋's avatar
潘栩锋 committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
                    {
                        //格式错误
                        FLY.ControlLibrary.Window_WarningTip.Show(
                            "格式错误",
                            $"{ca.ConnName} 地址格式错误! 端口出错");
                        return;
                    }
                    if (port < 1 || port > 65535)
                    {
                        //格式错误
                        FLY.ControlLibrary.Window_WarningTip.Show(
                            "格式错误",
                            $"{ca.ConnName} 地址格式错误! 端口应该在 1~65535 之间");
                        return;
                    }
                }
136 137 138 139 140 141 142 143 144

                var ep = Misc.StringConverter.ToIPEndPoint(ca.Address);
                if (ep == null) {
                    //不能转为ep
                    FLY.ControlLibrary.Window_WarningTip.Show(
                        "地址出错",
                        $"{ca.ConnName} 不能有效转换IPv4");
                    return;
                }
潘栩锋's avatar
潘栩锋 committed
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
            }

            int modifyCnt = 0;
            //检查完毕
            foreach (var ca in ConnAddrs)
            {
                var objCa = serviceClientManager.ConnAddrs.Find(_ca => _ca.ConnName == ca.ConnName);
                if (objCa == null)
                    continue;
                if (objCa.Addr != ca.Address) 
                {
                    modifyCnt++;
                    //重新连接
                    serviceClientManager.ReConnect(ca.ConnName, ca.Address);
                }
            };

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

                FLY.ControlLibrary.Window_Tip.ShowShortTime(
                    "修改成功",
                    $"{modifyCnt}个地址被修改,保存成功");
                return;
            }
            else 
            {
                FLY.ControlLibrary.Window_Tip.ShowShortTime(
                    "通知",
                    $"没有任何地址被修改");
                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
192

潘栩锋's avatar
潘栩锋 committed
193 194
    }
}