use of org.openmuc.framework.driver.spi.ChannelRecordContainer in project OpenMUC by isc-konstanz.
the class DataManager method handleInterruptEvent.
private void handleInterruptEvent() {
if (stopFlag) {
prepareStop();
return;
}
long currentTime = 0;
if (newRootConfigWithoutDefaults != null) {
currentTime = System.currentTimeMillis();
applyConfiguration(newRootConfigWithoutDefaults, currentTime);
newRootConfigWithoutDefaults = null;
newConfigSignal.countDown();
}
synchronized (receivedRecordContainers) {
List<ChannelRecordContainer> recordContainers;
LoggingController loggingController = new LoggingController(activeDataLoggers);
List<ChannelRecordContainerImpl> channelRecordContainerList = new ArrayList<>();
while ((recordContainers = receivedRecordContainers.poll()) != null) {
recordContainers.stream().map(recContainer -> (ChannelRecordContainerImpl) recContainer).filter(containerImpl -> containerImpl.getChannel().getChannelState() == ChannelState.LISTENING || containerImpl.getChannel().getDriverId().equals(DriverOptionsFactory.VIRTUAL)).forEach(containerImpl -> {
containerImpl.getChannel().setNewRecord(containerImpl.getRecord());
if (containerImpl.getChannel().isLoggingEvent())
channelRecordContainerList.add(containerImpl);
});
}
loggingController.deliverLogsToEventBasedLogServices(channelRecordContainerList);
}
synchronized (samplingTaskFinished) {
SamplingTask samplingTask;
while ((samplingTask = samplingTaskFinished.poll()) != null) {
samplingTask.storeValues();
samplingTask.device.taskFinished();
}
}
synchronized (tasksFinished) {
DeviceTask deviceTask;
while ((deviceTask = tasksFinished.poll()) != null) {
deviceTask.device.taskFinished();
}
}
synchronized (newDrivers) {
// Needed to synchronize with getRunningDrivers
synchronized (activeDrivers) {
activeDrivers.putAll(newDrivers);
}
for (Entry<String, DriverService> newDriverEntry : newDrivers.entrySet()) {
String driverId = newDriverEntry.getKey();
logger.info("Registered driver: " + driverId);
DriverConfigImpl driverConfig = rootConfig.driverConfigsById.get(driverId);
if (driverConfig == null) {
continue;
}
driverConfig.activeDriver = newDriverEntry.getValue();
for (DeviceConfigImpl deviceConfig : driverConfig.deviceConfigsById.values()) {
deviceConfig.device.driverRegisteredSignal();
}
}
newDrivers.clear();
}
synchronized (newServers) {
if (!newServers.isEmpty()) {
activeServers.addAll(newServers);
for (ServerService server : newServers) {
logger.info("Registered server: {}", server.getId());
notifyServer(server);
}
newServers.clear();
}
}
synchronized (newDataLoggers) {
if (!newDataLoggers.isEmpty()) {
activeDataLoggers.addAll(newDataLoggers);
for (DataLoggerService dataLogger : newDataLoggers) {
logger.info("Registered data logger: {}", dataLogger.getId());
dataLogger.setChannelsToLog(rootConfig.logChannels);
}
newDataLoggers.clear();
}
}
if (driverToBeRemovedId != null) {
DriverService removedDriver;
synchronized (activeDrivers) {
removedDriver = activeDrivers.remove(driverToBeRemovedId);
}
if (removedDriver == null) {
// drivers was removed before it was added to activeDrivers
newDrivers.remove(driverToBeRemovedId);
driverRemovedSignal.countDown();
} else {
DriverConfigImpl driverConfig = rootConfig.driverConfigsById.get(driverToBeRemovedId);
if (driverConfig != null) {
activeDeviceCountDown = driverConfig.deviceConfigsById.size();
if (activeDeviceCountDown > 0) {
// all devices have to be given a chance to finish their current task and disconnect:
for (DeviceConfigImpl deviceConfig : driverConfig.deviceConfigsById.values()) {
deviceConfig.device.driverDeregisteredSignal();
}
synchronized (driverRemovedSignal) {
if (activeDeviceCountDown == 0) {
driverRemovedSignal.countDown();
}
}
} else {
driverRemovedSignal.countDown();
}
} else {
driverRemovedSignal.countDown();
}
}
driverToBeRemovedId = null;
}
if (serverToBeRemoved != null) {
if (!activeServers.remove(serverToBeRemoved)) {
newServers.remove(serverToBeRemoved);
}
serverToBeRemoved = null;
serverRemovedSignal.countDown();
}
if (dataLoggerToBeRemoved != null) {
if (!activeDataLoggers.remove(dataLoggerToBeRemoved)) {
newDataLoggers.remove(dataLoggerToBeRemoved);
}
dataLoggerToBeRemoved = null;
dataLoggerRemovedSignal.countDown();
}
synchronized (connectionFailures) {
if (currentTime == 0) {
currentTime = System.currentTimeMillis();
}
Device connectionFailureDevice;
while ((connectionFailureDevice = connectionFailures.poll()) != null) {
connectionFailureDevice.connectFailureSignal(currentTime);
}
}
synchronized (connectedDevices) {
if (currentTime == 0) {
currentTime = System.currentTimeMillis();
}
Device connectedDevice;
while ((connectedDevice = connectedDevices.poll()) != null) {
connectedDevice.connectedSignal(currentTime);
}
}
synchronized (newWriteTasks) {
addTasksAndClear(newWriteTasks);
}
synchronized (newReadTasks) {
addTasksAndClear(newReadTasks);
}
synchronized (disconnectedDevices) {
Device connectedDevice;
while ((connectedDevice = disconnectedDevices.poll()) != null) {
connectedDevice.disconnectedSignal();
}
}
}
use of org.openmuc.framework.driver.spi.ChannelRecordContainer in project OpenMUC by isc-konstanz.
the class DriverConnection method setRecords.
private boolean setRecords(List<ChannelRecordContainer> containers, MBusConnection mBusConnection, long timestamp, List<DataRecord> dataRecords, String[] dibvibs) throws ConnectionException {
boolean selectForReadoutSet = false;
for (ChannelRecordContainer container : containers) {
String channelAddress = container.getChannelAddress();
if (channelAddress.startsWith("X")) {
String[] dibAndVib = channelAddress.split(":");
if (dibAndVib.length != 2) {
container.setRecord(new Record(Flag.DRIVER_ERROR_CHANNEL_ADDRESS_SYNTAX_INVALID));
}
List<DataRecord> dataRecordsToSelectForReadout = new ArrayList<>(1);
selectForReadoutSet = true;
try {
mBusConnection.selectForReadout(mBusAddress, dataRecordsToSelectForReadout);
sleep(delay);
} catch (SerialPortTimeoutException e) {
container.setRecord(new Record(Flag.DRIVER_ERROR_TIMEOUT));
continue;
} catch (IOException e) {
connectionInterface.close();
throw new ConnectionException(e);
}
VariableDataStructure variableDataStructure2 = null;
try {
variableDataStructure2 = mBusConnection.read(mBusAddress);
} catch (SerialPortTimeoutException e1) {
container.setRecord(new Record(Flag.DRIVER_ERROR_TIMEOUT));
continue;
} catch (IOException e1) {
connectionInterface.close();
throw new ConnectionException(e1);
}
DataRecord dataRecord = variableDataStructure2.getDataRecords().get(0);
setContainersRecord(timestamp, container, dataRecord);
continue;
}
int j = 0;
for (DataRecord dataRecord : dataRecords) {
if (dibvibs[j++].equalsIgnoreCase(channelAddress)) {
setContainersRecord(timestamp, container, dataRecord);
break;
}
}
if (container.getRecord() == null) {
container.setRecord(new Record(Flag.DRIVER_ERROR_CHANNEL_WITH_THIS_ADDRESS_NOT_FOUND));
}
}
return selectForReadoutSet;
}
use of org.openmuc.framework.driver.spi.ChannelRecordContainer in project OpenMUC by isc-konstanz.
the class DriverConnectionTest method testReadThrowsIOException.
@Test
public void testReadThrowsIOException() throws Exception {
MBusConnection con = mock(MBusConnection.class);
VariableDataStructure vds = new VariableDataStructure(NZR_ANSWER, 6, NZR_ANSWER.length - 6, null, null);
vds.decode();
when(con.read(anyInt())).thenThrow(new IOException());
ConnectionInterface serialIntervace = new ConnectionInterface(con, "/dev/ttyS100:5", delay, interfaces);
serialIntervace.increaseConnectionCounter();
String[] deviceAddressTokens = { "/dev/ttyS100", "5" };
int address = Integer.parseInt(deviceAddressTokens[1]);
DriverConnection driverCon = new DriverConnection(serialIntervace, address, null, delay);
List<ChannelRecordContainer> records = Arrays.asList(newChannelRecordContainer("04:03"));
driverCon.read(records, null, null);
Flag actualFlag = records.get(0).getRecord().getFlag();
assertEquals(Flag.DRIVER_ERROR_TIMEOUT, actualFlag);
}
use of org.openmuc.framework.driver.spi.ChannelRecordContainer in project OpenMUC by isc-konstanz.
the class DriverConnectionTest method testReadBcdDateLong.
@Test
public void testReadBcdDateLong() throws Exception {
MBusConnection con = mock(MBusConnection.class);
VariableDataStructure vds = new VariableDataStructure(SIEMENS_UH50_ANSWER, 6, SIEMENS_UH50_ANSWER.length - 6, null, null);
vds.decode();
when(con.read(anyInt())).thenReturn(vds);
ConnectionInterface serialIntervace = new ConnectionInterface(con, "/dev/ttyS100:5", delay, interfaces);
serialIntervace.increaseConnectionCounter();
String[] deviceAddressTokens = { "/dev/ttyS100", "5" };
DriverConnection mBusConnection = new DriverConnection(serialIntervace, Integer.parseInt(deviceAddressTokens[1]), null, delay);
List<ChannelRecordContainer> records = new LinkedList<>();
records.add(newChannelRecordContainer("09:74"));
records.add(newChannelRecordContainer("42:6c"));
records.add(newChannelRecordContainer("8c01:14"));
mBusConnection.read(records, null, null);
}
use of org.openmuc.framework.driver.spi.ChannelRecordContainer in project OpenMUC by isc-konstanz.
the class DriverConnectionTest method testReadThrowsTimeoutException.
@Test
public void testReadThrowsTimeoutException() throws Exception {
MBusConnection con = mock(MBusConnection.class);
VariableDataStructure vds = new VariableDataStructure(NZR_ANSWER, 6, NZR_ANSWER.length - 6, null, null);
vds.decode();
when(con.read(anyInt())).thenThrow(new SerialPortTimeoutException());
ConnectionInterface serialIntervace = new ConnectionInterface(con, "/dev/ttyS100:5", delay, interfaces);
serialIntervace.increaseConnectionCounter();
String[] deviceAddressTokens = { "/dev/ttyS100", "5" };
int address = Integer.parseInt(deviceAddressTokens[1]);
DriverConnection driverCon = new DriverConnection(serialIntervace, address, null, delay);
List<ChannelRecordContainer> records = Arrays.asList(newChannelRecordContainer("04:03"));
driverCon.read(records, null, null);
assertEquals(Flag.DRIVER_ERROR_TIMEOUT, records.get(0).getRecord().getFlag());
}
Aggregations