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