SetPLCUpdatePlan.cs 2.85 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2 3 4 5 6 7 8 9 10 11 12 13
using FLY.OBJComponents.IService;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows.Threading;

namespace FLY.OBJComponents.Client
{
    public class SetPLCUpdatePlan:IDisposable
    {
        IPLCProxySystemService PLCos;
14 15
        long planid=0;
        DispatcherTimer timer;//可能被销毁了
潘栩锋's avatar
潘栩锋 committed
16 17 18
        string objname;
        IEnumerable<string> propertynames;

19
        public SetPLCUpdatePlan(IPLCProxySystemService plsos, string objname, IEnumerable<string> propertynames)
潘栩锋's avatar
潘栩锋 committed
20 21 22 23
        {
            PLCos = plsos;

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

潘栩锋's avatar
潘栩锋 committed
26

27 28

            timer = new DispatcherTimer()//120s 内必须再次注册
潘栩锋's avatar
潘栩锋 committed
29
            {
30
                Interval = TimeSpan.FromSeconds(60)
潘栩锋's avatar
潘栩锋 committed
31
            };
32 33 34
            timer.Tick += (s, e) =>
            {
                if (planid != 0)
潘栩锋's avatar
潘栩锋 committed
35 36 37 38
                {
                    PLCos.FeedPlan(planid);
                }
            };
39 40
            
            if(IsTimeToSetPlan())
41
            {
42
                PLCos.SetPlan(this.objname, this.propertynames, (asyncContext, retData) =>
潘栩锋's avatar
潘栩锋 committed
43
                {
44
                    long planid = (long)retData;
45 46 47 48
                    this.planid = planid;
                    timer.Start();
                }, null);
            }
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
            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;
            }
69 70 71
        }
        private void PLCos_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
72
            if (e.PropertyName == nameof(PLCProxySystemServiceClient.IsConnected))
73
            {
74 75
                var _PLCos = PLCos as PLCProxySystemServiceClient;
                if (_PLCos.IsConnected)
76
                {
77
                    PLCos.SetPlan(objname, propertynames, (asyncContext, retData) =>
78
                    {
79
                        long planid = (long)retData;
80 81 82
                        this.planid = planid;
                        timer.Start();
                    }, null);
潘栩锋's avatar
潘栩锋 committed
83 84 85
                }
                else
                {
86 87
                    this.planid = 0;
                    timer.Stop();
潘栩锋's avatar
潘栩锋 committed
88
                }
89
            }
潘栩锋's avatar
潘栩锋 committed
90 91 92 93 94
        }

        public void Dispose()
        {
            timer.Stop();
95 96 97

            if(planid!=0)
                PLCos.RemovePlan(planid);
潘栩锋's avatar
潘栩锋 committed
98 99 100
        }
    }
}