markus / ConvertService / ServiceBase / Markus.Service.StationController / ViewModel / AliveViewModel.cs @ 1ae729e4
이력 | 보기 | 이력해설 | 다운로드 (7.86 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 | 1ae729e4 | taeseongkim | ItemsUpdate(newitems); |
115 | } |
||
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 | AliveItems.RemoveAt(i); |
||
171 | } |
||
172 | 0498c12e | taeseongkim | } |
173 | } |
||
174 | } |
||
175 | |||
176 | 1ae729e4 | taeseongkim | private void LogWrite(string log,bool IsError) |
177 | { |
||
178 | if(IsError) |
||
179 | { |
||
180 | System.Diagnostics.Trace.Fail(log); |
||
181 | } |
||
182 | else |
||
183 | { |
||
184 | System.Diagnostics.Trace.TraceInformation(log); |
||
185 | } |
||
186 | } |
||
187 | b92f142f | taeseongkim | |
188 | 06f13e11 | taeseongkim | |
189 | 1ae729e4 | taeseongkim | public static async Task<bool> SimplePingAsync(string uri) |
190 | 06f13e11 | taeseongkim | { |
191 | bool result = false; |
||
192 | |||
193 | try |
||
194 | { |
||
195 | using (System.Net.Http.HttpClient Client = new System.Net.Http.HttpClient()) |
||
196 | { |
||
197 | 1ae729e4 | taeseongkim | Client.Timeout = new TimeSpan(0, 5,0); |
198 | |||
199 | var message = await Client.GetAsync(uri); |
||
200 | 06f13e11 | taeseongkim | |
201 | 1ae729e4 | taeseongkim | System.Net.HttpStatusCode StatusCode = message.StatusCode; |
202 | 06f13e11 | taeseongkim | |
203 | switch (StatusCode) |
||
204 | { |
||
205 | |||
206 | case System.Net.HttpStatusCode.Accepted: |
||
207 | case System.Net.HttpStatusCode.OK: |
||
208 | result = true; |
||
209 | break; |
||
210 | } |
||
211 | } |
||
212 | } |
||
213 | 1ae729e4 | taeseongkim | catch (Exception ex) |
214 | 06f13e11 | taeseongkim | { |
215 | 1ae729e4 | taeseongkim | result = false; |
216 | 06f13e11 | taeseongkim | } |
217 | |||
218 | return result; |
||
219 | } |
||
220 | |||
221 | 0498c12e | taeseongkim | public override void Loaded() |
222 | { |
||
223 | base.Loaded(); |
||
224 | |||
225 | 6f6e7dbf | taeseongkim | if (!IsDesignMode) |
226 | { |
||
227 | backgroundWorker = new BackgroundWorker(); |
||
228 | backgroundWorker.DoWork += backgroundWorker_DoWork; |
||
229 | backgroundWorker.RunWorkerCompleted += backgroundWorker_RunWorkerCompleted; |
||
230 | backgroundWorker.WorkerReportsProgress = false; |
||
231 | backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker_ProgressChanged); |
||
232 | 0498c12e | taeseongkim | |
233 | 6f6e7dbf | taeseongkim | backgroundWorker.RunWorkerAsync(); |
234 | } |
||
235 | 0498c12e | taeseongkim | } |
236 | |||
237 | public override void Closed() |
||
238 | { |
||
239 | 6f6e7dbf | taeseongkim | if (backgroundWorker != null) |
240 | { |
||
241 | backgroundWorker.DoWork -= backgroundWorker_DoWork; |
||
242 | backgroundWorker.RunWorkerCompleted -= backgroundWorker_RunWorkerCompleted; |
||
243 | backgroundWorker.WorkerReportsProgress = false; |
||
244 | backgroundWorker.ProgressChanged -= new ProgressChangedEventHandler(backgroundWorker_ProgressChanged); |
||
245 | 0498c12e | taeseongkim | |
246 | 6f6e7dbf | taeseongkim | backgroundWorker.Dispose(); |
247 | } |
||
248 | 0498c12e | taeseongkim | |
249 | base.Closed(); |
||
250 | } |
||
251 | 6396f27e | taeseongkim | } |
252 | } |