프로젝트

일반

사용자정보

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

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

이력 | 보기 | 이력해설 | 다운로드 (8.18 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
                System.Threading.Thread.Sleep(new TimeSpan(0,0,3));
78

    
79
                if (!IsLoading)
80
                {
81
                    IsLoading = true;
82

    
83
                    try
84
                    {
85

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

    
88
                        foreach (var client in App.StationClientList)
89
                        {
90
                            if (await SimplePingAsync(client.Endpoint.Address.ToString()))
91
                            {
92
                                try
93
                                {
94
                                    var items = await client.AliveConvertListAsync();
95
                                    newitems.AddRange(items);
96
                                    System.Diagnostics.Trace.WriteLine($"{client.Endpoint.Address} ping");
97

    
98
                                    if (items.Count() == 0)
99
                                    {
100
                                        System.Diagnostics.Trace.WriteLine($"{client.Endpoint.Address} Alive Items is zero.");
101
                                    }
102
                                }
103
                                catch (Exception ex)
104
                                {
105
                                    System.Diagnostics.Trace.Fail($"{client.Endpoint.Address} {ex.Message}");
106
                                }
107
                            }
108
                            else
109
                            {
110
                                System.Diagnostics.Trace.Fail($"{client.Endpoint.Address} ping Error");
111
                            }
112
                        }
113

    
114
                        await System.Windows.Application.Current.Dispatcher.InvokeAsync(() => ItemsUpdate(newitems));
115
                    }
116
                    catch (Exception ex)
117
                    {
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
            }
224

    
225
            return result;
226
        }
227

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

    
232
            if (!App.IsDesignMode)
233
            {
234
                backgroundWorker = new BackgroundWorker();
235
                backgroundWorker.DoWork += backgroundWorker_DoWork;
236
                backgroundWorker.RunWorkerCompleted += backgroundWorker_RunWorkerCompleted;
237
                backgroundWorker.WorkerReportsProgress = false;
238
                backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker_ProgressChanged);
239

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

    
244
        public override void Closed()
245
        {
246
            if (backgroundWorker != null)
247
            {
248
                backgroundWorker.DoWork -= backgroundWorker_DoWork;
249
                backgroundWorker.RunWorkerCompleted -= backgroundWorker_RunWorkerCompleted;
250
                backgroundWorker.WorkerReportsProgress = false;
251
                backgroundWorker.ProgressChanged -= new ProgressChangedEventHandler(backgroundWorker_ProgressChanged);
252

    
253
                backgroundWorker.Dispose();
254
            }
255

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