org.bidib.wizard.api.utils.JsonNodeUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bidibwizard-api Show documentation
Show all versions of bidibwizard-api Show documentation
jBiDiB BiDiB Wizard API POM
package org.bidib.wizard.api.utils;
import java.util.List;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.bidib.api.json.types.NodeAddress;
import org.bidib.api.json.types.NodeInfo;
import org.bidib.api.json.types.node.NodeDetails;
import org.bidib.api.json.types.node.PortCountType;
import org.bidib.api.json.types.node.PortCountType.PortType;
import org.bidib.jbidibc.messages.BidibLibrary;
import org.bidib.jbidibc.messages.Feature;
import org.bidib.jbidibc.messages.StringData;
import org.bidib.jbidibc.messages.utils.ByteUtils;
import org.bidib.jbidibc.messages.utils.NodeUtils;
import org.bidib.wizard.api.model.BoosterNodeInterface;
import org.bidib.wizard.api.model.NodeInterface;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class JsonNodeUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(JsonNodeUtils.class);
public static NodeInfo toNodeInfo(final NodeInterface node) {
NodeInfo ni = new NodeInfo();
ni.setAddress(NodeUtils.formatAddressLong(node.getAddr()));
ni.setManufacturerId(NodeUtils.getVendorId(node.getUniqueId()));
ni.setProductId(NodeUtils.getPid(node.getUniqueId()));
ni.setProductName(node.getNode().getStoredString(StringData.INDEX_PRODUCTNAME));
String userName = node.getNode().getStoredString(StringData.INDEX_USERNAME);
if (StringUtils.isBlank(userName)) {
userName = ByteUtils.getUniqueIdAsString(node.getUniqueId());
}
ni.setUserName(userName);
ni
.setProtocolVersion(
node.getNode().getProtocolVersion() != null ? node.getNode().getProtocolVersion().toString() : "");
ni
.setSoftwareVersion(
node.getNode().getSoftwareVersion() != null ? node.getNode().getSoftwareVersion().toString() : "");
ni.setUniqueId(Long.toString(node.getUniqueId()));
ni.setFormattedUniqueId(ByteUtils.getUniqueIdAsString(node.getUniqueId()));
long uniqueId = node.getUniqueId();
if (NodeUtils.hasAccessoryFunctions(uniqueId)) {
ni.setClassAccessory(Boolean.TRUE);
}
if (NodeUtils.hasBoosterFunctions(uniqueId)) {
ni.setClassBooster(Boolean.TRUE);
}
if (NodeUtils.hasFeedbackFunctions(uniqueId)) {
ni.setClassFeedback(Boolean.TRUE);
}
if (NodeUtils.hasCommandStationFunctions(uniqueId)) {
ni.setClassCsDrive(Boolean.TRUE);
}
if (NodeUtils.hasCommandStationProgrammingFunctions(uniqueId)) {
ni.setClassCsProg(Boolean.TRUE);
}
if (NodeUtils.hasSubNodesFunctions(uniqueId)) {
ni.setClassInterface(Boolean.TRUE);
}
if (NodeUtils.hasSwitchFunctions(uniqueId)) {
ni.setClassSwitch(Boolean.TRUE);
}
LOGGER.info("Current uniqueId: {}", ByteUtils.formatHexUniqueId(Long.parseLong(ni.getUniqueId())));
if (NodeUtils.hasBoosterFunctions(node.getUniqueId())) {
BoosterNodeInterface boosterNode = node.getBoosterNode();
// add booster data
ni.getAdditionalProperties().put("boosterState", boosterNode.getBoosterStatus());
ni.getAdditionalProperties().put("boosterCurrent", boosterNode.getBoosterCurrent());
ni.getAdditionalProperties().put("boosterMaximumCurrent", boosterNode.getBoosterMaximumCurrent());
ni.getAdditionalProperties().put("boosterVoltage", boosterNode.getBoosterVoltage());
ni.getAdditionalProperties().put("boosterTemperature", boosterNode.getBoosterTemperature());
}
return ni;
}
/**
* Create the {@code NodeDetails} from the node information.
*
* @param node
* the node
* @return the node details
*/
public static NodeDetails toNodeDetails(final NodeInterface node) {
long uniqueId = node.getUniqueId();
LOGGER.info("Current uniqueId: {}", ByteUtils.formatHexUniqueId(uniqueId));
NodeDetails details = new NodeDetails();
details.setAddress(NodeUtils.formatAddressLong(node.getAddr()));
details.setUniqueId(Long.toString(uniqueId));
// check for switching
if (NodeUtils.hasSwitchFunctions(uniqueId)) {
// for flat port model we must iterate over the ports
if (node.isFlatPortModel()) {
addPortCount(node, details, node.getAnalogPorts(), PortType.ANALOGPORT);
addPortCount(node, details, node.getBacklightPorts(), PortType.BACKLIGHTPORT);
addPortCount(node, details, node.getInputPorts(), PortType.INPUTPORT);
addPortCount(node, details, node.getLightPorts(), PortType.LIGHTPORT);
addPortCount(node, details, node.getMotorPorts(), PortType.MOTORPORT);
addPortCount(node, details, node.getServoPorts(), PortType.SERVOPORT);
addPortCount(node, details, node.getSoundPorts(), PortType.SOUNDPORT);
addPortCount(node, details, node.getSwitchPorts(), PortType.SWITCHPORT);
addPortCount(node, details, node.getSwitchPairPorts(), PortType.SWITCHPAIRPORT);
}
else {
addPortCount(node, details, BidibLibrary.FEATURE_CTRL_ANALOGOUT_COUNT, PortType.ANALOGPORT);
addPortCount(node, details, BidibLibrary.FEATURE_CTRL_BACKLIGHT_COUNT, PortType.BACKLIGHTPORT);
addPortCount(node, details, BidibLibrary.FEATURE_CTRL_INPUT_COUNT, PortType.INPUTPORT);
addPortCount(node, details, BidibLibrary.FEATURE_CTRL_LIGHT_COUNT, PortType.LIGHTPORT);
addPortCount(node, details, BidibLibrary.FEATURE_CTRL_MOTOR_COUNT, PortType.MOTORPORT);
addPortCount(node, details, BidibLibrary.FEATURE_CTRL_SERVO_COUNT, PortType.SERVOPORT);
addPortCount(node, details, BidibLibrary.FEATURE_CTRL_SOUND_COUNT, PortType.SOUNDPORT);
addPortCount(node, details, BidibLibrary.FEATURE_CTRL_SWITCH_COUNT, PortType.SWITCHPORT);
}
}
// check for feedback
if (NodeUtils.hasFeedbackFunctions(uniqueId)) {
// feedback ports
addPortCount(node, details, BidibLibrary.FEATURE_BM_SIZE, PortType.FEEDBACKPORT);
}
List features = node.getNode().getFeatures();
if (CollectionUtils.isNotEmpty(features)) {
details.setFeatureCount(features.size());
}
else {
details.setFeatureCount(0);
}
return details;
}
private static void addPortCount(
final NodeInterface node, final NodeDetails nodeDetails, int featureId, PortType portType) {
int portCount = Feature.getIntFeatureValue(node.getNode().getFeatures(), featureId);
LOGGER.info("Add port count, portCount: {}, portType: {}", portCount, portType);
if (portCount > 0) {
nodeDetails.getPortCounts().add(new PortCountType(portType, portCount));
}
}
private static void addPortCount(
final NodeInterface node, final NodeDetails nodeDetails, List> ports, PortType portType) {
int portCount = ports.size();
LOGGER.info("Add port count, portCount: {}, portType: {}", portCount, portType);
if (portCount > 0) {
nodeDetails.getPortCounts().add(new PortCountType(portType, portCount));
}
}
/**
* Create the {@code NodeAddress} instance for the provided node.
*
* @param node
* the node
* @return the node address
*/
public static NodeAddress prepareNodeAddress(NodeInterface node) {
NodeAddress nodeAddress =
new NodeAddress()
.withAddress(NodeUtils.formatAddressLong(node.getAddr()))
.withUniqueId(Long.toString(node.getUniqueId()));
return nodeAddress;
}
}