프로젝트

일반

사용자정보

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

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

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

    
10

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

    
17
        private List<ConvertItem> aliveItems;
18
        private bool isLoading;
19

    
20
        public List<ConvertItem> AliveItems
21
        {
22
            get => aliveItems; set
23
            {
24
                aliveItems = value;
25
                OnPropertyChanged(() => AliveItems);
26
            }
27
        }
28

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

    
41
        public AliveViewModel()
42
        {
43
        }
44

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

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

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

    
62
                try
63
                {
64
                    IsLoading = true;
65

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

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

    
78

    
79
                  //newitems.UpdateWhere(AliveItems, new ConvertItem().ConvertID);
80

    
81
                    AliveItems = newitems;
82

    
83
                }
84
                catch (Exception ex)
85
                {
86
                }
87
            }
88
            
89
        }
90

    
91

    
92
        public static bool SimplePing(string uri)
93
        {
94
            bool result = false;
95

    
96
            try
97
            {
98
                using (System.Net.Http.HttpClient Client = new System.Net.Http.HttpClient())
99
                {
100

    
101
                    System.Net.Http.HttpResponseMessage responseMessage = Client.GetAsync(uri).Result;
102
                    System.Net.HttpStatusCode StatusCode = responseMessage.StatusCode;
103

    
104
                    switch (StatusCode)
105
                    {
106

    
107
                        case System.Net.HttpStatusCode.Accepted:
108
                        case System.Net.HttpStatusCode.OK:
109
                            result = true;
110
                            break;
111
                    }
112
                }
113
            }
114
            catch (Exception)
115
            {
116
            }
117

    
118
            return result;
119
        }
120

    
121
        public override void Loaded()
122
        {
123
            base.Loaded();
124

    
125
            if (!IsDesignMode)
126
            {
127
                backgroundWorker = new BackgroundWorker();
128
                backgroundWorker.DoWork += backgroundWorker_DoWork;
129
                backgroundWorker.RunWorkerCompleted += backgroundWorker_RunWorkerCompleted;
130
                backgroundWorker.WorkerReportsProgress = false;
131
                backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker_ProgressChanged);
132

    
133
                backgroundWorker.RunWorkerAsync();
134
            }
135
        }
136

    
137
        public override void Closed()
138
        {
139
            if (backgroundWorker != null)
140
            {
141
                backgroundWorker.DoWork -= backgroundWorker_DoWork;
142
                backgroundWorker.RunWorkerCompleted -= backgroundWorker_RunWorkerCompleted;
143
                backgroundWorker.WorkerReportsProgress = false;
144
                backgroundWorker.ProgressChanged -= new ProgressChangedEventHandler(backgroundWorker_ProgressChanged);
145

    
146
                backgroundWorker.Dispose();
147
            }
148

    
149
            base.Closed();
150
        }
151
    }
152
}
클립보드 이미지 추가 (최대 크기: 500 MB)