use of org.openhab.core.io.transport.modbus.ModbusCommunicationInterface in project openhab-addons by openhab.
the class StuderHandler method registerPollTask.
/**
* Register poll task
* This is where we set up our regular poller
*/
private synchronized void registerPollTask(int registerNumber) {
if (pollTasks.size() >= registers.length) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR);
throw new IllegalStateException("New pollTask invalid");
}
ModbusCommunicationInterface mycomms = comms;
StuderConfiguration studerConfig = config;
if (studerConfig == null || mycomms == null) {
throw new IllegalStateException("registerPollTask called without proper configuration");
}
logger.debug("Setting up regular polling");
ModbusReadRequestBlueprint request = new ModbusReadRequestBlueprint(studerConfig.slaveAddress, ModbusReadFunctionCode.READ_INPUT_REGISTERS, registerNumber, 2, studerConfig.maxTries);
long refreshMillis = studerConfig.refresh * 1000;
PollTask pollTask = mycomms.registerRegularPoll(request, refreshMillis, 1000, result -> {
if (result.getRegisters().isPresent()) {
ModbusRegisterArray reg = result.getRegisters().get();
handlePolledData(registerNumber, reg);
} else {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
return;
}
if (getThing().getStatus() != ThingStatus.ONLINE) {
updateStatus(ThingStatus.ONLINE);
}
}, this::handleError);
pollTasks.add(pollTask);
}
use of org.openhab.core.io.transport.modbus.ModbusCommunicationInterface in project openhab-addons by openhab.
the class StuderHandler method unregisterPollTasks.
private synchronized void unregisterPollTasks() {
if (pollTasks.isEmpty()) {
return;
}
logger.debug("Unregistering polling from ModbusManager");
ModbusCommunicationInterface mycomms = comms;
if (mycomms != null) {
for (PollTask t : pollTasks) {
mycomms.unregisterRegularPoll(t);
}
pollTasks.clear();
}
}
use of org.openhab.core.io.transport.modbus.ModbusCommunicationInterface in project openhab-addons by openhab.
the class E3DCThingHandler method dispose.
@Override
public void dispose() {
ModbusCommunicationInterface localComms = comms;
if (localComms != null) {
PollTask localInfoPoller = infoPoller;
if (localInfoPoller != null) {
localComms.unregisterRegularPoll(localInfoPoller);
}
PollTask localDataPoller = dataPoller;
if (localDataPoller != null) {
localComms.unregisterRegularPoll(localDataPoller);
}
}
// Comms will be close()'d by endpoint thing handler
comms = null;
}
use of org.openhab.core.io.transport.modbus.ModbusCommunicationInterface in project openhab-addons by openhab.
the class E3DCThingHandler method connectEndpoint.
/**
* Get a reference to the modbus endpoint
*/
@Nullable
private ModbusCommunicationInterface connectEndpoint() {
if (comms != null) {
return comms;
}
ModbusEndpointThingHandler slaveEndpointThingHandler = getEndpointThingHandler();
if (slaveEndpointThingHandler == null) {
@SuppressWarnings("null") String label = Optional.ofNullable(getBridge()).map(b -> b.getLabel()).orElse("<null>");
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE, String.format("Bridge '%s' is offline", label));
return null;
}
try {
slaveId = slaveEndpointThingHandler.getSlaveId();
comms = slaveEndpointThingHandler.getCommunicationInterface();
} catch (EndpointNotInitializedException e) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, String.format("Slave Endpoint not initialized"));
return null;
}
if (comms == null) {
@SuppressWarnings("null") String label = Optional.ofNullable(getBridge()).map(b -> b.getLabel()).orElse("<null>");
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE, String.format("Bridge '%s' not completely initialized", label));
return null;
} else {
return comms;
}
}
use of org.openhab.core.io.transport.modbus.ModbusCommunicationInterface in project openhab-addons by openhab.
the class StiebelEltronHandler method writeInt16.
/**
* @param address address of the value to be written on the modbus
* @param shortValue value to be written on the modbus
*/
protected void writeInt16(int address, short shortValue) {
StiebelEltronConfiguration myconfig = StiebelEltronHandler.this.config;
ModbusCommunicationInterface mycomms = StiebelEltronHandler.this.comms;
if (myconfig == null || mycomms == null) {
throw new IllegalStateException("registerPollTask called without proper configuration");
}
// big endian byte ordering
byte hi = (byte) (shortValue >> 8);
byte lo = (byte) shortValue;
ModbusRegisterArray data = new ModbusRegisterArray(hi, lo);
ModbusWriteRegisterRequestBlueprint request = new ModbusWriteRegisterRequestBlueprint(slaveId, address, data, false, myconfig.getMaxTries());
mycomms.submitOneTimeWrite(request, result -> {
if (hasConfigurationError()) {
return;
}
logger.debug("Successful write, matching request {}", request);
StiebelEltronHandler.this.updateStatus(ThingStatus.ONLINE);
}, failure -> {
StiebelEltronHandler.this.handleWriteError(failure);
});
}
Aggregations