프로젝트

일반

사용자정보

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

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

이력 | 보기 | 이력해설 | 다운로드 (6.5 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

    
22
        public System.Collections.ObjectModel.ObservableCollection<ConvertItem> AliveItems
23
        {
24
            get => aliveItems; set
25
            {
26
                aliveItems = value;
27
                OnPropertyChanged(() => AliveItems);
28
            }
29
        }
30

    
31
        public bool IsLoading
32
        {
33
            get => isLoading; set
34
            {
35
                if (isLoading != value)
36
                {
37
                    isLoading = value;
38
                    OnPropertyChanged(() => IsLoading);
39
                }
40
            }
41
        }
42

    
43
        public AliveViewModel()
44
        {
45
        }
46

    
47
        // 진행률에 변화가 있을때 이벤트가 발생
48
        private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
49
        {
50
        }
51

    
52
        // 일이 모두 마쳤을때 수행되어야할 코드
53
        private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
54
        {
55
        }
56

    
57
        // BackgroundWorker에서 수행할 일을 정의.
58
        private async void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
59
        {
60
            while (IsAcitve)
61
            {
62
                System.Threading.Thread.Sleep(5000);
63

    
64
                try
65
                {
66
                    IsLoading = true;
67

    
68
                    List<ConvertItem> newitems = new List<ConvertItem>();
69

    
70
                    foreach (var client in App.StationClientList)
71
                    {
72
                        //if (SimplePing(client.Endpoint.Address.ToString()))
73
                        //{
74
                        try
75
                        {
76
                            var items = await client.AliveConvertListAsync();
77
                            newitems.AddRange(items);
78
                        }
79
                        catch (Exception ex)
80
                        {
81
                            System.Diagnostics.Debug.WriteLine(ex.ToString());
82
                        }
83
                        
84
                        //}
85
                    }
86

    
87
                    foreach (var item in newitems)
88
                    {
89
                        item.OriginfilePath = HttpUtility.UrlDecode(item.OriginfilePath);
90
                    }
91

    
92
                    if (AliveItems == null)
93
                    {
94
                        AliveItems = new System.Collections.ObjectModel.ObservableCollection<ConvertItem>();
95

    
96
                        foreach (var item in newitems)
97
                        {
98
                            AliveItems.Add(item);
99
                        }
100
                    }
101
                    else
102
                    {
103
                        /// 데이터 업데이트
104
                        newitems.ForEach(newitem =>
105
                        {
106
                            AliveItems.UpdateWhere(changeitem => ConvertItemEx.ChangeValues(changeitem, newitem), x => x.ProjectNumber == newitem.ProjectNumber && x.ConvertID == newitem.ConvertID);
107
                        });
108

    
109
                        // 추가
110
                        foreach (var item in newitems)
111
                        {
112
                            if (AliveItems.Count(x => x.ConvertID == item.ConvertID && x.ProjectNumber == item.ProjectNumber) == 0)
113
                            {
114
                                AliveItems.Add(item);
115
                            }
116
                        }
117

    
118
                        /// 삭제
119

    
120
                        for (int i = AliveItems.Count() -1; i > -1; --i)
121
                        {
122
                            var item = AliveItems[i];
123

    
124
                            if (newitems.Count(x => x.ConvertID == item.ConvertID && x.ProjectNumber == item.ProjectNumber) == 0)
125
                            {
126
                                AliveItems.RemoveAt(i);
127
                            }
128
                        }
129
                    }
130
                }
131
                catch (Exception ex)
132
                {
133
                }
134
            }
135
            
136
        }
137

    
138
   
139

    
140

    
141
        public static bool SimplePing(string uri)
142
        {
143
            bool result = false;
144

    
145
            try
146
            {
147
                using (System.Net.Http.HttpClient Client = new System.Net.Http.HttpClient())
148
                {
149
                    Client.Timeout = new TimeSpan(0, 0, 10);
150

    
151
                    System.Net.Http.HttpResponseMessage responseMessage = Client.GetAsync(uri).Result;
152
                    System.Net.HttpStatusCode StatusCode = responseMessage.StatusCode;
153

    
154
                    switch (StatusCode)
155
                    {
156

    
157
                        case System.Net.HttpStatusCode.Accepted:
158
                        case System.Net.HttpStatusCode.OK:
159
                            result = true;
160
                            break;
161
                    }
162
                }
163
            }
164
            catch (Exception)
165
            {
166
            }
167

    
168
            return result;
169
        }
170

    
171
        public override void Loaded()
172
        {
173
            base.Loaded();
174

    
175
            if (!IsDesignMode)
176
            {
177
                backgroundWorker = new BackgroundWorker();
178
                backgroundWorker.DoWork += backgroundWorker_DoWork;
179
                backgroundWorker.RunWorkerCompleted += backgroundWorker_RunWorkerCompleted;
180
                backgroundWorker.WorkerReportsProgress = false;
181
                backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker_ProgressChanged);
182

    
183
                backgroundWorker.RunWorkerAsync();
184
            }
185
        }
186

    
187
        public override void Closed()
188
        {
189
            if (backgroundWorker != null)
190
            {
191
                backgroundWorker.DoWork -= backgroundWorker_DoWork;
192
                backgroundWorker.RunWorkerCompleted -= backgroundWorker_RunWorkerCompleted;
193
                backgroundWorker.WorkerReportsProgress = false;
194
                backgroundWorker.ProgressChanged -= new ProgressChangedEventHandler(backgroundWorker_ProgressChanged);
195

    
196
                backgroundWorker.Dispose();
197
            }
198

    
199
            base.Closed();
200
        }
201
    }
202
}
클립보드 이미지 추가 (최대 크기: 500 MB)