markus / ConvertService / ServiceBase / Markus.Service.StationController / ViewModel / MainViewModel.cs @ 45f9a2ad
이력 | 보기 | 이력해설 | 다운로드 (4.41 KB)
1 |
using System; |
---|---|
2 |
using System.Collections.Generic; |
3 |
using System.Linq; |
4 |
using System.ServiceModel; |
5 |
using System.Text; |
6 |
using System.Threading.Tasks; |
7 |
using System.Windows.Input; |
8 |
using Markus.Mvvm.ToolKit; |
9 |
using Markus.Service.Helper; |
10 |
using Markus.Service.StationController.StationService; |
11 |
using MaterialDesignExtensions.Model; |
12 |
|
13 |
namespace Markus.Service.StationController.ViewModel |
14 |
{ |
15 |
public class MainViewModel : ViewModelBase |
16 |
{ |
17 |
StationService.StationServiceClient client; |
18 |
System.Windows.Threading.DispatcherTimer timer; |
19 |
bool IsTimer; |
20 |
|
21 |
#region Command |
22 |
|
23 |
private ICommand _ClosingCommand; |
24 |
public ICommand ClosingCommand |
25 |
{ |
26 |
get => _ClosingCommand ?? (_ClosingCommand = new RelayCommand(param => this.Closing())); |
27 |
} |
28 |
|
29 |
private ICommand _LoadedCommand; |
30 |
public ICommand LoadedCommand |
31 |
{ |
32 |
get => _LoadedCommand ?? (_LoadedCommand = new RelayCommand(param => this.Loaded())); |
33 |
} |
34 |
|
35 |
#endregion Command |
36 |
|
37 |
#region 프로퍼티 |
38 |
|
39 |
private string id; |
40 |
|
41 |
public string Id |
42 |
{ |
43 |
get => id; set |
44 |
{ |
45 |
id = value; |
46 |
OnPropertyChanged(() => Id); |
47 |
} |
48 |
} |
49 |
|
50 |
private List<StationService.ConvertItem> aliveItems; |
51 |
|
52 |
public List<ConvertItem> AliveItems |
53 |
{ |
54 |
get => aliveItems; set |
55 |
{ |
56 |
aliveItems = value; |
57 |
OnPropertyChanged(() => AliveItems); |
58 |
} |
59 |
} |
60 |
|
61 |
private List<INavigationItem> navigationItems; |
62 |
|
63 |
public List<INavigationItem> NavigationItems |
64 |
{ |
65 |
get => navigationItems; set |
66 |
{ |
67 |
navigationItems = value; |
68 |
OnPropertyChanged(() => navigationItems); |
69 |
} |
70 |
} |
71 |
|
72 |
private INavigationItem selectNavigationItem; |
73 |
|
74 |
public INavigationItem SelectNavigationItem |
75 |
{ |
76 |
get => selectNavigationItem; set |
77 |
{ |
78 |
selectNavigationItem = value; |
79 |
OnPropertyChanged(() => SelectNavigationItem); |
80 |
} |
81 |
} |
82 |
|
83 |
#endregion 프로퍼티 |
84 |
|
85 |
private void Loaded() |
86 |
{ |
87 |
IsTimer = true; |
88 |
|
89 |
ServiceConnection(); |
90 |
|
91 |
timer = new System.Windows.Threading.DispatcherTimer(); |
92 |
timer.Interval = new TimeSpan(0, 0, 1); |
93 |
timer.Tick += Timer_Tick; |
94 |
timer.Start(); |
95 |
|
96 |
NavigationItems = new List<INavigationItem> |
97 |
{ |
98 |
new SubheaderNavigationItem(){ Subheader = "Service Settings",IsSelected = true }, |
99 |
new FirstLevelNavigationItem(){Label = "Service1"}, |
100 |
new DividerNavigationItem(), |
101 |
new FirstLevelNavigationItem(){Label = "Service2"}, |
102 |
}; |
103 |
|
104 |
SelectNavigationItem = NavigationItems.First(); |
105 |
} |
106 |
|
107 |
private void Closing() |
108 |
{ |
109 |
IsTimer = false; |
110 |
} |
111 |
|
112 |
private void ServiceConnection() |
113 |
{ |
114 |
try |
115 |
{ |
116 |
if(client != null) |
117 |
{ |
118 |
client.Abort(); |
119 |
client.AliveConvertListCompleted -= Client_AliveConvertListCompleted; |
120 |
client = null; |
121 |
} |
122 |
|
123 |
var configFileName = $"StationController.ini"; |
124 |
|
125 |
var config = ConfigHelper.AppConfig(configFileName); |
126 |
|
127 |
var serviceUri = config.GetValue("SERVICE", "ADDRESS", "http://172.20.101.119:9101/StationService"); |
128 |
|
129 |
BasicHttpBinding myBinding = new BasicHttpBinding(); |
130 |
EndpointAddress myEndpoint = new EndpointAddress(UriHelper.UriCreate(serviceUri)); |
131 |
client = new StationService.StationServiceClient(myBinding, myEndpoint); |
132 |
client.AliveConvertListCompleted += Client_AliveConvertListCompleted; |
133 |
} |
134 |
catch (Exception) |
135 |
{ |
136 |
} |
137 |
|
138 |
} |
139 |
|
140 |
private void Timer_Tick(object sender, EventArgs e) |
141 |
{ |
142 |
try |
143 |
{ |
144 |
if (!IsTimer) |
145 |
{ |
146 |
timer.Stop(); |
147 |
} |
148 |
|
149 |
client.AliveConvertListAsync(); |
150 |
} |
151 |
catch (Exception ex) |
152 |
{ |
153 |
ServiceConnection(); |
154 |
} |
155 |
} |
156 |
|
157 |
private void Client_AliveConvertListCompleted(object sender, AliveConvertListCompletedEventArgs e) |
158 |
{ |
159 |
AliveItems = e.Result; |
160 |
} |
161 |
|
162 |
|
163 |
|
164 |
} |
165 |
} |