-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeviceUpdateFESA_Magstat_22032019.java
More file actions
475 lines (349 loc) · 14.8 KB
/
Copy pathDeviceUpdateFESA_Magstat_22032019.java
File metadata and controls
475 lines (349 loc) · 14.8 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
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
package de.gsi.cs.co.ap.app.magstat.utils;
import cern.japc.AcquiredParameterValue;
import cern.japc.ParameterException;
import cern.japc.ParameterValueListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cern.japc.MapParameterValue;
import cern.japc.Parameter;
import cern.japc.Parameters;
import cern.japc.Selector;
import cern.japc.SubscriptionHandle;
import cern.japc.factory.ParameterFactory;
import cern.japc.group.support.SysoutFailSafeParameterValueListener;
import cern.lsa.domain.devices.Device;
// for library loggers
// import org.slf4j.Logger;
// import org.slf4j.LoggerFactory;
// for application loggers
// import de.gsi.cs.co.ap.common.gui.elements.logger.AppLogger;
/**
* @author fschirru
*/
public class DeviceUpdateFESA implements ParameterValueListener {
// You can choose a logger (needed imports are given in the import section
// as comments):
// for libraries:
// private static final Logger LOGGER =
// LoggerFactory.getLogger(DeviceUpdateFESA.class);
// for applications:
// private static final AppLogger LOGGER = AppLogger.getLogger();
private static final DeviceUpdateFESA INSTANCE = new DeviceUpdateFESA();
private final RetrieveDeviceSelector rdSel = RetrieveDeviceSelector.getInstance();
private final DeviceCalibrationCurveConverter dccc = DeviceCalibrationCurveConverter.getInstance();
private final static MagnetDataProcessorLSA mdpLSA = MagnetDataProcessorLSA.getInstance();
private static final Logger LOGGER = LoggerFactory.getLogger(DeviceUpdateFESA.class);
private final static String[] propertyName = { "Acquisition", "DCValue", "DCMode", "Status", "DCValueAcq" };
private final static Set<Device> myDevices = mdpLSA.getStaticDevices();
// private static final Selector NULL_SELECTOR = null;
// private static final Selector MULTIPLEXED_SELECTOR =
// SelectorFactory.newSelector(""); //
// SelectorFactory.newSelector("FAIR.BP.3");
private final AcquiredParameterValue parameterValue = null;
private String devicename;
// private double currentVal; // related to DCMode "true"
private final double currentValAcq = 99.99; // related to DCMode "false"
// private boolean dcMode;
private double voltageValAcq;
// private Double bFieldConverted;
// private Double bFieldConverted_v;
private List<Boolean> listOfBooleans = new ArrayList<>();
private List<String> listOfLabels = new ArrayList<>();
// private final AcquiredParameterValue parameterValue = null;
private ParameterException parameterException;
private SubscriptionHandle subscriptionHandle;
private static List<SubscriptionHandle> subscriptionHandlers = new ArrayList<>();
private static boolean existing_subscription = false;
private final MagnetDataJoiner mdpj = MagnetDataJoiner.getInstance();
/**
*
*/
public DeviceUpdateFESA() {
}
public static DeviceUpdateFESA getInstance() {
return INSTANCE;
}
public AcquiredParameterValue getParameterValue(final Selector selector, final String propertyname) {
try {
final String parametername = Parameters.buildParameterName(devicename, propertyname);
final Parameter parameter = ParameterFactory.newInstance().newParameter(parametername);
// final AcquiredParameterValue parameterValue =
// parameter.getValue(NULL_SELECTOR); // commented on
// 25/5/2018
final AcquiredParameterValue parameterValue = parameter.getValue(selector); // placed
// on
// 25/5/2018
final MapParameterValue mapParameterValue = (MapParameterValue) parameterValue.getValue();
final int powerState = mapParameterValue.getInt("powerState");
LOGGER.info("Parameter value for " + devicename + ", " + propertyname + ", " + " received : " + powerState);
} catch (final ParameterException e) {
LOGGER.error(e.getMessage());
}
return parameterValue;
}
public void subscribe(final Selector selector, final String propertyname) {
final String parametername = Parameters.buildParameterName(devicename, propertyname);
try {
final Parameter parameter = ParameterFactory.newInstance().newParameter(parametername);
// SubscriptionHandle conn =
// parameter.createSubscription(MULTIPLEXED_SELECTOR, this);
// Check if the subscription for a certain parameter is already
// created
// If so, use that one in the case of re-starting monitoring of the
// same parameter
existing_subscription = false;
// System.out.println("*****************************************" +
// subscriptionHandlers.size());
for (int s = 0; s < subscriptionHandlers.size(); s++) {
if (subscriptionHandlers.get(s).getParameter().equals(parameter)) {
if (subscriptionHandlers.get(s).getSelector().equals(selector)) {
// System.out.println(subscriptionHandlers.get(s).getParameter());
// System.out.println(parameter);
// System.out.println(subscriptionHandlers.get(s).getSelector());
// System.out.println(selector);
subscriptionHandlers.get(s).startMonitoring();
existing_subscription = true;
s = subscriptionHandlers.size(); // loop interruption
}
}
}
// System.out.println("*****************************************" +
// subscriptionHandlers.size());
if (!existing_subscription) {
subscriptionHandle = parameter.createSubscription(selector, this);
subscriptionHandlers.add(subscriptionHandle);
// System.out.println(subscriptionHandlers.size());
subscriptionHandle.startMonitoring();
LOGGER.info("Subscription for '" + propertyname + "' started");
}
} catch (final ParameterException e) {
LOGGER.error(e.getMessage());
}
}
@Override
public void valueReceived(final String parameterName, final AcquiredParameterValue value) {
final String propertyname = parameterName.substring(parameterName.indexOf("/") + 1);
final AcquiredParameterValue parameterValue_ = value;
final MapParameterValue mapParameterValue = (MapParameterValue) parameterValue_.getValue();
final String device_name = parameterName.substring(0, parameterName.indexOf("/"));
/*
* if (propertyname.equals("Acquisition") && parameterName.substring(0,
* parameterName.indexOf("/")).equals("GTS2QT12")) {
*
* System.out.println(); System.out.println(propertyname);
* System.out.println(parameterName.substring(0,
* parameterName.indexOf("/"))); // System.out.println(parameterValue_);
* System.out.println(mapParameterValue);
* System.out.println("Extracted current:" +
* mapParameterValue.getString("current"));
*
* }
*/
/*
* for (int i = 0; i < mapParameterValue.getNames().length; i++) {
*
* // LOGGER.info(i + ". Fieldname of " + propertyname + ": " +
* mapParameterValue.getNames()[i]); }
*/
if (propertyname.equals("Acquisition")) {
final double acquisitionCurrentValue = Double.parseDouble(mapParameterValue.getString("current"));
final double bFieldConverted = dccc.getValueFromCalibrationCurve("LOGICAL." + device_name,
acquisitionCurrentValue, "current");
// Attempt to retrieve voltage value for all the devices
double acquisitionVoltageValue;
double currentConverted_v = 0.0;
double bFieldConverted_v = 0.0;
try {
// Hall Probe value treatment
// 1) get the value in voltage from the Hall probe
// 2) convert this value into a current value
// 3) convert the current value into magnetic field value
acquisitionVoltageValue = Double.parseDouble(mapParameterValue.getString("voltage"));
// acquisitionVoltageValue = 0.0; change this value to test the
// voltage conversion
currentConverted_v = dccc.getValueFromCalibrationCurve("LOGICAL." + device_name,
acquisitionVoltageValue, "voltage");
bFieldConverted_v = dccc.getValueFromCalibrationCurve("LOGICAL." + device_name, currentConverted_v,
"current");
// bFieldConverted_v = acquisitionVoltageValue;
// bFieldConverted_v =
// dccc.getValueFromCalibrationCurve("LOGICAL." + device_name,
// acquisitionCurrentValue,
// "voltage"); // it looks like that the calibration function
// Bfield -> Voltage is missing.
} catch (final Exception e) {
acquisitionVoltageValue = 0.0;
bFieldConverted_v = acquisitionVoltageValue;
}
for (int k = 0; k < mdpj.getData().size(); k++) {
if (device_name.equals(mdpj.getData().get(k).getId())) {
mdpj.getData().get(k).setCurrent_meas(acquisitionCurrentValue);
// Update current difference for the device
mdpj.getData().get(k).setCurrent_diff(
-mdpj.getData().get(k).getCurrent_exp() + mdpj.getData().get(k).getCurrent_meas());
mdpj.getData().get(k).setBfield_meas(bFieldConverted);
// mdpj.getData().get(k).setBfield_meas(100.10);
mdpj.getData().get(k).setHallprobe(bFieldConverted_v);
k = mdpj.getData().size(); // exit for-loop
}
}
}
if (propertyname.equals("DCValueAcq")) {
final double acquisitionCurrentValueDC = Double.parseDouble(mapParameterValue.getString("current"));
/*
* // HallProbe will not be updated while DC mode is on because we
* have only one column // We can anyway see the changing in the
* magnet current (which is updated, see below)
*
* // final String device_name = parameterName.substring(0,
* parameterName.indexOf("/"));
*
* final String str = mapParameterValue.getString("current");
* currentValAcq = Double.parseDouble(str);
*
* // Treat the update of the HallProbes related only to dipoles if
* (device_name.equals("GTS3MU1") || device_name.equals("GTS3MU2")
* || device_name.equals("GTS4MU1") || device_name.equals("GHFSMU1")
* || device_name.equals("GTS5MU1") || device_name.equals("GTS6MU1")
* || device_name.equals("GTS7MU1")) {
*
* final String str_v = mapParameterValue.getString("voltage");
* voltageValAcq = Double.parseDouble(str_v);
*
* bFieldConverted_v = voltageValAcq;
*
* // System.out.println("HALLPROBE: " + bFieldConverted_v);
*
* } else {
*
* bFieldConverted_v = 0.0; }
*/
for (int k = 0; k < mdpj.getData().size(); k++) {
if (device_name.equals(mdpj.getData().get(k).getId())) {
mdpj.getData().get(k).setCurrent_meas_DC(acquisitionCurrentValueDC);
// mdpj.getData().get(k).setHallprobe(voltageValAcq);
// Add setting for DCVoltage
k = mdpj.getData().size(); // exit for-loop
}
}
}
if (propertyname.equals("DCMode")) {
// final String device_name = parameterName.substring(0,
// parameterName.indexOf("/"));
final String str = mapParameterValue.getString("mode");
for (int k = 0; k < mdpj.getData().size(); k++) {
if (device_name.equals(mdpj.getData().get(k).getId())) {
mdpj.getData().get(k).setDcmode(Boolean.valueOf(str));
k = mdpj.getData().size(); // exit for-loop
}
}
}
if (propertyname.equals("Status")) {
// final String device_name = parameterName.substring(0,
// parameterName.indexOf("/"));
final String str = mapParameterValue.getString("status");
final boolean[] arrayOfBooleans = mapParameterValue.getBooleans("detailedStatus");
listOfBooleans = toListBooleanArray(arrayOfBooleans);
final String[] arrayOfLabels = mapParameterValue.getStrings("detailedStatus_labels");
listOfLabels = toListStringArray(arrayOfLabels);
for (int k = 0; k < mdpj.getData().size(); k++) {
if (device_name.equals(mdpj.getData().get(k).getId())) {
mdpj.getData().get(k).setStatus(Integer.valueOf(str));
mdpj.getData().get(k).setListOfBooleans(listOfBooleans);
mdpj.getData().get(k).setListOfLabels(listOfLabels);
k = mdpj.getData().size(); // exit for-loop
}
}
}
// stopSubscription();
}
@Override
public void exceptionOccured(final String parameterName, final String description,
final ParameterException exception) {
String message;
if (exception == null) {
parameterException = new ParameterException(description);
message = parameterException.getMessage();
} else {
message = exception.getMessage();
}
LOGGER.error(message);
stopSubscription();
}
private void stopSubscription() {
subscriptionHandle.stopMonitoring();
LOGGER.info("subscription stopped");
}
/*
* close the logical channel of subscription
*
* @param conn logical channel of subscription
*/
public static void stopSubscriptions(final SubscriptionHandle subscriptionHandle) {
if (subscriptionHandle != null) {
// System.out.println("STOPPED!!!!!!!");
subscriptionHandle.stopMonitoring();
}
}
public List<SubscriptionHandle> get_subscriptionHandle() {
return subscriptionHandlers;
}
private List<Boolean> toListBooleanArray(final boolean[] booleanArray) {
final int arraySize = booleanArray.length;
for (int i = 0; i < arraySize; i++) {
listOfBooleans.add(booleanArray[i]);
}
return listOfBooleans;
}
private List<String> toListStringArray(final String[] stringArray) {
final int arraySize = stringArray.length;
for (int i = 0; i < arraySize; i++) {
listOfLabels.add(stringArray[i]);
}
return listOfLabels;
}
private void setDeviceName(final String deviceName) {
devicename = deviceName;
}
public void generateDeviceSubscriptions() {
for (final Device device : myDevices) {
// System.out.println("***SUBSTRIPTION GENERATION***");
// System.out.println(device.getName().substring(8));
setDeviceName(device.getName().substring(8));
final Selector newMULTIPLEXED_SELECTOR = rdSel.getSelector(device);
for (int p = 0; p < propertyName.length; p++) {
try {
subscribe(newMULTIPLEXED_SELECTOR, propertyName[p]);
} catch (final Exception e) {
System.out.println("Exception: " + e);
System.exit(0);
}
} // for property
}
}
public void closeAllSubscriptions() {
int sum = 0;
int size = 0;
if (get_subscriptionHandle().size() != 0) {
size = get_subscriptionHandle().size();
for (int i = 0; i < get_subscriptionHandle().size(); i++) {
if (get_subscriptionHandle().get(i).isMonitoring()) {
sum++;
}
System.out.println(i);
// System.out.println(duf.get_subscriptionHandle().get(i).toString());
System.out.println(get_subscriptionHandle().get(i).getSelector());
System.out.println(get_subscriptionHandle().get(i).getParameter());
stopSubscriptions(get_subscriptionHandle().get(i));
System.out.println();
}
}
System.out.println("SUM: " + sum);
System.out.println("SIZE: " + size);
// DeviceUpdateFESA.stopSubscriptions(duf.get_subscriptionHandle().get(5));
// System.out.println(duf.get_subscriptionHandle().size());
}
}