TempAreaPanel2.xaml.cs 7.45 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2 3 4
using FLY.DownBlowing.Client;
using FLY.DownBlowing.Common;
using FLY.DownBlowing.IService;
using FLY.OBJComponents.Client;
5
using FLY.OBJComponents.IService;
潘栩锋's avatar
潘栩锋 committed
6
using MultiLayout.UiModule;
7
using System;
潘栩锋's avatar
潘栩锋 committed
8
using System.Collections.Generic;
9
using System.ComponentModel;
潘栩锋's avatar
潘栩锋 committed
10 11 12
using System.Linq;
using System.Windows;
using System.Windows.Controls;
13
using System.Windows.Threading;
潘栩锋's avatar
潘栩锋 committed
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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
using Unity;

namespace FLY.DownBlowing.UI.Client.UiModule
{
    /// <summary>
    /// TempAreaPanel.xaml 的交互逻辑
    /// </summary>
    public partial class TempAreaPanel2 : UserControl
    {
        IDownBlowingSystemService downBlowingSystemService;
        IUnityContainer container;
        List<SetPLCUpdatePlan> setPlan_items = new List<SetPLCUpdatePlan>();

        static string[] update_propertynames;
        static TempAreaPanel2()
        {
            string[] propertynames = new string[] {
                nameof(Common.TempArea.TempPV1),
                nameof(Common.TempArea.TempSV1),
                nameof(Common.TempArea.IsHeating1),
                nameof(Common.TempArea.IsHeatingSet1),
                nameof(Common.TempArea.IsCooling1),
                nameof(Common.TempArea.IsSelfAdjusting1)
            };
            List<string> update_propertynames = new List<string>();
            for (int i = 0; i < 6; i++)
            {
                for (int j = 0; j < propertynames.Count(); j++)
                {
                    update_propertynames.Add(propertynames[j].Replace("1", (i + 1).ToString()));
                }
            }
            update_propertynames.Add(nameof(Common.TempArea.MeltingTemp));

            TempAreaPanel2.update_propertynames = update_propertynames.ToArray();
        }
        public TempAreaPanel2()
        {
            InitializeComponent();
        }


        [InjectionMethod]
        public void Init(
            IUnityContainer container,
            IDownBlowingSystemService downBlowingSystemService)
        {
            this.downBlowingSystemService = downBlowingSystemService;
            this.container = container;
            this.DataContext = this.downBlowingSystemService;

            if (this.downBlowingSystemService is DownBlowingSystemServiceClient)
            {
67
                (this.downBlowingSystemService as DownBlowingSystemServiceClient).ResetTAreasEvent += MWeighterCsService_ResetItemsEvent;
潘栩锋's avatar
潘栩锋 committed
68 69 70 71 72
            }
        }

        private void MWeighterCsService_ResetItemsEvent()
        {
73 74 75
            if (setPlan_items.Count() == 0)
                return;//从来没注册

潘栩锋's avatar
潘栩锋 committed
76 77 78 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 109 110 111 112 113 114 115
            //把多出来的删除
            int remove_cnt = setPlan_items.Count() - this.downBlowingSystemService.TAreas.Count();
            if (remove_cnt > 0)
            {
                for (int i = 0; i < remove_cnt; i++)
                {
                    SetPLCUpdatePlan plan = setPlan_items[setPlan_items.Count() - 1 - i];
                    plan.Dispose();
                }
                setPlan_items.RemoveRange(setPlan_items.Count() - remove_cnt, remove_cnt);
            }
            else
            {
                int start_idx = setPlan_items.Count();
                int add_cnt = -remove_cnt;
                for (int i = 0; i < add_cnt; i++)
                {
                    string objname = $"{nameof(IDownBlowingSystemService.TAreas)}[{start_idx + i}]";

                    SetPLCUpdatePlan plan = new SetPLCUpdatePlan(
                        this.downBlowingSystemService.PLCos,
                        objname,
                        update_propertynames);
                    setPlan_items.Add(plan);
                }
            }
        }

        private void btnTAreaClick(object sender, RoutedEventArgs e)
        {
            var button = sender as Button;
            var tempArea = button.Tag as TempArea;
            WdTempAreaSet w = new WdTempAreaSet();
            w.Init(this.downBlowingSystemService, tempArea);
            w.Owner = FLY.ControlLibrary.COMMON.GetWindow(this);
            w.ShowDialog();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
116 117 118 119 120 121 122 123 124 125 126
            if (setPlan_items.Count() == 0) {
                for (int i = 0; i < this.downBlowingSystemService.TAreas.Count(); i++)
                {
                    string objname = $"{nameof(IDownBlowingSystemService.TAreas)}[{i}]";
                    SetPLCUpdatePlan plan = new SetPLCUpdatePlan(
                        this.downBlowingSystemService.PLCos,
                        objname,
                        update_propertynames); ;
                    setPlan_items.Add(plan);
                }
            }
潘栩锋's avatar
潘栩锋 committed
127 128 129 130 131 132 133 134 135 136 137 138 139
        }

        private void UserControl_Unloaded(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < setPlan_items.Count(); i++)
            {
                setPlan_items[i].Dispose();
            }
            setPlan_items.Clear();
        }

    }

140
    public class SetPLCUpdatePlan : IDisposable
潘栩锋's avatar
潘栩锋 committed
141
    {
142 143 144 145 146 147 148
        IPLCProxySystemService PLCos;
        long planid = 0;
        DispatcherTimer timer;//可能被销毁了
        string objname;
        IEnumerable<string> propertynames;

        public SetPLCUpdatePlan(IPLCProxySystemService plsos, string objname, IEnumerable<string> propertynames)
潘栩锋's avatar
潘栩锋 committed
149
        {
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
            PLCos = plsos;

            this.propertynames = propertynames;
            this.objname = objname;// PLCos.ObjNames.First((kv) => kv.Value == obj).Key;



            timer = new DispatcherTimer()//120s 内必须再次注册
            {
                Interval = TimeSpan.FromSeconds(60)
            };
            timer.Tick += (s, e) =>
            {
                if (planid != 0)
                {
                    PLCos.FeedPlan(planid);
                }
            };
潘栩锋's avatar
潘栩锋 committed
168

169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
            if (IsTimeToSetPlan())
            {
                PLCos.SetPlan(this.objname, this.propertynames, (asyncContext, retData) =>
                {
                    long planid = (long)retData;
                    this.planid = planid;
                    timer.Start();
                }, null);
            }
            if (PLCos is PLCProxySystemServiceClient)
                PLCos.PropertyChanged += PLCos_PropertyChanged;

        }
        bool IsTimeToSetPlan()
        {
            if (PLCos is PLCProxySystemServiceClient)
            {
                var _PLCos = PLCos as PLCProxySystemServiceClient;
                if (_PLCos.IsConnected)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return true;
            }
        }
        private void PLCos_PropertyChanged(object sender, PropertyChangedEventArgs e)
潘栩锋's avatar
潘栩锋 committed
202
        {
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
            if (e.PropertyName == nameof(PLCProxySystemServiceClient.IsConnected))
            {
                var _PLCos = PLCos as PLCProxySystemServiceClient;
                if (_PLCos.IsConnected)
                {
                    PLCos.SetPlan(objname, propertynames, (asyncContext, retData) =>
                    {
                        long planid = (long)retData;
                        this.planid = planid;
                        timer.Start();
                    }, null);
                }
                else
                {
                    this.planid = 0;
                    timer.Stop();
                }
            }
潘栩锋's avatar
潘栩锋 committed
221 222
        }

223
        public void Dispose()
潘栩锋's avatar
潘栩锋 committed
224
        {
225 226 227 228
            timer.Stop();

            if (planid != 0)
                PLCos.RemovePlan(planid);
潘栩锋's avatar
潘栩锋 committed
229 230 231
        }
    }
}