프로젝트

일반

사용자정보

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

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

이력 | 보기 | 이력해설 | 다운로드 (8.31 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
                        System.Diagnostics.Debug.WriteLine(ex.ToString());
119
                    }
120

    
121
                    IsLoading = false;
122
                }
123

    
124
            }
125
        }
126

    
127
       
128

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

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

    
137
            if (AliveItems == null)
138
            {
139
                AliveItems = new System.Collections.ObjectModel.ObservableCollection<ConvertItem>();
140

    
141
                foreach (var item in newitems)
142
                {
143
                    AliveItems.Add(item);
144
                }
145
            }
146
            else
147
            {
148
                /// 데이터 업데이트
149
                newitems.ForEach(newitem =>
150
                {
151
                    AliveItems.UpdateWhere(changeitem => ConvertItemEx.ChangeValues(changeitem, newitem), x => x.ProjectNumber == newitem.ProjectNumber && x.ConvertID == newitem.ConvertID);
152
                });
153

    
154
                // 추가
155
                foreach (var item in newitems)
156
                {
157
                    if (AliveItems.Count(x => x.ConvertID == item.ConvertID && x.ProjectNumber == item.ProjectNumber) == 0)
158
                    {
159
                        AliveItems.Add(item);
160
                    }
161
                }
162

    
163
                /// 삭제
164

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

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

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

    
196

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

    
201
            try
202
            {
203
                using (System.Net.Http.HttpClient Client = new System.Net.Http.HttpClient())
204
                {
205
                    Client.Timeout = new TimeSpan(0, 0,60);
206
                    
207
                    var message = await Client.GetAsync(uri);
208

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

    
211
                    switch (StatusCode)
212
                    {
213

    
214
                        case System.Net.HttpStatusCode.Accepted:
215
                        case System.Net.HttpStatusCode.OK:
216
                            result = true;
217
                            break;
218
                    }
219
                }
220
            }
221
            catch (Exception ex)
222
            {
223
                result = false;
224
                System.Diagnostics.Debug.WriteLine(ex.ToString());
225
            }
226

    
227
            return result;
228
        }
229

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

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

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

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

    
255
                backgroundWorker.Dispose();
256
            }
257

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