Search in sources :

Example 1 with AttributeRef

use of org.openremote.model.attribute.AttributeRef in project openremote by openremote.

the class AbstractTcpServerProtocol method doUnlinkProtocolConfiguration.

@Override
protected void doUnlinkProtocolConfiguration(AssetAttribute protocolConfiguration) {
    final AttributeRef protocolRef = protocolConfiguration.getReferenceOrThrow();
    T tcpServer = tcpServerMap.remove(protocolRef);
    if (tcpServer == null) {
        return;
    }
    LOG.info("Removing TCP server instance");
    stopTcpServer(protocolRef, tcpServer);
    updateStatus(protocolRef, ConnectionStatus.DISCONNECTED);
}
Also used : AttributeRef(org.openremote.model.attribute.AttributeRef)

Example 2 with AttributeRef

use of org.openremote.model.attribute.AttributeRef in project openremote by openremote.

the class AgentResourceImpl method importLinkedAttributes.

@Override
public Asset[] importLinkedAttributes(RequestParams requestParams, String agentId, String protocolConfigurationName, String parentId, String realmId, FileInfo fileInfo) {
    AttributeRef protocolConfigRef = new AttributeRef(agentId, protocolConfigurationName);
    if (fileInfo == null || fileInfo.getContents() == null) {
        throw new BadRequestException("A file must be provided for import");
    }
    Pair<Asset, String> parentAndRealmId = getParentAssetAndRealmId(parentId, realmId);
    Asset[] assets = withAgentConnector(agentId, agentConnector -> {
        LOG.finer("Asking connector '" + agentConnector.value.getClass().getSimpleName() + "' to do linked attribute discovery using uploaded file for protocol configuration: " + protocolConfigRef);
        return agentConnector.value.getDiscoveredLinkedAttributes(protocolConfigRef, fileInfo);
    });
    try {
        // TODO: Allow user to select which assets/attributes are actually added to the DB
        persistAssets(assets, parentAndRealmId.key, parentAndRealmId.value);
        return assets;
    } catch (IllegalArgumentException e) {
        LOG.log(Level.WARNING, e.getMessage(), e);
        throw new NotFoundException(e.getMessage());
    } catch (UnsupportedOperationException e) {
        LOG.log(Level.WARNING, e.getMessage(), e);
        throw new NotSupportedException(e.getMessage());
    } catch (IllegalStateException e) {
        LOG.log(Level.SEVERE, e.getMessage(), e);
        throw new InternalServerErrorException(e.getMessage());
    }
}
Also used : AttributeRef(org.openremote.model.attribute.AttributeRef) ServerAsset(org.openremote.manager.asset.ServerAsset) Asset(org.openremote.model.asset.Asset)

Example 3 with AttributeRef

use of org.openremote.model.attribute.AttributeRef in project openremote by openremote.

the class AgentService method updateProtocolConfiguration.

/**
 * This should only be called by protocol implementations to request an update to
 * one of their own protocol configuration attributes.
 */
@Override
public void updateProtocolConfiguration(AssetAttribute protocolConfiguration) {
    if (protocolConfiguration == null || !protocolConfiguration.getReference().isPresent()) {
        LOG.warning("Cannot update invalid: " + protocolConfiguration);
        return;
    }
    AttributeRef protocolRef = protocolConfiguration.getReference().get();
    ServerAsset agent = assetStorageService.find(protocolRef.getEntityId(), true);
    if (agent == null || agent.getWellKnownType() != AssetType.AGENT || !agent.hasAttribute(protocolRef.getAttributeName())) {
        LOG.warning("Protocol configuration doesn't belong to a valid agent: " + protocolConfiguration);
        return;
    }
    // Check protocol configuration has changed
    @SuppressWarnings("ConstantConditions") AssetAttribute oldProtocolConfiguration = agent.getAttribute(protocolRef.getAttributeName()).get();
    if (oldProtocolConfiguration.equals(protocolConfiguration)) {
        // Protocol configuration hasn't changed so nothing to do here
        return;
    }
    agent.replaceAttribute(protocolConfiguration);
    LOG.fine("Updating agent protocol configuration: " + protocolRef);
    assetStorageService.merge(agent);
}
Also used : AttributeRef(org.openremote.model.attribute.AttributeRef)

Example 4 with AttributeRef

use of org.openremote.model.attribute.AttributeRef in project openremote by openremote.

the class AssetViewActivity method onAttributeEvent.

protected void onAttributeEvent(AttributeEvent attributeEvent) {
    for (AttributeView attributeView : attributeViews) {
        AssetAttribute assetAttribute = attributeView.getAttribute();
        Optional<AttributeRef> assetAttributeRef = assetAttribute.getReference();
        if (assetAttributeRef.map(ref -> ref.equals(attributeEvent.getAttributeRef())).orElse(false)) {
            assetAttribute.setValue(attributeEvent.getValue().orElse(null), attributeEvent.getTimestamp());
            attributeView.onAttributeChanged(attributeEvent.getTimestamp());
            break;
        }
    }
}
Also used : SimulatorState(org.openremote.model.simulator.SimulatorState) DatapointInterval(org.openremote.model.datapoint.DatapointInterval) AssetMapper(org.openremote.app.client.assets.AssetMapper) Environment(org.openremote.app.client.Environment) ProtocolConfiguration(org.openremote.model.asset.agent.ProtocolConfiguration) AttributeRef(org.openremote.model.attribute.AttributeRef) AssetDatapointResource(org.openremote.model.datapoint.AssetDatapointResource) AttributeViewImpl(org.openremote.app.client.assets.attributes.AttributeViewImpl) ObjectValueMapper(org.openremote.app.client.interop.value.ObjectValueMapper) FormButton(org.openremote.app.client.widget.FormButton) NumberDatapoint(org.openremote.model.datapoint.NumberDatapoint) ArrayList(java.util.ArrayList) AgentLink(org.openremote.model.asset.agent.AgentLink) Inject(javax.inject.Inject) ReadAssetAttributesEvent(org.openremote.model.asset.ReadAssetAttributesEvent) TenantFilter(org.openremote.model.event.shared.TenantFilter) AttributeEvent(org.openremote.model.attribute.AttributeEvent) URL(com.google.gwt.http.client.URL) AbstractAttributeViewExtension(org.openremote.app.client.assets.attributes.AbstractAttributeViewExtension) AttributeView(org.openremote.app.client.assets.attributes.AttributeView) AgentStatusEventMapper(org.openremote.app.client.assets.AgentStatusEventMapper) Consumer(org.openremote.model.interop.Consumer) AgentResource(org.openremote.model.asset.agent.AgentResource) MapResource(org.openremote.model.map.MapResource) AgentStatusEvent(org.openremote.model.asset.agent.AgentStatusEvent) AssetType(org.openremote.model.asset.AssetType) DatapointBrowser(org.openremote.app.client.datapoint.DatapointBrowser) JsonEditor(org.openremote.app.client.app.dialog.JsonEditor) Constants(org.openremote.model.Constants) AssetResource(org.openremote.model.asset.AssetResource) Provider(com.google.inject.Provider) List(java.util.List) AssetBrowser(org.openremote.app.client.assets.browser.AssetBrowser) NumberDatapointArrayMapper(org.openremote.app.client.datapoint.NumberDatapointArrayMapper) Optional(java.util.Optional) Values(org.openremote.model.value.Values) TextUtil.isNullOrEmpty(org.openremote.model.util.TextUtil.isNullOrEmpty) AssetAttribute(org.openremote.model.asset.AssetAttribute) Datapoint(org.openremote.model.datapoint.Datapoint) Collections(java.util.Collections) Simulator(org.openremote.app.client.simulator.Simulator) AttributeExecuteStatus(org.openremote.model.attribute.AttributeExecuteStatus) AttributeView(org.openremote.app.client.assets.attributes.AttributeView) AttributeRef(org.openremote.model.attribute.AttributeRef) AssetAttribute(org.openremote.model.asset.AssetAttribute)

Example 5 with AttributeRef

use of org.openremote.model.attribute.AttributeRef in project openremote by openremote.

the class ControllerProtocol method onPollingResponse.

/**
 * Polling request should return three different responses :
 * <ul>
 * <li>OK (200) : new values are available for at least one of the sensor provided in queryParam</li>
 * <li>TIMEOUT (408) : during the last 60 seconds following the start of the request, none of the sensors have new values</li>
 * <li>Others : error</li>
 * </ul>
 * <p>
 * In every case, we should start a new polling request directly. Only the 200 response induce an update of every linked attribute having a sensor
 * status updated.
 */
private void onPollingResponse(String deviceName, List<String> sensorNameList, Response response) {
    if (response != null) {
        if (response.getStatusInfo() == Response.Status.OK) {
            String responseBodyAsString = response.readEntity(String.class);
            LOG.info("### New sensors status received");
            LOG.finer("### Polling request body response : " + responseBodyAsString);
            ArrayNode statusArray = ValueUtil.convert(responseBodyAsString, ArrayNode.class);
            if (statusArray == null) {
                LOG.warning("### Polling response is not a JSON array or empty: " + responseBodyAsString);
            } else {
                statusArray.forEach(status -> {
                    String name = Optional.ofNullable(status.get("name")).flatMap(ValueUtil::getString).orElse(null);
                    String value = Optional.ofNullable(status.get("value")).flatMap(ValueUtil::getString).orElse(null);
                    /**
                     * For every sensors in the request body, find the linked attributeref and update value by calling {@link #updateAttributeValue}
                     */
                    controller.getSensorsListForDevice(deviceName).stream().filter(entry -> entry.getValue().getSensorName().equals(name)).forEach(e -> this.updateAttributeValue(e.getKey(), value));
                });
            }
        } else if (response.getStatusInfo() == Response.Status.REQUEST_TIMEOUT) {
            LOG.info("### Timeout from polling no changes on Controller side given sensors [device=" + deviceName + ", sensors=" + this.formatSensors(sensorNameList) + "]");
        } else {
            LOG.severe("### Status code received error : " + response.getStatus() + " --> " + response.getStatusInfo().getReasonPhrase());
        }
    } else {
        LOG.severe("### Received null response from polling (due to previous exception)");
    }
    // No matter status code, we're continuing to poll
    this.schedulePollingTask(deviceName);
}
Also used : java.util(java.util) ScheduledFuture(java.util.concurrent.ScheduledFuture) ControllerCommandMapped(org.openremote.agent.protocol.controller.command.ControllerCommandMapped) ConnectionStatus(org.openremote.model.asset.agent.ConnectionStatus) URISyntaxException(java.net.URISyntaxException) AttributeRef(org.openremote.model.attribute.AttributeRef) ValueDescriptor(org.openremote.model.value.ValueDescriptor) ValueUtil(org.openremote.model.util.ValueUtil) ResteasyWebTarget(org.jboss.resteasy.client.jaxrs.ResteasyWebTarget) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) WebTargetBuilder(org.openremote.container.web.WebTargetBuilder) Level(java.util.logging.Level) GlobalLock.withLockReturning(org.openremote.container.concurrent.GlobalLock.withLockReturning) Future(java.util.concurrent.Future) Attribute(org.openremote.model.attribute.Attribute) AttributeEvent(org.openremote.model.attribute.AttributeEvent) CONNECTION_POOL_SIZE(org.openremote.container.web.WebTargetBuilder.CONNECTION_POOL_SIZE) ConnectException(java.net.ConnectException) SyslogCategory(org.openremote.model.syslog.SyslogCategory) URI(java.net.URI) ControllerCommandBasic(org.openremote.agent.protocol.controller.command.ControllerCommandBasic) AttributeState(org.openremote.model.attribute.AttributeState) URIBuilder(org.apache.http.client.utils.URIBuilder) HTTPProtocol(org.openremote.agent.protocol.http.HTTPProtocol) Logger(java.util.logging.Logger) UnknownHostException(java.net.UnknownHostException) AbstractProtocol(org.openremote.agent.protocol.AbstractProtocol) Container(org.openremote.model.Container) PROTOCOL(org.openremote.model.syslog.SyslogCategory.PROTOCOL) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Response(javax.ws.rs.core.Response) ResteasyClient(org.jboss.resteasy.client.jaxrs.ResteasyClient) ProcessingException(javax.ws.rs.ProcessingException) WebTargetBuilder.createClient(org.openremote.container.web.WebTargetBuilder.createClient) GlobalLock.withLock(org.openremote.container.concurrent.GlobalLock.withLock) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Aggregations

AttributeRef (org.openremote.model.attribute.AttributeRef)42 Logger (java.util.logging.Logger)9 AttributeEvent (org.openremote.model.attribute.AttributeEvent)8 AttributeState (org.openremote.model.attribute.AttributeState)8 Attribute (org.openremote.model.attribute.Attribute)7 List (java.util.List)6 Level (java.util.logging.Level)6 Container (org.openremote.model.Container)6 SyslogCategory (org.openremote.model.syslog.SyslogCategory)6 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 ScheduledFuture (java.util.concurrent.ScheduledFuture)5 Consumer (java.util.function.Consumer)5 Asset (org.openremote.model.asset.Asset)5 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)4 HashMap (java.util.HashMap)4 TimeUnit (java.util.concurrent.TimeUnit)4 AbstractProtocol (org.openremote.agent.protocol.AbstractProtocol)4 AssetStorageService (org.openremote.manager.asset.AssetStorageService)4 ConnectionStatus (org.openremote.model.asset.agent.ConnectionStatus)4