Adapt addons to core watch service changes (#14004)
Signed-off-by: Jan N. Klug <github@klug.nrw>
This commit is contained in:
@@ -12,7 +12,6 @@
|
||||
*/
|
||||
package org.openhab.binding.miio.internal.basic;
|
||||
|
||||
import static java.nio.file.StandardWatchEventKinds.*;
|
||||
import static org.openhab.binding.miio.internal.MiIoBindingConstants.BINDING_DATABASE_PATH;
|
||||
|
||||
import java.io.File;
|
||||
@@ -20,9 +19,6 @@ import java.io.IOException;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.WatchEvent;
|
||||
import java.nio.file.WatchEvent.Kind;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
@@ -33,11 +29,14 @@ import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
import org.openhab.binding.miio.internal.MiIoBindingConstants;
|
||||
import org.openhab.binding.miio.internal.Utils;
|
||||
import org.openhab.core.service.AbstractWatchService;
|
||||
import org.openhab.core.OpenHAB;
|
||||
import org.openhab.core.service.WatchService;
|
||||
import org.osgi.framework.Bundle;
|
||||
import org.osgi.framework.FrameworkUtil;
|
||||
import org.osgi.service.component.annotations.Activate;
|
||||
import org.osgi.service.component.annotations.Component;
|
||||
import org.osgi.service.component.annotations.Deactivate;
|
||||
import org.osgi.service.component.annotations.Reference;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -50,23 +49,29 @@ import com.google.gson.JsonParseException;
|
||||
* The {@link MiIoDatabaseWatchService} creates a registry of database file per ModelId
|
||||
*
|
||||
* @author Marcel Verpaalen - Initial contribution
|
||||
* @author Jan N. Klug - Refactored to new WatchService
|
||||
*/
|
||||
@Component(service = MiIoDatabaseWatchService.class)
|
||||
@NonNullByDefault
|
||||
public class MiIoDatabaseWatchService extends AbstractWatchService {
|
||||
public class MiIoDatabaseWatchService implements WatchService.WatchEventListener {
|
||||
private static final String DATABASE_FILES = ".json";
|
||||
private static final Gson GSON = new GsonBuilder().serializeNulls().create();
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(MiIoDatabaseWatchService.class);
|
||||
private final WatchService watchService;
|
||||
private Map<String, URL> databaseList = new HashMap<>();
|
||||
private final Path watchPath;
|
||||
|
||||
@Activate
|
||||
public MiIoDatabaseWatchService() {
|
||||
super(BINDING_DATABASE_PATH);
|
||||
public MiIoDatabaseWatchService(@Reference(target = WatchService.CONFIG_WATCHER_FILTER) WatchService watchService) {
|
||||
this.watchService = watchService;
|
||||
this.watchPath = Path.of(BINDING_DATABASE_PATH).relativize(Path.of(OpenHAB.getConfigFolder()));
|
||||
watchService.registerListener(this, watchPath);
|
||||
|
||||
logger.debug(
|
||||
"Started miio basic devices local databases watch service. Watching for database files at path: {}",
|
||||
BINDING_DATABASE_PATH);
|
||||
processWatchEvent(null, null, Paths.get(BINDING_DATABASE_PATH));
|
||||
processWatchEvent(WatchService.Kind.CREATE, watchPath);
|
||||
populateDatabase();
|
||||
if (logger.isTraceEnabled()) {
|
||||
for (String device : databaseList.keySet()) {
|
||||
@@ -75,24 +80,17 @@ public class MiIoDatabaseWatchService extends AbstractWatchService {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean watchSubDirectories() {
|
||||
return true;
|
||||
@Deactivate
|
||||
public void deactivate() {
|
||||
watchService.unregisterListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Kind<?> @Nullable [] getWatchEventKinds(@Nullable Path directory) {
|
||||
return new Kind<?>[] { ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY };
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void processWatchEvent(@Nullable WatchEvent<?> event, @Nullable Kind<?> kind, @Nullable Path path) {
|
||||
if (path != null) {
|
||||
final Path p = path.getFileName();
|
||||
if (p != null && p.toString().endsWith(DATABASE_FILES)) {
|
||||
logger.debug("Local Databases file {} changed. Refreshing device database.", p.getFileName());
|
||||
populateDatabase();
|
||||
}
|
||||
public void processWatchEvent(WatchService.Kind kind, Path path) {
|
||||
final Path p = path.getFileName();
|
||||
if (p != null && p.toString().endsWith(DATABASE_FILES)) {
|
||||
logger.debug("Local Databases file {} changed. Refreshing device database.", p.getFileName());
|
||||
populateDatabase();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user