Search in sources :

Example 11 with AttributeRef

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

the class WebsocketAgentProtocol method doUnlinkAttribute.

@Override
protected void doUnlinkAttribute(String assetId, Attribute<?> attribute, WebsocketAgentLink agentLink) {
    AttributeRef attributeRef = new AttributeRef(assetId, attribute.getName());
    protocolMessageConsumers.removeIf((attrRefConsumer) -> attrRefConsumer.key.equals(attributeRef));
    attributeConnectedTasks.remove(attributeRef);
}
Also used : AttributeRef(org.openremote.model.attribute.AttributeRef)

Example 12 with AttributeRef

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

the class ZWaveProtocol method doUnlinkAttribute.

@Override
protected synchronized void doUnlinkAttribute(String assetId, Attribute<?> attribute, ZWaveAgentLink agentLink) {
    if (network == null) {
        return;
    }
    AttributeRef attributeRef = new AttributeRef(assetId, attribute.getName());
    Consumer<Value> sensorValueConsumer = sensorValueConsumerMap.remove(attributeRef);
    network.removeSensorValueConsumer(sensorValueConsumer);
}
Also used : AttributeRef(org.openremote.model.attribute.AttributeRef) Value(org.openremote.protocol.zwave.model.commandclasses.channel.value.Value)

Example 13 with AttributeRef

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

the class ZWaveProtocol method doLinkAttribute.

@Override
protected synchronized void doLinkAttribute(String assetId, Attribute<?> attribute, ZWaveAgentLink agentLink) {
    if (network == null) {
        return;
    }
    int nodeId = agentLink.getDeviceNodeId().orElse(0);
    int endpoint = agentLink.getDeviceEndpoint().orElse(0);
    String linkName = agentLink.getDeviceValue().orElse("");
    AttributeRef attributeRef = new AttributeRef(assetId, attribute.getName());
    Class<?> clazz = (attribute == null ? null : attribute.getType().getType());
    Consumer<Value> sensorValueConsumer = value -> updateLinkedAttribute(new AttributeState(attributeRef, toAttributeValue(value, clazz)));
    sensorValueConsumerMap.put(attributeRef, sensorValueConsumer);
    network.addSensorValueConsumer(nodeId, endpoint, linkName, sensorValueConsumer);
}
Also used : AssetTreeNode(org.openremote.model.asset.AssetTreeNode) AttributeState(org.openremote.model.attribute.AttributeState) ConnectionStatus(org.openremote.model.asset.agent.ConnectionStatus) ColourRGB(org.openremote.model.value.impl.ColourRGB) AttributeRef(org.openremote.model.attribute.AttributeRef) ProtocolAssetDiscovery(org.openremote.model.protocol.ProtocolAssetDiscovery) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Value(org.openremote.protocol.zwave.model.commandclasses.channel.value.Value) Logger(java.util.logging.Logger) AbstractProtocol(org.openremote.agent.protocol.AbstractProtocol) Container(org.openremote.model.Container) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) Consumer(java.util.function.Consumer) List(java.util.List) Future(java.util.concurrent.Future) Attribute(org.openremote.model.attribute.Attribute) AttributeEvent(org.openremote.model.attribute.AttributeEvent) Map(java.util.Map) AgentLink.getOrThrowAgentLinkProperty(org.openremote.model.asset.agent.AgentLink.getOrThrowAgentLinkProperty) SyslogCategory(org.openremote.model.syslog.SyslogCategory) AttributeState(org.openremote.model.attribute.AttributeState) AttributeRef(org.openremote.model.attribute.AttributeRef) Value(org.openremote.protocol.zwave.model.commandclasses.channel.value.Value)

Example 14 with AttributeRef

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

the class AssetDatapointResourceImpl method getDatapointExport.

@Override
public void getDatapointExport(AsyncResponse asyncResponse, String attributeRefsString, long fromTimestamp, long toTimestamp) {
    try {
        AttributeRef[] attributeRefs = JSON.readValue(attributeRefsString, AttributeRef[].class);
        for (AttributeRef attributeRef : attributeRefs) {
            if (isRestrictedUser() && !assetStorageService.isUserAsset(getUserId(), attributeRef.getId())) {
                throw new WebApplicationException(Response.Status.FORBIDDEN);
            }
            Asset<?> asset = assetStorageService.find(attributeRef.getId(), true);
            if (asset == null) {
                throw new WebApplicationException(Response.Status.NOT_FOUND);
            }
            if (!isTenantActiveAndAccessible(asset.getRealm())) {
                DATA_EXPORT_LOG.info("Forbidden access for user '" + getUsername() + "': " + asset);
                throw new WebApplicationException(Response.Status.FORBIDDEN);
            }
            asset.getAttribute(attributeRef.getName()).orElseThrow(() -> new WebApplicationException(Response.Status.NOT_FOUND));
        }
        DATA_EXPORT_LOG.info("User '" + getUsername() + "' started data export for " + attributeRefsString + " from " + fromTimestamp + " to " + toTimestamp);
        ScheduledFuture<File> exportFuture = assetDatapointService.exportDatapoints(attributeRefs, fromTimestamp, toTimestamp);
        asyncResponse.register((ConnectionCallback) disconnected -> {
            exportFuture.cancel(true);
        });
        File exportFile = null;
        try {
            exportFile = exportFuture.get();
            ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream());
            FileInputStream fin = new FileInputStream(exportFile);
            ZipEntry zipEntry = new ZipEntry(exportFile.getName());
            zipOut.putNextEntry(zipEntry);
            IOUtils.copy(fin, zipOut);
            zipOut.closeEntry();
            zipOut.close();
            fin.close();
            response.setContentType("application/zip");
            response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"dataexport.zip\"");
            asyncResponse.resume(response);
        } catch (Exception ex) {
            exportFuture.cancel(true);
            asyncResponse.resume(new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR));
            DATA_EXPORT_LOG.log(Level.SEVERE, "Exception in ScheduledFuture: ", ex);
        } finally {
            if (exportFile != null && exportFile.exists()) {
                try {
                    exportFile.delete();
                } catch (Exception e) {
                    DATA_EXPORT_LOG.log(Level.SEVERE, "Failed to delete temporary export file: " + exportFile.getPath(), e);
                }
            }
        }
    } catch (JsonProcessingException ex) {
        asyncResponse.resume(new BadRequestException(ex));
    }
}
Also used : AssetStorageService(org.openremote.manager.asset.AssetStorageService) ZipOutputStream(java.util.zip.ZipOutputStream) DatapointInterval(org.openremote.model.datapoint.DatapointInterval) ScheduledFuture(java.util.concurrent.ScheduledFuture) AttributeRef(org.openremote.model.attribute.AttributeRef) AssetDatapointResource(org.openremote.model.datapoint.AssetDatapointResource) LocalDateTime(java.time.LocalDateTime) DATA(org.openremote.model.syslog.SyslogCategory.DATA) JSON(org.openremote.model.util.ValueUtil.JSON) ManagerWebResource(org.openremote.manager.web.ManagerWebResource) Level(java.util.logging.Level) Attribute(org.openremote.model.attribute.Attribute) BadRequestException(javax.ws.rs.BadRequestException) SyslogCategory(org.openremote.model.syslog.SyslogCategory) ZipEntry(java.util.zip.ZipEntry) NotSupportedException(javax.ws.rs.NotSupportedException) ManagerIdentityService(org.openremote.manager.security.ManagerIdentityService) Asset(org.openremote.model.asset.Asset) AsyncResponse(javax.ws.rs.container.AsyncResponse) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Instant(java.time.Instant) Logger(java.util.logging.Logger) BeanParam(javax.ws.rs.BeanParam) ZoneId(java.time.ZoneId) ValueDatapoint(org.openremote.model.datapoint.ValueDatapoint) IOUtils(org.apache.commons.io.IOUtils) HttpHeaders(javax.ws.rs.core.HttpHeaders) Response(javax.ws.rs.core.Response) java.io(java.io) TimerService(org.openremote.container.timer.TimerService) DatapointPeriod(org.openremote.model.datapoint.DatapointPeriod) WebApplicationException(javax.ws.rs.WebApplicationException) ConnectionCallback(javax.ws.rs.container.ConnectionCallback) RequestParams(org.openremote.model.http.RequestParams) AttributeRef(org.openremote.model.attribute.AttributeRef) WebApplicationException(javax.ws.rs.WebApplicationException) ZipEntry(java.util.zip.ZipEntry) BadRequestException(javax.ws.rs.BadRequestException) NotSupportedException(javax.ws.rs.NotSupportedException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) WebApplicationException(javax.ws.rs.WebApplicationException) ZipOutputStream(java.util.zip.ZipOutputStream) BadRequestException(javax.ws.rs.BadRequestException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 15 with AttributeRef

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

the class UDPProtocol method doLinkAttribute.

@Override
protected void doLinkAttribute(String assetId, Attribute<?> attribute, DefaultAgentLink agentLink) {
    AttributeRef attributeRef = new AttributeRef(assetId, attribute.getName());
    Consumer<String> messageConsumer = ProtocolUtil.createGenericAttributeMessageConsumer(assetId, attribute, agentLink, timerService::getCurrentTimeMillis, this::updateLinkedAttribute);
    if (messageConsumer != null) {
        protocolMessageConsumers.add(new Pair<>(attributeRef, messageConsumer));
    }
}
Also used : AttributeRef(org.openremote.model.attribute.AttributeRef)

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