use of org.openmuc.framework.data.TypeConversionException in project OpenMUC by isc-konstanz.
the class ChannelImpl method convertValidRecord.
private Record convertValidRecord(Record record) {
Double scalingFactor = config.getScalingFactor();
Double scalingOffset = config.getValueOffset();
if (scalingFactor != null) {
try {
record = new Record(new DoubleValue(record.getValue().asDouble() * scalingFactor), record.getTimestamp(), record.getFlag());
} catch (TypeConversionException e) {
String msg = "Unable to apply scaling factor to channel " + config.getId() + " because a TypeConversionError occurred.";
logger.error(msg, e);
}
}
if (scalingOffset != null) {
try {
record = new Record(new DoubleValue(record.getValue().asDouble() + scalingOffset), record.getTimestamp(), record.getFlag());
} catch (TypeConversionException e) {
String msg = "Unable to apply scaling offset to channel " + config.getId() + " because a TypeConversionError occurred.";
logger.error(msg, e);
}
}
try {
switch(config.getValueType()) {
case BOOLEAN:
return new Record(new BooleanValue(record.getValue().asBoolean()), record.getTimestamp(), record.getFlag());
case BYTE:
return new Record(new ByteValue(record.getValue().asByte()), record.getTimestamp(), record.getFlag());
case SHORT:
return new Record(new ShortValue(record.getValue().asShort()), record.getTimestamp(), record.getFlag());
case INTEGER:
return new Record(new IntValue(record.getValue().asInt()), record.getTimestamp(), record.getFlag());
case LONG:
return new Record(new LongValue(record.getValue().asLong()), record.getTimestamp(), record.getFlag());
case FLOAT:
return new Record(new FloatValue(record.getValue().asFloat()), record.getTimestamp(), record.getFlag());
case DOUBLE:
return new Record(new DoubleValue(record.getValue().asDouble()), record.getTimestamp(), record.getFlag());
case BYTE_ARRAY:
return new Record(new ByteArrayValue(record.getValue().asByteArray()), record.getTimestamp(), record.getFlag());
case STRING:
default:
return new Record(new StringValue(record.getValue().toString()), record.getTimestamp(), record.getFlag());
}
} catch (TypeConversionException e) {
logger.error("Unable to convert value to configured value type because a TypeConversionError occured.", e);
return new Record(Flag.DRIVER_ERROR_CHANNEL_VALUE_TYPE_CONVERSION_EXCEPTION);
}
}
use of org.openmuc.framework.data.TypeConversionException in project OpenMUC by isc-konstanz.
the class Iec60870Connection method write.
@Override
public Object write(List<ChannelValueContainer> containers, Object containerListHandle) throws ConnectionException {
for (ChannelValueContainer channelValueContainer : containers) {
ChannelAddress channelAddress;
try {
channelAddress = new ChannelAddress(channelValueContainer.getChannelAddress());
Record record = new Record(channelValueContainer.getValue(), System.currentTimeMillis(), Flag.VALID);
Iec60870DataHandling.writeSingleCommand(record, channelAddress, clientConnection);
channelValueContainer.setFlag(Flag.VALID);
} catch (ArgumentSyntaxException e) {
channelValueContainer.setFlag(Flag.DRIVER_ERROR_CHANNEL_ADDRESS_SYNTAX_INVALID);
logger.error(e.getMessage());
throw new UnsupportedOperationException(e);
} catch (IOException e) {
channelValueContainer.setFlag(Flag.CONNECTION_EXCEPTION);
throw new ConnectionException(e);
} catch (TypeConversionException e) {
channelValueContainer.setFlag(Flag.DRIVER_ERROR_CHANNEL_VALUE_TYPE_CONVERSION_EXCEPTION);
logger.error(e.getMessage());
} catch (UnsupportedOperationException e) {
channelValueContainer.setFlag(Flag.DRIVER_ERROR_CHANNEL_ADDRESS_SYNTAX_INVALID);
logger.error(e.getMessage());
throw e;
}
}
return null;
}
use of org.openmuc.framework.data.TypeConversionException in project OpenMUC by isc-konstanz.
the class SlotsDb method log.
@Override
public void log(List<LoggingRecord> containers, long timestamp) {
for (LoggingRecord container : containers) {
Double value;
if (container.getRecord().getValue() == null) {
value = Double.NaN;
} else {
try {
value = container.getRecord().getValue().asDouble();
} catch (TypeConversionException e) {
value = Double.NaN;
}
}
try {
String channelId = container.getChannelId();
fileObjectProxy.appendValue(channelId, value, timestamp, container.getRecord().getFlag().getCode(), loggingIntervalsById.get(channelId));
} catch (IOException e) {
logger.error("error logging records", e);
}
}
}
use of org.openmuc.framework.data.TypeConversionException in project OpenMUC by isc-konstanz.
the class ToJson method getRestRecord.
private RestRecord getRestRecord(Record rc, ValueType valueType) throws ClassCastException {
Value value = rc.getValue();
Flag flag = rc.getFlag();
RestRecord rrc = new RestRecord();
rrc.setTimestamp(rc.getTimestamp());
try {
flag = getFlag(value, valueType, flag);
} catch (TypeConversionException e) {
flag = Flag.DRIVER_ERROR_CHANNEL_VALUE_TYPE_CONVERSION_EXCEPTION;
}
if (flag != Flag.VALID) {
rrc.setFlag(flag);
rrc.setValue(null);
return rrc;
}
rrc.setFlag(flag);
if (value == null) {
rrc.setValue(null);
return rrc;
}
switch(valueType) {
case FLOAT:
rrc.setValue(value.asFloat());
break;
case DOUBLE:
rrc.setValue(value.asDouble());
break;
case SHORT:
rrc.setValue(value.asShort());
break;
case INTEGER:
rrc.setValue(value.asInt());
break;
case LONG:
rrc.setValue(value.asLong());
break;
case BYTE:
rrc.setValue(value.asByte());
break;
case BOOLEAN:
rrc.setValue(value.asBoolean());
break;
case BYTE_ARRAY:
rrc.setValue(value.asByteArray());
break;
case STRING:
rrc.setValue(value.asString());
break;
default:
rrc.setValue(null);
break;
}
return rrc;
}
use of org.openmuc.framework.data.TypeConversionException in project OpenMUC by isc-konstanz.
the class Iec60870DataHandling method writeSingleCommand.
static void writeSingleCommand(Record record, ChannelAddress channelAddress, Connection clientConnection) throws IOException, UnsupportedOperationException, TypeConversionException {
int commonAddress = channelAddress.commonAddress();
boolean qualifierSelect = channelAddress.select();
int informationObjectAddress = channelAddress.ioa();
ASduType typeId = ASduType.typeFor(channelAddress.typeId());
Flag flag = record.getFlag();
Value value = record.getValue();
IeTime56 timestamp = new IeTime56(record.getTimestamp());
CauseOfTransmission cot = CauseOfTransmission.ACTIVATION;
if (flag == Flag.VALID && value != null) {
switch(typeId) {
case C_DC_NA_1:
DoubleCommandState doubleCommandState = value.asBoolean() ? DoubleCommandState.ON : DoubleCommandState.OFF;
clientConnection.doubleCommand(commonAddress, cot, informationObjectAddress, new IeDoubleCommand(doubleCommandState, 0, false));
break;
case C_DC_TA_1:
doubleCommandState = value.asBoolean() ? DoubleCommandState.ON : DoubleCommandState.OFF;
clientConnection.doubleCommandWithTimeTag(commonAddress, cot, informationObjectAddress, new IeDoubleCommand(doubleCommandState, 0, false), timestamp);
break;
case C_BO_NA_1:
IeBinaryStateInformation binaryStateInformation = new IeBinaryStateInformation(value.asInt());
clientConnection.bitStringCommand(commonAddress, cot, informationObjectAddress, binaryStateInformation);
break;
case C_BO_TA_1:
binaryStateInformation = new IeBinaryStateInformation(value.asInt());
clientConnection.bitStringCommandWithTimeTag(commonAddress, cot, informationObjectAddress, binaryStateInformation, timestamp);
break;
case // Writes only the current time, no values
C_CD_NA_1:
IeTime16 time16 = new IeTime16(record.getTimestamp());
clientConnection.delayAcquisitionCommand(commonAddress, cot, time16);
break;
case // Uses ByteArray Value [request, freeze]
C_CI_NA_1:
byte[] baQualifier = value.asByteArray();
if (baQualifier.length == 2) {
IeQualifierOfCounterInterrogation qualifier = new IeQualifierOfCounterInterrogation(baQualifier[0], baQualifier[1]);
clientConnection.counterInterrogation(commonAddress, cot, qualifier);
} else {
throw new TypeConversionException(typeId + "(" + typeId.getId() + "): Only byte array with length 2 allowed. byte[0]=request, byte[1]=freeze]");
}
break;
case // Writes only the current time, no values
C_CS_NA_1:
clientConnection.synchronizeClocks(commonAddress, new IeTime56(System.currentTimeMillis()));
break;
case C_IC_NA_1:
IeQualifierOfInterrogation ieQualifierOfInterrogation = new IeQualifierOfInterrogation(value.asInt());
clientConnection.interrogation(commonAddress, cot, ieQualifierOfInterrogation);
break;
case C_RC_NA_1:
IeRegulatingStepCommand regulatingStepCommand = getIeRegulatingStepCommand(typeId, value);
clientConnection.regulatingStepCommand(commonAddress, cot, informationObjectAddress, regulatingStepCommand);
break;
case C_RC_TA_1:
try {
regulatingStepCommand = getIeRegulatingStepCommand(typeId, value);
clientConnection.regulatingStepCommandWithTimeTag(commonAddress, cot, informationObjectAddress, regulatingStepCommand, timestamp);
} catch (Exception e) {
logger.error("", e);
}
break;
case C_RD_NA_1:
clientConnection.readCommand(commonAddress, informationObjectAddress);
break;
case C_RP_NA_1:
clientConnection.resetProcessCommand(commonAddress, new IeQualifierOfResetProcessCommand(value.asInt()));
break;
case C_SC_NA_1:
IeSingleCommand singleCommand = getIeSingeleCommand(typeId, value);
clientConnection.singleCommand(commonAddress, cot, informationObjectAddress, singleCommand);
break;
case C_SC_TA_1:
singleCommand = getIeSingeleCommand(typeId, value);
clientConnection.singleCommandWithTimeTag(commonAddress, cot, informationObjectAddress, singleCommand, timestamp);
break;
case C_SE_NA_1:
byte[] values = value.asByteArray();
int arrayLength = 6;
int valueLength = 4;
checkLength(typeId, values, arrayLength, "byte[0-3]=command state, byte[4]=qualifier of command, byte[5]=execute/select");
IeQualifierOfSetPointCommand ieQualifierOfSetPointCommand = getIeQualifierSetPointCommand(values, arrayLength);
IeNormalizedValue ieNormalizedValue = new IeNormalizedValue(bytesToSignedInt32(values, valueLength, false));
clientConnection.setNormalizedValueCommand(commonAddress, cot, informationObjectAddress, ieNormalizedValue, ieQualifierOfSetPointCommand);
break;
case C_SE_NB_1:
values = value.asByteArray();
arrayLength = 4;
checkLength(typeId, values, arrayLength, "byte[0-1]=command state, byte[2]=qualifier of command, byte[3]=execute/select");
ieQualifierOfSetPointCommand = getIeQualifierSetPointCommand(values, arrayLength);
IeScaledValue scaledValue = new IeScaledValue(bytesToSignedInt32(values, 2, false));
clientConnection.setScaledValueCommand(commonAddress, cot, informationObjectAddress, scaledValue, ieQualifierOfSetPointCommand);
break;
case C_SE_NC_1:
IeShortFloat shortFloat = new IeShortFloat(value.asFloat());
IeQualifierOfSetPointCommand qualifier = new IeQualifierOfSetPointCommand(0, qualifierSelect);
clientConnection.setShortFloatCommand(commonAddress, cot, informationObjectAddress, shortFloat, qualifier);
break;
case C_SE_TA_1:
values = value.asByteArray();
arrayLength = 6;
valueLength = 4;
checkLength(typeId, values, arrayLength, "byte[0-3]=command state, byte[4]=qualifier of command, byte[5]=execute/select");
ieQualifierOfSetPointCommand = getIeQualifierSetPointCommand(values, arrayLength);
ieNormalizedValue = new IeNormalizedValue(bytesToSignedInt32(values, valueLength, false));
clientConnection.setNormalizedValueCommandWithTimeTag(commonAddress, cot, informationObjectAddress, ieNormalizedValue, ieQualifierOfSetPointCommand, timestamp);
break;
case C_SE_TB_1:
values = value.asByteArray();
arrayLength = 4;
checkLength(typeId, values, arrayLength, "byte[0-1]=command state, byte[2]=qualifier of command, byte[3]=execute/select");
ieQualifierOfSetPointCommand = getIeQualifierSetPointCommand(values, arrayLength);
scaledValue = new IeScaledValue(bytesToSignedInt32(values, 2, false));
clientConnection.setScaledValueCommandWithTimeTag(commonAddress, cot, informationObjectAddress, scaledValue, ieQualifierOfSetPointCommand, timestamp);
break;
case C_SE_TC_1:
// TODO:
throw new UnsupportedOperationException("TypeID " + typeId + "(" + typeId.getId() + ") is not supported, yet.");
case C_TS_NA_1:
clientConnection.testCommand(commonAddress);
break;
case C_TS_TA_1:
clientConnection.testCommandWithTimeTag(commonAddress, new IeTestSequenceCounter(value.asInt()), timestamp);
break;
case F_AF_NA_1:
case F_DR_TA_1:
case F_FR_NA_1:
case F_LS_NA_1:
case F_SC_NA_1:
case F_SC_NB_1:
case F_SG_NA_1:
case F_SR_NA_1:
case M_BO_NA_1:
case M_BO_TA_1:
case M_BO_TB_1:
case M_DP_NA_1:
case M_DP_TA_1:
case M_DP_TB_1:
case M_EI_NA_1:
case M_EP_TA_1:
case M_EP_TB_1:
case M_EP_TC_1:
case M_EP_TD_1:
case M_EP_TE_1:
case M_EP_TF_1:
case M_IT_NA_1:
case M_IT_TA_1:
case M_IT_TB_1:
case M_ME_NA_1:
case M_ME_NB_1:
case M_ME_NC_1:
case M_ME_ND_1:
case M_ME_TA_1:
case M_ME_TB_1:
case M_ME_TC_1:
case M_ME_TD_1:
case M_ME_TE_1:
case M_ME_TF_1:
case M_PS_NA_1:
case M_SP_NA_1:
case M_SP_TA_1:
case M_SP_TB_1:
case M_ST_NA_1:
case M_ST_TA_1:
case M_ST_TB_1:
case P_AC_NA_1:
case P_ME_NA_1:
case P_ME_NB_1:
case P_ME_NC_1:
case PRIVATE_128:
case PRIVATE_129:
case PRIVATE_130:
case PRIVATE_131:
case PRIVATE_132:
case PRIVATE_133:
case PRIVATE_134:
case PRIVATE_135:
case PRIVATE_136:
case PRIVATE_137:
case PRIVATE_138:
case PRIVATE_139:
case PRIVATE_140:
case PRIVATE_141:
case PRIVATE_142:
case PRIVATE_143:
case PRIVATE_144:
case PRIVATE_145:
case PRIVATE_146:
case PRIVATE_147:
case PRIVATE_148:
case PRIVATE_149:
case PRIVATE_150:
case PRIVATE_151:
case PRIVATE_152:
case PRIVATE_153:
case PRIVATE_154:
case PRIVATE_155:
case PRIVATE_156:
case PRIVATE_157:
case PRIVATE_158:
case PRIVATE_159:
case PRIVATE_160:
case PRIVATE_161:
case PRIVATE_162:
case PRIVATE_163:
case PRIVATE_164:
case PRIVATE_165:
case PRIVATE_166:
case PRIVATE_167:
case PRIVATE_168:
case PRIVATE_169:
case PRIVATE_170:
case PRIVATE_171:
case PRIVATE_172:
case PRIVATE_173:
case PRIVATE_174:
case PRIVATE_175:
case PRIVATE_176:
case PRIVATE_177:
case PRIVATE_178:
case PRIVATE_179:
case PRIVATE_180:
case PRIVATE_181:
case PRIVATE_182:
case PRIVATE_183:
case PRIVATE_184:
case PRIVATE_185:
case PRIVATE_186:
case PRIVATE_187:
case PRIVATE_188:
case PRIVATE_189:
case PRIVATE_190:
case PRIVATE_191:
case PRIVATE_192:
case PRIVATE_193:
case PRIVATE_194:
case PRIVATE_195:
case PRIVATE_196:
case PRIVATE_197:
case PRIVATE_198:
case PRIVATE_199:
case PRIVATE_200:
case PRIVATE_201:
case PRIVATE_202:
case PRIVATE_203:
case PRIVATE_204:
case PRIVATE_205:
case PRIVATE_206:
case PRIVATE_207:
case PRIVATE_208:
case PRIVATE_209:
case PRIVATE_210:
case PRIVATE_211:
case PRIVATE_212:
case PRIVATE_213:
case PRIVATE_214:
case PRIVATE_215:
case PRIVATE_216:
case PRIVATE_217:
case PRIVATE_218:
case PRIVATE_219:
case PRIVATE_220:
case PRIVATE_221:
case PRIVATE_222:
case PRIVATE_223:
case PRIVATE_224:
case PRIVATE_225:
case PRIVATE_226:
case PRIVATE_227:
case PRIVATE_228:
case PRIVATE_229:
case PRIVATE_230:
case PRIVATE_231:
case PRIVATE_232:
case PRIVATE_233:
case PRIVATE_234:
case PRIVATE_235:
case PRIVATE_236:
case PRIVATE_237:
case PRIVATE_238:
case PRIVATE_239:
case PRIVATE_240:
case PRIVATE_241:
case PRIVATE_242:
case PRIVATE_243:
case PRIVATE_244:
case PRIVATE_245:
case PRIVATE_246:
case PRIVATE_247:
case PRIVATE_248:
case PRIVATE_249:
case PRIVATE_250:
case PRIVATE_251:
case PRIVATE_252:
case PRIVATE_253:
case PRIVATE_254:
case PRIVATE_255:
default:
throw new UnsupportedOperationException("TypeID " + typeId + "(" + typeId.getId() + ") is not supported, yet.");
}
}
}
Aggregations