프로젝트

일반

사용자정보

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

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

이력 | 보기 | 이력해설 | 다운로드 (8.23 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
using System.Web;
12

    
13
namespace Markus.Service.StationController.ViewModel
14
{
15
    public class AliveViewModel : Mvvm.ToolKit.ViewModelBase
16
    {
17
        BackgroundWorker backgroundWorker;
18

    
19
        private System.Collections.ObjectModel.ObservableCollection<ConvertItem> aliveItems;
20
        private bool isLoading;
21
        private System.Windows.Documents.FlowDocument connectionLog;
22

    
23
        public System.Collections.ObjectModel.ObservableCollection<ConvertItem> AliveItems
24
        {
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

    
45
        public System.Windows.Documents.FlowDocument ConnectionLog
46
        {
47
            get => connectionLog;
48
            set
49
            {
50
                if (connectionLog != value)
51
                {
52
                    connectionLog = value;
53
                    OnPropertyChanged(() => ConnectionLog);
54
                }
55
            }
56
        }
57

    
58
        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
                if (!IsLoading)
79
                {
80
                    IsLoading = true;
81

    
82
                    try
83
                    {
84

    
85
                        List<ConvertItem> newitems = new List<ConvertItem>();
86

    
87
                        foreach (var client in App.StationClientList)
88
                        {
89
                            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
                                    System.Diagnostics.Trace.Fail($"{client.Endpoint.Address} {ex.Message}");
105
                                }
106
                            }
107
                            else
108
                            {
109
                                System.Diagnostics.Trace.Fail($"{client.Endpoint.Address} ping Error");
110
                            }
111
                        }
112

    
113
                        await System.Windows.Application.Current.Dispatcher.InvokeAsync(() => ItemsUpdate(newitems));
114
                    }
115
                    catch (Exception ex)
116
                    {
117
                        System.Diagnostics.Debug.WriteLine(ex.ToString());
118
                    }
119

    
120
                    IsLoading = false;
121
                }
122

    
123
            }
124
        }
125

    
126

    
127

    
128
        private void ItemsUpdate(List<ConvertItem> newitems)
129
        {
130

    
131
            foreach (var item in newitems)
132
            {
133
                item.OriginfilePath = HttpUtility.UrlDecode(item.OriginfilePath);
134
            }
135

    
136
            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
                    }
160
                }
161

    
162
                /// 삭제
163

    
164
                for (int i = AliveItems.Count() - 1; i > -1; --i)
165
                {
166
                    var item = AliveItems[i];
167

    
168
                    if (newitems.Count(x => x.ConvertID == item.ConvertID && x.ProjectNumber == item.ProjectNumber) == 0)
169
                    {
170
                        try
171
                        {
172
                            AliveItems.RemoveAt(i);
173
                        }
174
                        catch (Exception ex)
175
                        {
176
                            System.Diagnostics.Debug.WriteLine(ex.ToString());
177
                        }
178
                    }
179
                }
180
            }
181
        }
182

    
183
        private void LogWrite(string log, bool IsError)
184
        {
185
            if (IsError)
186
            {
187
                System.Diagnostics.Trace.Fail(log);
188
            }
189
            else
190
            {
191
                System.Diagnostics.Trace.TraceInformation(log);
192
            }
193
        }
194

    
195

    
196
        public static async Task<bool> SimplePingAsync(string uri)
197
        {
198
            bool result = false;
199

    
200
            try
201
            {
202
                using (System.Net.Http.HttpClient Client = new System.Net.Http.HttpClient())
203
                {
204
                    Client.Timeout = new TimeSpan(0, 0, 60);
205

    
206
                    var message = await Client.GetAsync(uri);
207

    
208
                    System.Net.HttpStatusCode StatusCode = message.StatusCode;
209

    
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
            catch (Exception ex)
221
            {
222
                result = false;
223
                System.Diagnostics.Debug.WriteLine(ex.ToString());
224
            }
225

    
226
            return result;
227
        }
228

    
229
        public override void Loaded()
230
        {
231
            base.Loaded();
232

    
233
            if (!App.IsDesignMode)
234
            {
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

    
241
                backgroundWorker.RunWorkerAsync();
242
            }
243
        }
244

    
245
        public override void Closed()
246
        {
247
            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

    
254
                backgroundWorker.Dispose();
255
            }
256

    
257
            base.Closed();
258
        }
259
    }
260
}
클립보드 이미지 추가 (최대 크기: 500 MB)