use of org.openhab.binding.modbus.internal.handler.ModbusDataThingHandler in project openhab-addons by openhab.
the class ModbusDataHandlerTest method testWriteRealTransformation5.
@Test
public void testWriteRealTransformation5() throws InvalidSyntaxException {
captureModbusWrites();
mockTransformation("PLUS", new TransformationService() {
@Override
public String transform(String arg, String source) throws TransformationException {
return String.valueOf(Integer.parseInt(arg) + Integer.parseInt(source));
}
});
mockTransformation("CONCAT", new TransformationService() {
@Override
public String transform(String function, String source) throws TransformationException {
return source + function;
}
});
mockTransformation("MULTIPLY", new MultiplyTransformation());
ModbusDataThingHandler dataHandler = testWriteHandlingGeneric("50", "MULTIPLY:3∩PLUS(2)∩CONCAT(0)", ModbusConstants.ValueType.INT16, "holding", ModbusWriteFunctionCode.WRITE_SINGLE_REGISTER, "number", new DecimalType("2"), null, bundleContext);
assertSingleStateUpdate(dataHandler, CHANNEL_LAST_WRITE_SUCCESS, is(notNullValue(State.class)));
assertSingleStateUpdate(dataHandler, CHANNEL_LAST_WRITE_ERROR, is(nullValue(State.class)));
assertThat(writeRequests.size(), is(equalTo(1)));
ModbusWriteRequestBlueprint writeRequest = writeRequests.get(0);
assertThat(writeRequest.getFunctionCode(), is(equalTo(ModbusWriteFunctionCode.WRITE_SINGLE_REGISTER)));
assertThat(writeRequest.getReference(), is(equalTo(50)));
assertThat(((ModbusWriteRegisterRequestBlueprint) writeRequest).getRegisters().size(), is(equalTo(1)));
assertThat(((ModbusWriteRegisterRequestBlueprint) writeRequest).getRegisters().getRegister(0), is(equalTo(/* (2*3 + 2) + '0' */
80)));
}
use of org.openhab.binding.modbus.internal.handler.ModbusDataThingHandler in project openhab-addons by openhab.
the class ModbusDataHandlerTest method testWriteRealTransformation3.
@Test
public void testWriteRealTransformation3() throws InvalidSyntaxException {
captureModbusWrites();
mockTransformation("RANDOM", new TransformationService() {
@Override
public String transform(String function, String source) throws TransformationException {
return "5";
}
});
ModbusDataThingHandler dataHandler = testWriteHandlingGeneric("50", "RANDOM(foobar)", ModbusConstants.ValueType.INT16, "holding", ModbusWriteFunctionCode.WRITE_SINGLE_REGISTER, "number", new DecimalType("2"), null, bundleContext);
assertSingleStateUpdate(dataHandler, CHANNEL_LAST_WRITE_SUCCESS, is(notNullValue(State.class)));
assertSingleStateUpdate(dataHandler, CHANNEL_LAST_WRITE_ERROR, is(nullValue(State.class)));
assertThat(writeRequests.size(), is(equalTo(1)));
ModbusWriteRequestBlueprint writeRequest = writeRequests.get(0);
assertThat(writeRequest.getFunctionCode(), is(equalTo(ModbusWriteFunctionCode.WRITE_SINGLE_REGISTER)));
assertThat(writeRequest.getReference(), is(equalTo(50)));
assertThat(((ModbusWriteRegisterRequestBlueprint) writeRequest).getRegisters().size(), is(equalTo(1)));
assertThat(((ModbusWriteRegisterRequestBlueprint) writeRequest).getRegisters().getRegister(0), is(equalTo(5)));
}
use of org.openhab.binding.modbus.internal.handler.ModbusDataThingHandler in project openhab-addons by openhab.
the class ModbusDataHandlerTest method testOnRegistersNaNFloatInRegisters.
@Test
public void testOnRegistersNaNFloatInRegisters() throws InvalidSyntaxException {
ModbusDataThingHandler dataHandler = testReadHandlingGeneric(ModbusReadFunctionCode.READ_MULTIPLE_REGISTERS, "0", "default", ModbusConstants.ValueType.FLOAT32, null, new ModbusRegisterArray(// equivalent of floating point NaN
new byte[] { (byte) 0x7f, (byte) 0xc0, (byte) 0x00, (byte) 0x00 }), null, bundleContext);
assertSingleStateUpdate(dataHandler, CHANNEL_LAST_READ_SUCCESS, is(notNullValue(State.class)));
assertSingleStateUpdate(dataHandler, CHANNEL_LAST_READ_ERROR, is(nullValue(State.class)));
// UNDEF is treated as "boolean true" (OPEN/ON) since it is != 0.
assertSingleStateUpdate(dataHandler, CHANNEL_CONTACT, OpenClosedType.OPEN);
assertSingleStateUpdate(dataHandler, CHANNEL_SWITCH, OnOffType.ON);
assertSingleStateUpdate(dataHandler, CHANNEL_DIMMER, OnOffType.ON);
assertSingleStateUpdate(dataHandler, CHANNEL_NUMBER, UnDefType.UNDEF);
assertSingleStateUpdate(dataHandler, CHANNEL_ROLLERSHUTTER, UnDefType.UNDEF);
assertSingleStateUpdate(dataHandler, CHANNEL_STRING, UnDefType.UNDEF);
}
use of org.openhab.binding.modbus.internal.handler.ModbusDataThingHandler in project openhab-addons by openhab.
the class ModbusPollerThingHandlerTest method testRegistersPassedToChildDataThings.
@Test
public void testRegistersPassedToChildDataThings() throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException {
PollTask pollTask = Mockito.mock(PollTask.class);
doReturn(pollTask).when(comms).registerRegularPoll(notNull(), eq(150l), eq(0L), notNull(), notNull());
Configuration pollerConfig = new Configuration();
pollerConfig.put("refresh", 150L);
pollerConfig.put("start", 5);
pollerConfig.put("length", 13);
pollerConfig.put("type", "coil");
poller = createPollerThingBuilder("poller").withConfiguration(pollerConfig).withBridge(endpoint.getUID()).build();
addThing(poller);
verifyEndpointBasicInitInteraction();
assertThat(poller.getStatus(), is(equalTo(ThingStatus.ONLINE)));
ArgumentCaptor<ModbusReadCallback> callbackCapturer = ArgumentCaptor.forClass(ModbusReadCallback.class);
verify(comms).registerRegularPoll(notNull(), eq(150l), eq(0L), callbackCapturer.capture(), notNull());
ModbusReadCallback readCallback = callbackCapturer.getValue();
assertNotNull(readCallback);
ModbusReadRequestBlueprint request = Mockito.mock(ModbusReadRequestBlueprint.class);
ModbusRegisterArray registers = Mockito.mock(ModbusRegisterArray.class);
ModbusPollerThingHandler thingHandler = (ModbusPollerThingHandler) poller.getHandler();
assertNotNull(thingHandler);
ModbusDataThingHandler child1 = Mockito.mock(ModbusDataThingHandler.class);
ModbusDataThingHandler child2 = Mockito.mock(ModbusDataThingHandler.class);
AsyncModbusReadResult result = new AsyncModbusReadResult(request, registers);
// has one data child
thingHandler.childHandlerInitialized(child1, Mockito.mock(Thing.class));
readCallback.handle(result);
verify(child1).onReadResult(result);
verifyNoMoreInteractions(child1);
verifyNoMoreInteractions(child2);
reset(child1);
// two children (one child initialized)
thingHandler.childHandlerInitialized(child2, Mockito.mock(Thing.class));
readCallback.handle(result);
verify(child1).onReadResult(result);
verify(child2).onReadResult(result);
verifyNoMoreInteractions(child1);
verifyNoMoreInteractions(child2);
reset(child1);
reset(child2);
// one child disposed
thingHandler.childHandlerDisposed(child1, Mockito.mock(Thing.class));
readCallback.handle(result);
verify(child2).onReadResult(result);
verifyNoMoreInteractions(child1);
verifyNoMoreInteractions(child2);
}
use of org.openhab.binding.modbus.internal.handler.ModbusDataThingHandler in project openhab-addons by openhab.
the class ModbusPollerThingHandlerTest method testRefreshWithPreviousData.
/**
* When there's no recently received data, refresh() will re-use that instead
*
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws NoSuchFieldException
* @throws SecurityException
*/
@Test
public void testRefreshWithPreviousData() throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException {
Configuration pollerConfig = new Configuration();
pollerConfig.put("refresh", 0L);
pollerConfig.put("start", 5);
pollerConfig.put("length", 13);
pollerConfig.put("type", "coil");
pollerConfig.put("cacheMillis", 10000L);
poller = createPollerThingBuilder("poller").withConfiguration(pollerConfig).withBridge(endpoint.getUID()).build();
addThing(poller);
verifyEndpointBasicInitInteraction();
ModbusDataThingHandler child1 = Mockito.mock(ModbusDataThingHandler.class);
ModbusPollerThingHandler thingHandler = (ModbusPollerThingHandler) poller.getHandler();
assertNotNull(thingHandler);
thingHandler.childHandlerInitialized(child1, Mockito.mock(Thing.class));
assertThat(poller.getStatus(), is(equalTo(ThingStatus.ONLINE)));
verify(comms, never()).submitOneTimePoll(any(), any(), any());
// data is received
ModbusReadCallback pollerReadCallback = getPollerCallback(thingHandler);
ModbusReadRequestBlueprint request = Mockito.mock(ModbusReadRequestBlueprint.class);
ModbusRegisterArray registers = Mockito.mock(ModbusRegisterArray.class);
AsyncModbusReadResult result = new AsyncModbusReadResult(request, registers);
pollerReadCallback.handle(result);
// data child receives the data
verify(child1).onReadResult(result);
verifyNoMoreInteractions(child1);
reset(child1);
// call refresh
// cache is still valid, we should not have real data poll this time
thingHandler.refresh();
verify(comms, never()).submitOneTimePoll(any(), any(), any());
// data child receives the cached data
verify(child1).onReadResult(result);
verifyNoMoreInteractions(child1);
}
Aggregations