Search in sources :

Example 11 with ValueType

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();
}
Also used : WrongCharacterException(org.openmuc.framework.datalogger.ascii.exceptions.WrongCharacterException) ValueType(org.openmuc.framework.data.ValueType) Value(org.openmuc.framework.data.Value) LogChannel(org.openmuc.framework.datalogger.spi.LogChannel) Record(org.openmuc.framework.data.Record) LoggingRecord(org.openmuc.framework.datalogger.spi.LoggingRecord) LoggingRecord(org.openmuc.framework.datalogger.spi.LoggingRecord) WrongScalingException(org.openmuc.framework.datalogger.ascii.exceptions.WrongScalingException)

Example 12 with ValueType

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);
}
Also used : MBusConnection(org.openmuc.jmbus.MBusConnection) ChannelScanInfo(org.openmuc.framework.config.ChannelScanInfo) ValueType(org.openmuc.framework.data.ValueType) VariableDataStructure(org.openmuc.jmbus.VariableDataStructure) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 13 with ValueType

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;
}
Also used : JsonArray(com.google.gson.JsonArray) ChannelScanInfo(org.openmuc.framework.config.ChannelScanInfo) ValueType(org.openmuc.framework.data.ValueType) JsonElement(com.google.gson.JsonElement) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject)

Aggregations

ValueType (org.openmuc.framework.data.ValueType)13 ChannelScanInfo (org.openmuc.framework.config.ChannelScanInfo)6 DoubleValue (org.openmuc.framework.data.DoubleValue)2 Record (org.openmuc.framework.data.Record)2 StringValue (org.openmuc.framework.data.StringValue)2 Value (org.openmuc.framework.data.Value)2 OctetString (org.openmuc.jsml.structures.OctetString)2 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 IOException (java.io.IOException)1 Connection (java.sql.Connection)1 ResultSet (java.sql.ResultSet)1 ResultSetMetaData (java.sql.ResultSetMetaData)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 Timestamp (java.sql.Timestamp)1 ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1