Search in sources :

Example 1 with Scalar

use of org.eclipse.scanning.api.points.Scalar in project gda-core by openGDA.

the class ScannableNexusWrapperTest method testSetPosition_withScanPosition.

@Test
public void testSetPosition_withScanPosition() throws Exception {
    // Note: nexus writing tested separately by ScannableNexusWrapperScanTest
    // Arrange
    final double newPosition = 3.8;
    final int scanIndex = 38;
    IPositionListener posListener = mock(IPositionListener.class);
    ((IPositionListenable) scannable).addPositionListener(posListener);
    final IPosition scanPosition = new Scalar<Double>("sax", scanIndex, newPosition);
    assertThat(scannable.getPosition(), is(equalTo(3.7)));
    // Act
    scannable.setPosition(newPosition, scanPosition);
    // Assert
    assertThat(scannable.getPosition(), is(equalTo(newPosition)));
    ((IPositionListenable) scannable).removePositionListener(posListener);
    ArgumentCaptor<PositionEvent> captor = ArgumentCaptor.forClass(PositionEvent.class);
    InOrder order = inOrder(posListener);
    order.verify(posListener).positionWillPerform(captor.capture());
    order.verify(posListener).positionChanged(captor.capture());
    order.verify(posListener).positionPerformed(captor.capture());
    order.verifyNoMoreInteractions();
    List<PositionEvent> posEvents = captor.getAllValues();
    assertThat(posEvents, hasSize(3));
    int i = 0;
    for (PositionEvent posEvent : posEvents) {
        // willPerform event doesn't have level set
        assertThat(posEvent.getLevel(), is(i == 0 ? 0 : 5));
        IPosition position = posEvent.getPosition();
        assertThat(position.getNames(), contains(scannable.getName()));
        // index not set for positionChanged
        assertThat(position.getIndex("sax"), is(i == 1 ? -1 : 38));
        assertThat(position.getValue(scannable.getName()), is(equalTo(Double.valueOf(newPosition))));
        i++;
    }
}
Also used : IPosition(org.eclipse.scanning.api.points.IPosition) InOrder(org.mockito.InOrder) IPositionListenable(org.eclipse.scanning.api.scan.event.IPositionListenable) IPositionListener(org.eclipse.scanning.api.scan.event.IPositionListener) Scalar(org.eclipse.scanning.api.points.Scalar) PositionEvent(org.eclipse.scanning.api.scan.PositionEvent) Test(org.junit.Test)

Example 2 with Scalar

use of org.eclipse.scanning.api.points.Scalar in project gda-core by openGDA.

the class ScannableNexusWrapper method setPosition.

@Override
public Object setPosition(Object value, IPosition scanPosition) throws ScanningException {
    try {
        final Scannable scannable = getScannable();
        if (value != null) {
            final int index = (scanPosition == null ? -1 : scanPosition.getIndex(getName()));
            final IPosition position = new Scalar<Object>(getName(), index, value);
            positionDelegate.firePositionWillPerform(position);
            logger.debug("Moving scannable {} to position {} at {}", scannable.getName(), value, scanPosition);
            scannable.moveTo(value);
            positionDelegate.firePositionPerformed(getLevel(), position);
        } else {
            logger.debug("Ignoring request to move scannable {} to position {} at {}", scannable.getName(), value, scanPosition);
        }
        if (scanPosition != null && getScannableNexusDevice(false) != null) {
            // It stops it being read again.
            return getScannableNexusDevice(false).writePosition(value, scanPosition);
        }
    } catch (Exception e) {
        throw new ScanningException("Could not set position of scannable " + getName(), e);
    }
    // new position.
    return null;
}
Also used : IPosition(org.eclipse.scanning.api.points.IPosition) ScanningException(org.eclipse.scanning.api.scan.ScanningException) IScannable(org.eclipse.scanning.api.IScannable) AbstractScannable(org.eclipse.scanning.api.AbstractScannable) Scannable(gda.device.Scannable) DeviceException(gda.device.DeviceException) NexusException(org.eclipse.dawnsci.nexus.NexusException) ScanningException(org.eclipse.scanning.api.scan.ScanningException) Scalar(org.eclipse.scanning.api.points.Scalar)

Example 3 with Scalar

use of org.eclipse.scanning.api.points.Scalar in project gda-core by openGDA.

the class ScannableNexusWrapperTest method testNullSetPosition_withScanPosition.

@Test
public void testNullSetPosition_withScanPosition() throws Exception {
    // Note: nexus writing tested separately by ScannableNexusWrapperScanTest
    // Arrange
    final int scanIndex = 38;
    IPositionListener posListener = mock(IPositionListener.class);
    ((IPositionListenable) scannable).addPositionListener(posListener);
    final IPosition scanPosition = new Scalar<Double>("sax", scanIndex, null);
    assertThat(scannable.getPosition(), is(equalTo(3.7)));
    // Act
    scannable.setPosition(null, scanPosition);
    // Assert
    assertThat(scannable.getPosition(), is(equalTo(3.7)));
    ((IPositionListenable) scannable).removePositionListener(posListener);
    verifyZeroInteractions(posListener);
}
Also used : IPosition(org.eclipse.scanning.api.points.IPosition) IPositionListenable(org.eclipse.scanning.api.scan.event.IPositionListenable) IPositionListener(org.eclipse.scanning.api.scan.event.IPositionListener) Scalar(org.eclipse.scanning.api.points.Scalar) Test(org.junit.Test)

Aggregations

IPosition (org.eclipse.scanning.api.points.IPosition)3 Scalar (org.eclipse.scanning.api.points.Scalar)3 IPositionListenable (org.eclipse.scanning.api.scan.event.IPositionListenable)2 IPositionListener (org.eclipse.scanning.api.scan.event.IPositionListener)2 Test (org.junit.Test)2 DeviceException (gda.device.DeviceException)1 Scannable (gda.device.Scannable)1 NexusException (org.eclipse.dawnsci.nexus.NexusException)1 AbstractScannable (org.eclipse.scanning.api.AbstractScannable)1 IScannable (org.eclipse.scanning.api.IScannable)1 PositionEvent (org.eclipse.scanning.api.scan.PositionEvent)1 ScanningException (org.eclipse.scanning.api.scan.ScanningException)1 InOrder (org.mockito.InOrder)1