Skip to content

Commit

Permalink
Fixed tests, new unique identifier generator, experimental UPnP protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Bauer committed Jun 10, 2017
1 parent 7a80e2a commit 94574fd
Show file tree
Hide file tree
Showing 23 changed files with 302 additions and 146 deletions.
2 changes: 2 additions & 0 deletions agent/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ dependencies {

compile "com.github.calimero:calimero-core:$calimeroVersion"
compile "com.github.calimero:calimero-tools:$calimeroVersion"

compile "org.fourthline.cling:cling-core:$clingVersion"

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.openremote.agent.protocol;

import org.openremote.container.ContainerService;
import org.openremote.model.asset.Asset;
import org.openremote.model.asset.AssetAttribute;
import org.openremote.model.attribute.AttributeEvent;

Expand All @@ -34,6 +35,20 @@ public interface ProtocolAssetService extends ContainerService {
*/
void updateProtocolConfiguration(AssetAttribute protocolConfiguration);

/**
* Protocols may store assets in the context or update existing assets. A unique identifier
* must be set by the protocol implementor, as well as a parent identifier. This operation
* stores transient or detached state and returns the current state.
*/
Asset mergeAsset(Asset asset);

/**
* Protocols may remove assets from the context store.
*
* @return <code>false</code> if the delete could not be performed (asset may have children?)
*/
boolean deleteAsset(String assetId);

/**
* Protocols can send arbitrary attribute change events for regular processing.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
package org.openremote.agent.protocol.upnp;

import com.fasterxml.uuid.Generators;
import org.fourthline.cling.UpnpService;
import org.fourthline.cling.UpnpServiceImpl;
import org.fourthline.cling.model.message.header.STAllHeader;
import org.fourthline.cling.model.meta.Device;
import org.fourthline.cling.registry.DefaultRegistryListener;
import org.fourthline.cling.registry.Registry;
import org.fourthline.cling.registry.RegistryListener;
import org.openremote.agent.protocol.AbstractProtocol;
import org.openremote.model.asset.Asset;
import org.openremote.model.asset.AssetAttribute;
import org.openremote.model.asset.AssetType;
import org.openremote.model.attribute.AttributeEvent;
import org.openremote.model.attribute.AttributeRef;
import org.openremote.model.attribute.MetaItem;
import org.openremote.model.value.Values;

import java.util.Collection;
import java.util.Optional;
import java.util.logging.Logger;

import static org.openremote.model.Constants.PROTOCOL_NAMESPACE;
import static org.openremote.model.asset.AssetMeta.LABEL;
import static org.openremote.model.attribute.AttributeType.STRING;

// TODO Experimental

public class UpnpProtocol extends AbstractProtocol {

private static final Logger LOG = Logger.getLogger(UpnpProtocol.class.getName());

public static final String PROTOCOL_NAME = PROTOCOL_NAMESPACE + ":upnp";

static protected UpnpService INSTANCE = null;
final protected RegistryListener registryListener = new DefaultRegistryListener() {
@Override
public void deviceAdded(Registry registry, Device device) {
LOG.fine("UPnP device discovered: " + device.getDisplayString());
for (AttributeRef attributeRef : linkedProtocolConfigurations.keySet()) {
storeAssets(attributeRef.getEntityId(), device);
}
}

@Override
public void deviceRemoved(Registry registry, Device device) {
LOG.fine("UPnP device gone: " + device.getDisplayString());
if (!assetService.deleteAsset(getAssetId(device))) {
LOG.warning("UPnP device gone but can't delete discovered asset: " + device);
}
}

};

@Override
public String getProtocolName() {
return PROTOCOL_NAME;
}

@Override
protected void doLinkProtocolConfiguration(AssetAttribute protocolConfiguration) {
protocolConfiguration.getAssetId().ifPresent(agentId -> {
Collection<Device> devices = getService().getRegistry().getDevices();
if (devices.size() > 0) {
LOG.fine("Storing UPnP devices as child assets of agent: " + agentId);
for (Device device : getService().getRegistry().getDevices()) {
storeAssets(agentId, device);
}
}
});
}

@Override
protected void doUnlinkProtocolConfiguration(AssetAttribute protocolConfiguration) {
if (linkedProtocolConfigurations.size() == 1) {
closeService();
}
}

@Override
protected void doLinkAttribute(AssetAttribute attribute, AssetAttribute protocolConfiguration) {


}

@Override
protected void doUnlinkAttribute(AssetAttribute attribute, AssetAttribute protocolConfiguration) {

}

@Override
protected void processLinkedAttributeWrite(AttributeEvent event, AssetAttribute protocolConfiguration) {

}

protected UpnpService getService() {
if (INSTANCE == null) {
INSTANCE = new UpnpServiceImpl(registryListener);
INSTANCE.getControlPoint().search(new STAllHeader());
}
return INSTANCE;
}

protected void closeService() {
if (INSTANCE != null) {
INSTANCE.shutdown();
INSTANCE = null;
}
}

protected void storeAssets(String agentId, Device device) {
Asset asset = createAsset(agentId, device);
asset = assetService.mergeAsset(asset);
}

protected Asset createAsset(String parentId, Device device) {
Asset asset = new Asset(
device.getDisplayString(),
AssetType.THING
);
asset.setParentId(parentId);
asset.setId(getAssetId(device));

Optional.ofNullable(device.getType().getDisplayString()).ifPresent(v ->
asset.addAttributes(
new AssetAttribute("type", STRING, Values.create(v))
.addMeta(new MetaItem(LABEL, Values.create("Device Type")))
)
);

Optional.ofNullable(device.getDetails().getManufacturerDetails().getManufacturer()).ifPresent(v ->
asset.addAttributes(
new AssetAttribute("manufacturer", STRING, Values.create(v))
.addMeta(new MetaItem(LABEL, Values.create("Manufacturer")))
)
);

Optional.ofNullable(device.getDetails().getFriendlyName()).ifPresent(v ->
asset.addAttributes(
new AssetAttribute("friendlyName", STRING, Values.create(v))
.addMeta(new MetaItem(LABEL, Values.create("Friendly Name")))
)
);

Optional.ofNullable(device.getDetails().getModelDetails().getModelNumber()).ifPresent(v ->
asset.addAttributes(
new AssetAttribute("modelNumber", STRING, Values.create(v))
.addMeta(new MetaItem(LABEL, Values.create("Model Number")))
)
);

Optional.ofNullable(device.getDetails().getModelDetails().getModelName()).ifPresent(v ->
asset.addAttributes(
new AssetAttribute("modelName", STRING, Values.create(v))
.addMeta(new MetaItem(LABEL, Values.create("Model Name")))
)
);

Optional.ofNullable(device.getDetails().getModelDetails().getModelDescription()).ifPresent(v ->
asset.addAttributes(
new AssetAttribute("modelDescription", STRING, Values.create(v))
.addMeta(new MetaItem(LABEL, Values.create("Model Description")))
)
);

Optional.ofNullable(device.getDetails().getSerialNumber()).ifPresent(v ->
asset.addAttributes(
new AssetAttribute("serialNumber", STRING, Values.create(v))
.addMeta(new MetaItem(LABEL, Values.create("Serial Number")))
)
);

return asset;
}

protected String getAssetId(Device device) {
return Generators.nameBasedGenerator().generate(
device.getIdentity().getUdn().getIdentifierString()
).toString();
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
org.openremote.agent.protocol.simulator.SimulatorProtocol
org.openremote.agent.protocol.macro.MacroProtocol
org.openremote.agent.protocol.timer.TimerProtocol
org.openremote.agent.protocol.knx.KNXProtocol
org.openremote.agent.protocol.knx.KNXProtocol
org.openremote.agent.protocol.upnp.UpnpProtocol
2 changes: 2 additions & 0 deletions container/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ dependencies {
compile "org.apache.camel:camel-script:$camelVersion"
compile "org.apache.camel:camel-groovy:$camelVersion"

compile "com.fasterxml.uuid:java-uuid-generator:$uuidGeneratorVersion"

// Test environment dependencies
compile "org.glassfish.tyrus:tyrus-client:$tyrusVersion"
compile "org.glassfish.tyrus:tyrus-container-grizzly-client:$tyrusVersion"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@
*/
package org.openremote.container.persistence;

import com.fasterxml.uuid.Generators;
import com.fasterxml.uuid.impl.RandomBasedGenerator;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.IdentifierGenerator;
import org.openremote.container.util.IdentifierUtil;
import org.openremote.model.IdentifiableEntity;

import java.io.Serializable;
Expand All @@ -33,13 +34,15 @@
*/
public class UniqueIdentifierGenerator implements IdentifierGenerator {

final protected RandomBasedGenerator generator = Generators.randomBasedGenerator();

@Override
public Serializable generate(SharedSessionContractImplementor session, Object object) throws HibernateException {
if (object instanceof IdentifiableEntity) {
IdentifiableEntity identifiableEntity = (IdentifiableEntity) object;
if (identifiableEntity.getId() != null)
return identifiableEntity.getId();
}
return IdentifierUtil.generateGlobalUniqueId();
return generator.generate().toString();
}
}

This file was deleted.

4 changes: 3 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ tyrusVersion = 1.10
sunriseSunsetCalculatorVersion = 1.2
droolsVersion = 6.5.0.Final
quartzVersion= 2.2.3
calimeroVersion = 2.3
calimeroVersion = 2.3
clingVersion = 2.1.1
uuidGeneratorVersion = 3.1.3
Loading

0 comments on commit 94574fd

Please sign in to comment.