markus / ConvertService / ServiceBase / Markus.Service.StationController / ViewModel / AliveViewModel.cs @ ff4b1e6e
이력 | 보기 | 이력해설 | 다운로드 (6.03 KB)
1 |
using Markus.Service.WcfClient.StationServiceTask; |
---|---|
2 |
using System; |
3 |
using System.Collections.Generic; |
4 |
using System.ComponentModel; |
5 |
using System.Linq; |
6 |
using System.Text; |
7 |
using System.Threading.Tasks; |
8 |
using Markus.Service.Extensions; |
9 |
using Markus.Service.Helper; |
10 |
using Markus.Service.StationController.Extensions; |
11 |
|
12 |
namespace Markus.Service.StationController.ViewModel |
13 |
{ |
14 |
public class AliveViewModel :Mvvm.ToolKit.ViewModelBase |
15 |
{ |
16 |
BackgroundWorker backgroundWorker; |
17 |
|
18 |
private System.Collections.ObjectModel.ObservableCollection<ConvertItem> aliveItems; |
19 |
private bool isLoading; |
20 |
|
21 |
public System.Collections.ObjectModel.ObservableCollection<ConvertItem> AliveItems |
22 |
{ |
23 |
get => aliveItems; set |
24 |
{ |
25 |
aliveItems = value; |
26 |
OnPropertyChanged(() => AliveItems); |
27 |
} |
28 |
} |
29 |
|
30 |
public bool IsLoading |
31 |
{ |
32 |
get => isLoading; set |
33 |
{ |
34 |
if (isLoading != value) |
35 |
{ |
36 |
isLoading = value; |
37 |
OnPropertyChanged(() => IsLoading); |
38 |
} |
39 |
} |
40 |
} |
41 |
|
42 |
public AliveViewModel() |
43 |
{ |
44 |
} |
45 |
|
46 |
// 진행률에 변화가 있을때 이벤트가 발생 |
47 |
private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) |
48 |
{ |
49 |
} |
50 |
|
51 |
// 일이 모두 마쳤을때 수행되어야할 코드 |
52 |
private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) |
53 |
{ |
54 |
} |
55 |
|
56 |
// BackgroundWorker에서 수행할 일을 정의. |
57 |
private async void backgroundWorker_DoWork(object sender, DoWorkEventArgs e) |
58 |
{ |
59 |
while (IsAcitve) |
60 |
{ |
61 |
System.Threading.Thread.Sleep(1000); |
62 |
|
63 |
try |
64 |
{ |
65 |
IsLoading = true; |
66 |
|
67 |
List<ConvertItem> newitems = new List<ConvertItem>(); |
68 |
|
69 |
foreach (var client in App.StationClientList) |
70 |
{ |
71 |
if (SimplePing(client.Endpoint.Address.ToString())) |
72 |
{ |
73 |
var items = await client.AliveConvertListAsync(); |
74 |
newitems.AddRange(items); |
75 |
} |
76 |
} |
77 |
|
78 |
if (AliveItems == null) |
79 |
{ |
80 |
AliveItems = new System.Collections.ObjectModel.ObservableCollection<ConvertItem>(); |
81 |
|
82 |
foreach (var item in newitems) |
83 |
{ |
84 |
AliveItems.Add(item); |
85 |
} |
86 |
} |
87 |
else |
88 |
{ |
89 |
/// 데이터 업데이트 |
90 |
newitems.ForEach(newitem => |
91 |
{ |
92 |
AliveItems.UpdateWhere(changeitem => ConvertItemEx.ChangeValues(changeitem, newitem), x => x.ProjectNumber == newitem.ProjectNumber && x.ConvertID == newitem.ConvertID); |
93 |
}); |
94 |
|
95 |
// 추가 |
96 |
foreach (var item in newitems) |
97 |
{ |
98 |
if (AliveItems.Count(x => x.ConvertID == item.ConvertID && x.ProjectNumber == item.ProjectNumber) == 0) |
99 |
{ |
100 |
AliveItems.Add(item); |
101 |
} |
102 |
} |
103 |
|
104 |
/// 삭제 |
105 |
|
106 |
for (int i = AliveItems.Count() -1; i > -1; --i) |
107 |
{ |
108 |
var item = AliveItems[i]; |
109 |
|
110 |
if (newitems.Count(x => x.ConvertID == item.ConvertID && x.ProjectNumber == item.ProjectNumber) == 0) |
111 |
{ |
112 |
AliveItems.RemoveAt(i); |
113 |
} |
114 |
} |
115 |
} |
116 |
} |
117 |
catch (Exception ex) |
118 |
{ |
119 |
} |
120 |
} |
121 |
|
122 |
} |
123 |
|
124 |
|
125 |
|
126 |
|
127 |
public static bool SimplePing(string uri) |
128 |
{ |
129 |
bool result = false; |
130 |
|
131 |
try |
132 |
{ |
133 |
using (System.Net.Http.HttpClient Client = new System.Net.Http.HttpClient()) |
134 |
{ |
135 |
Client.Timeout = new TimeSpan(0, 0, 10); |
136 |
|
137 |
System.Net.Http.HttpResponseMessage responseMessage = Client.GetAsync(uri).Result; |
138 |
System.Net.HttpStatusCode StatusCode = responseMessage.StatusCode; |
139 |
|
140 |
switch (StatusCode) |
141 |
{ |
142 |
|
143 |
case System.Net.HttpStatusCode.Accepted: |
144 |
case System.Net.HttpStatusCode.OK: |
145 |
result = true; |
146 |
break; |
147 |
} |
148 |
} |
149 |
} |
150 |
catch (Exception) |
151 |
{ |
152 |
} |
153 |
|
154 |
return result; |
155 |
} |
156 |
|
157 |
public override void Loaded() |
158 |
{ |
159 |
base.Loaded(); |
160 |
|
161 |
if (!IsDesignMode) |
162 |
{ |
163 |
backgroundWorker = new BackgroundWorker(); |
164 |
backgroundWorker.DoWork += backgroundWorker_DoWork; |
165 |
backgroundWorker.RunWorkerCompleted += backgroundWorker_RunWorkerCompleted; |
166 |
backgroundWorker.WorkerReportsProgress = false; |
167 |
backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker_ProgressChanged); |
168 |
|
169 |
backgroundWorker.RunWorkerAsync(); |
170 |
} |
171 |
} |
172 |
|
173 |
public override void Closed() |
174 |
{ |
175 |
if (backgroundWorker != null) |
176 |
{ |
177 |
backgroundWorker.DoWork -= backgroundWorker_DoWork; |
178 |
backgroundWorker.RunWorkerCompleted -= backgroundWorker_RunWorkerCompleted; |
179 |
backgroundWorker.WorkerReportsProgress = false; |
180 |
backgroundWorker.ProgressChanged -= new ProgressChangedEventHandler(backgroundWorker_ProgressChanged); |
181 |
|
182 |
backgroundWorker.Dispose(); |
183 |
} |
184 |
|
185 |
base.Closed(); |
186 |
} |
187 |
} |
188 |
} |