org.bidib.wizard.simulation.OneSTSimulator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bidibwizard-simulation Show documentation
Show all versions of bidibwizard-simulation Show documentation
jBiDiB BiDiB Wizard Simulation POM
The newest version!
package org.bidib.wizard.simulation;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.commons.lang3.StringUtils;
import org.bidib.jbidibc.messages.AddressData;
import org.bidib.jbidibc.messages.BidibLibrary;
import org.bidib.jbidibc.messages.BidibPort;
import org.bidib.jbidibc.messages.Feature;
import org.bidib.jbidibc.messages.FeedbackAddressData;
import org.bidib.jbidibc.messages.LcConfigX;
import org.bidib.jbidibc.messages.LcMacro;
import org.bidib.jbidibc.messages.enums.AddressTypeEnum;
import org.bidib.jbidibc.messages.enums.EnrailmentDirectionEnum;
import org.bidib.jbidibc.messages.enums.LcMacroOperationCode;
import org.bidib.jbidibc.messages.enums.LcMacroState;
import org.bidib.jbidibc.messages.enums.LcOutputType;
import org.bidib.jbidibc.messages.enums.PortModelEnum;
import org.bidib.jbidibc.messages.enums.SwitchPortEnum;
import org.bidib.jbidibc.messages.exception.ProtocolException;
import org.bidib.jbidibc.messages.message.AccessoryGetMessage;
import org.bidib.jbidibc.messages.message.AccessoryParaGetMessage;
import org.bidib.jbidibc.messages.message.AccessoryParaResponse;
import org.bidib.jbidibc.messages.message.AccessoryParaSetMessage;
import org.bidib.jbidibc.messages.message.AccessorySetMessage;
import org.bidib.jbidibc.messages.message.AccessoryStateResponse;
import org.bidib.jbidibc.messages.message.BidibMessageInterface;
import org.bidib.jbidibc.messages.message.BidibRequestFactory;
import org.bidib.jbidibc.messages.message.FeedbackAddressResponse;
import org.bidib.jbidibc.messages.message.FeedbackConfidenceResponse;
import org.bidib.jbidibc.messages.message.FeedbackGetRangeMessage;
import org.bidib.jbidibc.messages.message.FeedbackMultipleResponse;
import org.bidib.jbidibc.messages.message.LcConfigGetMessage;
import org.bidib.jbidibc.messages.message.LcConfigResponse;
import org.bidib.jbidibc.messages.message.LcConfigSetMessage;
import org.bidib.jbidibc.messages.message.LcConfigXGetAllMessage;
import org.bidib.jbidibc.messages.message.LcConfigXGetMessage;
import org.bidib.jbidibc.messages.message.LcConfigXResponse;
import org.bidib.jbidibc.messages.message.LcConfigXSetMessage;
import org.bidib.jbidibc.messages.message.LcKeyMessage;
import org.bidib.jbidibc.messages.message.LcKeyResponse;
import org.bidib.jbidibc.messages.message.LcMacroGetMessage;
import org.bidib.jbidibc.messages.message.LcMacroHandleMessage;
import org.bidib.jbidibc.messages.message.LcMacroParaGetMessage;
import org.bidib.jbidibc.messages.message.LcMacroParaResponse;
import org.bidib.jbidibc.messages.message.LcMacroParaSetMessage;
import org.bidib.jbidibc.messages.message.LcMacroResponse;
import org.bidib.jbidibc.messages.message.LcMacroSetMessage;
import org.bidib.jbidibc.messages.message.LcMacroStateResponse;
import org.bidib.jbidibc.messages.message.LcNotAvailableResponse;
import org.bidib.jbidibc.messages.message.LcOutputMessage;
import org.bidib.jbidibc.messages.message.LcPortQueryMessage;
import org.bidib.jbidibc.messages.message.LcStatResponse;
import org.bidib.jbidibc.messages.message.VendorGetMessage;
import org.bidib.jbidibc.messages.message.VendorResponse;
import org.bidib.jbidibc.messages.port.BytePortConfigValue;
import org.bidib.jbidibc.messages.port.PortConfigValue;
import org.bidib.jbidibc.messages.utils.ByteUtils;
import org.bidib.jbidibc.messages.utils.MacroUtils;
import org.bidib.jbidibc.messages.utils.NodeUtils;
import org.bidib.jbidibc.simulation.SimulationBidibMessageProcessor;
import org.bidib.jbidibc.simulation.SwitchingFunctionsNode;
import org.bidib.jbidibc.simulation.annotation.BidibNodeSimulator;
import org.bidib.jbidibc.simulation.annotation.BidibNodeSimulators;
import org.bidib.jbidibc.simulation.nodes.InputPortType;
import org.bidib.jbidibc.simulation.nodes.PortType;
import org.bidib.jbidibc.simulation.nodes.ServoPortType;
import org.bidib.jbidibc.simulation.nodes.SwitchPortType;
import org.bidib.wizard.model.ports.FeedbackPort;
import org.bidib.wizard.model.ports.InputPort;
import org.bidib.wizard.model.ports.Port;
import org.bidib.wizard.model.ports.ServoPort;
import org.bidib.wizard.model.ports.SwitchPort;
import org.bidib.wizard.model.status.FeedbackPortStatus;
import org.bidib.wizard.model.status.InputPortStatus;
import org.bidib.wizard.model.status.SwitchPortStatus;
import org.bidib.wizard.simulation.events.InputPortStatusEvent;
import org.bidib.wizard.simulation.events.ServoPortStatusEvent;
import org.bidib.wizard.simulation.events.SwitchPortStatusEvent;
import org.bidib.wizard.simulation.macro.MacroContainer;
import org.bushe.swing.event.EventBus;
import org.bushe.swing.event.annotation.AnnotationProcessor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@BidibNodeSimulators({ @BidibNodeSimulator(vid = "13", pid = "121"), @BidibNodeSimulator(vid = "13", pid = "130"),
@BidibNodeSimulator(vid = "251", pid = "222") })
public class OneSTSimulator extends LightControlSimulator implements SwitchingFunctionsNode {
private static final Logger LOGGER = LoggerFactory.getLogger(OneSTSimulator.class);
private static final String SIMULATION_PANEL_CLASS =
"org.bidib.wizard.simulation.client.view.panel.LightControlPanel";
private final Map feedbackPorts = new HashMap();
private final AtomicBoolean statusFreeze = new AtomicBoolean();
private final AtomicBoolean statusValid = new AtomicBoolean();
private final AtomicBoolean statusSignal = new AtomicBoolean();
public OneSTSimulator(byte[] nodeAddress, long uniqueId, boolean autoAddFeature,
SimulationBidibMessageProcessor messageReceiver, final BidibRequestFactory bidibRequestFactory) {
super(nodeAddress, uniqueId, autoAddFeature, messageReceiver, bidibRequestFactory);
}
@Override
public String getSimulationPanelClass() {
return SIMULATION_PANEL_CLASS;
}
private void setupFeedbackPorts() {
for (int id = 0; id < 16; id++) {
FeedbackPort port = new FeedbackPort();
port.setId(id);
// port.setStatus(id % 3 == 0 ? FeedbackPortStatus.FREE : FeedbackPortStatus.OCCUPIED);
port.setStatus(FeedbackPortStatus.FREE);
feedbackPorts.put(id, port);
}
}
@Override
public void start() {
LOGGER.info("Start the simulator for address: {}", getAddress());
AnnotationProcessor.process(this);
// prepare the input ports
setupInputPorts();
// prepare the switch ports
setupSwitchPorts();
// prepare the servo ports
setupServoPorts();
// prepare the feedback ports
setupFeedbackPorts();
super.start();
}
@Override
public void stop() {
AnnotationProcessor.unprocess(this);
super.stop();
}
@Override
protected void configureServoPort(final ServoPort port) {
Map> portConfig = new LinkedHashMap<>();
portConfig.put(BidibLibrary.BIDIB_PCFG_SERVO_ADJ_L, new BytePortConfigValue(ByteUtils.getLowByte(20)));
portConfig.put(BidibLibrary.BIDIB_PCFG_SERVO_ADJ_H, new BytePortConfigValue(ByteUtils.getLowByte(150)));
portConfig.put(BidibLibrary.BIDIB_PCFG_SERVO_SPEED, new BytePortConfigValue(ByteUtils.getLowByte(4)));
portConfig.put(BidibLibrary.BIDIB_PCFG_SERVO_EXTRA, new BytePortConfigValue(ByteUtils.getLowByte(0)));
portConfig.put(BidibLibrary.BIDIB_PCFG_MOVE_TYPE, new BytePortConfigValue(ByteUtils.getLowByte(0x12)));
port.setPortConfigX(portConfig);
}
@Override
protected byte[] prepareResponse(BidibMessageInterface bidibMessage) {
byte[] response = null;
switch (ByteUtils.getInt(bidibMessage.getType())) {
case BidibLibrary.MSG_LC_OUTPUT:
response = processLcOutputRequest(bidibMessage);
break;
case BidibLibrary.MSG_LC_CONFIG_GET:
response = processLcConfigGetRequest(bidibMessage);
break;
case BidibLibrary.MSG_LC_CONFIG_SET:
response = processLcConfigSetRequest(bidibMessage);
break;
case BidibLibrary.MSG_LC_KEY_QUERY:
response = processLcKeyQueryRequest(bidibMessage);
break;
case BidibLibrary.MSG_LC_PORT_QUERY:
response = processLcPortQueryRequest(bidibMessage);
break;
case BidibLibrary.MSG_LC_CONFIGX_SET:
response = processLcConfigXSetRequest(bidibMessage);
break;
case BidibLibrary.MSG_LC_CONFIGX_GET:
response = processLcConfigXGetRequest(bidibMessage);
break;
case BidibLibrary.MSG_LC_CONFIGX_GET_ALL:
processLcConfigXGetAllRequest(bidibMessage);
break;
case BidibLibrary.MSG_LC_MACRO_HANDLE:
response = processLcMacroHandleRequest(bidibMessage);
break;
case BidibLibrary.MSG_LC_MACRO_PARA_GET:
response = processLcMacroParaGetRequest(bidibMessage);
break;
case BidibLibrary.MSG_LC_MACRO_PARA_SET:
response = processLcMacroParaSetRequest(bidibMessage);
break;
case BidibLibrary.MSG_LC_MACRO_GET:
response = processLcMacroGetRequest(bidibMessage);
break;
case BidibLibrary.MSG_LC_MACRO_SET:
response = processLcMacroSetRequest(bidibMessage);
break;
case BidibLibrary.MSG_ACCESSORY_SET:
response = processAccessorySetRequest(bidibMessage);
break;
case BidibLibrary.MSG_ACCESSORY_GET:
response = processAccessoryGetRequest(bidibMessage);
break;
case BidibLibrary.MSG_ACCESSORY_PARA_SET:
response = processAccessoryParaSetRequest(bidibMessage);
break;
case BidibLibrary.MSG_ACCESSORY_PARA_GET:
response = processAccessoryParaGetRequest(bidibMessage);
break;
//
case BidibLibrary.MSG_BM_GET_RANGE:
response = processBmGetRangeRequest(bidibMessage);
break;
case BidibLibrary.MSG_BM_MIRROR_MULTIPLE:
processBmMirrorMultipleRequest(bidibMessage);
break;
case BidibLibrary.MSG_BM_ADDR_GET_RANGE:
processBmAddrGetRangeRequest(bidibMessage);
break;
case BidibLibrary.MSG_BM_GET_CONFIDENCE:
response = processBmGetConfidenceRequest(bidibMessage);
break;
default:
response = super.prepareResponse(bidibMessage);
break;
}
return response;
}
@Override
protected byte[] processBmGetRangeRequest(BidibMessageInterface bidibMessage) {
LOGGER.info("Process the FeedbackGetRangeMessage: {}", bidibMessage);
byte[] response = null;
try {
FeedbackGetRangeMessage feedbackGetRangeMessage = (FeedbackGetRangeMessage) bidibMessage;
int baseAddress = feedbackGetRangeMessage.getBeginRange();
int end = feedbackGetRangeMessage.getEndRange();
int feedbackSize =
ByteUtils.getLowByte(feedbackGetRangeMessage.getEndRange() - feedbackGetRangeMessage.getBeginRange());
byte value = 0x00;
int index = 0;
byte[] feedbackMultiple = new byte[feedbackSize / 8];
int position = feedbackMultiple.length;
for (int portNum = end; portNum > baseAddress; portNum--) {
value = (byte) ((value & 0xFF) << 1);
FeedbackPort fbp = feedbackPorts.get(portNum - 1);
byte status = 0;
if (fbp != null) {
status = ByteUtils.getLowByte(fbp.getStatus().getType().getType(), 0x01);
}
value |= status;
index++;
if (index > 7) {
feedbackMultiple[position - 1] = value;
value = 0;
index = 0;
position--;
}
}
LOGGER.info("Prepared feedback multiple: {}", ByteUtils.bytesToHex(feedbackMultiple));
FeedbackMultipleResponse feedbackMultipleResponse =
new FeedbackMultipleResponse(bidibMessage.getAddr(), getNextSendNum(),
ByteUtils.getLowByte(baseAddress), ByteUtils.getLowByte(feedbackSize), feedbackMultiple);
response = feedbackMultipleResponse.getContent();
}
catch (ProtocolException ex) {
LOGGER.warn("Create feedbackMultiple response failed.", ex);
}
return response;
}
@Override
protected void processBmMirrorMultipleRequest(BidibMessageInterface bidibMessage) {
LOGGER.info("Process the FeedbackMirrorMultipleMessage: {}, do nothing ...", bidibMessage);
}
protected void processBmAddrGetRangeRequest(BidibMessageInterface bidibMessage) {
try {
for (FeedbackPort port : feedbackPorts.values()) {
int detectorNumber = port.getId();
List bidibAddresses = new ArrayList<>();
List addresses = port.getAddresses();
for (FeedbackAddressData addressData : addresses) {
final EnrailmentDirectionEnum enrailmentDirection = addressData.getType();
AddressTypeEnum addressType = null;
switch (enrailmentDirection) {
case LOCOMOTIVE_LEFT:
case LOCOMOTIVE_RIGHT:
addressType = AddressTypeEnum.LOCOMOTIVE_FORWARD;
break;
case BASIC_ACCESSORY:
addressType = AddressTypeEnum.ACCESSORY;
break;
case EXTENDED_ACCESSORY:
addressType = AddressTypeEnum.EXTENDED_ACCESSORY;
break;
default:
break;
}
AddressData bidibAddress = new AddressData(addressData.getAddress(), addressType);
bidibAddresses.add(bidibAddress);
}
FeedbackAddressResponse feedbackAddressResponse =
new FeedbackAddressResponse(bidibMessage.getAddr(), getNextSendNum(), detectorNumber,
bidibAddresses);
byte[] response = feedbackAddressResponse.getContent();
LOGGER.info("Prepare feedbackAddressResponse: {}", ByteUtils.bytesToHex(response));
sendSpontanousResponse(response);
}
}
catch (ProtocolException ex) {
LOGGER.warn("Create feedbackAddress response failed.", ex);
}
}
@Override
protected byte[] processBmGetConfidenceRequest(BidibMessageInterface bidibMessage) {
byte[] response = null;
try {
byte valid = (byte) (statusValid.get() ? 1 : 0);
byte freeze = (byte) (statusFreeze.get() ? 1 : 0);
byte signal = (byte) (statusSignal.get() ? 1 : 0);
// TODO if more than a single GBM16T is attached we must set more bits? See 4.7.4. Uplink: Nachrichten für
// Belegtmelder --> MSG_BM_CONFIDENCE
// Test with real system: See MainMessageListener.confidence()
FeedbackConfidenceResponse feedbackConfidenceResponse =
new FeedbackConfidenceResponse(bidibMessage.getAddr(), getNextSendNum(), valid, freeze, signal);
response = feedbackConfidenceResponse.getContent();
}
catch (ProtocolException ex) {
LOGGER.warn("Create feedbackConfidence response failed.", ex);
}
return response;
}
@Override
protected byte[] processLcOutputRequest(BidibMessageInterface bidibMessage) {
LOGGER.info("Process the LcOutput request: {}", bidibMessage);
byte[] response = null;
try {
LcOutputMessage lcOutputMessage = (LcOutputMessage) bidibMessage;
LcOutputType outputType = lcOutputMessage.getOutputType(getPortModel());
int outputNumber = lcOutputMessage.getOutputNumber(getPortModel());
byte outputStatus = lcOutputMessage.getOutputStatus();
Port> port = null;
switch (outputType) {
case SWITCHPORT:
SwitchPort switchPort = switchPorts.get(Integer.valueOf(outputNumber));
switchPort.setStatus(SwitchPortStatus.valueOf(SwitchPortEnum.valueOf(outputStatus)));
port = switchPort;
break;
case SERVOPORT:
ServoPort servoPort = servoPorts.get(Integer.valueOf(outputNumber));
servoPort.setValue(ByteUtils.getInteger(outputStatus));
port = servoPort;
break;
default:
LOGGER.warn("LcOutput request for unsupported port type detected: {}", outputType);
break;
}
BidibPort bidibPort = prepareBidibPort(getPortModel(), outputType, outputNumber);
if (port != null) {
LcStatResponse lcStatResponse =
new LcStatResponse(bidibMessage.getAddr(), getNextSendNum(), bidibPort,
lcOutputMessage.getOutputStatus());
response = lcStatResponse.getContent();
}
else {
LcNotAvailableResponse lcNotAvailableResponse =
new LcNotAvailableResponse(bidibMessage.getAddr(), getNextSendNum(), bidibPort);
response = lcNotAvailableResponse.getContent();
}
if (port != null) {
switch (outputType) {
case SWITCHPORT:
publishSwitchPortChange(port);
break;
case SERVOPORT:
publishServoPortChange(port);
break;
default:
break;
}
}
}
catch (ProtocolException ex) {
LOGGER.warn("Create LcStat response failed.", ex);
}
return response;
}
private void publishSwitchPortChange(Port> port) {
SwitchPort switchPort = (SwitchPort) port;
SwitchPortStatus status = switchPort.getStatus();
LOGGER.info("The switchport status has changed, notify the listeners, nodeAddress: {}", nodeAddress);
EventBus.publish(new SwitchPortStatusEvent(NodeUtils.formatAddress(nodeAddress), switchPort.getId(), status));
}
private void publishServoPortChange(Port> port) {
ServoPort servoPort = (ServoPort) port;
Integer value = servoPort.getValue();
LOGGER.info("The servoport status has changed, notify the listeners, nodeAddress: {}", nodeAddress);
EventBus.publish(new ServoPortStatusEvent(NodeUtils.formatAddress(nodeAddress), servoPort.getId(), value));
}
@Override
protected byte[] processLcConfigGetRequest(BidibMessageInterface bidibMessage) {
LOGGER.info("Process the LcConfigGet request: {}", bidibMessage);
byte[] response = null;
try {
LcConfigGetMessage lcConfigGetMessage = (LcConfigGetMessage) bidibMessage;
LcOutputType outputType = lcConfigGetMessage.getPortType(getPortModel());
int outputNumber = lcConfigGetMessage.getPortNumber(getPortModel());
Port> port = null;
switch (outputType) {
case SWITCHPORT:
port = switchPorts.get(Integer.valueOf(outputNumber));
break;
case SERVOPORT:
if (outputNumber != 1) { // test
port = servoPorts.get(Integer.valueOf(outputNumber));
}
break;
default:
LOGGER.warn("LcConfigGet request for unsupported port type detected: {}", outputType);
break;
}
BidibPort bidibPort = prepareBidibPort(getPortModel(), outputType, outputNumber);
if (port != null) {
LcConfigResponse lcConfigResponse =
new LcConfigResponse(bidibMessage.getAddr(), getNextSendNum(), bidibPort, port.getPortConfig());
response = lcConfigResponse.getContent();
}
else {
LcNotAvailableResponse lcNotAvailableResponse =
new LcNotAvailableResponse(bidibMessage.getAddr(), getNextSendNum(), bidibPort);
response = lcNotAvailableResponse.getContent();
}
}
catch (ProtocolException ex) {
LOGGER.warn("Create LcConfig response failed.", ex);
}
return response;
}
@Override
protected byte[] processLcConfigSetRequest(BidibMessageInterface bidibMessage) {
LOGGER.info("Process the LcConfigSet request: {}", bidibMessage);
byte[] response = null;
try {
LcConfigSetMessage lcConfigSetMessage = (LcConfigSetMessage) bidibMessage;
LcOutputType outputType = lcConfigSetMessage.getPortType(getPortModel());
int outputNumber = lcConfigSetMessage.getPortNumber(getPortModel());
Port> port = null;
switch (outputType) {
case SWITCHPORT:
SwitchPort switchPort = switchPorts.get(Integer.valueOf(outputNumber));
if (switchPort != null) {
switchPort.setPortConfig(lcConfigSetMessage.getPortConfig());
port = switchPort;
}
else {
LOGGER.warn("Switchport not available, outputNumber: {}", outputNumber);
}
break;
case SERVOPORT:
ServoPort servoPort = servoPorts.get(Integer.valueOf(outputNumber));
if (servoPort != null && outputNumber != 1) {
servoPort.setPortConfig(lcConfigSetMessage.getPortConfig());
port = servoPort;
}
else {
LOGGER.warn("Servoport not available, outputNumber: {}", outputNumber);
}
break;
default:
LOGGER.warn("LcConfigSet request for unsupported port type detected: {}", outputType);
break;
}
BidibPort bidibPort = prepareBidibPort(getPortModel(), outputType, outputNumber);
if (port != null) {
LcConfigResponse magicResponse =
new LcConfigResponse(bidibMessage.getAddr(), getNextSendNum(), bidibPort, port.getPortConfig());
response = magicResponse.getContent();
}
else {
LOGGER.warn("No port assigned!");
LcNotAvailableResponse magicResponse =
new LcNotAvailableResponse(bidibMessage.getAddr(), getNextSendNum(), bidibPort);
response = magicResponse.getContent();
}
}
catch (ProtocolException ex) {
LOGGER.warn("Create LcStat response failed.", ex);
}
return response;
}
@Override
protected byte[] processLcConfigXSetRequest(BidibMessageInterface bidibMessage) {
LOGGER.info("Process the LcConfigXSet request: {}", bidibMessage);
byte[] response = null;
try {
LcConfigXSetMessage lcConfigXSetMessage = (LcConfigXSetMessage) bidibMessage;
int outputNumber = lcConfigXSetMessage.getPortNumber(getPortModel());
LcOutputType outputType = lcConfigXSetMessage.getPortType(getPortModel());
Port> port = null;
switch (outputType) {
case SWITCHPORT:
LOGGER.info("Set the switch port config, outputNumber: {}", outputNumber);
SwitchPort switchPort = switchPorts.get(Integer.valueOf(outputNumber));
if (switchPort != null) {
switchPort.setPortConfigX(lcConfigXSetMessage.getLcConfigX(messageLogger).getPortConfig());
port = switchPort;
}
else {
LOGGER.warn("Switchport not available, outputNumber: {}", outputNumber);
}
break;
case SERVOPORT:
ServoPort servoPort = servoPorts.get(Integer.valueOf(outputNumber));
if (servoPort != null) {
servoPort.setPortConfigX(lcConfigXSetMessage.getLcConfigX(messageLogger).getPortConfig());
port = servoPort;
}
else {
LOGGER.warn("Servoport not available, outputNumber: {}", servoPort);
}
break;
default:
LOGGER.warn("LcConfigSet request for unsupported port type detected: {}", outputType);
break;
}
BidibPort bidibPort = prepareBidibPort(getPortModel(), outputType, outputNumber);
if (port != null) {
LcConfigX lcConfigX =
new LcConfigX(bidibPort, lcConfigXSetMessage.getLcConfigX(messageLogger).getPortConfig());
LcConfigXResponse lcConfigXResponse =
new LcConfigXResponse(bidibMessage.getAddr(), getNextSendNum(),
LcConfigX.getCodedPortConfig(null, lcConfigX, getPortModel()));
// byte[] content =
// ByteUtils.concat(new byte[] { outputType.getType(), ByteUtils.getLowByte(outputNumber) },
// MessageUtils.getCodedPortConfig(lcConfigXSetMessage.getLcConfigX(), getPortModel()));
//
// LcConfigXResponse lcConfigXResponse =
// new LcConfigXResponse(bidibMessage.getAddr(), getNextSendNum(), content);
response = lcConfigXResponse.getContent();
}
else {
LOGGER.warn("No port assigned!");
LcNotAvailableResponse magicResponse =
new LcNotAvailableResponse(bidibMessage.getAddr(), getNextSendNum(), bidibPort);
response = magicResponse.getContent();
}
}
catch (ProtocolException ex) {
LOGGER.warn("Create LcConfigX response failed.", ex);
}
return response;
}
@Override
protected byte[] processLcConfigXGetRequest(BidibMessageInterface bidibMessage) {
LOGGER.info("Process the LcConfigXGet request: {}", bidibMessage);
byte[] response = null;
try {
LcConfigXGetMessage lcConfigXGetMessage = (LcConfigXGetMessage) bidibMessage;
int outputNumber = lcConfigXGetMessage.getPortNumber(getPortModel());
LcOutputType outputType = lcConfigXGetMessage.getPortType(getPortModel());
Port> port = null;
// byte[] portConfig = null;
Map> values = new LinkedHashMap<>();
switch (outputType) {
case SWITCHPORT:
port = switchPorts.get(Integer.valueOf(outputNumber));
SwitchPort switchPort = (SwitchPort) port;
values
.put(BidibLibrary.BIDIB_PCFG_TICKS,
new BytePortConfigValue(ByteUtils.getLowByte(switchPort.getSwitchOffTime())));
values
.put(BidibLibrary.BIDIB_PCFG_SWITCH_CTRL,
new BytePortConfigValue(switchPort.getOutputBehaviour().getType()));
break;
case SERVOPORT:
port = servoPorts.get(Integer.valueOf(outputNumber));
ServoPort servoPort = (ServoPort) port;
values
.put(BidibLibrary.BIDIB_PCFG_SERVO_SPEED,
new BytePortConfigValue(ByteUtils.getLowByte(servoPort.getSpeed())));
values
.put(BidibLibrary.BIDIB_PCFG_SERVO_ADJ_L,
new BytePortConfigValue(ByteUtils.getLowByte(servoPort.getTrimDown())));
values
.put(BidibLibrary.BIDIB_PCFG_SERVO_ADJ_H,
new BytePortConfigValue(ByteUtils.getLowByte(servoPort.getTrimUp())));
if (servoPort.getServoExtra() != null) {
values
.put(BidibLibrary.BIDIB_PCFG_SERVO_EXTRA,
new BytePortConfigValue(ByteUtils.getLowByte(servoPort.getServoExtra())));
}
if (servoPort.isPortConfigKeySupported(BidibLibrary.BIDIB_PCFG_MOVE_TYPE)) {
values
.put(BidibLibrary.BIDIB_PCFG_MOVE_TYPE,
new BytePortConfigValue(ByteUtils.getLowByte(servoPort.prepareMoveType())));
}
break;
default:
LOGGER.warn("LcConfigGet request for unsupported port type detected: {}", outputType);
break;
}
LOGGER.info("Return config of port: {}", port);
BidibPort bidibPort = prepareBidibPort(getPortModel(), outputType, outputNumber);
LcConfigX lcConfigX = new LcConfigX(bidibPort, values);
LcConfigXResponse lcConfigXResponse =
new LcConfigXResponse(bidibMessage.getAddr(), getNextSendNum(),
LcConfigX.getCodedPortConfig(null, lcConfigX, getPortModel()));
response = lcConfigXResponse.getContent();
}
catch (ProtocolException ex) {
LOGGER.warn("Create LcConfigX response failed.", ex);
}
return response;
}
@Override
protected void processLcConfigXGetAllRequest(BidibMessageInterface bidibMessage) {
LOGGER.info("Process the LcConfigXGetAll request: {}", bidibMessage);
byte[] response = null;
try {
LcConfigXGetAllMessage lcConfigXGetAllMessage = (LcConfigXGetAllMessage) bidibMessage;
LcOutputType outputType = lcConfigXGetAllMessage.getPortTypeFrom(getPortModel());
// TODO evaluate port type/range to
Map> values = new LinkedHashMap<>();
if (outputType != null) {
LOGGER.info("Get all ports for output type: {}", outputType);
switch (outputType) {
case SERVOPORT:
for (ServoPort servoPort : servoPorts.values()) {
values.clear();
values.putAll(servoPort.getPortConfigX());
LOGGER.info("Return config of servo port: {}", servoPort);
BidibPort bidibPort = prepareBidibPort(getPortModel(), outputType, servoPort.getId());
LcConfigX lcConfigX = new LcConfigX(bidibPort, values);
LcConfigXResponse lcConfigXResponse =
new LcConfigXResponse(bidibMessage.getAddr(), getNextSendNum(),
LcConfigX.getCodedPortConfig(null, lcConfigX, getPortModel()));
response = lcConfigXResponse.getContent();
LOGGER.info("Prepared lcConfigXResponse: {}", ByteUtils.bytesToHex(response));
sendSpontanousResponse(response);
response = null;
}
break;
case SWITCHPORT:
for (SwitchPort switchPort : switchPorts.values()) {
values.clear();
values.putAll(switchPort.getPortConfigX());
LOGGER.info("Return config of switch port: {}", switchPort);
BidibPort bidibPort = prepareBidibPort(getPortModel(), outputType, switchPort.getId());
LcConfigX lcConfigX = new LcConfigX(bidibPort, values);
LcConfigXResponse lcConfigXResponse =
new LcConfigXResponse(bidibMessage.getAddr(), getNextSendNum(),
LcConfigX.getCodedPortConfig(null, lcConfigX, getPortModel()));
response = lcConfigXResponse.getContent();
LOGGER.info("Prepared lcConfigXResponse: {}", ByteUtils.bytesToHex(response));
sendSpontanousResponse(response);
response = null;
}
break;
case INPUTPORT:
for (InputPort inputPort : inputPorts.values()) {
values.clear();
values.putAll(inputPort.getPortConfigX());
LOGGER.info("Return config of input port: {}", inputPort);
BidibPort bidibPort = prepareBidibPort(getPortModel(), outputType, inputPort.getId());
LcConfigX lcConfigX = new LcConfigX(bidibPort, values);
LcConfigXResponse lcConfigXResponse =
new LcConfigXResponse(bidibMessage.getAddr(), getNextSendNum(),
LcConfigX.getCodedPortConfig(null, lcConfigX, getPortModel()));
response = lcConfigXResponse.getContent();
LOGGER.info("Prepared lcConfigXResponse: {}", ByteUtils.bytesToHex(response));
sendSpontanousResponse(response);
response = null;
}
break;
default:
LOGGER.warn("Unsupported port type requested: {}", outputType);
break;
}
}
else {
// deliver servo ports
for (ServoPort servoPort : servoPorts.values()) {
values.clear();
values.putAll(servoPort.getPortConfigX());
LOGGER.info("Return config of servo port: {}", servoPort);
BidibPort bidibPort = prepareBidibPort(getPortModel(), LcOutputType.SERVOPORT, servoPort.getId());
LcConfigX lcConfigX = new LcConfigX(bidibPort, values);
LcConfigXResponse lcConfigXResponse =
new LcConfigXResponse(bidibMessage.getAddr(), getNextSendNum(),
LcConfigX.getCodedPortConfig(null, lcConfigX, getPortModel()));
response = lcConfigXResponse.getContent();
LOGGER.info("Prepared lcConfigXResponse: {}", ByteUtils.bytesToHex(response));
sendSpontanousResponse(response);
response = null;
}
// deliver switch ports
for (SwitchPort switchPort : switchPorts.values()) {
values.clear();
values.putAll(switchPort.getPortConfigX());
LOGGER.info("Return config of switch port: {}", switchPort);
BidibPort bidibPort = prepareBidibPort(getPortModel(), LcOutputType.SWITCHPORT, switchPort.getId());
LcConfigX lcConfigX = new LcConfigX(bidibPort, values);
LcConfigXResponse lcConfigXResponse =
new LcConfigXResponse(bidibMessage.getAddr(), getNextSendNum(),
LcConfigX.getCodedPortConfig(null, lcConfigX, getPortModel()));
response = lcConfigXResponse.getContent();
LOGGER.info("Prepared lcConfigXResponse: {}", ByteUtils.bytesToHex(response));
sendSpontanousResponse(response);
response = null;
}
// deliver input ports
for (InputPort inputPort : inputPorts.values()) {
values.clear();
values.putAll(inputPort.getPortConfigX());
LOGGER.info("Return config of input port: {}", inputPort);
BidibPort bidibPort = prepareBidibPort(getPortModel(), LcOutputType.INPUTPORT, inputPort.getId());
LcConfigX lcConfigX = new LcConfigX(bidibPort, values);
LcConfigXResponse lcConfigXResponse =
new LcConfigXResponse(bidibMessage.getAddr(), getNextSendNum(),
LcConfigX.getCodedPortConfig(null, lcConfigX, getPortModel()));
response = lcConfigXResponse.getContent();
LOGGER.info("Prepared lcConfigXResponse: {}", ByteUtils.bytesToHex(response));
sendSpontanousResponse(response);
response = null;
}
}
}
catch (ProtocolException ex) {
LOGGER.warn("Create lcConfigXResponse response failed.", ex);
}
}
@Override
protected byte[] processLcKeyQueryRequest(BidibMessageInterface bidibMessage) {
LOGGER.info("Process the LcKeyQuery request: {}", bidibMessage);
byte[] response = null;
byte keyState = 0;
Port> port = null;
try {
LcKeyMessage lcKeyMessage = (LcKeyMessage) bidibMessage;
int portNumber = lcKeyMessage.getBidibPort().getPortNumber(PortModelEnum.type);
port = inputPorts.get(portNumber);
keyState = port.getStatus().getType().getType();
LcKeyResponse lcKeyResponse =
new LcKeyResponse(bidibMessage.getAddr(), getNextSendNum(), ByteUtils.getLowByte(portNumber), keyState);
response = lcKeyResponse.getContent();
}
catch (ProtocolException ex) {
LOGGER.warn("Create LcKey response failed.", ex);
}
if (port != null) {
publishInputPortChange(port);
}
return response;
}
private void publishInputPortChange(Port> port) {
InputPort inputPort = (InputPort) port;
InputPortStatus status = inputPort.getStatus();
LOGGER.info("The inputport status has changed, notify the listeners, nodeAddress: {}", nodeAddress);
EventBus.publish(new InputPortStatusEvent(NodeUtils.formatAddress(nodeAddress), inputPort.getId(), status));
}
@Override
protected byte[] processLcPortQueryRequest(BidibMessageInterface bidibMessage) {
LOGGER.info("Process the LcPortQuery request: {}", bidibMessage);
byte[] response = null;
byte portState = 0;
try {
final LcPortQueryMessage lcPortQueryMessage = (LcPortQueryMessage) bidibMessage;
LcOutputType outputType = lcPortQueryMessage.getPortType(getPortModel());
int portNumber = lcPortQueryMessage.getPortNumber(getPortModel());
switch (outputType) {
case INPUTPORT:
portState = inputPorts.get(portNumber).getStatus().getType().getType();
break;
case SWITCHPORT:
portState = switchPorts.get(portNumber).getStatus().getType().getType();
break;
case SERVOPORT:
portState = ByteUtils.getLowByte(servoPorts.get(portNumber).getValue());
break;
default:
LOGGER.warn("LcOutputQuery for unsupported port type detected: {}", outputType);
break;
}
BidibPort bidibPort = prepareBidibPort(getPortModel(), outputType, portNumber);
LcStatResponse lcStatResponse =
new LcStatResponse(bidibMessage.getAddr(), getNextSendNum(), bidibPort, portState);
response = lcStatResponse.getContent();
}
catch (ProtocolException ex) {
LOGGER.warn("Create LcStat response failed.", ex);
}
return response;
}
@Override
protected byte[] processLcMacroParaGetRequest(BidibMessageInterface bidibMessage) {
LOGGER.info("Process the LcMacroParaGet request: {}", bidibMessage);
byte[] response = null;
try {
LcMacroParaGetMessage lcMacroParaGetMessage = (LcMacroParaGetMessage) bidibMessage;
int macroNumber = lcMacroParaGetMessage.getMacroNumber();
int paramId = lcMacroParaGetMessage.getParameterIndex();
LOGGER.info("Process macroNumber: {}, paramId: {}", macroNumber, paramId);
// return the stored parameter value
MacroContainer container = macros.get(macroNumber);
if (container == null) {
LOGGER.info("Create new MacroContainer for macro number: {}", macroNumber);
// initialize a new macro container
container = new MacroContainer(macroNumber);
macros.put(macroNumber, container);
}
byte[] parameter = container.getMacroParameter(paramId);
LcMacroParaResponse lcMacroParaResponse =
new LcMacroParaResponse(bidibMessage.getAddr(), getNextSendNum(), (byte) macroNumber, (byte) paramId,
parameter);
response = lcMacroParaResponse.getContent();
}
catch (ProtocolException ex) {
LOGGER.warn("Create LcMacroPara response failed.", ex);
}
return response;
}
@Override
protected byte[] processLcMacroParaSetRequest(BidibMessageInterface bidibMessage) {
LOGGER.info("Process the LcMacroParaSet request: {}", bidibMessage);
byte[] response = null;
try {
LcMacroParaSetMessage lcMacroParaSetMessage = (LcMacroParaSetMessage) bidibMessage;
int macroNumber = lcMacroParaSetMessage.getMacroNumber();
int paramId = lcMacroParaSetMessage.getParameterIndex();
byte[] parameter = lcMacroParaSetMessage.getValue();
// store the parameter value
MacroContainer container = macros.get(macroNumber);
if (container == null) {
// initialize a new macro container
container = new MacroContainer(macroNumber);
macros.put(macroNumber, container);
}
container.setMacroParameter(paramId, parameter);
LcMacroParaResponse lcMacroParaResponse =
new LcMacroParaResponse(bidibMessage.getAddr(), getNextSendNum(), (byte) macroNumber, (byte) paramId,
parameter);
response = lcMacroParaResponse.getContent();
}
catch (ProtocolException ex) {
LOGGER.warn("Create LcMacroPara response failed.", ex);
}
return response;
}
@Override
protected byte[] processLcMacroHandleRequest(BidibMessageInterface bidibMessage) {
LOGGER.info("Process the LcMacroHandle request: {}", bidibMessage);
byte[] response = null;
try {
LcMacroHandleMessage lcMacroHandleMessage = (LcMacroHandleMessage) bidibMessage;
Integer macroNumber = lcMacroHandleMessage.getMacroNumber();
LcMacroOperationCode lcMacroOperationCode = lcMacroHandleMessage.getMacroOperationCode();
LOGGER
.info("Handle macro request, macroNumber: {}, lcMacroOperationCode: {}", macroNumber,
lcMacroOperationCode);
// TODO store the macro state value
LcMacroState macroState = null;
switch (lcMacroOperationCode) {
case START:
macroState = LcMacroState.RUNNING;
break;
case DELETE:
macroState = LcMacroState.DELETE;
LOGGER.info("Remove macro with number: {}", macroNumber);
macros.remove(macroNumber);
break;
case OFF:
macroState = LcMacroState.OFF;
break;
case RESTORE:
macroState = LcMacroState.RESTORE;
break;
case SAVE:
macroState = LcMacroState.SAVE;
break;
default:
macroState = LcMacroState.NOTEXIST;
break;
}
LcMacroStateResponse lcMacroStateResponse =
new LcMacroStateResponse(bidibMessage.getAddr(), getNextSendNum(), ByteUtils.getLowByte(macroNumber),
macroState);
response = lcMacroStateResponse.getContent();
}
catch (ProtocolException ex) {
LOGGER.warn("Create LcMacroState response failed.", ex);
}
return response;
}
@Override
protected byte[] processLcMacroGetRequest(BidibMessageInterface bidibMessage) {
LOGGER.info("Process the LcMacroGet request: {}", bidibMessage);
byte[] response = null;
try {
LcMacroGetMessage lcMacroGetMessage = (LcMacroGetMessage) bidibMessage;
Integer macroNumber = lcMacroGetMessage.getMacroNumber();
int stepNumber = lcMacroGetMessage.getStep();
// if (stepNumber == 0) {
// LOGGER.warn("Simulate errors.");
// return null;
// }
// fetch the macro content and test if the macro is available
MacroContainer container = macros.get(macroNumber);
if (container == null) {
// initialize a new macro container
container = new MacroContainer(macroNumber);
macros.put(macroNumber, container);
}
LcMacro macroStep = container.getMacroStep(stepNumber);
LcMacro value = macroStep;
LcMacroResponse lcMacroResponse =
new LcMacroResponse(bidibMessage.getAddr(), getNextSendNum(), ByteUtils.getLowByte(macroNumber),
ByteUtils.getLowByte(stepNumber), value);
response = lcMacroResponse.getContent();
}
catch (ProtocolException ex) {
LOGGER.warn("Create LcMacro response failed.", ex);
}
return response;
}
@Override
protected byte[] processLcMacroSetRequest(BidibMessageInterface bidibMessage) {
LOGGER.info("Process the LcMacroSet request: {}", bidibMessage);
byte[] response = null;
try {
LcMacroSetMessage lcMacroSetMessage = (LcMacroSetMessage) bidibMessage;
int macroNumber = lcMacroSetMessage.getMacroNumber();
int stepNumber = lcMacroSetMessage.getStep();
LcMacro macroStep = MacroUtils.getMacro(lcMacroSetMessage.getData());
LOGGER.info("Current macroNumber: {}, stepNumber: {}, macroStep: {}", macroNumber, stepNumber, macroStep);
// fetch the macro content and test if the macro container is available
MacroContainer container = macros.get(macroNumber);
if (container == null) {
container = new MacroContainer(macroNumber);
macros.put(macroNumber, container);
}
container.setMacroStep(stepNumber, macroStep);
try {
LcMacroResponse lcMacroResponse =
new LcMacroResponse(bidibMessage.getAddr(), getNextSendNum(), ByteUtils.getLowByte(macroNumber),
ByteUtils.getLowByte(stepNumber), macroStep);
response = lcMacroResponse.getContent();
}
catch (NullPointerException npe) {
LOGGER.warn("create response failed.", npe);
}
}
catch (ProtocolException ex) {
LOGGER.warn("Create LcMacro response failed.", ex);
}
return response;
}
@Override
protected byte[] processAccessorySetRequest(BidibMessageInterface bidibMessage) {
LOGGER.info("Process the AccessorySet request: {}", bidibMessage);
byte[] response = null;
try {
AccessorySetMessage accessorySetMessage = (AccessorySetMessage) bidibMessage;
int accessoryNumber = accessorySetMessage.getAccessoryNumber();
int aspect = accessorySetMessage.getAspect();
byte[] value = new byte[] { 0, 0, 0 };
AccessoryStateResponse accessoryStateResponse =
new AccessoryStateResponse(bidibMessage.getAddr(), getNextSendNum(), (byte) accessoryNumber,
(byte) aspect, value);
response = accessoryStateResponse.getContent();
}
catch (ProtocolException ex) {
LOGGER.warn("Create AccessoryState response failed.", ex);
}
return response;
}
@Override
protected byte[] processAccessoryGetRequest(BidibMessageInterface bidibMessage) {
LOGGER.info("Process the AccessoryGet request: {}", bidibMessage);
byte[] response = null;
try {
AccessoryGetMessage accessoryGetMessage = (AccessoryGetMessage) bidibMessage;
int accessoryNumber = accessoryGetMessage.getAccessoryNumber();
int aspect = 0;
byte[] value = new byte[] { 0, 0, 0 };
AccessoryStateResponse accessoryStateResponse =
new AccessoryStateResponse(bidibMessage.getAddr(), getNextSendNum(), (byte) accessoryNumber,
(byte) aspect, value);
response = accessoryStateResponse.getContent();
}
catch (ProtocolException ex) {
LOGGER.warn("Create AccessoryState response failed.", ex);
}
return response;
}
@Override
protected byte[] processAccessoryParaSetRequest(BidibMessageInterface bidibMessage) {
LOGGER.info("Process the AccessoryParaSet request: {}", bidibMessage);
byte[] response = null;
try {
AccessoryParaSetMessage accessoryParaSetMessage = (AccessoryParaSetMessage) bidibMessage;
int accessoryNumber = accessoryParaSetMessage.getAccessoryNumber();
int paraNumber = accessoryParaSetMessage.getParaNumber();
byte[] value = accessoryParaSetMessage.getValue();
AccessoryParaResponse accessoryParaResponse =
new AccessoryParaResponse(bidibMessage.getAddr(), getNextSendNum(),
ByteUtils.getLowByte(accessoryNumber), ByteUtils.getLowByte(paraNumber), value);
response = accessoryParaResponse.getContent();
}
catch (ProtocolException ex) {
LOGGER.warn("Create AccessoryPara response failed.", ex);
}
return response;
}
@Override
protected byte[] processAccessoryParaGetRequest(BidibMessageInterface bidibMessage) {
LOGGER.info("Process the AccessoryParaGet request: {}", bidibMessage);
byte[] response = null;
try {
AccessoryParaGetMessage accessoryParaGetMessage = (AccessoryParaGetMessage) bidibMessage;
int accessoryNumber = accessoryParaGetMessage.getAccessoryNumber();
int paraNumber = accessoryParaGetMessage.getParaNumber();
// TODO provide the correct data here ...
byte[] value = new byte[] { 0, 0, 0, 0 };
AccessoryParaResponse accessoryParaResponse =
new AccessoryParaResponse(bidibMessage.getAddr(), getNextSendNum(),
ByteUtils.getLowByte(accessoryNumber), ByteUtils.getLowByte(paraNumber), value);
response = accessoryParaResponse.getContent();
}
catch (ProtocolException ex) {
LOGGER.warn("Create AccessoryPara response failed.", ex);
}
return response;
}
@Override
protected byte[] processVendorGetRequest(BidibMessageInterface bidibMessage) {
byte[] response = null;
try {
VendorGetMessage vendorGetMessage = (VendorGetMessage) bidibMessage;
String vendorDataName = vendorGetMessage.getVendorDataName();
LOGGER.info("Get the vendor data with name: {}", vendorDataName);
// fetch the value from the stored data ...
String vendorDataValue = configurationVariables.get(vendorDataName);
if (StringUtils.isBlank(vendorDataValue)) {
vendorDataValue = "";
}
if ("10001".equals(vendorDataName)) {
Feature feature = getFeature(BidibLibrary.FEATURE_BM_SIZE);
if (feature == null || feature.getValue() == 0) {
LOGGER.info("Skip return result for GBM16T.");
return null;
}
}
VendorResponse vendorResponse =
new VendorResponse(bidibMessage.getAddr(), getNextSendNum(), vendorDataName, vendorDataValue);
response = vendorResponse.getContent();
}
catch (ProtocolException ex) {
LOGGER.warn("Create vendor response failed.", ex);
}
return response;
}
@Override
public void queryStatus(Class> portClass) {
if (InputPort.class.equals(portClass)) {
for (InputPort inputPort : inputPorts.values()) {
publishInputPortChange(inputPort);
}
}
else if (SwitchPort.class.equals(portClass)) {
for (SwitchPort switchPort : switchPorts.values()) {
publishSwitchPortChange(switchPort);
}
}
else if (ServoPort.class.equals(portClass)) {
for (ServoPort servoPort : servoPorts.values()) {
publishServoPortChange(servoPort);
}
}
}
@Override
public void setPortsConfig(PortType portType) {
if (portType == null) {
return;
}
LOGGER.info("Set the port config: {}", portType);
if (portType instanceof SwitchPortType) {
switchPortCount = portType.getCount();
LOGGER.info("Set the number of switchPorts: {}", switchPortCount);
}
else if (portType instanceof InputPortType) {
inputPortCount = portType.getCount();
inputPortOffset = (portType.getOffset() != null ? portType.getOffset() : 0);
}
else if (portType instanceof ServoPortType) {
servoPortCount = portType.getCount();
}
}
}