markus / ConvertService / ServiceBase / Markus.Service.StationController / ViewModel / AliveViewModel.cs @ 06f13e11
이력 | 보기 | 이력해설 | 다운로드 (4.29 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 | 6396f27e | taeseongkim | |
10 | 0498c12e | taeseongkim | |
11 | 6396f27e | taeseongkim | namespace Markus.Service.StationController.ViewModel |
12 | { |
||
13 | 0498c12e | taeseongkim | public class AliveViewModel :Mvvm.ToolKit.ViewModelBase |
14 | 6396f27e | taeseongkim | { |
15 | 0498c12e | taeseongkim | BackgroundWorker backgroundWorker; |
16 | |||
17 | 06f13e11 | taeseongkim | private List<ConvertItem> aliveItems; |
18 | 0498c12e | taeseongkim | private bool isLoading; |
19 | |||
20 | public List<ConvertItem> AliveItems |
||
21 | { |
||
22 | get => aliveItems; set |
||
23 | { |
||
24 | aliveItems = value; |
||
25 | OnPropertyChanged(() => AliveItems); |
||
26 | } |
||
27 | } |
||
28 | |||
29 | public bool IsLoading |
||
30 | { |
||
31 | get => isLoading; set |
||
32 | { |
||
33 | if (isLoading != value) |
||
34 | { |
||
35 | isLoading = value; |
||
36 | OnPropertyChanged(() => IsLoading); |
||
37 | } |
||
38 | } |
||
39 | } |
||
40 | |||
41 | public AliveViewModel() |
||
42 | { |
||
43 | } |
||
44 | |||
45 | // 진행률에 변화가 있을때 이벤트가 발생 |
||
46 | private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) |
||
47 | { |
||
48 | } |
||
49 | |||
50 | // 일이 모두 마쳤을때 수행되어야할 코드 |
||
51 | private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) |
||
52 | { |
||
53 | } |
||
54 | |||
55 | // BackgroundWorker에서 수행할 일을 정의. |
||
56 | private async void backgroundWorker_DoWork(object sender, DoWorkEventArgs e) |
||
57 | { |
||
58 | while (IsAcitve) |
||
59 | { |
||
60 | System.Threading.Thread.Sleep(1000); |
||
61 | |||
62 | try |
||
63 | { |
||
64 | IsLoading = true; |
||
65 | |||
66 | 0157b158 | taeseongkim | List<ConvertItem> newitems = new List<ConvertItem>(); |
67 | |||
68 | foreach (var client in App.StationClientList) |
||
69 | { |
||
70 | 06f13e11 | taeseongkim | if (SimplePing(client.Endpoint.Address.ToString())) |
71 | { |
||
72 | var items = await client.AliveConvertListAsync(); |
||
73 | newitems.AddRange(items); |
||
74 | } |
||
75 | 0157b158 | taeseongkim | } |
76 | |||
77 | newitems.Update(AliveItems); |
||
78 | |||
79 | AliveItems = newitems; |
||
80 | |||
81 | 0498c12e | taeseongkim | } |
82 | catch (Exception ex) |
||
83 | { |
||
84 | } |
||
85 | } |
||
86 | |||
87 | } |
||
88 | |||
89 | 06f13e11 | taeseongkim | |
90 | public static bool SimplePing(string uri) |
||
91 | { |
||
92 | bool result = false; |
||
93 | |||
94 | try |
||
95 | { |
||
96 | using (System.Net.Http.HttpClient Client = new System.Net.Http.HttpClient()) |
||
97 | { |
||
98 | |||
99 | System.Net.Http.HttpResponseMessage responseMessage = Client.GetAsync(uri).Result; |
||
100 | System.Net.HttpStatusCode StatusCode = responseMessage.StatusCode; |
||
101 | |||
102 | switch (StatusCode) |
||
103 | { |
||
104 | |||
105 | case System.Net.HttpStatusCode.Accepted: |
||
106 | case System.Net.HttpStatusCode.OK: |
||
107 | result = true; |
||
108 | break; |
||
109 | } |
||
110 | } |
||
111 | } |
||
112 | catch (Exception) |
||
113 | { |
||
114 | } |
||
115 | |||
116 | return result; |
||
117 | } |
||
118 | |||
119 | 0498c12e | taeseongkim | public override void Loaded() |
120 | { |
||
121 | base.Loaded(); |
||
122 | |||
123 | 6f6e7dbf | taeseongkim | if (!IsDesignMode) |
124 | { |
||
125 | backgroundWorker = new BackgroundWorker(); |
||
126 | backgroundWorker.DoWork += backgroundWorker_DoWork; |
||
127 | backgroundWorker.RunWorkerCompleted += backgroundWorker_RunWorkerCompleted; |
||
128 | backgroundWorker.WorkerReportsProgress = false; |
||
129 | backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker_ProgressChanged); |
||
130 | 0498c12e | taeseongkim | |
131 | 6f6e7dbf | taeseongkim | backgroundWorker.RunWorkerAsync(); |
132 | } |
||
133 | 0498c12e | taeseongkim | } |
134 | |||
135 | public override void Closed() |
||
136 | { |
||
137 | 6f6e7dbf | taeseongkim | if (backgroundWorker != null) |
138 | { |
||
139 | backgroundWorker.DoWork -= backgroundWorker_DoWork; |
||
140 | backgroundWorker.RunWorkerCompleted -= backgroundWorker_RunWorkerCompleted; |
||
141 | backgroundWorker.WorkerReportsProgress = false; |
||
142 | backgroundWorker.ProgressChanged -= new ProgressChangedEventHandler(backgroundWorker_ProgressChanged); |
||
143 | 0498c12e | taeseongkim | |
144 | 6f6e7dbf | taeseongkim | backgroundWorker.Dispose(); |
145 | } |
||
146 | 0498c12e | taeseongkim | |
147 | base.Closed(); |
||
148 | } |
||
149 | 6396f27e | taeseongkim | } |
150 | } |