프로젝트

일반

사용자정보

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

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

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

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

    
18
        private System.Collections.ObjectModel.ObservableCollection<ConvertItem> aliveItems;
19
        private bool isLoading;
20

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

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

    
42
        public AliveViewModel()
43
        {
44
        }
45

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

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

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

    
63
                try
64
                {
65
                    IsLoading = true;
66

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

    
69
                    foreach (var client in App.StationClientList)
70
                    {
71
                        if (SimplePing(client.Endpoint.Address.ToString()))
72
                        {
73
                            var items = await client.AliveConvertListAsync();
74
                            newitems.AddRange(items);
75
                        }
76
                    }
77

    
78
                    if (AliveItems == null)
79
                    {
80
                        AliveItems = new System.Collections.ObjectModel.ObservableCollection<ConvertItem>();
81

    
82
                        foreach (var item in newitems)
83
                        {
84
                            AliveItems.Add(item);
85
                        }
86
                    }
87
                    else
88
                    {
89
                        /// 데이터 업데이트
90
                        newitems.ForEach(newitem =>
91
                        {
92
                            AliveItems.UpdateWhere(changeitem => ConvertItemEx.ChangeValues(changeitem, newitem), x => x.ProjectNumber == newitem.ProjectNumber && x.ConvertID == newitem.ConvertID);
93
                        });
94

    
95
                        // 추가
96
                        foreach (var item in newitems)
97
                        {
98
                            if (AliveItems.Count(x => x.ConvertID == item.ConvertID && x.ProjectNumber == item.ProjectNumber) == 0)
99
                            {
100
                                AliveItems.Add(item);
101
                            }
102
                        }
103

    
104
                        /// 삭제
105

    
106
                        for (int i = AliveItems.Count() -1; i > -1; --i)
107
                        {
108
                            var item = AliveItems[i];
109

    
110
                            if (newitems.Count(x => x.ConvertID == item.ConvertID && x.ProjectNumber == item.ProjectNumber) == 0)
111
                            {
112
                                AliveItems.RemoveAt(i);
113
                            }
114
                        }
115
                    }
116
                }
117
                catch (Exception ex)
118
                {
119
                }
120
            }
121
            
122
        }
123

    
124
   
125

    
126

    
127
        public static bool SimplePing(string uri)
128
        {
129
            bool result = false;
130

    
131
            try
132
            {
133
                using (System.Net.Http.HttpClient Client = new System.Net.Http.HttpClient())
134
                {
135

    
136
                    System.Net.Http.HttpResponseMessage responseMessage = Client.GetAsync(uri).Result;
137
                    System.Net.HttpStatusCode StatusCode = responseMessage.StatusCode;
138

    
139
                    switch (StatusCode)
140
                    {
141

    
142
                        case System.Net.HttpStatusCode.Accepted:
143
                        case System.Net.HttpStatusCode.OK:
144
                            result = true;
145
                            break;
146
                    }
147
                }
148
            }
149
            catch (Exception)
150
            {
151
            }
152

    
153
            return result;
154
        }
155

    
156
        public override void Loaded()
157
        {
158
            base.Loaded();
159

    
160
            if (!IsDesignMode)
161
            {
162
                backgroundWorker = new BackgroundWorker();
163
                backgroundWorker.DoWork += backgroundWorker_DoWork;
164
                backgroundWorker.RunWorkerCompleted += backgroundWorker_RunWorkerCompleted;
165
                backgroundWorker.WorkerReportsProgress = false;
166
                backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker_ProgressChanged);
167

    
168
                backgroundWorker.RunWorkerAsync();
169
            }
170
        }
171

    
172
        public override void Closed()
173
        {
174
            if (backgroundWorker != null)
175
            {
176
                backgroundWorker.DoWork -= backgroundWorker_DoWork;
177
                backgroundWorker.RunWorkerCompleted -= backgroundWorker_RunWorkerCompleted;
178
                backgroundWorker.WorkerReportsProgress = false;
179
                backgroundWorker.ProgressChanged -= new ProgressChangedEventHandler(backgroundWorker_ProgressChanged);
180

    
181
                backgroundWorker.Dispose();
182
            }
183

    
184
            base.Closed();
185
        }
186
    }
187
}
클립보드 이미지 추가 (최대 크기: 500 MB)