markus / ConvertService / ServiceBase / Markus.Service.StationController / ViewModel / AliveViewModel.cs @ a8f1a865
이력 | 보기 | 이력해설 | 다운로드 (8.19 KB)
1 | 06f13e11 | taeseongkim | using Markus.Service.WcfClient.StationServiceTask; |
---|---|---|---|
2 | 0498c12e | taeseongkim | using System; |
3 | 6396f27e | taeseongkim | using System.Collections.Generic; |
4 | 0498c12e | taeseongkim | using System.ComponentModel; |
5 | 6396f27e | taeseongkim | using System.Linq; |
6 | using System.Text; |
||
7 | using System.Threading.Tasks; |
||
8 | 0157b158 | taeseongkim | using Markus.Service.Extensions; |
9 | b92f142f | taeseongkim | using Markus.Service.Helper; |
10 | using Markus.Service.StationController.Extensions; |
||
11 | 60723dc9 | taeseongkim | using System.Web; |
12 | 0498c12e | taeseongkim | |
13 | 6396f27e | taeseongkim | namespace Markus.Service.StationController.ViewModel |
14 | { |
||
15 | 0498c12e | taeseongkim | public class AliveViewModel :Mvvm.ToolKit.ViewModelBase |
16 | 6396f27e | taeseongkim | { |
17 | 0498c12e | taeseongkim | BackgroundWorker backgroundWorker; |
18 | |||
19 | b92f142f | taeseongkim | private System.Collections.ObjectModel.ObservableCollection<ConvertItem> aliveItems; |
20 | 0498c12e | taeseongkim | private bool isLoading; |
21 | 1ae729e4 | taeseongkim | private System.Windows.Documents.FlowDocument connectionLog; |
22 | 0498c12e | taeseongkim | |
23 | b92f142f | taeseongkim | public System.Collections.ObjectModel.ObservableCollection<ConvertItem> AliveItems |
24 | 0498c12e | taeseongkim | { |
25 | get => aliveItems; set |
||
26 | { |
||
27 | aliveItems = value; |
||
28 | OnPropertyChanged(() => AliveItems); |
||
29 | } |
||
30 | } |
||
31 | |||
32 | public bool IsLoading |
||
33 | { |
||
34 | get => isLoading; set |
||
35 | { |
||
36 | if (isLoading != value) |
||
37 | { |
||
38 | isLoading = value; |
||
39 | OnPropertyChanged(() => IsLoading); |
||
40 | } |
||
41 | } |
||
42 | } |
||
43 | |||
44 | 1ae729e4 | taeseongkim | |
45 | public System.Windows.Documents.FlowDocument ConnectionLog |
||
46 | { |
||
47 | get => connectionLog; |
||
48 | set |
||
49 | { |
||
50 | if(connectionLog != value) |
||
51 | { |
||
52 | connectionLog = value; |
||
53 | OnPropertyChanged(() => ConnectionLog); |
||
54 | } |
||
55 | } |
||
56 | } |
||
57 | |||
58 | 0498c12e | taeseongkim | public AliveViewModel() |
59 | { |
||
60 | } |
||
61 | |||
62 | // 진행률에 변화가 있을때 이벤트가 발생 |
||
63 | private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) |
||
64 | { |
||
65 | } |
||
66 | |||
67 | // 일이 모두 마쳤을때 수행되어야할 코드 |
||
68 | private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) |
||
69 | { |
||
70 | } |
||
71 | |||
72 | // BackgroundWorker에서 수행할 일을 정의. |
||
73 | private async void backgroundWorker_DoWork(object sender, DoWorkEventArgs e) |
||
74 | { |
||
75 | while (IsAcitve) |
||
76 | { |
||
77 | 1ae729e4 | taeseongkim | System.Threading.Thread.Sleep(new TimeSpan(0,0,3)); |
78 | 0498c12e | taeseongkim | |
79 | 1ae729e4 | taeseongkim | if (!IsLoading) |
80 | 0498c12e | taeseongkim | { |
81 | IsLoading = true; |
||
82 | |||
83 | 1ae729e4 | taeseongkim | try |
84 | 0157b158 | taeseongkim | { |
85 | 1ae729e4 | taeseongkim | |
86 | List<ConvertItem> newitems = new List<ConvertItem>(); |
||
87 | |||
88 | foreach (var client in App.StationClientList) |
||
89 | 60723dc9 | taeseongkim | { |
90 | 1ae729e4 | taeseongkim | if (await SimplePingAsync(client.Endpoint.Address.ToString())) |
91 | { |
||
92 | try |
||
93 | { |
||
94 | var items = await client.AliveConvertListAsync(); |
||
95 | newitems.AddRange(items); |
||
96 | System.Diagnostics.Trace.WriteLine($"{client.Endpoint.Address} ping"); |
||
97 | |||
98 | if (items.Count() == 0) |
||
99 | { |
||
100 | System.Diagnostics.Trace.WriteLine($"{client.Endpoint.Address} Alive Items is zero."); |
||
101 | } |
||
102 | } |
||
103 | catch (Exception ex) |
||
104 | { |
||
105 | System.Diagnostics.Trace.TraceError($"{client.Endpoint.Address} {ex.Message}"); |
||
106 | } |
||
107 | } |
||
108 | else |
||
109 | { |
||
110 | System.Diagnostics.Trace.TraceError($"{client.Endpoint.Address} ping Error"); |
||
111 | } |
||
112 | 60723dc9 | taeseongkim | } |
113 | |||
114 | a34f58f6 | taeseongkim | await System.Windows.Application.Current.Dispatcher.InvokeAsync(() => ItemsUpdate(newitems)); |
115 | 1ae729e4 | taeseongkim | } |
116 | catch (Exception ex) |
||
117 | 60723dc9 | taeseongkim | { |
118 | 0157b158 | taeseongkim | } |
119 | |||
120 | 1ae729e4 | taeseongkim | IsLoading = false; |
121 | } |
||
122 | 0a89a17f | taeseongkim | |
123 | 1ae729e4 | taeseongkim | } |
124 | } |
||
125 | 0157b158 | taeseongkim | |
126 | 1ae729e4 | taeseongkim | |
127 | b92f142f | taeseongkim | |
128 | 1ae729e4 | taeseongkim | private void ItemsUpdate(List<ConvertItem> newitems) |
129 | { |
||
130 | b92f142f | taeseongkim | |
131 | 1ae729e4 | taeseongkim | foreach (var item in newitems) |
132 | { |
||
133 | item.OriginfilePath = HttpUtility.UrlDecode(item.OriginfilePath); |
||
134 | } |
||
135 | 0157b158 | taeseongkim | |
136 | 1ae729e4 | taeseongkim | if (AliveItems == null) |
137 | { |
||
138 | AliveItems = new System.Collections.ObjectModel.ObservableCollection<ConvertItem>(); |
||
139 | |||
140 | foreach (var item in newitems) |
||
141 | { |
||
142 | AliveItems.Add(item); |
||
143 | } |
||
144 | } |
||
145 | else |
||
146 | { |
||
147 | /// 데이터 업데이트 |
||
148 | newitems.ForEach(newitem => |
||
149 | { |
||
150 | AliveItems.UpdateWhere(changeitem => ConvertItemEx.ChangeValues(changeitem, newitem), x => x.ProjectNumber == newitem.ProjectNumber && x.ConvertID == newitem.ConvertID); |
||
151 | }); |
||
152 | |||
153 | // 추가 |
||
154 | foreach (var item in newitems) |
||
155 | { |
||
156 | if (AliveItems.Count(x => x.ConvertID == item.ConvertID && x.ProjectNumber == item.ProjectNumber) == 0) |
||
157 | { |
||
158 | AliveItems.Add(item); |
||
159 | b92f142f | taeseongkim | } |
160 | 0498c12e | taeseongkim | } |
161 | 1ae729e4 | taeseongkim | |
162 | /// 삭제 |
||
163 | |||
164 | for (int i = AliveItems.Count() - 1; i > -1; --i) |
||
165 | 0498c12e | taeseongkim | { |
166 | 1ae729e4 | taeseongkim | var item = AliveItems[i]; |
167 | |||
168 | if (newitems.Count(x => x.ConvertID == item.ConvertID && x.ProjectNumber == item.ProjectNumber) == 0) |
||
169 | { |
||
170 | a34f58f6 | taeseongkim | try |
171 | { |
||
172 | AliveItems.RemoveAt(i); |
||
173 | } |
||
174 | catch (Exception ex) |
||
175 | { |
||
176 | System.Diagnostics.Debug.WriteLine(ex.ToString()); |
||
177 | } |
||
178 | 1ae729e4 | taeseongkim | } |
179 | 0498c12e | taeseongkim | } |
180 | } |
||
181 | } |
||
182 | |||
183 | 1ae729e4 | taeseongkim | private void LogWrite(string log,bool IsError) |
184 | { |
||
185 | if(IsError) |
||
186 | { |
||
187 | System.Diagnostics.Trace.Fail(log); |
||
188 | } |
||
189 | else |
||
190 | { |
||
191 | System.Diagnostics.Trace.TraceInformation(log); |
||
192 | } |
||
193 | } |
||
194 | b92f142f | taeseongkim | |
195 | 06f13e11 | taeseongkim | |
196 | 1ae729e4 | taeseongkim | public static async Task<bool> SimplePingAsync(string uri) |
197 | 06f13e11 | taeseongkim | { |
198 | bool result = false; |
||
199 | |||
200 | try |
||
201 | { |
||
202 | using (System.Net.Http.HttpClient Client = new System.Net.Http.HttpClient()) |
||
203 | { |
||
204 | 1ae729e4 | taeseongkim | Client.Timeout = new TimeSpan(0, 5,0); |
205 | |||
206 | var message = await Client.GetAsync(uri); |
||
207 | 06f13e11 | taeseongkim | |
208 | 1ae729e4 | taeseongkim | System.Net.HttpStatusCode StatusCode = message.StatusCode; |
209 | 06f13e11 | taeseongkim | |
210 | switch (StatusCode) |
||
211 | { |
||
212 | |||
213 | case System.Net.HttpStatusCode.Accepted: |
||
214 | case System.Net.HttpStatusCode.OK: |
||
215 | result = true; |
||
216 | break; |
||
217 | } |
||
218 | } |
||
219 | } |
||
220 | 1ae729e4 | taeseongkim | catch (Exception ex) |
221 | 06f13e11 | taeseongkim | { |
222 | 1ae729e4 | taeseongkim | result = false; |
223 | 06f13e11 | taeseongkim | } |
224 | |||
225 | return result; |
||
226 | } |
||
227 | |||
228 | 0498c12e | taeseongkim | public override void Loaded() |
229 | { |
||
230 | base.Loaded(); |
||
231 | |||
232 | a34f58f6 | taeseongkim | if (!App.IsDesignMode) |
233 | 6f6e7dbf | taeseongkim | { |
234 | backgroundWorker = new BackgroundWorker(); |
||
235 | backgroundWorker.DoWork += backgroundWorker_DoWork; |
||
236 | backgroundWorker.RunWorkerCompleted += backgroundWorker_RunWorkerCompleted; |
||
237 | backgroundWorker.WorkerReportsProgress = false; |
||
238 | backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker_ProgressChanged); |
||
239 | 0498c12e | taeseongkim | |
240 | 6f6e7dbf | taeseongkim | backgroundWorker.RunWorkerAsync(); |
241 | } |
||
242 | 0498c12e | taeseongkim | } |
243 | |||
244 | public override void Closed() |
||
245 | { |
||
246 | 6f6e7dbf | taeseongkim | if (backgroundWorker != null) |
247 | { |
||
248 | backgroundWorker.DoWork -= backgroundWorker_DoWork; |
||
249 | backgroundWorker.RunWorkerCompleted -= backgroundWorker_RunWorkerCompleted; |
||
250 | backgroundWorker.WorkerReportsProgress = false; |
||
251 | backgroundWorker.ProgressChanged -= new ProgressChangedEventHandler(backgroundWorker_ProgressChanged); |
||
252 | 0498c12e | taeseongkim | |
253 | 6f6e7dbf | taeseongkim | backgroundWorker.Dispose(); |
254 | } |
||
255 | 0498c12e | taeseongkim | |
256 | base.Closed(); |
||
257 | } |
||
258 | 6396f27e | taeseongkim | } |
259 | } |