DynAreaIO.xaml.cs 10.3 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2
using CommunityToolkit.Mvvm.Input;
using FLY.OBJComponents.IService;
3 4
using FLY.Thick.Base.Common;
using FLY.Thick.Base.IService;
潘栩锋's avatar
潘栩锋 committed
5 6
using FObjBase.Reflect;
using MultiLayout;
7 8 9
using MultiLayout.UiModule;
using System;
using System.ComponentModel;
潘栩锋's avatar
潘栩锋 committed
10
using System.Globalization;
11 12 13 14 15 16 17 18 19 20 21 22 23 24
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Threading;
using Unity;

namespace FLY.Thick.Base.UI.UiModule
{
    /// <summary>
    /// DynAreaIO.xaml 的交互逻辑
    /// </summary>
    public partial class DynAreaIO : UserControl
    {
25
        DynAreaErrorVm2 errorVm;
26
        DynAreaIOVm ioVm;
27 28 29 30 31 32 33 34 35

        IUnityContainer container;

        public DynAreaIO()
        {
            InitializeComponent();
        }
        [InjectionMethod]
        public void Init(
36 37
            IUnityContainer container,
            IWarningSystem2Service warningSystem)
38 39
        {
            this.container = container;
40
            ioVm = container.Resolve<DynAreaIOVm>();
41 42 43

            errorVm = new DynAreaErrorVm2();

44 45 46
            var serverName = (string)Application.Current.FindResource("str.DynArea.TGauge");
            //TODO, 只能翻译 FLY.Base.Common.ERRNOs 定义的。  就算是它的子类,也需要提前调用 子类静态构造函数
            errorVm.Init(container, warningSystem, serverName, ERRNOs.GetDescription);
47

48 49
            this.DataContext = ioVm;
            grid_error.DataContext = errorVm;
50 51 52
        }
    }

53
    public class DynAreaIOVm : INotifyPropertyChanged
54 55 56 57 58 59 60 61
    {
        #region 延时推送 MARKNO
        const int MARKNO_UPDATEERROR = 1;
        #endregion

        public event PropertyChangedEventHandler PropertyChanged;

        public UInt16 IStatus { get; set; } = 0xffff;
62
        public UInt16 OStatus { get; set; } = 0x00ff;
63

64
        public bool IsIOShowNo { get; set; } = false;
65 66 67 68 69 70
        public int Hrs { get; set; } = 3;

        /// <summary>
        /// 是永久使用
        /// </summary>
        public bool IsForever { get; set; } = false;
71 72 73 74 75 76

        public RelayCommand OpenIoTipCmd { get; private set; }
        public RelayCommand OpenAccessCmd { get; private set; }

        DynArea dynArea;
        IUnityContainer container;
潘栩锋's avatar
潘栩锋 committed
77
        public DynAreaIOVm()
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
        {
            OpenIoTipCmd = new RelayCommand(OpenIoTip);
            OpenAccessCmd = new RelayCommand(OpenAccess);
        }


        [InjectionMethod]
        public void Init(
            IUnityContainer container,
            ITDGageService gageService)
        {
            this.dynArea = gageService.DynArea;
            this.container = container;

            Misc.BindingOperations.SetBinding(this.dynArea, nameof(this.dynArea.IStatus), this, nameof(IStatus));
            Misc.BindingOperations.SetBinding(this.dynArea, nameof(this.dynArea.OStatus), this, nameof(OStatus));
94
            Misc.BindingOperations.SetBinding(this.dynArea, nameof(this.dynArea.IsIOShowNo), this, nameof(IsIOShowNo));
95
            Misc.BindingOperations.SetBinding(this.dynArea, nameof(this.dynArea.Hrs), this, nameof(Hrs));
96

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
            UpdateIsForever();

            this.dynArea.PropertyChanged += (s, e) =>
            {
                if (e.PropertyName == nameof(this.dynArea.Hrs))
                {
                    UpdateIsForever();
                }
            };
        }
        void UpdateIsForever()
        {
            if (dynArea.Hrs > 50000)
                IsForever = true;
            else
                IsForever = false;
        }

        private void OpenAccess()
        {
            WdFlyADAccess w = new WdFlyADAccess();
            var flyAdService = container.Resolve<FLY.Thick.Base.IService.IFlyADService>();
            w.Init(flyAdService);
            w.Owner = App.Current.MainWindow;
            w.ShowDialog();
        }

        private void OpenIoTip()
        {
            var w = new WdIOTip();
            container.BuildUp(w);

            w.Owner = App.Current.MainWindow;
            w.ShowDialog();
        }
    }
    public class DynAreaIOVmUt : DynAreaIOVm
    {
潘栩锋's avatar
潘栩锋 committed
135
        public DynAreaIOVmUt()
136 137 138 139 140 141 142 143
        {
            IStatus = 0xff7f;
            OStatus = 0x0005;
            Hrs = 2000;
            IsForever = false;
        }
    }

144

145
    public class DynAreaErrorVm2 : INotifyPropertyChanged
146 147 148 149
    {
        /// <summary>
        /// 有异常
        /// </summary>
150 151 152 153 154 155
        public bool IsError { get; set; }
        /// <summary>
        /// 异常消息
        /// </summary>
        public string ErrMsg { get; set; }

156 157 158 159 160
        public RelayCommand OpenWarningCmd { get; }

        public event PropertyChangedEventHandler PropertyChanged;


161
        IWarningSystem2Service warningSystem;
162 163
        IUnityContainer container;

164
        /// <summary>
165
        /// 服务名称,用于显示 XXX服务器连接断开
166
        /// </summary>
167 168
        string serverName;

169 170
        DispatcherTimer timer_error;
        private int reason_list_index = -1;
171 172

        bool isTGauge;
173 174 175

        ToDescriptionHandler toDescription;

176 177 178 179 180 181 182 183 184
        public DynAreaErrorVm2()
        {
            OpenWarningCmd = new RelayCommand(OpenWarning);
        }


        public void Init(
            IUnityContainer container,
            IWarningSystem2Service warningSystem,
185 186
            string serverName,
            ToDescriptionHandler toDescription)
187 188 189 190
        {
            this.container = container;
            this.warningSystem = warningSystem;
            this.serverName = serverName;
191
            this.toDescription = toDescription;
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231

            //报警原因轮流显示
            timer_error = new DispatcherTimer();
            timer_error.Interval = TimeSpan.FromSeconds(3);
            timer_error.Tick += (s, e) =>
            {
                reason_list_index--;
                if (reason_list_index < 0)
                    if (warningSystem.ReasonList != null && warningSystem.ReasonList.Count() > 0)
                        reason_list_index = warningSystem.ReasonList.Count();
                    else
                        reason_list_index = -1;
                UpdateError();
            };
            warningSystem.PropertyChanged += WarningSystem_PropertyChanged;

            UpdateError();
        }

        private void WarningSystem_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == nameof(Reflect_SeviceClient.IsConnected))
            {
                UpdateError();
            }
            else if (e.PropertyName == nameof(warningSystem.ReasonList))
            {
                if (warningSystem.ReasonList != null && warningSystem.ReasonList.Count() > 0)
                    reason_list_index = warningSystem.ReasonList.Count() - 1;
                else
                    reason_list_index = -1;

                UpdateError();
            }
        }
        void UpdateError()
        {
            if (this.warningSystem is FObjBase.FObjServiceClient)
            {
                var client = this.warningSystem as FObjBase.FObjServiceClient;
232

233
                string state_msg = (string)Application.Current.TryFindResource("str.DynArea.ServerDC");
234 235
                if (!client.IsConnected)
                {
236
                    ErrMsg = $"{serverName}{state_msg}";
237 238 239 240 241 242 243 244 245 246 247 248 249 250
                    IsError = true;
                    reason_list_index = -1;
                    timer_error.Stop();
                    return;
                }
            }

            if (warningSystem.ReasonList != null && warningSystem.ReasonList.Count() > 0)
            {
                if (reason_list_index >= warningSystem.ReasonList.Count())
                    reason_list_index = warningSystem.ReasonList.Count() - 1;
                else if (reason_list_index < 0)
                    reason_list_index = 0;

251
                //通过资源翻译报警信息
252 253 254 255 256 257 258 259
                if (toDescription != null)
                {
                    ErrMsg = toDescription(warningSystem.ReasonList[reason_list_index].ErrCode);
                }
                else
                {
                    ErrMsg = warningSystem.ReasonList[reason_list_index].Description;
                }
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
                IsError = true;
                timer_error.Start();
            }
            else
            {
                IsError = false;
                ErrMsg = "";
                reason_list_index = -1;
                timer_error.Stop();
            }
        }

        private void OpenWarning()
        {
            if (!WdPassword.Authorize("Warning"))
                return;
276 277

            if (isTGauge)
278
            {
279
                //测厚仪的
280
                var p = new PgErrorTable();
281
                p.Init(container, warningSystem, toDescription);
282 283 284 285 286
                FlyLayoutManager.NavigationService.Navigate(p);
            }
            else
            {
                var p = new PgErrorTable2();
287
                p.Init(container, warningSystem, toDescription);
288 289
                FlyLayoutManager.NavigationService.Navigate(p);
            }
290 291 292
        }

    }
293 294
    public class UiModule2_DynAreaIO : IUiModule2
    {
295
        public string Title => "测厚.IO状态(报警)";
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316

        public ComponentType Type => ComponentType.DynArea;

        public bool IsUnique => true;

        public FrameworkElement GetComponent(int id, IUnityContainer container)
        {
            DynAreaIO graph = container.Resolve<DynAreaIO>();
            return graph;
        }

        public FrameworkElement GetThumbnail()
        {
            return new System.Windows.Controls.Grid();
        }

        public void MatchParam(int[] IDs)
        {

        }
    }
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350

    public class IONumberConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            bool isIOShowNo = true;
            int idx = 0;
            if (value is bool)
            {
                isIOShowNo = (bool)value;
            }
            if (!(parameter is string))
            {
                return null;
            }
            if (!int.TryParse((string)parameter, out idx))
            {
                return null;
            }
            if (isIOShowNo)
            {
                return (idx + 1).ToString();
            }
            else
            {
                return (idx).ToString();
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
351
}