Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP][io.upnp] Sends periodic M-Search for devices #4527

Open
wants to merge 1 commit into
base: 4.3.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
import org.jupnp.model.gena.CancelReason;
import org.jupnp.model.gena.GENASubscription;
import org.jupnp.model.message.UpnpResponse;
import org.jupnp.model.message.discovery.OutgoingSearchRequest;
import org.jupnp.model.message.header.UDNHeader;
import org.jupnp.model.message.header.UpnpHeader;
import org.jupnp.model.meta.Action;
import org.jupnp.model.meta.Device;
import org.jupnp.model.meta.DeviceIdentity;
Expand All @@ -46,6 +49,8 @@
import org.jupnp.model.types.UDN;
import org.jupnp.registry.Registry;
import org.jupnp.registry.RegistryListener;
import org.jupnp.transport.Router;
import org.jupnp.transport.RouterException;
import org.openhab.core.common.ThreadPoolManager;
import org.openhab.core.io.transport.upnp.UpnpIOParticipant;
import org.openhab.core.io.transport.upnp.UpnpIOService;
Expand Down Expand Up @@ -361,6 +366,7 @@ public boolean isRegistered(UpnpIOParticipant participant) {
public void registerParticipant(UpnpIOParticipant participant) {
if (participant != null) {
participants.add(participant);
sendDeviceSearchRequest(participant);
}
}

Expand Down Expand Up @@ -434,6 +440,10 @@ public UPNPPollingRunnable(UpnpIOParticipant participant, String serviceID, Stri
public void run() {
// It is assumed that during addStatusListener() a check is made whether the participant is correctly
// registered

// before polling, send a search request to the device to keep it registered
sendDeviceSearchRequest(participant);

try {
Device device = getDevice(participant);
if (device != null) {
Expand Down Expand Up @@ -471,6 +481,27 @@ public void run() {
}
}

/**
* Send a device search request to the UPnP remote device.
*
* Some devices, such as LinkPlay based systems (WiiM, Arylic, etc.) loose their registrations over time. Sending a
* periodic search request will help keep the device registered.
*/
protected void sendDeviceSearchRequest(UpnpIOParticipant participant) {
try {
UpnpHeader<UDN> searchTarget = new UDNHeader(new UDN(participant.getUDN()));
OutgoingSearchRequest searchRequest = new OutgoingSearchRequest(searchTarget, 5);
Router router = upnpService.getRouter();
// this would only be null if running unit tests
if (router != null) {
router.send(searchRequest);
logger.debug("M-SEARCH query sent for device UDN: {}", searchTarget.getValue());
}
} catch (RouterException e) {
logger.debug("Failed to send M-SEARCH", e);
}
}

@Override
public void addStatusListener(UpnpIOParticipant participant, String serviceID, String actionID, int interval) {
if (participant != null) {
Expand Down