/
Siam.java
382 lines (323 loc) · 11.9 KB
/
Siam.java
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
package siam;
import java.io.IOException;
import java.io.PrintWriter;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.server.RMISocketFactory;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import java.util.Map.Entry;
import org.mbari.siam.distributed.Device;
import org.mbari.siam.distributed.DeviceNotFound;
import org.mbari.siam.distributed.Instrument;
import org.mbari.siam.distributed.NoDataException;
import org.mbari.siam.distributed.Node;
import org.mbari.siam.distributed.NodeInfo;
import org.mbari.siam.distributed.PacketParser;
import org.mbari.siam.distributed.PortNotFound;
import org.mbari.siam.distributed.SensorDataPacket;
import org.mbari.siam.distributed.PacketParser.Field;
import org.mbari.siam.utils.PrintUtils;
import org.mbari.siam.utils.SiamSocketFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import siam.InstrumentSample.SampleDatum;
/**
* Implementation of the high-level API to access SIAM functionality.
*
* <p>
* See <a
* href="http://oidemo.mbari.org:1451/siam-site/content/utilityReference.html"
* >the SIAM utility reference</a> for a general description of the operations.
*
* <p>
* Note: this implementation is very preliminary and can certainly be improved;
* it is just a quick basis for the prototype.
*
* @author carueda
*/
public class Siam implements ISiam {
private static Logger log = LoggerFactory.getLogger(Siam.class);
// Copied verbatim from NodeUtility
private static final String getNodeURL(String input) {
if (log.isDebugEnabled()) {
log.debug("getNodeURL(): input=" + input);
}
if (input.startsWith("rmi://")) {
return input;
}
else if (input.startsWith("//")) {
return "rmi:" + input;
}
else {
return "rmi://" + input + "/node";
}
}
private static Node _getNode(String host) throws NotBoundException, IOException {
String nodeURL = getNodeURL(host);
RMISocketFactory.setSocketFactory(new SiamSocketFactory(host));
log.info("Looking up '" + nodeURL + "' to connect with SIAM node ...");
Node node = (Node) Naming.lookup(nodeURL.toString());
log.info("Connected to SIAM node. (" +node.getClass().getName()+ ")");
return node;
}
/**
* Logs a simplified version of the throwable and its chain of causes
* without the full stacktrace.
*/
private static void _logError(Throwable t) {
String prefix = "";
StringBuilder sb = new StringBuilder();
while (t != null) {
sb.append(prefix + t.getClass().getName() + ": " + t.getMessage()
+ "\n");
t = t.getCause();
prefix = "caused by: ";
}
log.error(sb.toString());
}
// /////////
// Instance.
// /////////
private final String host;
// assigned by start()
private Node node;
private long nodeId;
private NodeInfo nodeInfo;
/**
* Creates a point of access to a SIAM node instance. To actually enable the
* connection, call {@link #start()}.
*
* @param host
*/
public Siam(String host) {
this.host = host;
}
private void _checkConnection() {
if (node == null) {
throw new RuntimeException("start() must be called first");
}
}
public void start() throws Exception {
if (node != null) {
throw new RuntimeException("already connected");
}
try {
node = _getNode(host);
nodeId = node.getId();
nodeInfo = node.getNodeInfo();
}
catch (Exception e) {
_logError(e);
throw e;
}
}
public long getNodeId() {
_checkConnection();
return nodeId;
}
public String getNodeInfo() {
_checkConnection();
return nodeInfo.toString();
}
public List<PortItem> listPorts() throws Exception {
_checkConnection();
return new PortLister(node).listPorts();
}
/**
* helper
*/
private Instrument _getInstrument(String portName) throws Exception {
try {
Device device = node.getDevice(portName.getBytes());
if (device instanceof Instrument) {
Instrument instrument = (Instrument) device;
return instrument;
}
else {
throw new Exception("Device on port " + portName
+ " is not an Instrument");
}
}
catch (PortNotFound e) {
throw new Exception("Port " + portName + " not found");
}
catch (DeviceNotFound e) {
throw new Exception("Device not found on port " + portName);
}
catch (NoDataException e) {
throw new Exception("No data from instrument on port " + portName);
}
}
public String getPortStatus(String port) throws Exception {
_checkConnection();
Instrument instrument = _getInstrument(port);
return SiamUtils.statusMnem(instrument.getStatus());
}
/**
* Adapted from SIAM's GetLastSample
*/
public InstrumentSample getPortLastSample(String portName) throws Exception {
_checkConnection();
Instrument instrument = _getInstrument(portName);
SensorDataPacket sdp = instrument.getLastSample();
// get metadata
HashMap<String, String> md = new HashMap<String, String>();
md.put("parentId", String.valueOf(sdp.getParentId()));
md.put("recordType", String.valueOf(sdp.getRecordType()));
md.put("systemTime", String.valueOf(sdp.systemTime()));
md.put("seqNo", String.valueOf(sdp.sequenceNo()));
md.put("mdref", String.valueOf(sdp.metadataRef()));
md.put("nBytes", String.valueOf(sdp.dataBuffer().length));
md.put("buffer", PrintUtils.getAscii(sdp.dataBuffer(), 0, 0));
InstrumentSample sample = new InstrumentSample(md);
// get data
SensorDataPacket packet = instrument.getLastSample();
PacketParser parser = instrument.getParser();
PacketParser.Field[] fields = parser.parseFields(packet);
for (int j = 0; j < fields.length; j++) {
if (fields[j] == null) {
continue;
}
String name = fields[j].getName();
Object value = fields[j].getValue();
String units = fields[j].getUnits();
SampleDatum datum = new SampleDatum(name, value, units);
sample.addDatum(datum);
}
return sample;
}
/**
* @return the names of the channels for the given instrument.
*/
public List<String> getPortChannels(String portName) throws Exception {
_checkConnection();
/*
* Strategy: get last sample, parse that sample, and get the fields from
* the parser.
*/
Instrument instrument = _getInstrument(portName);
SensorDataPacket sdp = instrument.getLastSample();
Field[] fields = instrument.getParser().parseFields(sdp);
List<String> result = new ArrayList<String>();
for (Field field : fields) {
String name = field.getName();
result.add(name);
}
return result;
}
/**
* Adapted from SIAM's PrintInstrumentProperties
*/
public Map<String, String> getPortProperties(String portName)
throws Exception {
_checkConnection();
Instrument instrument = _getInstrument(portName);
HashMap<String, String> result = new LinkedHashMap<String, String>();
@SuppressWarnings("unchecked")
Vector<byte[]> properties = instrument.getProperties();
for (int j = 0; j < properties.size(); j++) {
byte[] property = properties.elementAt(j);
String entry = new String(property);
String[] toks = entry.split("=", 2);
String key = toks[0].trim();
String value = toks.length == 2 ? toks[1].trim() : "??";
result.put(key, value);
}
return result;
}
public String getTurbineName(String portName, String channelName) throws Exception {
Map<String, String> props = getPortProperties(portName);
String serviceName = props.get("serviceName");
String isiID = props.get("isiID");
if (serviceName == null || serviceName.trim().length() == 0) {
throw new Exception("'serviceName' property no associated with instrument");
}
if (isiID == null || isiID.trim().length() == 0) {
throw new Exception("'isiID' property no associated with instrument");
}
serviceName = serviceName.trim();
isiID = isiID.trim();
String turbineName = serviceName.replace(' ', '_') + "-" + isiID + "/"
+ channelName;
return turbineName;
}
/**
* Adapted from SIAM's SetInstrumentProperty
*/
public Map<String, String> setPortProperties(String portName,
Map<String, String> params) throws Exception {
_checkConnection();
Instrument instrument = _getInstrument(portName);
StringBuilder sb = new StringBuilder();
for (Entry<String, String> e : params.entrySet()) {
sb.append(e.getKey() + "=" + e.getValue() + "\n");
}
String string = sb.toString();
if (log.isDebugEnabled()) {
log.debug("properties string: " + string);
}
HashMap<String, String> result = new LinkedHashMap<String, String>();
// TODO: How to (easily) discriminate which properties are actually set
// and which are not?
//
instrument.setProperty(string.getBytes(), new byte[0]);
// everything OK. For each param, indicate "OK"
for (Entry<String, String> e : params.entrySet()) {
result.put(e.getKey(), "OK");
}
if (log.isDebugEnabled()) {
log.debug("Everything OK: " + result);
}
return result;
}
/**
* test program:
*
* @param args
* [host [port]]
*/
public static void main(String[] args) throws Exception {
final String host = args.length >= 1 ? args[0] : "localhost";
final String port = args.length >= 2 ? args[1] : "testPort";
Siam siam = new Siam(host);
siam.start();
PrintWriter out = new PrintWriter(System.out, true);
// /////////////////////////////////////////////////////////
// listPorts:
out.println("**listPorts:");
List<PortItem> list = siam.listPorts();
PortLister.list(siam.getNodeId(), list, out);
out.println();
// /////////////////////////////////////////////////////////
// getPortStatus:
out.println("**getPortStatus: port=" + port);
String status = siam.getPortStatus(port);
out.println(" status: " + status);
out.println();
// /////////////////////////////////////////////////////////
// getPortLastSample:
out.println("**getPortLastSample: port=" + port);
InstrumentSample sample = siam.getPortLastSample(port);
Map<String, String> md = sample.getMd();
out.println(" result metadata: " + md);
out.println(" result data: ");
for (SampleDatum datum : sample.getData()) {
out.printf("'%s' = %s (%s)%n",
datum.getName(),
datum.getValue(),
datum.getUnits());
}
out.println();
// /////////////////////////////////////////////////////////
// getPortLastSample:
out.println("**getPortProperties: port=" + port);
Map<String, String> portProps = siam.getPortProperties(port);
out.println(" result: " + portProps);
out.println();
}
}