프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

markus / ConvertService / ServiceBase / Markus.Service.StationController / ViewModel / AliveViewModel.cs @ 29000595

이력 | 보기 | 이력해설 | 다운로드 (8.23 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 2decfbdf alzkakdixm
    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 2decfbdf alzkakdixm
                if (connectionLog != value)
51 1ae729e4 taeseongkim
                {
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
78 1ae729e4 taeseongkim
                if (!IsLoading)
79 0498c12e taeseongkim
                {
80
                    IsLoading = true;
81
82 1ae729e4 taeseongkim
                    try
83 0157b158 taeseongkim
                    {
84 1ae729e4 taeseongkim
85
                        List<ConvertItem> newitems = new List<ConvertItem>();
86
87
                        foreach (var client in App.StationClientList)
88 60723dc9 taeseongkim
                        {
89 1ae729e4 taeseongkim
                            if (await SimplePingAsync(client.Endpoint.Address.ToString()))
90
                            {
91
                                try
92
                                {
93
                                    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
                                    {
99
                                        System.Diagnostics.Trace.WriteLine($"{client.Endpoint.Address} Alive Items is zero.");
100
                                    }
101
                                }
102
                                catch (Exception ex)
103
                                {
104 65fbe3cb taeseongkim
                                    System.Diagnostics.Trace.Fail($"{client.Endpoint.Address} {ex.Message}");
105 1ae729e4 taeseongkim
                                }
106
                            }
107
                            else
108
                            {
109 65fbe3cb taeseongkim
                                System.Diagnostics.Trace.Fail($"{client.Endpoint.Address} ping Error");
110 1ae729e4 taeseongkim
                            }
111 60723dc9 taeseongkim
                        }
112
113 a34f58f6 taeseongkim
                        await System.Windows.Application.Current.Dispatcher.InvokeAsync(() => ItemsUpdate(newitems));
114 1ae729e4 taeseongkim
                    }
115
                    catch (Exception ex)
116 60723dc9 taeseongkim
                    {
117 a6e5055d alzkakdixm
                        System.Diagnostics.Debug.WriteLine(ex.ToString());
118 0157b158 taeseongkim
                    }
119
120 1ae729e4 taeseongkim
                    IsLoading = false;
121
                }
122 0a89a17f taeseongkim
123 1ae729e4 taeseongkim
            }
124
        }
125 0157b158 taeseongkim
126 2decfbdf alzkakdixm
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 a34f58f6 taeseongkim
                        try
171
                        {
172
                            AliveItems.RemoveAt(i);
173
                        }
174
                        catch (Exception ex)
175
                        {
176
                            System.Diagnostics.Debug.WriteLine(ex.ToString());
177
                        }
178 1ae729e4 taeseongkim
                    }
179 0498c12e taeseongkim
                }
180
            }
181
        }
182
183 2decfbdf alzkakdixm
        private void LogWrite(string log, bool IsError)
184 1ae729e4 taeseongkim
        {
185 2decfbdf alzkakdixm
            if (IsError)
186 1ae729e4 taeseongkim
            {
187
                System.Diagnostics.Trace.Fail(log);
188
            }
189
            else
190
            {
191
                System.Diagnostics.Trace.TraceInformation(log);
192
            }
193
        }
194 b92f142f taeseongkim
195 06f13e11 taeseongkim
196 1ae729e4 taeseongkim
        public static async Task<bool> SimplePingAsync(string uri)
197 06f13e11 taeseongkim
        {
198
            bool result = false;
199
200
            try
201
            {
202
                using (System.Net.Http.HttpClient Client = new System.Net.Http.HttpClient())
203
                {
204 2decfbdf alzkakdixm
                    Client.Timeout = new TimeSpan(0, 0, 60);
205
206 1ae729e4 taeseongkim
                    var message = await Client.GetAsync(uri);
207 06f13e11 taeseongkim
208 1ae729e4 taeseongkim
                    System.Net.HttpStatusCode StatusCode = message.StatusCode;
209 06f13e11 taeseongkim
210
                    switch (StatusCode)
211
                    {
212
213
                        case System.Net.HttpStatusCode.Accepted:
214
                        case System.Net.HttpStatusCode.OK:
215
                            result = true;
216
                            break;
217
                    }
218
                }
219
            }
220 1ae729e4 taeseongkim
            catch (Exception ex)
221 06f13e11 taeseongkim
            {
222 1ae729e4 taeseongkim
                result = false;
223 a6e5055d alzkakdixm
                System.Diagnostics.Debug.WriteLine(ex.ToString());
224 06f13e11 taeseongkim
            }
225
226
            return result;
227
        }
228
229 0498c12e taeseongkim
        public override void Loaded()
230
        {
231
            base.Loaded();
232
233 a34f58f6 taeseongkim
            if (!App.IsDesignMode)
234 6f6e7dbf taeseongkim
            {
235
                backgroundWorker = new BackgroundWorker();
236
                backgroundWorker.DoWork += backgroundWorker_DoWork;
237
                backgroundWorker.RunWorkerCompleted += backgroundWorker_RunWorkerCompleted;
238
                backgroundWorker.WorkerReportsProgress = false;
239
                backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker_ProgressChanged);
240 0498c12e taeseongkim
241 6f6e7dbf taeseongkim
                backgroundWorker.RunWorkerAsync();
242
            }
243 0498c12e taeseongkim
        }
244
245
        public override void Closed()
246
        {
247 6f6e7dbf taeseongkim
            if (backgroundWorker != null)
248
            {
249
                backgroundWorker.DoWork -= backgroundWorker_DoWork;
250
                backgroundWorker.RunWorkerCompleted -= backgroundWorker_RunWorkerCompleted;
251
                backgroundWorker.WorkerReportsProgress = false;
252
                backgroundWorker.ProgressChanged -= new ProgressChangedEventHandler(backgroundWorker_ProgressChanged);
253 0498c12e taeseongkim
254 6f6e7dbf taeseongkim
                backgroundWorker.Dispose();
255
            }
256 0498c12e taeseongkim
257
            base.Closed();
258
        }
259 6396f27e taeseongkim
    }
260
}
클립보드 이미지 추가 (최대 크기: 500 MB)