프로젝트

일반

사용자정보

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

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

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

    
118
                        IsLoading = false;
119
                    }
120

    
121
                }
122
            }
123

    
124

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

    
137

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

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

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

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

    
152
                        switch (StatusCode)
153
                        {
154

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

    
167
                return result;
168
            }
169

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

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

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

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

    
195
                    backgroundWorker.Dispose();
196
                }
197

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