use of org.openmuc.framework.data.ValueType in project OpenMUC by isc-konstanz.
the class LogFileWriter method getLoggingLine.
private String getLoggingLine(List<LoggingRecord> logRecordContainer, Map<String, LogChannel> logChannelList, Calendar calendar, boolean isError32) {
sb.setLength(0);
LoggerUtils.setLoggerTimestamps(sb, calendar);
for (int i = 0; i < logRecordContainer.size(); i++) {
int size = Const.VALUE_SIZE_MINIMAL;
boolean left = true;
Record record = logRecordContainer.get(i).getRecord();
String channelId = logRecordContainer.get(i).getChannelId();
LogChannel logChannel = logChannelList.get(channelId);
sbValue.setLength(0);
if (record != null) {
Value recordValue = record.getValue();
Record recordBackup = null;
if (isError32) {
recordBackup = logRecordContainer.get(i).getRecord();
logRecordContainer.set(i, new LoggingRecord(logChannel, new Record(Flag.DATA_LOGGING_NOT_ACTIVE)));
}
record = logRecordContainer.get(i).getRecord();
if (record.getFlag() == Flag.VALID) {
if (recordValue == null) {
// write error flag
LoggerUtils.buildError(sbValue, Flag.CANNOT_WRITE_NULL_VALUE);
size = getDataTypeSize(logChannel, i);
} else {
ValueType valueType = logChannel.getValueType();
switch(valueType) {
case BOOLEAN:
sbValue.append((recordValue.asShort())).toString();
break;
case LONG:
sbValue.append((recordValue.asLong())).toString();
size = Const.VALUE_SIZE_LONG;
break;
case INTEGER:
sbValue.append((recordValue.asInt())).toString();
size = Const.VALUE_SIZE_INTEGER;
break;
case SHORT:
sbValue.append((recordValue.asShort())).toString();
size = Const.VALUE_SIZE_SHORT;
break;
case DOUBLE:
case FLOAT:
size = Const.VALUE_SIZE_DOUBLE;
try {
IESDataFormatUtils.convertDoubleToStringWithMaxLength(sbValue, recordValue.asDouble(), size);
} catch (WrongScalingException e) {
LoggerUtils.buildError(sbValue, Flag.UNKNOWN_ERROR);
logger.error(e.getMessage() + " ChannelId: " + channelId);
}
break;
case BYTE_ARRAY:
left = false;
size = checkMinimalValueSize(getDataTypeSize(logChannel, i));
byte[] byteArray = recordValue.asByteArray();
if (byteArray.length > size) {
LoggerUtils.buildError(sbValue, Flag.UNKNOWN_ERROR);
logger.error("The byte array is too big, length is ", byteArray.length, " but max. length allowed is ", size, ", ChannelId: ", channelId);
} else {
sbValue.append(Const.HEXADECIMAL);
LoggerUtils.byteArrayToHexString(sbValue, byteArray);
}
break;
case STRING:
left = false;
size = checkMinimalValueSize(getDataTypeSize(logChannel, i));
sbValue.append(recordValue.asString());
int valueLength = sbValue.length();
try {
checkStringValue(sbValue);
} catch (WrongCharacterException e) {
LoggerUtils.buildError(sbValue, Flag.UNKNOWN_ERROR);
logger.error(e.getMessage());
}
if (valueLength > size) {
LoggerUtils.buildError(sbValue, Flag.UNKNOWN_ERROR);
logger.error("The string is too big, length is ", valueLength, " but max. length allowed is ", size, ", ChannelId: ", channelId);
}
break;
case BYTE:
sbValue.append(String.format("0x%02x", recordValue.asByte()));
break;
default:
throw new RuntimeException("unsupported valueType");
}
}
} else {
// write error flag
LoggerUtils.buildError(sbValue, record.getFlag());
size = checkMinimalValueSize(getDataTypeSize(logChannel, i));
}
if (isError32) {
logRecordContainer.set(i, new LoggingRecord(logChannel, recordBackup));
}
} else {
// got no data
LoggerUtils.buildError(sbValue, Flag.UNKNOWN_ERROR);
size = checkMinimalValueSize(getDataTypeSize(logChannel, i));
}
if (left) {
LoggerUtils.addSpaces(sbValue.length(), size, sb);
sb.append(sbValue);
} else {
sb.append(sbValue);
LoggerUtils.addSpaces(sbValue.length(), size, sb);
}
if (LoggerUtils.hasNext(logRecordContainer, i)) {
sb.append(Const.SEPARATOR);
}
}
// All systems with the same newline charter
sb.append(Const.LINESEPARATOR);
return sb.toString();
}
use of org.openmuc.framework.data.ValueType in project OpenMUC by isc-konstanz.
the class DriverConnectionTest method testScanForChannelsByteArray.
@Test
public void testScanForChannelsByteArray() 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);
mBusConnection.disconnect();
List<ChannelScanInfo> scanForChannels = mBusConnection.scanForChannels(null);
for (ChannelScanInfo info : scanForChannels) {
System.out.println(info.getDescription() + " " + info.getUnit());
}
ValueType actual = mBusConnection.scanForChannels(null).get(22).getValueType();
assertEquals(ValueType.LONG, actual);
}
use of org.openmuc.framework.data.ValueType in project OpenMUC by isc-konstanz.
the class FromJson method getChannelScanInfoList.
public List<ChannelScanInfo> getChannelScanInfoList() {
List<ChannelScanInfo> returnValue = new ArrayList<>();
// TODO: another name?
JsonElement jse = jsonObject.get(Const.CHANNELS);
JsonArray jsa;
if (jse.isJsonArray()) {
jsa = jse.getAsJsonArray();
Iterator<JsonElement> jseIterator = jsa.iterator();
while (jseIterator.hasNext()) {
JsonObject jso = jseIterator.next().getAsJsonObject();
String channelAddress = getString(jso.get(Const.ADDRESS));
String channelSettings = getString(jso.get(Const.SETTINGS));
ValueType valueType = ValueType.valueOf(getString(jso.get(Const.VALUE_TYPE)));
int valueTypeLength = getInt(jso.get(Const.VALUE_TYPE_LENGTH));
String description = getString(jso.get(Const.DESCRIPTION));
boolean readable = getBoolean(jso.get(Const.READABLE));
boolean writeable = getBoolean(jso.get(Const.WRITEABLE));
String metadata = getString(jso.get(Const.METADATA));
returnValue.add(new ChannelScanInfo(channelAddress, channelSettings, description, valueType, valueTypeLength, readable, writeable, metadata));
}
} else {
returnValue = null;
}
return returnValue;
}
Aggregations