1
2
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
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;
long planid=0;
DispatcherTimer timer;//可能被销毁了
string objname;
IEnumerable<string> propertynames;
public SetPLCUpdatePlan(IPLCProxySystemService plsos, string objname, IEnumerable<string> propertynames)
{
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);
}
};
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)
{
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();
}
}
}
public void Dispose()
{
timer.Stop();
if(planid!=0)
PLCos.RemovePlan(planid);
}
}
}