프로젝트

일반

사용자정보

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

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

이력 | 보기 | 이력해설 | 다운로드 (6.03 KB)

1 06f13e11 taeseongkim
using Markus.Service.WcfClient.StationServiceTask;
2 0498c12e taeseongkim
using System;
3 6396f27e taeseongkim
using System.Collections.Generic;
4 0498c12e taeseongkim
using System.ComponentModel;
5 6396f27e taeseongkim
using System.Linq;
6
using System.Text;
7
using System.Threading.Tasks;
8 0157b158 taeseongkim
using Markus.Service.Extensions;
9 b92f142f taeseongkim
using Markus.Service.Helper;
10
using Markus.Service.StationController.Extensions;
11 0498c12e taeseongkim
12 6396f27e taeseongkim
namespace Markus.Service.StationController.ViewModel
13
{
14 0498c12e taeseongkim
    public class AliveViewModel :Mvvm.ToolKit.ViewModelBase
15 6396f27e taeseongkim
    {
16 0498c12e taeseongkim
        BackgroundWorker backgroundWorker;
17
18 b92f142f taeseongkim
        private System.Collections.ObjectModel.ObservableCollection<ConvertItem> aliveItems;
19 0498c12e taeseongkim
        private bool isLoading;
20
21 b92f142f taeseongkim
        public System.Collections.ObjectModel.ObservableCollection<ConvertItem> AliveItems
22 0498c12e taeseongkim
        {
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 0157b158 taeseongkim
                    List<ConvertItem> newitems = new List<ConvertItem>();
68
69
                    foreach (var client in App.StationClientList)
70
                    {
71 06f13e11 taeseongkim
                        if (SimplePing(client.Endpoint.Address.ToString()))
72
                        {
73
                            var items = await client.AliveConvertListAsync();
74
                            newitems.AddRange(items);
75
                        }
76 0157b158 taeseongkim
                    }
77
78 b92f142f taeseongkim
                    if (AliveItems == null)
79
                    {
80
                        AliveItems = new System.Collections.ObjectModel.ObservableCollection<ConvertItem>();
81 0a89a17f taeseongkim
82 b92f142f taeseongkim
                        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 0157b158 taeseongkim
95 b92f142f taeseongkim
                        // 추가
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 0157b158 taeseongkim
110 b92f142f taeseongkim
                            if (newitems.Count(x => x.ConvertID == item.ConvertID && x.ProjectNumber == item.ProjectNumber) == 0)
111
                            {
112
                                AliveItems.RemoveAt(i);
113
                            }
114
                        }
115
                    }
116 0498c12e taeseongkim
                }
117
                catch (Exception ex)
118
                {
119
                }
120
            }
121
            
122
        }
123
124 b92f142f taeseongkim
   
125
126 06f13e11 taeseongkim
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 ff4b1e6e taeseongkim
                    Client.Timeout = new TimeSpan(0, 0, 10);
136 06f13e11 taeseongkim
137
                    System.Net.Http.HttpResponseMessage responseMessage = Client.GetAsync(uri).Result;
138
                    System.Net.HttpStatusCode StatusCode = responseMessage.StatusCode;
139
140
                    switch (StatusCode)
141
                    {
142
143
                        case System.Net.HttpStatusCode.Accepted:
144
                        case System.Net.HttpStatusCode.OK:
145
                            result = true;
146
                            break;
147
                    }
148
                }
149
            }
150
            catch (Exception)
151
            {
152
            }
153
154
            return result;
155
        }
156
157 0498c12e taeseongkim
        public override void Loaded()
158
        {
159
            base.Loaded();
160
161 6f6e7dbf taeseongkim
            if (!IsDesignMode)
162
            {
163
                backgroundWorker = new BackgroundWorker();
164
                backgroundWorker.DoWork += backgroundWorker_DoWork;
165
                backgroundWorker.RunWorkerCompleted += backgroundWorker_RunWorkerCompleted;
166
                backgroundWorker.WorkerReportsProgress = false;
167
                backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker_ProgressChanged);
168 0498c12e taeseongkim
169 6f6e7dbf taeseongkim
                backgroundWorker.RunWorkerAsync();
170
            }
171 0498c12e taeseongkim
        }
172
173
        public override void Closed()
174
        {
175 6f6e7dbf taeseongkim
            if (backgroundWorker != null)
176
            {
177
                backgroundWorker.DoWork -= backgroundWorker_DoWork;
178
                backgroundWorker.RunWorkerCompleted -= backgroundWorker_RunWorkerCompleted;
179
                backgroundWorker.WorkerReportsProgress = false;
180
                backgroundWorker.ProgressChanged -= new ProgressChangedEventHandler(backgroundWorker_ProgressChanged);
181 0498c12e taeseongkim
182 6f6e7dbf taeseongkim
                backgroundWorker.Dispose();
183
            }
184 0498c12e taeseongkim
185
            base.Closed();
186
        }
187 6396f27e taeseongkim
    }
188
}
클립보드 이미지 추가 (최대 크기: 500 MB)