Search in sources :

Example 6 with AttributeRef

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

the class AgentResourceImpl method searchForLinkedAttributes.

@Override
public Asset[] searchForLinkedAttributes(RequestParams requestParams, String agentId, String protocolConfigurationName, String parentId, String realmId) {
    AttributeRef protocolConfigRef = new AttributeRef(agentId, protocolConfigurationName);
    Pair<Asset, String> parentAndRealmId = getParentAssetAndRealmId(parentId, realmId);
    // TODO: Allow user to select which assets/attributes are actually added to the DB
    Asset[] assets = withAgentConnector(agentId, agentConnector -> {
        LOG.finer("Asking connector '" + agentConnector.value.getClass().getSimpleName() + "' to do linked attribute discovery for protocol configuration: " + protocolConfigRef);
        return agentConnector.value.getDiscoveredLinkedAttributes(protocolConfigRef);
    });
    try {
        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());
    }
}
Also used : AttributeRef(org.openremote.model.attribute.AttributeRef) ServerAsset(org.openremote.manager.asset.ServerAsset) Asset(org.openremote.model.asset.Asset)

Example 7 with AttributeRef

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

the class AbstractTcpServerProtocol method doLinkProtocolConfiguration.

@Override
protected void doLinkProtocolConfiguration(AssetAttribute protocolConfiguration) {
    final AttributeRef protocolRef = protocolConfiguration.getReferenceOrThrow();
    if (!protocolConfiguration.isEnabled()) {
        updateStatus(protocolRef, ConnectionStatus.DISABLED);
        return;
    }
    int port = protocolConfiguration.getMetaItem(META_PROTOCOL_PORT).flatMap(AbstractValueHolder::getValueAsInteger).orElseThrow(() -> new IllegalArgumentException("Missing or invalid require meta item: " + META_PROTOCOL_PORT));
    Optional<StringValue> bindAddress = Values.getMetaItemValueOrThrow(protocolConfiguration, META_PROTOCOL_BIND_ADDRESS, StringValue.class, false, true);
    LOG.info("Creating TCP server instance");
    T tcpServer = createTcpServer(port, bindAddress.map(StringValue::getString).orElse(null), protocolConfiguration);
    tcpServerMap.put(protocolRef, tcpServer);
    startTcpServer(protocolRef, tcpServer);
    if (!tcpServer.isStarted()) {
        LOG.warning("Failed to start TCP server instance");
        updateStatus(protocolRef, ConnectionStatus.ERROR);
    } else {
        updateStatus(protocolRef, ConnectionStatus.CONNECTED);
    }
}
Also used : AttributeRef(org.openremote.model.attribute.AttributeRef) StringValue(org.openremote.model.value.StringValue)

Example 8 with AttributeRef

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

the class AssetViewActivity method onAgentStatusEvent.

protected void onAgentStatusEvent(AgentStatusEvent event) {
    for (AttributeView attributeView : attributeViews) {
        AssetAttribute assetAttribute = attributeView.getAttribute();
        Optional<AttributeRef> assetAttributeRef = assetAttribute.getReference();
        if (asset.getWellKnownType() == AssetType.AGENT) {
            if (assetAttributeRef.map(ref -> ref.equals(event.getProtocolConfiguration())).orElse(false)) {
                attributeView.setStatus(event.getConnectionStatus());
            }
        } else {
            AgentLink.getAgentLink(assetAttribute).filter(agentLink -> agentLink.equals(event.getProtocolConfiguration())).ifPresent(agentLink -> {
                attributeView.setStatus(event.getConnectionStatus());
            });
        }
    }
}
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 9 with AttributeRef

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

the class AssetViewActivity method createSimulator.

protected Simulator createSimulator(AssetAttribute attribute, AttributeViewImpl view) {
    AttributeRef protocolConfigurationRef = attribute.getReferenceOrThrow();
    return new Simulator(environment, this.view.getStyle(), view, attribute, protocolConfigurationRef, () -> {
        activeSimulators.add(protocolConfigurationRef);
        updateSimulatorSubscription();
    }, () -> {
        activeSimulators.remove(protocolConfigurationRef);
        updateSimulatorSubscription();
    });
}
Also used : AttributeRef(org.openremote.model.attribute.AttributeRef) Simulator(org.openremote.app.client.simulator.Simulator)

Example 10 with AttributeRef

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

the class AssetDatapointService method aggregateDatapoints.

public NumberDatapoint[] aggregateDatapoints(AssetAttribute attribute, DatapointInterval datapointInterval, long timestamp) {
    LOG.fine("Aggregating datapoints for: " + attribute);
    AttributeRef attributeRef = attribute.getReferenceOrThrow();
    return persistenceService.doReturningTransaction(entityManager -> entityManager.unwrap(Session.class).doReturningWork(new AbstractReturningWork<NumberDatapoint[]>() {

        @Override
        public NumberDatapoint[] execute(Connection connection) throws SQLException {
            String truncateX;
            String step;
            String interval;
            Function<Timestamp, String> labelFunction;
            SimpleDateFormat dayFormat = new SimpleDateFormat("dd. MMM yyyy");
            SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
            switch(datapointInterval) {
                case HOUR:
                    truncateX = "minute";
                    step = "1 minute";
                    interval = "1 hour";
                    labelFunction = timeFormat::format;
                    break;
                case DAY:
                    truncateX = "hour";
                    step = "1 hour";
                    interval = "1 day";
                    labelFunction = timeFormat::format;
                    break;
                case WEEK:
                    truncateX = "day";
                    step = "1 day";
                    interval = "7 day";
                    labelFunction = dayFormat::format;
                    break;
                case MONTH:
                    truncateX = "day";
                    step = "1 day";
                    interval = "1 month";
                    labelFunction = dayFormat::format;
                    break;
                case YEAR:
                    truncateX = "month";
                    step = "1 month";
                    interval = "1 year";
                    labelFunction = dayFormat::format;
                    break;
                default:
                    throw new IllegalArgumentException("Can't handle interval: " + datapointInterval);
            }
            StringBuilder query = new StringBuilder();
            query.append("select TS as X, coalesce(AVG_VALUE, null) as Y " + " from ( " + "       select date_trunc(?, GS)::timestamp TS " + "       from generate_series(to_timestamp(?) - ?, to_timestamp(?), ?) GS " + "       ) TS " + "  left join ( " + "       select " + "           date_trunc(?, to_timestamp(TIMESTAMP / 1000))::timestamp as TS, ");
            switch(attribute.getTypeOrThrow().getValueType()) {
                case NUMBER:
                    query.append(" AVG(VALUE::text::numeric) as AVG_VALUE ");
                    break;
                case BOOLEAN:
                    query.append(" AVG(case when VALUE::text::boolean is true then 1 else 0 end) as AVG_VALUE ");
                    break;
                default:
                    throw new IllegalArgumentException("Can't aggregate number datapoints for type of: " + attribute);
            }
            query.append(" from ASSET_DATAPOINT " + "         where " + "           to_timestamp(TIMESTAMP / 1000) >= to_timestamp(?) - ? " + "           and " + "           to_timestamp(TIMESTAMP / 1000) <= to_timestamp(?) " + "           and " + "           ENTITY_ID = ? and ATTRIBUTE_NAME = ? " + "         group by TS " + "  ) DP using (TS) " + " order by TS asc ");
            PreparedStatement st = connection.prepareStatement(query.toString());
            long timestampSeconds = timestamp / 1000;
            st.setString(1, truncateX);
            st.setLong(2, timestampSeconds);
            st.setObject(3, new PGInterval(interval));
            st.setLong(4, timestampSeconds);
            st.setObject(5, new PGInterval(step));
            st.setString(6, truncateX);
            st.setLong(7, timestampSeconds);
            st.setObject(8, new PGInterval(interval));
            st.setLong(9, timestampSeconds);
            st.setString(10, attributeRef.getEntityId());
            st.setString(11, attributeRef.getAttributeName());
            try (ResultSet rs = st.executeQuery()) {
                List<NumberDatapoint> result = new ArrayList<>();
                while (rs.next()) {
                    String label = labelFunction.apply(rs.getTimestamp(1));
                    Number value = rs.getObject(2) != null ? rs.getDouble(2) : null;
                    result.add(new NumberDatapoint(label, value));
                }
                return result.toArray(new NumberDatapoint[result.size()]);
            }
        }
    }));
}
Also used : AttributeRef(org.openremote.model.attribute.AttributeRef) AbstractReturningWork(org.hibernate.jdbc.AbstractReturningWork) ArrayList(java.util.ArrayList) PGInterval(org.postgresql.util.PGInterval) NumberDatapoint(org.openremote.model.datapoint.NumberDatapoint) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

AttributeRef (org.openremote.model.attribute.AttributeRef)10 ArrayList (java.util.ArrayList)3 Simulator (org.openremote.app.client.simulator.Simulator)3 NumberDatapoint (org.openremote.model.datapoint.NumberDatapoint)3 URL (com.google.gwt.http.client.URL)2 Provider (com.google.inject.Provider)2 Collections (java.util.Collections)2 List (java.util.List)2 Optional (java.util.Optional)2 Inject (javax.inject.Inject)2 Environment (org.openremote.app.client.Environment)2 JsonEditor (org.openremote.app.client.app.dialog.JsonEditor)2 AgentStatusEventMapper (org.openremote.app.client.assets.AgentStatusEventMapper)2 AssetMapper (org.openremote.app.client.assets.AssetMapper)2 AbstractAttributeViewExtension (org.openremote.app.client.assets.attributes.AbstractAttributeViewExtension)2 AttributeView (org.openremote.app.client.assets.attributes.AttributeView)2 AttributeViewImpl (org.openremote.app.client.assets.attributes.AttributeViewImpl)2 AssetBrowser (org.openremote.app.client.assets.browser.AssetBrowser)2 DatapointBrowser (org.openremote.app.client.datapoint.DatapointBrowser)2 NumberDatapointArrayMapper (org.openremote.app.client.datapoint.NumberDatapointArrayMapper)2