Search in sources :

Example 1 with ConnectionStatus

use of org.openremote.model.asset.agent.ConnectionStatus in project openremote by openremote.

the class HttpClientProtocol method updateConnectionStatus.

protected void updateConnectionStatus(HttpClientRequest request, AttributeRef protocolConfigurationRef, int responseCode) {
    Response.Status status = Response.Status.fromStatusCode(responseCode);
    ConnectionStatus connectionStatus = ConnectionStatus.CONNECTED;
    if (status == null) {
        connectionStatus = ConnectionStatus.UNKNOWN;
    } else {
        switch(status.getFamily()) {
            case INFORMATIONAL:
                connectionStatus = ConnectionStatus.ERROR;
                break;
            case SUCCESSFUL:
                connectionStatus = ConnectionStatus.CONNECTED;
                break;
            case REDIRECTION:
                connectionStatus = ConnectionStatus.ERROR;
                break;
            case CLIENT_ERROR:
                if (responseCode == 401 || responseCode == 402 || responseCode == 403) {
                    connectionStatus = ConnectionStatus.ERROR_AUTHENTICATION;
                } else {
                    connectionStatus = ConnectionStatus.ERROR_CONFIGURATION;
                }
                break;
            case SERVER_ERROR:
                connectionStatus = ConnectionStatus.ERROR;
                break;
            case OTHER:
                break;
        }
    }
    LOG.fine("Updating connection status based on polling response code: URI=" + request + ", ResponseCode=" + responseCode + ", Status=" + connectionStatus);
    updateStatus(protocolConfigurationRef, connectionStatus);
}
Also used : Response(javax.ws.rs.core.Response) ConnectionStatus(org.openremote.model.asset.agent.ConnectionStatus)

Example 2 with ConnectionStatus

use of org.openremote.model.asset.agent.ConnectionStatus in project openremote by openremote.

the class BluetoothMeshProtocol method doStart.

// Implements AbstractProtocol ------------------------------------------------------------------
@Override
protected synchronized void doStart(Container container) throws Exception {
    LOG.info("Starting Bluetooth Mesh protocol.");
    String meshNetKeyParam = agent.getNetworkKey().orElseThrow(() -> {
        String msg = "No Bluetooth Mesh network key provided for protocol: " + this;
        LOG.warning(msg);
        return new IllegalArgumentException(msg);
    });
    Integer netKeyIndex = extractIndex(meshNetKeyParam, DEFAULT_NETWORK_KEY_INDEX);
    String netKeyAsString = extractKey(meshNetKeyParam);
    if (netKeyIndex == null || netKeyAsString == null) {
        String msg = "Format of network key '" + meshNetKeyParam + "' is invalid for protocol: " + this;
        LOG.warning(msg);
        throw new IllegalArgumentException(msg);
    }
    NetworkKey networkKey = new NetworkKey(netKeyIndex, MeshParserUtils.toByteArray(netKeyAsString));
    String meshAppKeyParam = agent.getApplicationKey().orElseThrow(() -> {
        String msg = "No Bluetooth Mesh application key provided for protocol: " + this;
        LOG.warning(msg);
        return new IllegalArgumentException(msg);
    });
    Integer appKeyIndex = extractIndex(meshAppKeyParam, DEFAULT_APPLICATION_KEY_INDEX);
    String appKeyAsString = extractKey(meshAppKeyParam);
    if (appKeyIndex == null || appKeyAsString == null) {
        String msg = "Format of application key '" + meshAppKeyParam + "' is invalid for protocol: " + this;
        LOG.warning(msg);
        throw new IllegalArgumentException(msg);
    }
    ApplicationKey applicationKey = new ApplicationKey(appKeyIndex, MeshParserUtils.toByteArray(appKeyAsString));
    String proxyAddress = agent.getProxyAddress().orElse(null);
    proxyAddress = proxyAddress != null ? proxyAddress.trim() : null;
    if (proxyAddress != null && !proxyAddress.matches(REGEXP_PROXY_ADDRESS)) {
        String msg = "Format of proxy address '" + proxyAddress + "' is invalid for protocol: " + this;
        LOG.warning(msg);
        throw new IllegalArgumentException(msg);
    }
    String sourceAddressParam = agent.getSourceAddress().orElseThrow(() -> {
        String msg = "No Bluetooth Mesh unicast source address provided for protocol: " + this;
        LOG.warning(msg);
        return new IllegalArgumentException(msg);
    });
    final Integer sourceAddress = toIntegerAddress(sourceAddressParam, null);
    if (sourceAddress == null) {
        String msg = "Format of Bluetooth Mesh unicast source address '" + sourceAddressParam + "' is invalid for protocol: " + this;
        throw new IllegalArgumentException(msg);
    }
    int sequenceNumberParam = agent.getSequenceNumber().orElse(DEFAULT_SEQUENCE_NUMBER);
    final int mtuParam = agent.getMtu().orElse(DEFAULT_MTU);
    Map<Integer, ApplicationKey> applicationKeyMap = new HashMap<>();
    applicationKeyMap.put(appKeyIndex, applicationKey);
    final ScheduledExecutorService finalExecutorService = executorService;
    Consumer<ConnectionStatus> statusConsumer = new Consumer<ConnectionStatus>() {

        @Override
        public void accept(ConnectionStatus connectionStatus) {
            setConnectionStatus(connectionStatus);
            if (connectionStatus == ConnectionStatus.CONNECTED) {
                finalExecutorService.execute(() -> updateAllAttributes());
            }
        }
    };
    BluetoothMeshProtocol.sequenceNumberManager.load();
    Integer oldSequenceNumber = BluetoothMeshProtocol.sequenceNumberManager.getSequenceNumber(networkKey, sourceAddress);
    if (oldSequenceNumber == null) {
        oldSequenceNumber = sequenceNumberParam;
        BluetoothMeshProtocol.sequenceNumberManager.save(networkKey, sourceAddress, oldSequenceNumber);
    }
    BluetoothMeshProtocol.initMainThread(executorService);
    meshNetwork = new BluetoothMeshNetwork(BluetoothMeshProtocol.bluetoothCentral, BluetoothMeshProtocol.sequenceNumberManager, BluetoothMeshProtocol.mainThread, proxyAddress, sourceAddress, networkKey, applicationKeyMap, mtuParam, oldSequenceNumber, executorService, statusConsumer);
    BluetoothMeshProtocol.addNetwork(meshNetwork);
    BluetoothMeshProtocol.mainThread.enqueue(() -> meshNetwork.start());
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) HashMap(java.util.HashMap) Consumer(java.util.function.Consumer) ConnectionStatus(org.openremote.model.asset.agent.ConnectionStatus)

Example 3 with ConnectionStatus

use of org.openremote.model.asset.agent.ConnectionStatus in project openremote by openremote.

the class AbstractVelbusProtocol method doLinkAttribute.

@Override
protected void doLinkAttribute(AssetAttribute attribute, AssetAttribute protocolConfiguration) {
    Pair<VelbusNetwork, Consumer<ConnectionStatus>> velbusNetworkConsumerPair = networkConfigurationMap.get(protocolConfiguration.getReferenceOrThrow());
    if (velbusNetworkConsumerPair == null) {
        LOG.info("Protocol Configuration doesn't have a valid VelbusNetwork so cannot link");
        return;
    }
    VelbusNetwork velbusNetwork = velbusNetworkConsumerPair.key;
    // Get the device that this attribute is linked to
    int deviceAddress = getVelbusDeviceAddress(attribute);
    // Get the property that this attribute is linked to
    String property = getVelbusDevicePropertyLink(attribute);
    AttributeRef attributeRef = attribute.getReferenceOrThrow();
    ValueType valueType = attribute.getType().orElse(AttributeType.STRING).getValueType();
    LOG.fine("Linking attribute to device '" + deviceAddress + "' and property '" + property + "': " + attributeRef);
    Consumer<DevicePropertyValue> propertyValueConsumer = propertyValue -> updateLinkedAttribute(new AttributeState(attributeRef, propertyValue != null ? propertyValue.toValue(valueType) : null));
    attributePropertyValueConsumers.put(attributeRef, propertyValueConsumer);
    velbusNetwork.addPropertyValueConsumer(deviceAddress, property, propertyValueConsumer);
}
Also used : java.util(java.util) CodecUtil(org.openremote.container.util.CodecUtil) ConnectionStatus(org.openremote.model.asset.agent.ConnectionStatus) VelbusConfiguration(org.openremote.agent.protocol.velbus.VelbusConfiguration) org.openremote.agent.protocol(org.openremote.agent.protocol) FeatureProcessor(org.openremote.agent.protocol.velbus.device.FeatureProcessor) VelbusDeviceType(org.openremote.agent.protocol.velbus.device.VelbusDeviceType) Level(java.util.logging.Level) AgentLink(org.openremote.model.asset.agent.AgentLink) Document(org.w3c.dom.Document) PROTOCOL_NAMESPACE(org.openremote.model.Constants.PROTOCOL_NAMESPACE) REGEXP_PATTERN_INTEGER_POSITIVE_NON_ZERO(org.openremote.model.util.TextUtil.REGEXP_PATTERN_INTEGER_POSITIVE_NON_ZERO) InputSource(org.xml.sax.InputSource) NodeList(org.w3c.dom.NodeList) ValueType(org.openremote.model.value.ValueType) Asset(org.openremote.model.asset.Asset) AssetType(org.openremote.model.asset.AssetType) Pair(org.openremote.model.util.Pair) FileInfo(org.openremote.model.file.FileInfo) EnumUtil(org.openremote.model.util.EnumUtil) Logger(java.util.logging.Logger) AssetMeta(org.openremote.model.asset.AssetMeta) Consumer(java.util.function.Consumer) Element(org.w3c.dom.Element) StringReader(java.io.StringReader) org.openremote.model.attribute(org.openremote.model.attribute) Values(org.openremote.model.value.Values) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) TextUtil.isNullOrEmpty(org.openremote.model.util.TextUtil.isNullOrEmpty) AssetAttribute(org.openremote.model.asset.AssetAttribute) DevicePropertyValue(org.openremote.agent.protocol.velbus.device.DevicePropertyValue) Consumer(java.util.function.Consumer) ValueType(org.openremote.model.value.ValueType) DevicePropertyValue(org.openremote.agent.protocol.velbus.device.DevicePropertyValue)

Example 4 with ConnectionStatus

use of org.openremote.model.asset.agent.ConnectionStatus in project openremote by openremote.

the class AgentHealthStatusProvider method getHealthStatus.

@Override
public Object getHealthStatus() {
    AtomicInteger connectedCount = new AtomicInteger(0);
    AtomicInteger disabledCount = new AtomicInteger(0);
    AtomicInteger errorCount = new AtomicInteger(0);
    AtomicInteger otherCount = new AtomicInteger(0);
    ObjectNode objectValue = ValueUtil.JSON.createObjectNode();
    objectValue.put("agents", agentService.getAgents().size());
    objectValue.put("protocols", agentService.protocolInstanceMap.size());
    for (Agent<?, ?, ?> agent : agentService.getAgents().values()) {
        ConnectionStatus status = agent.getAgentStatus().orElse(null);
        if (status != null) {
            switch(status) {
                case DISCONNECTED:
                case CONNECTING:
                case DISCONNECTING:
                case WAITING:
                    otherCount.incrementAndGet();
                    break;
                case CONNECTED:
                    connectedCount.incrementAndGet();
                    break;
                case DISABLED:
                    disabledCount.incrementAndGet();
                    break;
                case ERROR:
                    errorCount.incrementAndGet();
                    break;
            }
        } else {
            otherCount.incrementAndGet();
        }
        ObjectNode agentValue = ValueUtil.JSON.createObjectNode();
        agentValue.put("name", agent.getName());
        agentValue.put("status", status != null ? status.name() : "null");
        agentValue.put("type", agent.getType());
        objectValue.set(agent.getId(), agentValue);
    }
    objectValue.put("totalAgents", agentService.agentMap.size());
    objectValue.put("connectedAgents", connectedCount.get());
    objectValue.put("errorAgents", errorCount.get());
    objectValue.put("disabledAgents", disabledCount.get());
    objectValue.put("otherAgents", otherCount.get());
    return objectValue;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConnectionStatus(org.openremote.model.asset.agent.ConnectionStatus)

Aggregations

ConnectionStatus (org.openremote.model.asset.agent.ConnectionStatus)4 Consumer (java.util.function.Consumer)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 StringReader (java.io.StringReader)1 java.util (java.util)1 HashMap (java.util.HashMap)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Level (java.util.logging.Level)1 Logger (java.util.logging.Logger)1 Response (javax.ws.rs.core.Response)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 org.openremote.agent.protocol (org.openremote.agent.protocol)1 VelbusConfiguration (org.openremote.agent.protocol.velbus.VelbusConfiguration)1 DevicePropertyValue (org.openremote.agent.protocol.velbus.device.DevicePropertyValue)1 FeatureProcessor (org.openremote.agent.protocol.velbus.device.FeatureProcessor)1 VelbusDeviceType (org.openremote.agent.protocol.velbus.device.VelbusDeviceType)1 CodecUtil (org.openremote.container.util.CodecUtil)1 PROTOCOL_NAMESPACE (org.openremote.model.Constants.PROTOCOL_NAMESPACE)1 Asset (org.openremote.model.asset.Asset)1