]> git.basschouten.com Git - openhab-addons.git/blob
faf5ea11243b1d50d2f0a2f5d29f3f291050fc48
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.samsungtv.internal;
14
15 import java.io.File;
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;
22
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;
30
31 /**
32  * The {@link SamsungTvAppWatchService} provides a list of apps for >2020 Samsung TV's
33  * File should be in json format
34  *
35  * @author Nick Waterton - Initial contribution
36  * @author Nick Waterton - Refactored to new WatchService
37  */
38 @Component(service = SamsungTvAppWatchService.class)
39 @NonNullByDefault
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";
43
44     private final Logger logger = LoggerFactory.getLogger(SamsungTvAppWatchService.class);
45     private final RemoteControllerWebSocket remoteControllerWebSocket;
46     private String host = "";
47     private boolean started = false;
48     int count = 0;
49
50     public SamsungTvAppWatchService(String host, RemoteControllerWebSocket remoteControllerWebSocket) {
51         this.host = host;
52         this.remoteControllerWebSocket = remoteControllerWebSocket;
53     }
54
55     public void start() {
56         File file = new File(APPS_PATH, APPS_FILE);
57         if (file.exists() && !getStarted()) {
58             logger.info("{}: Starting Apps File monitoring service", host);
59             started = true;
60             readFileApps();
61         } else if (count++ == 0) {
62             logger.warn("{}: cannot start Apps File monitoring service, file {} does not exist", host, file.toString());
63             remoteControllerWebSocket.addKnownAppIds();
64         }
65     }
66
67     public boolean getStarted() {
68         return started;
69     }
70
71     /**
72      * Check file path for existance
73      *
74      */
75     public boolean checkFileDir() {
76         File file = new File(APPS_PATH, APPS_FILE);
77         return file.exists();
78     }
79
80     public void readFileApps() {
81         processWatchEvent(WatchService.Kind.MODIFY, Paths.get(APPS_PATH, APPS_FILE));
82     }
83
84     public boolean watchSubDirectories() {
85         return false;
86     }
87
88     @Override
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);
92             try {
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());
100             }
101         }
102     }
103 }