-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInterface.cs
445 lines (355 loc) · 14.8 KB
/
Interface.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace stationMeteo
{
public partial class Interface : Form
{
private Random random;
private int CELL_SIZE = 30;
private int timePassedInSecond;
private List<Measure> knownMeasures;
private List<Measure> measuresReceived;
private List<SystemID> knownSystemID;
private List<Measure> alarms;
private String[] alarmSources = { "0x55", "0" };
private String[] alarmDetails = { "0x00", "0x55", "0xaa", "0xff" };
private String[] alarmStatuses = { "0x00", "0x55", "0xaa", "0xff" };
private Database database;
public User user;
public Interface()
{
InitializeComponent();
}
private void Interface_Load(object sender, EventArgs e)
{
timePassedInSecond = 0;
trame_timer.Start();
watchdog_timer.Start();
random = new Random();
knownMeasures = new List<Measure>();
measuresReceived = new List<Measure>();
knownSystemID = new List<SystemID>();
alarms = new List<Measure>();
database = new Database();
user = null;
initUi();
}
private void initUi()
{
tabControl1.TabPages[4].Enabled = false;
buildMeasureGrid();
buildConfigurationGrid();
buildAlarmGrid();
fillComboBoxAlarmID();
}
public void login(User user)
{
this.user = user;
if (user.permission.allowConfigAlarm == true)
{
tabControl1.TabPages[4].Enabled = true;
}
}
private void buildMeasureGrid()
{
int columnCount = 6;
DataTable dataTable = new DataTable();
dataTable.Columns.Add("ID");
dataTable.Columns.Add("Last measure");
dataTable.Columns.Add("Units");
dataTable.Columns.Add("Time (ms)");
dataTable.Columns.Add("Alarm Low");
dataTable.Columns.Add("Alarm High");
foreach (Measure measure in knownMeasures)
{
DataRow dataRow = dataTable.NewRow();
dataRow[0] = measure.ID;
dataRow[1] = measure.tenLastMeasures.Last().Value;
dataRow[2] = measure.units;
dataRow[3] = measure.time;
dataRow[4] = measure.alarmLowText;
dataRow[5] = measure.alarmHighText;
dataTable.Rows.Add(dataRow);
}
dgv_measures.DataSource = dataTable;
dgv_measures.RowTemplate.Height = CELL_SIZE;
for (int x = 0; x < columnCount; x++)
{
dgv_measures.Columns[x].Width = dgv_measures.Width / columnCount;
}
for (int y = 0; y < knownMeasures.Count; y++)
{
dgv_measures.Rows[y].Height = CELL_SIZE;
}
}
private void buildSystemIDGrid()
{
int columnCount = 4;
DataTable dataTable = new DataTable();
dataTable.Columns.Add("ID");
dataTable.Columns.Add("Alarm Source");
dataTable.Columns.Add("Alarm Detail");
dataTable.Columns.Add("Alarm Status");
foreach(SystemID systemID in knownSystemID)
{
DataRow dataRow = dataTable.NewRow();
dataRow[0] = systemID.ID;
dataRow[1] = systemID.alarmSource;
dataRow[2] = systemID.alarmDetail;
dataRow[3] = systemID.alarmStatus;
dataTable.Rows.Add(dataRow);
}
dgv_systemID.DataSource = dataTable;
dgv_systemID.RowTemplate.Height = CELL_SIZE;
for (int x = 0; x < columnCount; x++)
{
dgv_systemID.Columns[x].Width = dgv_systemID.Width / columnCount;
}
for (int y = 0; y < knownSystemID.Count; y++)
{
dgv_systemID.Rows[y].Height = CELL_SIZE;
}
}
private void buildConfigurationGrid()
{
int columnCount = 5;
DataTable dataTable = new DataTable();
dataTable.Columns.Add("ID");
dataTable.Columns.Add("Measurement type");
dataTable.Columns.Add("Format");
dataTable.Columns.Add("Min.Value");
dataTable.Columns.Add("Max.Value");
foreach (Measure measure in measuresReceived)
{
DataRow dataRow = dataTable.NewRow();
dataRow[0] = measure.ID;
dataRow[1] = measure.measurementType == null ? "undefined" : measure.measurementType;
dataRow[2] = measure.format == null ? "undefined" : measure.format;
dataRow[3] = measure.minValue == null ? 0 : measure.minValue;
dataRow[4] = measure.maxValue == null ? 0 : measure.maxValue;
dataTable.Rows.Add(dataRow);
}
dgv_configurations.DataSource = dataTable;
dgv_configurations.RowTemplate.Height = CELL_SIZE;
for (int x = 0; x < columnCount; x++)
{
dgv_configurations.Columns[x].Width = dgv_measures.Width / columnCount;
}
for (int y = 0; y < measuresReceived.Count; y++)
{
dgv_configurations.Rows[y].Height = CELL_SIZE;
}
}
private void buildAlarmGrid()
{
int columnCount = 4;
DataTable dataTable = new DataTable();
dataTable.Columns.Add("ID");
dataTable.Columns.Add("Measurement type");
dataTable.Columns.Add("Alarm Low");
dataTable.Columns.Add("Alarm High");
foreach (Measure measure in alarms)
{
DataRow dataRow = dataTable.NewRow();
dataRow[0] = measure.ID;
dataRow[1] = measure.measurementType;
dataRow[2] = measure.alarmLow;
dataRow[3] = measure.alarmHigh;
dataTable.Rows.Add(dataRow);
}
dgv_alarm.DataSource = dataTable;
dgv_alarm.RowTemplate.Height = CELL_SIZE;
for (int x = 0; x < columnCount; x++)
{
dgv_alarm.Columns[x].Width = dgv_alarm.Width / columnCount;
}
for (int y = 0; y < alarms.Count; y++)
{
dgv_alarm.Rows[y].Height = CELL_SIZE;
}
}
private void buildChart(Measure measure)
{
chart_measure.Series["Mesure"].Points.Clear();
chart_measure.ChartAreas[0].AxisY.Maximum = (double)measure.maxValue;
chart_measure.ChartAreas[0].AxisY.Minimum = (double)measure.minValue;
foreach(KeyValuePair<int, Byte> lastMeasure in measure.tenLastMeasures)
{
chart_measure.Series["Mesure"].Points.AddXY(lastMeasure.Key, lastMeasure.Value);
}
}
private void fillComboBoxAlarmID()
{
foreach (Measure measure in knownMeasures)
{
cb_alarmID.Items.Add(measure.ID);
}
}
/**
* Charge une nouvelle mesure, met à jour la fenetre de configuration & affiche dans la fenetre de mesures
* Tri la fenetre des mesure à chaque nouveau ajout
*/
public void loadNewMeasure(Measure newMeasure)
{
int oldMeasureIndex = measuresReceived.FindIndex(measureReceived => measureReceived.ID == newMeasure.ID);
measuresReceived[oldMeasureIndex] = newMeasure;
if (newMeasure.units != null)
{
knownMeasures.Add(newMeasure);
knownMeasures.Sort((knownMeasure1, knownMeasure2) => knownMeasure1.ID.CompareTo(knownMeasure2.ID));
cb_alarmID.Items.Add(newMeasure.ID);
cb_idChart.Items.Add(newMeasure.ID);
buildMeasureGrid();
buildConfigurationGrid();
}
}
public void loadNewIDSystem(Byte ID)
{
String alarmSource = alarmSources[random.Next(0, alarmSources.Length)] ;
String alarmDetail = alarmDetails[random.Next(0, alarmDetails.Length)];
String alarmStatus = alarmStatuses[random.Next(0, alarmStatuses.Length)];
if (alarmSource == "0") {
if (knownMeasures.Count > 0)
{
alarmSource = knownMeasures[random.Next(0, knownMeasures.Count)].ID.ToString();
knownSystemID.Add(new SystemID(ID, alarmSource, alarmDetail, alarmStatus));
} else
{
knownSystemID.Add(new SystemID(ID, "none", alarmDetail, alarmStatus));
}
} else
{
knownSystemID.Add(new SystemID(ID, alarmSource, alarmDetail, alarmStatus));
}
buildSystemIDGrid();
}
public void updateMeasure(Measure measure, int value)
{
Byte rightValue = (Byte) ((value / ((Math.Pow(256, 3)) - 1) * (measure.maxValue - measure.minValue) + measure.minValue));
measure.tenLastMeasures.Add(timePassedInSecond, rightValue);
if (rightValue > measure.alarmHigh)
{
measure.alarmHighText = "ALARM";
}
else
{
measure.alarmHighText = "OK";
}
if (rightValue < measure.alarmLow)
{
measure.alarmLowText = "ALARM";
} else
{
measure.alarmLowText = "OK";
}
buildMeasureGrid();
}
private void sendTrame(object sender, EventArgs e)
{
Byte ID = (Byte) random.Next(1, 51); //Crée un id
int value = random.Next(0, 16000000);
Tram tram = new Tram(ID, value);
Measure isKnownMeasure = knownMeasures.Find(knownMeasure => knownMeasure.ID == tram.ID); //Si la mesure à déjà été configuré, met à jour sa valeur sinon affiche la tram indefinie dans le tableau de configuration
if (isKnownMeasure != null)
{
updateMeasure(isKnownMeasure, value);
} else
{
displayIDToConfigurationTab(tram);
}
timePassedInSecond += 1;
}
/*
* Crée une mesure inconnue uniquement identifiable par l'id reçue par la trame
* Verifie si la mesure inconnue à déjà été envoyé par la trame
* Si la mesure n'a pas encore été envoyé et que la liste n'est pas pleine,
* affiche la mesure incconu pour la configurer et trie la liste pour afficher les ID du plus petit au plus grand
* Si la tram envoyé apartient au Systeme, pas besoin qu'elle soit inconnu ni configuré
*/
private void displayIDToConfigurationTab(Tram tram)
{
Measure measure;
measure = new Measure(tram.ID, null, null, "24bits", null, null, null, null, null, null);
Measure measureAlreadyReceived = measuresReceived.Find(measureReceived => measure.ID == measureReceived.ID);
if (measureAlreadyReceived == null && measuresReceived.Count < 50)
{
measuresReceived.Add(measure);
measuresReceived.Sort((measureReceived1, measureReceived2) => measureReceived1.ID.CompareTo(measureReceived2.ID));
buildConfigurationGrid();
}
}
private void sendWatchdog(object sender, EventArgs e)
{
Tram tram = new Tram(0, 0);
displayIDToConfigurationTab(tram);
}
private void onAlarmLoaded(object sender, EventArgs e)
{
Byte selectedID = (Byte)cb_alarmID.SelectedItem;
Measure measure = knownMeasures.Find(knownMeasure => knownMeasure.ID == selectedID);
measure.alarmLow = Byte.Parse(tb_alarmBelow.Text);
measure.alarmHigh = Byte.Parse(tb_alarmUp.Text);
alarms.Add(measure);
buildAlarmGrid();
}
private void onSelectedAlarmIDChanged(object sender, EventArgs e)
{
Byte selectedID = (Byte) cb_alarmID.SelectedItem;
Measure measure = knownMeasures.Find(knownMeasure => knownMeasure.ID == selectedID );
tb_alarmBelow.Text = measure.alarmLow.ToString();
tb_alarmUp.Text = measure.alarmHigh.ToString();
lb_minMeasure.Text = measure.minValue.ToString();
lb_maxMeasure.Text = measure.maxValue.ToString();
}
private void onChartIDSelected(object sender, EventArgs e)
{
Byte ID = (Byte)cb_idChart.SelectedItem;
Measure measure = knownMeasures.Find(knownMeasure => knownMeasure.ID == ID);
buildChart(measure);
}
private void goToConfigurationForm(object sender, EventArgs e)
{
if (user != null && user.permission.allowCreateID)
{
MeasurementConfiguration measurementConfigurationForm = new MeasurementConfiguration(this, measuresReceived);
measurementConfigurationForm.Show();
}
else
{
MessageBox.Show("Vous n'avez pas l'autorisation de configurer des ID !", "Contacter votre administrateur", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void goToAuthenticationForm(object sender, EventArgs e)
{
Authentication authenticationForm = new Authentication(this);
authenticationForm.ShowDialog();
}
private void goToUserCreationForm(object sender, EventArgs e)
{
if (user != null && user.permission.allowUserCreation == true)
{
UserCreation userCreationForm = new UserCreation(this);
userCreationForm.Show();
} else
{
MessageBox.Show("Vous n'avez pas l'autorisation de rajouter des utilisateurs !", "Contacter votre administrateur", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void onDisconnect(object sender, EventArgs e)
{
if (user != null)
{
MessageBox.Show("Vous vous êtes déconnecté ", "Vous n'avez plus de de droits", MessageBoxButtons.OK, MessageBoxIcon.Information);
user = null;
tabControl1.TabPages[4].Enabled = false;
}
}
}
}