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
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
using System.ComponentModel;
using System.Threading;
using VirtualConveyor.Cache.Entity;
using VirtualConveyor.Common.Entity;
using VirtualConveyor.Properties;
using VirtualConveyor.Utils;
namespace VirtualConveyor.HeartBeat.Entity
{
    public class HeartBeatEntity:BindableBase
    {
        /// <summary>
        /// PLC心跳
        /// </summary>
        private int wPlcHeartBeat;
 
        public int WPlcHeartBeat
        {
            get { return wPlcHeartBeat; }
            set { SetProperty(ref wPlcHeartBeat, value); }
        }
 
        /// <summary>
        /// 告警信息
        /// </summary>
        private int wAlarmCode;
 
        public int WAlarmCode
        {
            get { return wAlarmCode; }
            set { SetProperty(ref wAlarmCode, value); }
        }
 
        /// <summary>
        /// 服务器心跳
        /// </summary>
        private bool serverHeartBeat;
 
        public bool ServerHeartBeat
        {
            get { return serverHeartBeat; }
            set { SetProperty(ref serverHeartBeat, value); }
        }
 
        /// <summary>
        /// WCS需要写入的OPC地址
        /// </summary>
        public OpcReadItem  OpcReadItems{ get; set; }
 
 
        /// <summary>
        /// WCS需要读取的OPC地址
        /// </summary>
        public OpcWriteItem OpcWriteItems { get; set; }
 
 
        /// <summary>
        /// 写入心跳的线程
        /// </summary>
        private Thread WritePlcHeartBeatThread { get; set; }
 
 
        public HeartBeatEntity()
        {
            OpcReadItems = new OpcReadItem
            {
                ServerHeartBeat = "S7:[@LOCALSERVER]DB1,X159.0"
            };
 
            OpcWriteItems = new OpcWriteItem
            {
                PlcHeartBeat = "S7:[@LOCALSERVER]DB1,INT160",
                AlarmCode = "S7:[@LOCALSERVER]DB1,W162" 
            };
 
 
            this.PropertyChanged += new PropertyChangedEventHandler(OnPropertyChanged);
            WritePlcHeartBeatThread = new Thread(new ThreadStart(WritePlcHeartBeat));
            ThreadHelper.StartThread(WritePlcHeartBeatThread);
        }
 
 
        /// <summary>
        /// 析构函数
        /// </summary>
        ~HeartBeatEntity() 
        {
            LogTextHelper.WriteLine(Resources.LogDir, "{0}的析构函数别调用", this.ToString());
            ThreadHelper.CloseThread(WritePlcHeartBeatThread); 
        }
 
        public void CloseWritePlcHeartBeatThread() { ThreadHelper.CloseThread(WritePlcHeartBeatThread); }
 
 
        /// <summary>
        /// 心跳属性值发生变化时触发的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {
                case "WPlcHeartBeat":
                    CacheEntity.OpcWcfServiceClient.WriteValue(this.OpcWriteItems.PlcHeartBeat, WPlcHeartBeat);
                    break;
                case "WAlarmCode":
                    CacheEntity.OpcWcfServiceClient.WriteValue(this.OpcWriteItems.AlarmCode, WAlarmCode);
                    break;
                
            }
        }
 
        /// <summary>
        /// 写入心跳
        /// </summary>
        private void WritePlcHeartBeat()
        {
            while (true)
            {
                WPlcHeartBeat = WPlcHeartBeat > 999 ? 0 : WPlcHeartBeat + 1;
                Thread.Sleep(1000);
            }
        }
 
 
 
    }
}