프로젝트

일반

사용자정보

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

markus / ConvertService / ServiceBase / Markus.Service.StationController / ViewModel / TotalStatusViewModel.cs @ 29000595

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

1
using Markus.Service.WcfClient.StationServiceTask;
2
using System;
3
using System.Collections.Generic;
4
using System.Collections.ObjectModel;
5
using System.ComponentModel;
6
using System.Linq;
7
using System.Text;
8
using System.Threading.Tasks;
9
using System.Web;
10

    
11
namespace Markus.Service.StationController.ViewModel
12
{
13
    public class TotalStatusViewModel : Mvvm.ToolKit.ViewModelBase
14
    {
15

    
16
        BackgroundWorker backgroundWorker;
17
        private System.Windows.Documents.FlowDocument connectionLog;
18
        private bool isLoading;
19

    
20
        public ObservableCollection<Data.StatusViewItem> items;
21

    
22
        public System.Collections.ObjectModel.ObservableCollection<Data.StatusViewItem> Items
23
        {
24
            get => items; set
25
            {
26
                items = value;
27
                OnPropertyChanged(() => Items);
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

    
44
        public System.Windows.Documents.FlowDocument ConnectionLog
45
        {
46
            get => connectionLog;
47
            set
48
            {
49
                if (connectionLog != value)
50
                {
51
                    connectionLog = value;
52
                    OnPropertyChanged(() => ConnectionLog);
53
                }
54
            }
55
        }
56

    
57
        public TotalStatusViewModel()
58
        {
59
        }
60

    
61
        // 진행률에 변화가 있을때 이벤트가 발생
62
        private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
63
        {
64
        }
65

    
66
        // 일이 모두 마쳤을때 수행되어야할 코드
67
        private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
68
        {
69
        }
70

    
71
        // BackgroundWorker에서 수행할 일을 정의.
72
        private async void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
73
        {
74
            while (IsAcitve)
75
            {
76
                System.Threading.Thread.Sleep(new TimeSpan(0, 0, 3));
77

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

    
82
                    try
83
                    {
84

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

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

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

    
113
                    }
114
                    catch (Exception ex)
115
                    {
116
                        System.Diagnostics.Debug.WriteLine(ex.ToString());
117
                    }
118

    
119
                    IsLoading = false;
120
                }
121

    
122
            }
123
        }
124

    
125

    
126
        private void LogWrite(string log, bool IsError)
127
        {
128
            if (IsError)
129
            {
130
                System.Diagnostics.Trace.Fail(log);
131
            }
132
            else
133
            {
134
                System.Diagnostics.Trace.TraceInformation(log);
135
            }
136
        }
137

    
138

    
139
        public static async Task<bool> SimplePingAsync(string uri)
140
        {
141
            bool result = false;
142

    
143
            try
144
            {
145
                using (System.Net.Http.HttpClient Client = new System.Net.Http.HttpClient())
146
                {
147
                    Client.Timeout = new TimeSpan(0, 5, 0);
148

    
149
                    var message = await Client.GetAsync(uri);
150

    
151
                    System.Net.HttpStatusCode StatusCode = message.StatusCode;
152

    
153
                    switch (StatusCode)
154
                    {
155

    
156
                        case System.Net.HttpStatusCode.Accepted:
157
                        case System.Net.HttpStatusCode.OK:
158
                            result = true;
159
                            break;
160
                    }
161
                }
162
            }
163
            catch (Exception ex)
164
            {
165
                result = false;
166
                System.Diagnostics.Debug.WriteLine(ex.ToString());
167
            }
168

    
169
            return result;
170
        }
171

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

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

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

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

    
197
                backgroundWorker.Dispose();
198
            }
199

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