WdFlyADAccess.xaml.cs 6.31 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
using System; 
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 System.ComponentModel;

using System.Net;

using FLY.Thick.Base.Common;
using FLY.Thick.Base.Client;
using FLY.Thick.Base.IService;
using FObjBase;
22
using GalaSoft.MvvmLight.Command;
潘栩锋's avatar
潘栩锋 committed
23

潘栩锋's avatar
潘栩锋 committed
24
namespace FLY.Thick.Base.UI
潘栩锋's avatar
潘栩锋 committed
25 26
{
    /// <summary>
27
    /// WdFlyADAccess.xaml 的交互逻辑
潘栩锋's avatar
潘栩锋 committed
28
    /// </summary>
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
    public partial class WdFlyADAccess : FLY.ControlLibrary.WindowBigClose
    {
        WdFlyADAccessVm viewModel;
        /// <summary>
        /// 
        /// </summary>
        public WdFlyADAccess()
        {
            InitializeComponent();
            viewModel = new WdFlyADAccessVm();
            this.DataContext = viewModel;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="flyADService"></param>
46
        public void Init(IFlyADService flyADService)
47 48 49 50 51 52 53 54 55 56
        {
            viewModel.Init(flyADService);
        }

    }

    /// <summary>
    /// WdFlyADAccess 的 ViewModel
    /// </summary>
    public class WdFlyADAccessVm : INotifyPropertyChanged
潘栩锋's avatar
潘栩锋 committed
57
    {
58
        IFlyADService mFlyADService = null;
潘栩锋's avatar
潘栩锋 committed
59

60 61 62 63


        public event PropertyChangedEventHandler PropertyChanged;

潘栩锋's avatar
潘栩锋 committed
64 65 66 67 68 69 70 71 72 73 74 75
        /// <summary>
        /// 使用码
        /// </summary>
        [PropertyChanged.DoNotCheckEquality]
        public byte[] Access { get; set; } = new byte[8];

        /// <summary>
        /// 序列码
        /// </summary>
        [PropertyChanged.DoNotCheckEquality]
        public byte[] Code { get; set; } = new byte[7];

76 77 78 79 80 81 82 83
        public RelayCommand ApplyCmd { get; private set; }


        public WdFlyADAccessVm()
        {
            ApplyCmd = new RelayCommand(Apply);
        }

潘栩锋's avatar
潘栩锋 committed
84 85 86
        /// <summary>
        /// 
        /// </summary>
87
        /// <param name="flyADService"></param>
88
        public void Init(IFlyADService flyADService)
潘栩锋's avatar
潘栩锋 committed
89
        {
90
            mFlyADService = flyADService;
潘栩锋's avatar
潘栩锋 committed
91

92 93 94 95 96 97 98 99 100
            mFlyADService.GetAccessInfo(
                new AsyncCBHandler(
                    delegate (object AsyncState, object retData)
                    {
                        AccessInfo a = (AccessInfo)retData;
                        Access = a.access;
                        Code = a.code;
                    }), null);
        }
潘栩锋's avatar
潘栩锋 committed
101

102
        void Apply()
潘栩锋's avatar
潘栩锋 committed
103 104 105
        {
            if ((Access == null) || (Access.Length != 8))
            {
106
                FLY.ControlLibrary.Window_Tip.Show("错误", "长度不对,需要 8位 byte!!",
潘栩锋's avatar
潘栩锋 committed
107 108 109 110 111
                    TimeSpan.FromSeconds(2));
                return;
            }
            if (mFlyADService == null)
                return;
112

潘栩锋's avatar
潘栩锋 committed
113 114 115
            mFlyADService.SetAccess(
                Access,
                new AsyncCBHandler(
116
                    delegate (object AsyncState, object retData)
潘栩锋's avatar
潘栩锋 committed
117 118 119 120 121 122 123 124 125 126
                    {
                        AccessInfo a = (AccessInfo)retData;
                        Access = a.access;
                        Code = a.code;
                        switch (a.ret)
                        {
                            case AREA_ERR.NO_ERR:
                                {
                                    FLY.ControlLibrary.Window_Tip.Show("成功", "设置成功!!",
                                        TimeSpan.FromSeconds(2));
127 128
                                }
                                break;
潘栩锋's avatar
潘栩锋 committed
129 130 131 132
                            case AREA_ERR.DUP_ACCESS:
                                {
                                    FLY.ControlLibrary.Window_Tip.Show("错误", "授权码重复!!",
                                        TimeSpan.FromSeconds(2));
133 134
                                }
                                break;
潘栩锋's avatar
潘栩锋 committed
135 136
                            case AREA_ERR.ERR_ACCESS:
                                {
137
                                    FLY.ControlLibrary.Window_Tip.Show("错误", "授权码不正确!!",
潘栩锋's avatar
潘栩锋 committed
138
                                        TimeSpan.FromSeconds(2));
139 140
                                }
                                break;
潘栩锋's avatar
潘栩锋 committed
141 142 143 144 145 146
                        }
                    }), null);
            FLY.ControlLibrary.Window_Tip.Show("通知", "请等待。。。。。。",
                TimeSpan.FromSeconds(2));
        }
    }
潘栩锋's avatar
潘栩锋 committed
147 148 149
    /// <summary>
    /// 
    /// </summary>
潘栩锋's avatar
潘栩锋 committed
150 151 152 153
    public class BytesConverter : IValueConverter
    {
        #region IValueConverter 成员

潘栩锋's avatar
潘栩锋 committed
154 155 156 157 158 159 160 161
        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
潘栩锋's avatar
潘栩锋 committed
162 163 164 165 166 167 168 169 170 171 172
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return "";

            byte[] bs = (byte[])value;
            string str = "";
            for (int i = 0; i < bs.Length; i++)
                str += bs[i].ToString("X2");
            return str;
        }
潘栩锋's avatar
潘栩锋 committed
173 174 175 176 177 178 179 180
        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
潘栩锋's avatar
潘栩锋 committed
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string str = (string)value;
            string h = "";
            List<byte> bs = new List<byte>();
            for (int i = 0; i < str.Length; i++)
            {
                if (str[i] != ' ')
                    h += str[i];

                if (((str[i] == ' ') && (h.Length != 0)) ||
                    (h.Length >= 2))
                {
                    byte b = 0;
                    if (byte.TryParse(h, System.Globalization.NumberStyles.AllowHexSpecifier, null, out b))
                    {
                        bs.Add(b);
                    }
                    else
                    {
                        break;
                    }
                    h = "";
                }
            }

            return bs.ToArray();
        }

        #endregion
    }
}