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