Skip to content

Commit

Permalink
feat(plc-simulator/cbus): unified cal data handling
Browse files Browse the repository at this point in the history
  • Loading branch information
sruehl committed Sep 27, 2022
1 parent 04ff774 commit 9878491
Showing 1 changed file with 133 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ private void syncChannelRead(ChannelHandlerContext ctx, Object msg) throws Excep
LOGGER.info("Sending echo");
ctx.write(msg);
}
handleReset((RequestReset) request);
LOGGER.info("Handling RequestReset\n{}", request);
handleReset();
} else if (request instanceof RequestSmartConnectShortcut) {
handleSmartConnect((RequestSmartConnectShortcut) request);
}
Expand All @@ -142,118 +143,7 @@ private void syncChannelRead(ChannelHandlerContext ctx, Object msg) throws Excep
private void handleDirectCommandAccess(ChannelHandlerContext ctx, RequestDirectCommandAccess requestDirectCommandAccess) {
CALData calData = requestDirectCommandAccess.getCalData();
LOGGER.info("Handling RequestDirectCommandAccess\n{}", requestDirectCommandAccess);

// TODO: handle other cal data type
if (calData instanceof CALDataWrite) {
CALDataWrite calDataWrite = (CALDataWrite) calData;
Runnable acknowledger = () -> {
CALDataAcknowledge calDataAcknowledge = new CALDataAcknowledge(CALCommandTypeContainer.CALCommandAcknowledge, null, calDataWrite.getParamNo(), (short) 0x0, requestContext);
CALReplyShort calReply = new CALReplyShort((byte) 0x0, calDataAcknowledge, cBusOptions, requestContext);
EncodedReplyCALReply encodedReply = new EncodedReplyCALReply((byte) 0x0, calReply, cBusOptions, requestContext);
ReplyEncodedReply replyEncodedReply = new ReplyEncodedReply((byte) 0x0, encodedReply, null, cBusOptions, requestContext);
ReplyOrConfirmation replyOrConfirmation = new ReplyOrConfirmationReply((byte) 0x0, replyEncodedReply, new ResponseTermination(), cBusOptions, requestContext);
if (requestDirectCommandAccess.getAlpha() != null) {
replyOrConfirmation = new ReplyOrConfirmationConfirmation((byte) 0x0, new Confirmation(requestDirectCommandAccess.getAlpha(), null, ConfirmationType.CONFIRMATION_SUCCESSFUL), replyOrConfirmation, cBusOptions, requestContext);
}
CBusMessageToClient cBusMessageToClient = new CBusMessageToClient(replyOrConfirmation, requestContext, cBusOptions);
LOGGER.info("Sending ack\n{}", cBusMessageToClient);
ctx.writeAndFlush(cBusMessageToClient);
};
switch (calDataWrite.getParamNo().getParameterType()) {
case APPLICATION_ADDRESS_1:
ApplicationAddress1 applicationAddress1 = ((ParameterValueApplicationAddress1) calDataWrite.getParameterValue()).getValue();
monitorApplicationAddress1 = applicationAddress1.getAddress();
acknowledger.run();
return;
case APPLICATION_ADDRESS_2:
ApplicationAddress2 applicationAddress2 = ((ParameterValueApplicationAddress2) calDataWrite.getParameterValue()).getValue();
monitorApplicationAddress2 = applicationAddress2.getAddress();
acknowledger.run();
return;
case INTERFACE_OPTIONS_1:
InterfaceOptions1 interfaceOptions1 = ((ParameterValueInterfaceOptions1) calDataWrite.getParameterValue()).getValue();
idmon = interfaceOptions1.getIdmon();
monitor = interfaceOptions1.getMonitor();
if (monitor) startMMIMonitor(ctx);
else stopMMIMonitor();
smart = interfaceOptions1.getSmart();
srchk = interfaceOptions1.getSrchk();
// TODO: add support for xonxoff
// xonxoff = interfaceOptions1.getXonXoff();
connect = interfaceOptions1.getConnect();
if (connect) startSALMonitor(ctx);
else stopSALMonitor();
buildCBusOptions();
acknowledger.run();
return;
case INTERFACE_OPTIONS_2:
InterfaceOptions2 interfaceOptions2 = ((ParameterValueInterfaceOptions2) calDataWrite.getParameterValue()).getValue();
// TODO: add support for burden
// burden = interfaceOptions2.getBurden();
// TODO: add support for clockgen
// clockgen = interfaceOptions2.getClockGen();
buildCBusOptions();
acknowledger.run();
return;
case INTERFACE_OPTIONS_3:
InterfaceOptions3 interfaceOptions3Value = ((ParameterValueInterfaceOptions3) calDataWrite.getParameterValue()).getValue();
boolean oldExstat = exstat;
exstat = interfaceOptions3Value.getExstat();
if (oldExstat != exstat) {
LOGGER.info("Restarting monitors");
stopMMIMonitor();
startMMIMonitor(ctx);
stopSALMonitor();
startSALMonitor(ctx);
}
pun = interfaceOptions3Value.getPun();
// TODO: add support for localsal
// localsal = interfaceOptions3Value.getLocalSal();
pcn = interfaceOptions3Value.getPcn();
buildCBusOptions();
acknowledger.run();
return;
case BAUD_RATE_SELECTOR:
BaudRateSelector baudRateSelector = ((ParameterValueBaudRateSelector) calDataWrite.getParameterValue()).getValue();
// TODO: add support for baudrate
// baudrate = baudRateSelector.getValue();
buildCBusOptions();
acknowledger.run();
return;
case INTERFACE_OPTIONS_1_POWER_UP_SETTINGS:
InterfaceOptions1 interfaceOptions1PowerUpSettings = ((ParameterValueInterfaceOptions1PowerUpSettings) calDataWrite.getParameterValue()).getValue().getInterfaceOptions1();
idmon = interfaceOptions1PowerUpSettings.getIdmon();
monitor = interfaceOptions1PowerUpSettings.getMonitor();
if (monitor) startMMIMonitor(ctx);
else stopMMIMonitor();
smart = interfaceOptions1PowerUpSettings.getSmart();
srchk = interfaceOptions1PowerUpSettings.getSrchk();
// TODO: add support for xonxoff
// xonxoff = interfaceOptions1PowerUpSettings.getXonXoff();
connect = interfaceOptions1PowerUpSettings.getConnect();
if (connect) startSALMonitor(ctx);
else stopSALMonitor();
buildCBusOptions();
acknowledger.run();
return;
case CUSTOM_MANUFACTURER:
// TODO: handle other param typed
acknowledger.run();
return;
case SERIAL_NUMBER:
// TODO: handle other param typed
acknowledger.run();
return;
case CUSTOM_TYPE:
// TODO: handle other param typed
acknowledger.run();
return;
default:
throw new IllegalStateException("Unmapped type");
}
} else if (calData instanceof CALDataIdentify) {
handleCalDataIdentify(ctx, (CALDataIdentify) calData, requestDirectCommandAccess.getAlpha());
}
handleCalData(ctx, calData, requestDirectCommandAccess.getAlpha());
}

private void handleRequestCommand(ChannelHandlerContext ctx, RequestCommand requestCommand) {
Expand Down Expand Up @@ -286,10 +176,7 @@ private void handleRequestCommand(ChannelHandlerContext ctx, RequestCommand requ
return;
}
CALData calData = command.getCalData();
// TODO: handle other Datatypes
if (calData instanceof CALDataIdentify) {
handleCalDataIdentify(ctx, (CALDataIdentify) calData, requestCommand.getAlpha());
}
handleCalData(ctx, calData, requestCommand.getAlpha());
return;
} else if (cbusCommand instanceof CBusCommandPointToMultiPoint) {
CBusCommandPointToMultiPoint cBusCommandPointToMultiPoint = (CBusCommandPointToMultiPoint) cbusCommand;
Expand Down Expand Up @@ -378,6 +265,134 @@ private void handleStatusRequestBinary(ChannelHandlerContext ctx, RequestCommand
sendStatusBytes(ctx, "First parts {}", application, (byte) 0x0, statusBytes, requestCommand.getAlpha(), cBusOptions);
}

private void handleCalData(ChannelHandlerContext ctx, CALData calData, Alpha alpha) {
if (calData instanceof CALDataGetStatus) {
// TODO: implement me
} else if (calData instanceof CALDataIdentify) {
handleCalDataIdentify(ctx, (CALDataIdentify) calData, alpha);
} else if (calData instanceof CALDataRecall) {
// TODO: implement me
} else if (calData instanceof CALDataReset) {
CALDataReset calDataReset = (CALDataReset) calData;
if (smart || connect) {
// On reset, we need to send the echo if we had not sent it above
LOGGER.info("Sending echo");
ctx.write(calDataReset);
}
LOGGER.info("Handling CALDataReset\n{}", calDataReset);
handleReset();
} else if (calData instanceof CALDataWrite) {
CALDataWrite calDataWrite = (CALDataWrite) calData;
Runnable acknowledger = () -> {
CALDataAcknowledge calDataAcknowledge = new CALDataAcknowledge(CALCommandTypeContainer.CALCommandAcknowledge, null, calDataWrite.getParamNo(), (short) 0x0, requestContext);
CALReplyShort calReply = new CALReplyShort((byte) 0x0, calDataAcknowledge, cBusOptions, requestContext);
EncodedReplyCALReply encodedReply = new EncodedReplyCALReply((byte) 0x0, calReply, cBusOptions, requestContext);
ReplyEncodedReply replyEncodedReply = new ReplyEncodedReply((byte) 0x0, encodedReply, null, cBusOptions, requestContext);
ReplyOrConfirmation replyOrConfirmation = new ReplyOrConfirmationReply((byte) 0x0, replyEncodedReply, new ResponseTermination(), cBusOptions, requestContext);
if (alpha != null) {
replyOrConfirmation = new ReplyOrConfirmationConfirmation((byte) 0x0, new Confirmation(alpha, null, ConfirmationType.CONFIRMATION_SUCCESSFUL), replyOrConfirmation, cBusOptions, requestContext);
}
CBusMessageToClient cBusMessageToClient = new CBusMessageToClient(replyOrConfirmation, requestContext, cBusOptions);
LOGGER.info("Sending ack\n{}", cBusMessageToClient);
ctx.writeAndFlush(cBusMessageToClient);
};
switch (calDataWrite.getParamNo().getParameterType()) {
case APPLICATION_ADDRESS_1:
ApplicationAddress1 applicationAddress1 = ((ParameterValueApplicationAddress1) calDataWrite.getParameterValue()).getValue();
monitorApplicationAddress1 = applicationAddress1.getAddress();
acknowledger.run();
return;
case APPLICATION_ADDRESS_2:
ApplicationAddress2 applicationAddress2 = ((ParameterValueApplicationAddress2) calDataWrite.getParameterValue()).getValue();
monitorApplicationAddress2 = applicationAddress2.getAddress();
acknowledger.run();
return;
case INTERFACE_OPTIONS_1:
InterfaceOptions1 interfaceOptions1 = ((ParameterValueInterfaceOptions1) calDataWrite.getParameterValue()).getValue();
idmon = interfaceOptions1.getIdmon();
monitor = interfaceOptions1.getMonitor();
if (monitor) startMMIMonitor(ctx);
else stopMMIMonitor();
smart = interfaceOptions1.getSmart();
srchk = interfaceOptions1.getSrchk();
// TODO: add support for xonxoff
// xonxoff = interfaceOptions1.getXonXoff();
connect = interfaceOptions1.getConnect();
if (connect) startSALMonitor(ctx);
else stopSALMonitor();
buildCBusOptions();
acknowledger.run();
return;
case INTERFACE_OPTIONS_2:
InterfaceOptions2 interfaceOptions2 = ((ParameterValueInterfaceOptions2) calDataWrite.getParameterValue()).getValue();
// TODO: add support for burden
// burden = interfaceOptions2.getBurden();
// TODO: add support for clockgen
// clockgen = interfaceOptions2.getClockGen();
buildCBusOptions();
acknowledger.run();
return;
case INTERFACE_OPTIONS_3:
InterfaceOptions3 interfaceOptions3Value = ((ParameterValueInterfaceOptions3) calDataWrite.getParameterValue()).getValue();
boolean oldExstat = exstat;
exstat = interfaceOptions3Value.getExstat();
if (oldExstat != exstat) {
LOGGER.info("Restarting monitors");
stopMMIMonitor();
startMMIMonitor(ctx);
stopSALMonitor();
startSALMonitor(ctx);
}
pun = interfaceOptions3Value.getPun();
// TODO: add support for localsal
// localsal = interfaceOptions3Value.getLocalSal();
pcn = interfaceOptions3Value.getPcn();
buildCBusOptions();
acknowledger.run();
return;
case BAUD_RATE_SELECTOR:
BaudRateSelector baudRateSelector = ((ParameterValueBaudRateSelector) calDataWrite.getParameterValue()).getValue();
// TODO: add support for baudrate
// baudrate = baudRateSelector.getValue();
buildCBusOptions();
acknowledger.run();
return;
case INTERFACE_OPTIONS_1_POWER_UP_SETTINGS:
InterfaceOptions1 interfaceOptions1PowerUpSettings = ((ParameterValueInterfaceOptions1PowerUpSettings) calDataWrite.getParameterValue()).getValue().getInterfaceOptions1();
idmon = interfaceOptions1PowerUpSettings.getIdmon();
monitor = interfaceOptions1PowerUpSettings.getMonitor();
if (monitor) startMMIMonitor(ctx);
else stopMMIMonitor();
smart = interfaceOptions1PowerUpSettings.getSmart();
srchk = interfaceOptions1PowerUpSettings.getSrchk();
// TODO: add support for xonxoff
// xonxoff = interfaceOptions1PowerUpSettings.getXonXoff();
connect = interfaceOptions1PowerUpSettings.getConnect();
if (connect) startSALMonitor(ctx);
else stopSALMonitor();
buildCBusOptions();
acknowledger.run();
return;
case CUSTOM_MANUFACTURER:
// TODO: handle other param typed
acknowledger.run();
return;
case SERIAL_NUMBER:
// TODO: handle other param typed
acknowledger.run();
return;
case CUSTOM_TYPE:
// TODO: handle other param typed
acknowledger.run();
return;
default:
throw new IllegalStateException("Unmapped type");
}
} else {
throw new IllegalStateException("Unmapped type: " + calData.getClass());
}
}

private static CBusMessage createCBusMessageForReply(Alpha alpha, CALReply calReply, CBusOptions cBusOptions) {
EncodedReply encodedReply = new EncodedReplyCALReply((byte) 0x0, calReply, CBusServerAdapter.cBusOptions, CBusServerAdapter.requestContext);
ReplyEncodedReply replyEncodedReply = new ReplyEncodedReply((byte) 0xC0, encodedReply, null, CBusServerAdapter.cBusOptions, CBusServerAdapter.requestContext);
Expand Down Expand Up @@ -590,8 +605,7 @@ private static void handleSmartConnect(RequestSmartConnectShortcut requestSmartC
buildCBusOptions();
}

private void handleReset(RequestReset requestReset) {
LOGGER.info("Handling RequestReset\n{}", requestReset);
private void handleReset() {
connect = false;
smart = false;
idmon = false;
Expand Down

0 comments on commit 9878491

Please sign in to comment.