-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainViewModel.cs
250 lines (225 loc) · 8.67 KB
/
MainViewModel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
using ACS.SPiiPlusNET;
using CommunityToolkit.Mvvm.ComponentModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using System.Windows.Threading;
namespace HorizontalList
{
public class MainViewModel : ObservableObject
{
private Api _api;
private DispatcherTimer _timer;
public MainViewModel()
{
_api = new Api();
InitProducts();
_timer = new DispatcherTimer();
_timer.Interval = TimeSpan.FromMilliseconds(100);
_timer.Tick += Timer_Tick;
_timer.Start();
}
private void Timer_Tick(object sender, EventArgs e)
{
UpdateControllerStatus();
}
public int _selectedTabIndex;
public int SelectedTabIndex
{
get => _selectedTabIndex;
set => SetProperty(ref _selectedTabIndex, value);
}
private ObservableCollection<Product> _products = new ObservableCollection<Product>();
public ObservableCollection<Product> Products
{
get => _products;
set => SetProperty(ref _products, value);
}
private void InitProducts()
{
//List<Product> productList = new List<Product>();
//Task.Run(() =>
//{
//productList.Add(new Product("SystemSetupMC4U", "10.0.0.91", ConvertToColorBrush(checkIfOnline("10.0.0.91"))));
//productList.Add(new Product("UpgraderMC4U", "10.0.0.91", ConvertToColorBrush(checkIfOnline("10.0.0.92"))));
//productList.Add(new Product("Gantry", "10.0.0.94", ConvertToColorBrush(checkIfOnline("10.0.0.94"))));
//// new Product("SystemSetupMC4U", "10.0.0.91", ),
Products.Add(new Product("SPiiPlusNT-HP", "10.0.0.91"));
Products.Add(new Product("IDMsm", "10.0.0.92"));
Products.Add(new Product("ECMsm Absolute Encoders", "10.0.0.93"));
Products.Add(new Product("Gantry", "10.0.0.94"));
Products.Add(new Product("IDMsa", "10.0.0.96"));
Products.Add(new Product("IDMsa Routing", "10.0.0.97"));
Products.Add(new Product("ECMsm", "10.0.0.98"));
Products.Add(new Product("UDMpc", "10.0.0.101"));
Products.Add(new Product("LCMv2", "10.0.0.101"));
Products.Add(new Product("IOMnt", "10.0.0.101"));
Products.Add(new Product("UDMpa", "10.0.0.101"));
Products.Add(new Product("SDMnt", "10.0.0.101"));
Products.Add(new Product("PDMnt", "10.0.0.101"));
Products.Add(new Product("UDIhp", "10.0.0.101"));
Products.Add(new Product("UDMmc", "10.0.0.101"));
Products.Add(new Product("Beckhoff", "10.0.0.101"));
Products.Add(new Product("PDIcl", "10.0.0.101"));
Products.Add(new Product("UDMlc", "10.0.0.101"));
Products.Add(new Product("NPApm", "10.0.0.101"));
Products.Add(new Product("IOMpsDigital", "10.0.0.101"));
Products.Add(new Product("DC-HP", "10.0.0.101"));
Products.Add(new Product("SPiiPlusEC21Nodes", "10.0.0.101"));
Products.Add(new Product("LCI", "10.0.0.102"));
Products.Add(new Product("SPiiPlusCMhv", "10.0.0.103"));
Products.Add(new Product("SPiiPlusCMnt", "10.0.0.104"));
Products.Add(new Product("IOMpsAnalog", "10.0.0.106"));
Products.Add(new Product("DC-LT", "10.0.0.106"));
Products.Add(new Product("SPiiPlusCMhp_108", "10.0.0.108"));
Products.Add(new Product("UDMsm", "10.0.0.109"));
Products.Add(new Product("ECMma Dual Loop", "10.0.0.110"));
Products.Add(new Product("UDMsa", "10.0.0.111"));
Products.Add(new Product("UDMxa", "10.0.0.112"));
Products.Add(new Product("SPiiPlusCMhp", "10.0.0.113"));
Products.Add(new Product("SPiiPlusCMbaDualLoop", "10.0.0.117"));
Products.Add(new Product("SPiiPlusEC32Nodes", "10.0.0.118"));
Products.Add(new Product("ECMma", "10.0.0.120"));
Products.Add(new Product("UDMma", "10.0.0.121"));
Products.Add(new Product("IDMma", "10.0.0.122"));
Products.Add(new Product("ECMsa", "10.0.0.123"));
Products.Add(new Product("MP4UNPMpc", "10.0.0.126"));
Products.Add(new Product("SPiiPlusCMxa", "10.0.0.128"));
Products.Add(new Product("DualEtherCAT", "10.0.0.131"));
Products.Add(new Product("UDMdx", "10.0.0.132"));
Products.Add(new Product("SpiiPlus SC", "10.0.0.135"));
Task.Run(() =>
{
foreach (var product in Products)
{
string error = CheckError(product.ControllerIP);
Application.Current.Dispatcher.Invoke(() =>
{
product.Error = error;
});
}
});
//Application.Current.Dispatcher.Invoke(() =>
//{
// ListViewProducts.ItemsSource = productList;
//});
//});
}
private string CheckError(string controllerIp)
{
int error = 0;
string ErrStr;
int port = 701;
ErrStr = error.ToString();
try
{
_api.OpenCommEthernet(controllerIp, port);
_api.CloseComm();
error = _api.GetLastError();
}
catch
{
//product.Error = ErrStr;
}
return "";
}
private async void UpdateControllerStatus()
{
foreach (Product product in Products)
{
await Task.Run(() =>
{
string statusColor = "Red";
statusColor = CheckIfOnline(product);
Application.Current.Dispatcher.Invoke(() =>
{
product.StatusColor = ConvertToColorBrush(statusColor);
});
});
}
}
private string CheckIfOnline(Product prod)
{
prod.midTest = true;
string Online = "GreenYellow";
string Error = "Red";
string Rebooted = "Yellow";
bool pingable = false;
Ping pinger = null;
try
{
pinger = new Ping();
PingReply reply = pinger.Send(prod.ControllerIP);
//if success: V
// 1. reset prod.OutOfPing = 0
// 2. return GREEN
//else:
// 1.Take current time and assignd it to a value outOfPing++
// 2.if(outOfPing>200)
// 2.1 outOfPing=201;
// 3.return red
//else:
// 1.return orange
pingable = reply.Status == IPStatus.Success;
if (pingable)
{
prod.OutOfPing = 0;
return Online;
}
else
{
prod.OutOfPing++;
if (prod.OutOfPing > 200)
{
prod.OutOfPing = 201;
return Error;
}
else
{
return Rebooted;
}
}
}
catch (PingException)
{
//in case some channel is open
return Error;
}
finally
{
prod.midTest = false;
if (pinger != null)
{
pinger.Dispose();
}
}
// Get connection info
}
private SolidColorBrush ConvertToColorBrush(string colorName)
{
SolidColorBrush brush;
switch (colorName)
{
case "GreenYellow":
brush = new SolidColorBrush(Colors.GreenYellow);
break;
case "Red":
brush = new SolidColorBrush(Colors.Red);
break;
case "Yellow":
brush = new SolidColorBrush(Colors.Yellow);
break;
default:
brush = new SolidColorBrush(Colors.Transparent);
break;
}
return brush;
}
}
}