2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.samsungtv.internal;
16 import java.io.IOException;
17 import java.nio.file.Files;
18 import java.nio.file.Path;
19 import java.nio.file.Paths;
20 import java.util.List;
21 import java.util.stream.Collectors;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.openhab.binding.samsungtv.internal.protocol.RemoteControllerWebSocket;
25 import org.openhab.core.OpenHAB;
26 import org.openhab.core.service.WatchService;
27 import org.osgi.service.component.annotations.Component;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * The {@link SamsungTvAppWatchService} provides a list of apps for >2020 Samsung TV's
33 * File should be in json format
35 * @author Nick Waterton - Initial contribution
36 * @author Nick Waterton - Refactored to new WatchService
38 @Component(service = SamsungTvAppWatchService.class)
40 public class SamsungTvAppWatchService implements WatchService.WatchEventListener {
41 private static final String APPS_PATH = OpenHAB.getConfigFolder() + File.separator + "services";
42 private static final String APPS_FILE = "samsungtv.cfg";
44 private final Logger logger = LoggerFactory.getLogger(SamsungTvAppWatchService.class);
45 private final RemoteControllerWebSocket remoteControllerWebSocket;
46 private String host = "";
47 private boolean started = false;
50 public SamsungTvAppWatchService(String host, RemoteControllerWebSocket remoteControllerWebSocket) {
52 this.remoteControllerWebSocket = remoteControllerWebSocket;
56 File file = new File(APPS_PATH, APPS_FILE);
57 if (file.exists() && !getStarted()) {
58 logger.info("{}: Starting Apps File monitoring service", host);
61 } else if (count++ == 0) {
62 logger.warn("{}: cannot start Apps File monitoring service, file {} does not exist", host, file.toString());
63 remoteControllerWebSocket.addKnownAppIds();
67 public boolean getStarted() {
72 * Check file path for existance
75 public boolean checkFileDir() {
76 File file = new File(APPS_PATH, APPS_FILE);
80 public void readFileApps() {
81 processWatchEvent(WatchService.Kind.MODIFY, Paths.get(APPS_PATH, APPS_FILE));
84 public boolean watchSubDirectories() {
89 public void processWatchEvent(WatchService.Kind kind, Path path) {
90 if (path.endsWith(APPS_FILE) && kind != WatchService.Kind.DELETE) {
91 logger.debug("{}: Updating Apps list from FILE {}", host, path);
93 @SuppressWarnings("null")
94 List<String> allLines = Files.lines(path).filter(line -> !line.trim().startsWith("#"))
95 .collect(Collectors.toList());
96 logger.debug("{}: Updated Apps list, {} apps in list", host, allLines.size());
97 remoteControllerWebSocket.updateAppList(allLines);
98 } catch (IOException e) {
99 logger.debug("{}: Cannot read apps file: {}", host, e.getMessage());