use of org.eclipse.smarthome.config.discovery.DiscoveryListener in project smarthome by eclipse.
the class HueBridgeNupnpDiscoveryOSGITest method invalidBridgesAreNotDiscovered.
@Test
public void invalidBridgesAreNotDiscovered() {
List<DiscoveryResult> oldResults = inbox.stream().filter(forThingTypeUID(BRIDGE_THING_TYPE_UID)).collect(Collectors.toList());
for (final DiscoveryResult oldResult : oldResults) {
inbox.remove(oldResult.getThingUID());
}
sut = new ConfigurableBridgeNupnpDiscoveryMock();
registerService(sut, DiscoveryService.class.getName());
final Map<ThingUID, DiscoveryResult> results = new HashMap<>();
registerDiscoveryListener(new DiscoveryListener() {
@Override
public void thingDiscovered(DiscoveryService source, DiscoveryResult result) {
results.put(result.getThingUID(), result);
}
@Override
public void thingRemoved(DiscoveryService source, ThingUID thingUID) {
}
@Override
public Collection<ThingUID> removeOlderResults(DiscoveryService source, long timestamp, Collection<ThingTypeUID> thingTypeUIDs, ThingUID bridgeUID) {
return null;
}
});
// missing ip
discoveryResult = "[{\"id\":\"001788fffe20057f\",\"internalipaddress\":}]";
sut.startScan();
waitForAssert(() -> {
assertThat(results.size(), is(0));
});
// missing id
discoveryResult = "[{\"id\":\"\",\"internalipaddress\":192.168.30.22}]";
sut.startScan();
waitForAssert(() -> {
assertThat(results.size(), is(0));
});
// id < 10
discoveryResult = "[{\"id\":\"012345678\",\"internalipaddress\":192.168.30.22}]";
sut.startScan();
waitForAssert(() -> {
assertThat(results.size(), is(0));
});
// bridge indicator not part of id
discoveryResult = "[{\"id\":\"0123456789\",\"internalipaddress\":192.168.30.22}]";
sut.startScan();
waitForAssert(() -> {
assertThat(results.size(), is(0));
});
// bridge indicator at wrong position (-1)
discoveryResult = "[{\"id\":\"01234" + HueBridgeNupnpDiscovery.BRIDGE_INDICATOR + "7891\",\"internalipaddress\":192.168.30.22}]";
sut.startScan();
waitForAssert(() -> {
assertThat(results.size(), is(0));
});
// bridge indicator at wrong position (+1)
discoveryResult = "[{\"id\":\"0123456" + HueBridgeNupnpDiscovery.BRIDGE_INDICATOR + "7891\",\"internalipaddress\":192.168.30.22}]";
sut.startScan();
waitForAssert(() -> {
assertThat(results.size(), is(0));
});
// bridge not reachable
discoveryResult = "[{\"id\":\"001788fffe20057f\",\"internalipaddress\":192.168.30.1}]";
sut.startScan();
waitForAssert(() -> {
assertThat(results.size(), is(0));
});
// invalid bridge description
expBridgeDescription = "";
;
discoveryResult = "[{\"id\":\"001788fffe20057f\",\"internalipaddress\":" + ip1 + "}]";
sut.startScan();
waitForAssert(() -> {
assertThat(results.size(), is(0));
});
waitForAssert(() -> {
assertThat(inbox.stream().filter(forThingTypeUID(BRIDGE_THING_TYPE_UID)).collect(Collectors.toList()).size(), is(0));
});
}
use of org.eclipse.smarthome.config.discovery.DiscoveryListener in project smarthome by eclipse.
the class TradfriDiscoveryServiceTest method setUp.
@Before
public void setUp() {
initMocks(this);
when(handler.getThing()).thenReturn(BridgeBuilder.create(GATEWAY_TYPE_UID, "1").build());
discovery = new TradfriDiscoveryService(handler);
listener = new DiscoveryListener() {
@Override
public void thingRemoved(DiscoveryService source, ThingUID thingUID) {
}
@Override
public void thingDiscovered(DiscoveryService source, DiscoveryResult result) {
discoveryResult = result;
}
@Override
public Collection<ThingUID> removeOlderResults(DiscoveryService source, long timestamp, Collection<ThingTypeUID> thingTypeUIDs, ThingUID bridgeUID) {
return null;
}
};
discovery.addDiscoveryListener(listener);
}
use of org.eclipse.smarthome.config.discovery.DiscoveryListener in project smarthome by eclipse.
the class DiscoveryServiceRegistryImpl method thingDiscovered.
@Override
public synchronized void thingDiscovered(final DiscoveryService source, final DiscoveryResult result) {
synchronized (cachedResults) {
cachedResults.remove(source, result);
cachedResults.put(source, result);
}
for (final DiscoveryListener listener : this.listeners) {
try {
AccessController.doPrivileged(new PrivilegedAction<@Nullable Void>() {
@Override
@Nullable
public Void run() {
listener.thingDiscovered(source, result);
return null;
}
});
} catch (Exception ex) {
logger.error("Cannot notify the DiscoveryListener {} on Thing discovered event!", listener.getClass().getName(), ex);
}
}
}
use of org.eclipse.smarthome.config.discovery.DiscoveryListener in project smarthome by eclipse.
the class HueLightDiscoveryServiceOSGiTest method hueLightRegistration.
@Test
public void hueLightRegistration() {
FullLight light = new FullLight();
light.setId("1");
light.setModelID("LCT001");
light.setType("Extended color light");
AsyncResultWrapper<DiscoveryResult> resultWrapper = new AsyncResultWrapper<DiscoveryResult>();
registerDiscoveryListener(new DiscoveryListener() {
@Override
public void thingDiscovered(DiscoveryService source, DiscoveryResult result) {
resultWrapper.set(result);
}
@Override
public void thingRemoved(DiscoveryService source, ThingUID thingUID) {
}
@Override
public Collection<ThingUID> removeOlderResults(DiscoveryService source, long timestamp, Collection<ThingTypeUID> thingTypeUIDs, ThingUID bridgeUID) {
return null;
}
});
discoveryService.onLightAdded(null, light);
waitForAssert(() -> {
assertTrue(resultWrapper.isSet());
});
final DiscoveryResult result = resultWrapper.getWrappedObject();
assertThat(result.getFlag(), is(DiscoveryResultFlag.NEW));
assertThat(result.getThingUID().toString(), is("hue:0210:testBridge:" + light.getId()));
assertThat(result.getThingTypeUID(), is(THING_TYPE_EXTENDED_COLOR_LIGHT));
assertThat(result.getBridgeUID(), is(hueBridge.getUID()));
assertThat(result.getProperties().get(LIGHT_ID), is(light.getId()));
}
use of org.eclipse.smarthome.config.discovery.DiscoveryListener in project smarthome by eclipse.
the class HueBridgeNupnpDiscoveryOSGITest method validBridgesAreDiscovered.
@Test
public void validBridgesAreDiscovered() {
List<DiscoveryResult> oldResults = inbox.stream().filter(forThingTypeUID(BRIDGE_THING_TYPE_UID)).collect(Collectors.toList());
for (final DiscoveryResult oldResult : oldResults) {
inbox.remove(oldResult.getThingUID());
}
sut = new ConfigurableBridgeNupnpDiscoveryMock();
registerService(sut, DiscoveryService.class.getName());
discoveryResult = validBridgeDiscoveryResult;
final Map<ThingUID, DiscoveryResult> results = new HashMap<>();
registerDiscoveryListener(new DiscoveryListener() {
@Override
public void thingDiscovered(DiscoveryService source, DiscoveryResult result) {
results.put(result.getThingUID(), result);
}
@Override
public void thingRemoved(DiscoveryService source, ThingUID thingUID) {
}
@Override
public Collection<ThingUID> removeOlderResults(DiscoveryService source, long timestamp, Collection<ThingTypeUID> thingTypeUIDs, ThingUID bridgeUID) {
return null;
}
});
sut.startScan();
waitForAssert(() -> {
assertThat(results.size(), is(2));
assertThat(results.get(BRIDGE_THING_UID_1), is(notNullValue()));
checkDiscoveryResult(results.get(BRIDGE_THING_UID_1), ip1, sn1);
assertThat(results.get(BRIDGE_THING_UID_2), is(notNullValue()));
checkDiscoveryResult(results.get(BRIDGE_THING_UID_2), ip2, sn2);
final List<DiscoveryResult> inboxResults = inbox.stream().filter(forThingTypeUID(BRIDGE_THING_TYPE_UID)).collect(Collectors.toList());
assertTrue(inboxResults.size() >= 2);
assertThat(inboxResults.stream().filter(result -> result.getThingUID().equals(BRIDGE_THING_UID_1)).findFirst().orElse(null), is(notNullValue()));
assertThat(inboxResults.stream().filter(result -> result.getThingUID().equals(BRIDGE_THING_UID_2)).findFirst().orElse(null), is(notNullValue()));
});
}
Aggregations