29
|
1 using System;
|
|
2 using System.Threading.Tasks;
|
|
3 using Common.Messages;
|
|
4 using GalaSoft.MvvmLight;
|
|
5 using GalaSoft.MvvmLight.Messaging;
|
31
|
6 using Server.Listeners;
|
29
|
7
|
|
8 namespace Server.EndPoints
|
|
9 {
|
31
|
10 public class MessageEndPoint<T, TY> : ObservableObject
|
|
11 where T : ClientMessage
|
|
12 where TY : LogMessage
|
29
|
13 {
|
|
14 #region Fields
|
|
15
|
|
16 private readonly IListener _listener;
|
31
|
17 private readonly object _locker;
|
29
|
18
|
|
19 #endregion
|
|
20
|
|
21 #region Constructor
|
|
22
|
|
23 public MessageEndPoint(IListener listener)
|
|
24 {
|
|
25 _listener = listener;
|
31
|
26 _locker = new object();
|
29
|
27 Init();
|
|
28 }
|
|
29
|
|
30 #endregion
|
|
31
|
31
|
32 #region Messaging
|
|
33
|
29
|
34 public void Init()
|
|
35 {
|
|
36 Messenger.Default.Register<T>(this, ClientMessageReceived);
|
31
|
37 Messenger.Default.Register<TY>(this, LogMessageReceived);
|
|
38 Messenger.Default.Register<OverrideSwitchMessage>(this, SwitchMessageReceived);
|
29
|
39 }
|
|
40
|
31
|
41 private void SwitchMessageReceived(OverrideSwitchMessage message)
|
|
42 {
|
|
43 if (message.Switch == Switch.Off)
|
|
44 {
|
|
45 IsListening = false;
|
|
46 }
|
|
47 else
|
|
48 {
|
|
49 IsListening = true;
|
|
50 }
|
|
51 }
|
|
52
|
|
53 protected void LogMessageReceived(TY message)
|
29
|
54 {
|
|
55 this.DisplayLog = DisplayLog + Environment.NewLine + message.Body;
|
|
56 }
|
|
57
|
|
58 protected void ClientMessageReceived(T message)
|
|
59 {
|
|
60 this.DisplayCount++;
|
31
|
61 }
|
|
62
|
|
63 #endregion
|
29
|
64
|
|
65 #region Properties
|
|
66
|
|
67 #region IsListening
|
|
68
|
|
69 public const string IsListeningPropertyName = "IsListening";
|
|
70 private bool _isListening;
|
|
71 public bool IsListening
|
|
72 {
|
|
73 get { return _isListening; }
|
|
74 set
|
|
75 {
|
|
76 if (_isListening == value) return;
|
|
77 _isListening = value;
|
|
78 RaisePropertyChanged(() => IsListening);
|
|
79 ToggleListener();
|
|
80 }
|
|
81 }
|
|
82
|
|
83 #endregion
|
|
84
|
|
85 #region DisplayText
|
|
86
|
|
87 public const string DisplayTextPropertyName = "DisplayText";
|
|
88 private string _displayText;
|
|
89 public string DisplayText
|
|
90 {
|
|
91 get { return _displayText; }
|
|
92 set
|
|
93 {
|
|
94 if (_displayText == value) return;
|
|
95 _displayText = value;
|
|
96 RaisePropertyChanged(() => DisplayText);
|
|
97 }
|
|
98 }
|
|
99
|
|
100 #endregion
|
|
101
|
|
102 #region DisplayCount
|
|
103
|
|
104 public const string DisplayCountPropertyName = "DisplayCount";
|
31
|
105 private int _displayCount;
|
29
|
106 public int DisplayCount
|
|
107 {
|
|
108 get { return _displayCount; }
|
|
109 set
|
|
110 {
|
31
|
111 lock (_locker)
|
|
112 {
|
|
113 if (_displayCount == value) return;
|
|
114 _displayCount = value;
|
|
115 RaisePropertyChanged(() => DisplayCount);
|
|
116 }
|
29
|
117 }
|
|
118 }
|
|
119 #endregion
|
|
120
|
|
121 #region DisplayLog
|
|
122
|
|
123 public const string DisplayLogPropertyName = "DisplayLog";
|
|
124 private string _displayLog = string.Empty;
|
|
125 public string DisplayLog
|
|
126 {
|
|
127 get { return _displayLog; }
|
|
128 set
|
|
129 {
|
|
130 if (_displayLog == value) return;
|
|
131 _displayLog = value;
|
|
132 RaisePropertyChanged(() => DisplayLog);
|
|
133 IsLogChanged = true;
|
|
134 }
|
|
135 }
|
|
136
|
|
137 #endregion
|
|
138
|
|
139 #region IsLogChanged
|
|
140
|
|
141 public const string IsLogChangedPropertyName = "IsLogChanged";
|
|
142 private bool _isLogChanged;
|
|
143 public bool IsLogChanged
|
|
144 {
|
|
145 get { return _isLogChanged; }
|
|
146 set
|
|
147 {
|
|
148 if (_isLogChanged == value) return;
|
|
149 _isLogChanged = value;
|
|
150 RaisePropertyChanged(() => IsLogChanged);
|
|
151 }
|
|
152 }
|
|
153
|
|
154 #endregion
|
|
155
|
|
156 #region ToolTip
|
|
157
|
|
158 public const string ToolTipPropertyName = "ToolTip";
|
|
159 private string _toolTip = string.Empty;
|
|
160 public string ToolTip
|
|
161 {
|
|
162 get { return _toolTip; }
|
|
163 set
|
|
164 {
|
|
165 if (_toolTip == value) return;
|
|
166 _toolTip = value;
|
|
167 RaisePropertyChanged(() => ToolTip);
|
|
168 }
|
|
169 }
|
|
170
|
|
171 #endregion
|
|
172
|
|
173 #endregion
|
|
174
|
|
175 #region Commands
|
|
176
|
|
177 private void ToggleListener()
|
|
178 {
|
|
179 if (null == _listener) return;
|
|
180
|
|
181 if (_listener.IsListening)
|
|
182 Task.Factory.StartNew(_listener.Stop);
|
|
183 else
|
|
184 Task.Factory.StartNew(_listener.Start);
|
|
185 }
|
|
186
|
|
187 #endregion
|
|
188 }
|
31
|
189 } |