-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDriveDataProcessorFESA_25022019_mapExample.java
More file actions
412 lines (293 loc) · 12 KB
/
DriveDataProcessorFESA_25022019_mapExample.java
File metadata and controls
412 lines (293 loc) · 12 KB
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
package de.gsi.csco.ap.app_drivestat.utils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import cern.accsoft.commons.domain.particletransfers.ParticleTransfer;
import cern.japc.AcquiredParameterValue;
import cern.japc.MapParameterValue;
import cern.japc.Parameter;
import cern.japc.ParameterException;
import cern.japc.Parameters;
import cern.japc.Selector;
import cern.japc.factory.ParameterFactory;
import cern.japc.factory.SelectorFactory;
import cern.lsa.client.DeviceService;
import cern.lsa.client.Services;
import cern.lsa.domain.devices.Device;
import cern.lsa.domain.devices.DeviceMetaTypeEnum;
import cern.lsa.domain.devices.factory.DevicesRequestBuilder;
import cern.lsa.domain.settings.BeamProductionChain;
import cern.lsa.domain.settings.Contexts;
import cern.lsa.domain.settings.Pattern;
/**
* @author fschirru
*/
public class DriveDataProcessorFESA {
private static final DriveDataProcessorFESA INSTANCE = new DriveDataProcessorFESA();
private static final DeviceUserNameGenerator dung = DeviceUserNameGenerator.getInstance();
private static final Selector MULTIPLEXED_SELECTOR = SelectorFactory.newSelector("FAIR.SELECTOR.ALL");
private static final String[] DRIVE_TYPES = new String[] { "DS", "PLA" };
private static final String[] propertyname = { "POSIABSI", "CONSTANT", "CONSTANT", "POSITI" };
// private final static String[] propertyname = { "POSIABSS", "CONSTANT",
// "CONSTANT", "POSITI" };
private static final String[] propertyfield = { "data_pos", "minPosition", "maxPosition", "position" };
// private final static String[] propertyfield = { "pos", "minPosition",
// "maxPosition", "position" };
private static final DeviceComparator dc = new DeviceComparator();
private static final DeviceUpdateFESA list_of_substricptions = new DeviceUpdateFESA();
private static String parameterName;
private static Parameter parameter;
private static AcquiredParameterValue ParameterValue;
private static MapParameterValue mapParameterValue;
private static Object[] fieldValue = new Object[propertyname.length];
private final List<Device> mySortedDevices = new ArrayList<>();
private final List<Object[]> driveDataList = new ArrayList<>();
// private final List<Object[]> tempData = new ArrayList<>(1); // deviceName
// +// fieldValues
private final Object[] tempData = new Object[5];
private BeamProductionChain chain;
private Set<Device> myDevices = new HashSet<>();
private final DeviceService deviceService = Services.getDeviceService();
private Set<ParticleTransfer> particleTransfers;
private int p; // the property number
private boolean subscription_exists;
private boolean chainData_exists;
int counter = 0;
private final Map<BeamProductionChain, List<Object[]>> drivesDataMap = new HashMap<>();
public DriveDataProcessorFESA() {
}
public static DriveDataProcessorFESA getInstance() {
return INSTANCE;
}
public void setBeamChain(final BeamProductionChain chain) {
this.chain = chain;
}
public void setDevices() {
chainData_exists = false;
final Set<String> drive_Types = new HashSet<>();
Collections.addAll(drive_Types, DRIVE_TYPES);
particleTransfers = Contexts.getParticleTransfersFromBeamProcesses(chain.getDrivableBeamProcesses());
// Filter on drives per particleTransfers and types
final DevicesRequestBuilder devicesRequestBuilder = new DevicesRequestBuilder();
devicesRequestBuilder.setParticleTransfers(particleTransfers);
devicesRequestBuilder.setMetaType(DeviceMetaTypeEnum.ACTUAL);
devicesRequestBuilder.setDeviceTypeNames(drive_Types);
myDevices = deviceService.findDevices(devicesRequestBuilder.build());
// Check if the chain was already processed with all related drives
for (final BeamProductionChain key : drivesDataMap.keySet()) {
if (key.equals(chain)) {
System.out.println("***Data already processed***");
System.out.println("key : " + key);
System.out.println("value : " + drivesDataMap.get(key));
chainData_exists = true;
getDevicesData(drivesDataMap.get(key));
break;
}
}
if (!chainData_exists) {
getDevicesData(null);
}
}
// get data from hardware
private void getDevicesData(final List<Object[]> data) {
// Clear list to avoid summing up of items while changing the chain
driveDataList.clear();
mySortedDevices.clear();
counter = 0;
for (final Device device : myDevices) {
if (dung.isDeviceNameExisting(device.getName())) {
// Data processing according to the device type
if (device.getDeviceType().getName().equals("DS")) {
for (int p = 0; p < 3; p++) {
try {
parameterName = Parameters.buildParameterName(device.getName(), propertyname[p]);
parameter = ParameterFactory.newInstance().newParameter(parameterName);
ParameterValue = parameter.getValue(MULTIPLEXED_SELECTOR);
mapParameterValue = (MapParameterValue) ParameterValue.getValue();
// acquire the value of the considered property of
// the related device.
fieldValue[p] = mapParameterValue.getString(propertyfield[p]);
} catch (final ParameterException e) {
System.out.println(e);
fieldValue[p] = "0";
}
}
fieldValue[3] = "0"; // by default for DS devices
}
if (device.getDeviceType().getName().equals("PLA")) {
for (int p = 3; p < 4; p++) {
try {
parameterName = Parameters.buildParameterName(device.getName(), propertyname[p]);
parameter = ParameterFactory.newInstance().newParameter(parameterName);
ParameterValue = parameter.getValue(MULTIPLEXED_SELECTOR);
mapParameterValue = (MapParameterValue) ParameterValue.getValue();
// acquire the value of the considered property of
// the related device.
fieldValue[p] = mapParameterValue.getString(propertyfield[p]);
} catch (final ParameterException e) {
System.out.println(e);
fieldValue[p] = "0";
}
}
fieldValue[0] = "0"; // by default for PLA devices
fieldValue[1] = "0"; // by default for PLA devices
fieldValue[2] = "0"; // by default for PLA devices
}
// store temporary data in the array
tempData[0] = device.getName();
tempData[1] = fieldValue[0];
tempData[2] = fieldValue[1];
tempData[3] = fieldValue[2];
tempData[4] = fieldValue[3];
// Once the device has been recognized and data acquired
// the latter will be stored in the driveDataList for
// visualization in the main table
try {
if (!chainData_exists) {
driveDataList.add(new Object[] { device.getName(), fieldValue[0], fieldValue[1], fieldValue[2],
fieldValue[3] });
cloneDriveData(driveDataList);
} else {
System.out.println(tempData[0] + " " + tempData[1] + " " + tempData[2] + " " + tempData[3] + " "
+ tempData[4]);
data.set(counter, tempData);
counter++;
}
mySortedDevices.add(device);
} catch (final Exception e) {
System.out.println(e);
System.exit(0);
}
} // End if deviceNameExists
} // End for Device
if (chainData_exists) {
System.out.println(data.size());
for (int i = 0; i < data.size(); i++) {
System.out.println(data.get(i)[0] + " " + data.get(i)[1] + " " + data.get(i)[2] + " " + data.get(i)[3]
+ " " + data.get(i)[4]);
}
}
// retrieveDataFromFESA();
mySortedDevices.sort(dc); /*- Perform a sort respect device actual position */
}
private void cloneDriveData(final List<Object[]> drivesData) {
// Create a copy of list of processed drives and related chain
final List<Object[]> drivesDataCopy = new ArrayList<>(drivesData.size());
final BeamProductionChain chainCopy = chain;
for (final Object[] item : drivesData) {
drivesDataCopy.add(item);
}
// Store into Map for future usege
drivesDataMap.put(chainCopy, drivesDataCopy);
/*
*
* final Iterator<Map.Entry<BeamProductionChain, List<Object[]>>> it =
* drivesDataMap.entrySet().iterator();
*
* while (it.hasNext()) {
*
* final Map.Entry<BeamProductionChain, List<Object[]>> pair =
* it.next();
*
* // System.out.println(pair//Update Drives //
* startDataRecording();.getKey() + " = " + pair.getValue()); //
* it.remove(); // avoids a ConcurrentModificationException
*
* System.out.println(pair.getKey()); if (!pair.getValue().isEmpty()) {
* System.out.println(pair.getValue().get(0)[0].toString()); }
*
* } /*
*
* for (final Map.Entry<BeamProductionChain, List<Object[]>> e :
* drivesDataMap.entrySet()) {
*
* final Object key = e.getKey();
*
* System.out.println(key);
*
* final Object value = e.getValue();
*
* System.out.println((Object[])value.); }
*/
}
public List<Object[]> getDriveData() {
return driveDataList;
}
public List<Device> getSortedDevices() {
return mySortedDevices;
}
// test method to check all devices processed within the getDeviceData
// method
public void retrieveDataFromFESA() {
System.out.println("******FESA DATA******" + "Size: " + driveDataList.size());
for (int i = 0; i < driveDataList.size(); i++) {
try {
System.out.println(driveDataList.get(i)[0] + " - " + driveDataList.get(i)[1] + " - "
+ driveDataList.get(i)[2] + " - " + driveDataList.get(i)[3] + " - " + driveDataList.get(i)[4]);
} catch (final Exception e) {
System.out.println(e);
System.exit(0);
}
}
}
public void generateDeviceSubscriptions() {
/*- for "DS" devices the only property to be updated is p = 0 while for "PLA" p = 3 */
for (final Device device : myDevices) {
if (dung.isDeviceNameExisting(device.getName())) {
subscription_exists = false;
if (device.getDeviceType().getName().equals("DS")) {
p = 0;
} else {
p = 3; // case "PLA" device
}
final String parametername = Parameters.buildParameterName(device.getName(), propertyname[p]);
Parameter parameter = null;
try {
parameter = ParameterFactory.newInstance().newParameter(parametername);
} catch (final ParameterException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
/*- Check if subscription is already initialized for a particular parameter and selector */
for (int s = 0; s < list_of_substricptions.get_subscriptionHandle().size(); s++) {
if (list_of_substricptions.get_subscriptionHandle().get(s).getParameter() == parameter
&& list_of_substricptions.get_subscriptionHandle().get(s)
.getSelector() == MULTIPLEXED_SELECTOR) {
System.out.println("Subscription already initialized! Index: " + s);
System.out.println(
"Restarting monitoring..." + "Parameter " + parameter.getName() + " Index: " + s);
subscription_exists = true;
try {
list_of_substricptions.get_subscriptionHandle().get(s).startMonitoring();
} catch (final ParameterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
}
if (!subscription_exists) {
System.out.println("A new instance has been created for " + device.getName());
final DeviceUpdateFESA duf = new DeviceUpdateFESA(device.getName());
try {
duf.subscribe(MULTIPLEXED_SELECTOR, propertyname[p]);
} catch (final Exception e) {
System.out.println("Exception: " + e);
System.exit(0);
}
}
} // end if filter on device user-friendly names
}
}
public void closeDeviceSubscriptions() {
for (int s = 0; s < list_of_substricptions.get_subscriptionHandle().size(); s++) {
list_of_substricptions.get_subscriptionHandle().get(s).stopMonitoring();
System.out.println("Subscription stopped. Index " + s);
}
}
}