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());
}
}
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);
}
}
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());
});
}
}
}
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();
});
}
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()]);
}
}
}));
}
Aggregations