Skip to content

Commit

Permalink
[DAQ-857] make _RunnableDevice implement IAttributableDevice
Browse files Browse the repository at this point in the history
  • Loading branch information
mpdickie committed Sep 6, 2017
1 parent e52e0f3 commit 9533e8d
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@
package org.eclipse.scanning.event.remote;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.eclipse.scanning.api.ValidationException;
import org.eclipse.scanning.api.annotation.ui.DeviceType;
import org.eclipse.scanning.api.device.IActivatable;
import org.eclipse.scanning.api.device.IAttributableDevice;
import org.eclipse.scanning.api.device.IRunnableDevice;
import org.eclipse.scanning.api.device.models.DeviceRole;
import org.eclipse.scanning.api.device.models.ScanMode;
Expand All @@ -26,19 +29,20 @@
import org.eclipse.scanning.api.event.scan.DeviceInformation;
import org.eclipse.scanning.api.event.scan.DeviceRequest;
import org.eclipse.scanning.api.event.scan.DeviceState;
import org.eclipse.scanning.api.malcolm.attributes.IDeviceAttribute;
import org.eclipse.scanning.api.points.IPosition;
import org.eclipse.scanning.api.scan.ScanningException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

class _RunnableDevice<M> extends _AbstractRemoteDevice<M> implements IRunnableDevice<M>, IActivatable {
class _RunnableDevice<M> extends _AbstractRemoteDevice<M> implements IRunnableDevice<M>, IActivatable, IAttributableDevice {

private static final Logger logger = LoggerFactory.getLogger(_RunnableDevice.class);

_RunnableDevice(DeviceRequest req, URI uri, IEventService eservice) throws EventException, InterruptedException {
super(req,
super(req,
Long.getLong("org.eclipse.scanning.event.remote.runnableDeviceTimeout", 1000),
uri,
uri,
eservice);
}

Expand Down Expand Up @@ -184,4 +188,38 @@ public boolean isAlive() {
public void setAlive(boolean alive) {
info.setAlive(alive);
}

@SuppressWarnings("unchecked")
@Override
public <T> IDeviceAttribute<T> getAttribute(String attributeName) throws ScanningException {
try {
DeviceRequest req = new DeviceRequest(name);
req.setAttributeName(attributeName);
DeviceRequest res = requester.post(req);
merge((DeviceInformation<M>)req.getDeviceInformation());
return (IDeviceAttribute<T>) res.getAttributes().get(attributeName);
} catch (Exception e) {
throw new ScanningException(e);
}
}

@SuppressWarnings("unchecked")
@Override
public List<IDeviceAttribute<?>> getAllAttributes() throws ScanningException {
try {
DeviceRequest req = new DeviceRequest(name);
req.setGetAllAttributes(true);
DeviceRequest res = requester.post(req);
merge((DeviceInformation<M>) req.getDeviceInformation());
return new ArrayList<>(res.getAttributes().values());
} catch (Exception e) {
throw new ScanningException(e);
}
}

@SuppressWarnings("unchecked")
@Override
public <T> T getAttributeValue(String attributeName) throws ScanningException {
return (T) getAttribute(attributeName).getValue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,9 @@ public <T> IDeviceAttribute<T> getAttribute(String attributeName) throws Scannin

@SuppressWarnings("unchecked")
IDeviceAttribute<T> attribute = (IDeviceAttribute<T>) allAttributes.get(attributeName);
if (attribute == null) {
throw new MalcolmDeviceException("No such attribute: " + attributeName);
}
return attribute;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,4 +332,15 @@ public void testGetAllAttributes() throws Exception {
assertEquals(0, totalSteps.getValue());
}

@Test
public void testGetUnknownAttribute() throws Exception {
DeviceRequest req = new DeviceRequest();
req.setDeviceName("malcolm");
req.setAttributeName("unknown");
DeviceRequest res = requester.post(req);
assertNotNull(res);
assertTrue(res.getAttributes() == null || res.getAttributes().isEmpty());
assertEquals("No such attribute: unknown", res.getErrorMessage());
}

}

0 comments on commit 9533e8d

Please sign in to comment.