markus / ConvertService / ServiceBase / Markus.Service.StationController / ViewModel / AliveViewModel.cs @ 8ff1bf3a
이력 | 보기 | 이력해설 | 다운로드 (4.35 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 | 0a89a17f | taeseongkim | |
77 | 0157b158 | taeseongkim | |
78 | 0a89a17f | taeseongkim | |
79 | //newitems.UpdateWhere(AliveItems, new ConvertItem().ConvertID); |
||
80 | 0157b158 | taeseongkim | |
81 | AliveItems = newitems; |
||
82 | |||
83 | 0498c12e | taeseongkim | } |
84 | catch (Exception ex) |
||
85 | { |
||
86 | } |
||
87 | } |
||
88 | |||
89 | } |
||
90 | |||
91 | 06f13e11 | taeseongkim | |
92 | public static bool SimplePing(string uri) |
||
93 | { |
||
94 | bool result = false; |
||
95 | |||
96 | try |
||
97 | { |
||
98 | using (System.Net.Http.HttpClient Client = new System.Net.Http.HttpClient()) |
||
99 | { |
||
100 | |||
101 | System.Net.Http.HttpResponseMessage responseMessage = Client.GetAsync(uri).Result; |
||
102 | System.Net.HttpStatusCode StatusCode = responseMessage.StatusCode; |
||
103 | |||
104 | switch (StatusCode) |
||
105 | { |
||
106 | |||
107 | case System.Net.HttpStatusCode.Accepted: |
||
108 | case System.Net.HttpStatusCode.OK: |
||
109 | result = true; |
||
110 | break; |
||
111 | } |
||
112 | } |
||
113 | } |
||
114 | catch (Exception) |
||
115 | { |
||
116 | } |
||
117 | |||
118 | return result; |
||
119 | } |
||
120 | |||
121 | 0498c12e | taeseongkim | public override void Loaded() |
122 | { |
||
123 | base.Loaded(); |
||
124 | |||
125 | 6f6e7dbf | taeseongkim | if (!IsDesignMode) |
126 | { |
||
127 | backgroundWorker = new BackgroundWorker(); |
||
128 | backgroundWorker.DoWork += backgroundWorker_DoWork; |
||
129 | backgroundWorker.RunWorkerCompleted += backgroundWorker_RunWorkerCompleted; |
||
130 | backgroundWorker.WorkerReportsProgress = false; |
||
131 | backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker_ProgressChanged); |
||
132 | 0498c12e | taeseongkim | |
133 | 6f6e7dbf | taeseongkim | backgroundWorker.RunWorkerAsync(); |
134 | } |
||
135 | 0498c12e | taeseongkim | } |
136 | |||
137 | public override void Closed() |
||
138 | { |
||
139 | 6f6e7dbf | taeseongkim | if (backgroundWorker != null) |
140 | { |
||
141 | backgroundWorker.DoWork -= backgroundWorker_DoWork; |
||
142 | backgroundWorker.RunWorkerCompleted -= backgroundWorker_RunWorkerCompleted; |
||
143 | backgroundWorker.WorkerReportsProgress = false; |
||
144 | backgroundWorker.ProgressChanged -= new ProgressChangedEventHandler(backgroundWorker_ProgressChanged); |
||
145 | 0498c12e | taeseongkim | |
146 | 6f6e7dbf | taeseongkim | backgroundWorker.Dispose(); |
147 | } |
||
148 | 0498c12e | taeseongkim | |
149 | base.Closed(); |
||
150 | } |
||
151 | 6396f27e | taeseongkim | } |
152 | } |