forked from openhab/openhab-addons
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Nick Waterton <[email protected]>
- Loading branch information
1 parent
597f01e
commit 30c6368
Showing
43 changed files
with
5,423 additions
and
1,839 deletions.
There are no files selected for viewing
Empty file.
645 changes: 604 additions & 41 deletions
645
bundles/org.openhab.binding.samsungtv/README.md
100644 → 100755
Large diffs are not rendered by default.
Oops, something went wrong.
Empty file.
Empty file modified
0
bundles/org.openhab.binding.samsungtv/src/main/feature/feature.xml
100644 → 100755
Empty file.
103 changes: 103 additions & 0 deletions
103
...sungtv/src/main/java/org/openhab/binding/samsungtv/internal/SamsungTvAppWatchService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/** | ||
* Copyright (c) 2010-2023 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.binding.samsungtv.internal; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.openhab.binding.samsungtv.internal.protocol.RemoteControllerWebSocket; | ||
import org.openhab.core.OpenHAB; | ||
import org.openhab.core.service.WatchService; | ||
import org.osgi.service.component.annotations.Component; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* The {@link SamsungTvAppWatchService} provides a list of apps for >2020 Samsung TV's | ||
* File should be in json format | ||
* | ||
* @author Nick Waterton - Initial contribution | ||
* @author Nick Waterton - Refactored to new WatchService | ||
*/ | ||
@Component(service = SamsungTvAppWatchService.class) | ||
@NonNullByDefault | ||
public class SamsungTvAppWatchService implements WatchService.WatchEventListener { | ||
private static final String APPS_PATH = OpenHAB.getConfigFolder() + File.separator + "services"; | ||
private static final String APPS_FILE = "samsungtv.cfg"; | ||
|
||
private final Logger logger = LoggerFactory.getLogger(SamsungTvAppWatchService.class); | ||
private final RemoteControllerWebSocket remoteControllerWebSocket; | ||
private String host = ""; | ||
private boolean started = false; | ||
int count = 0; | ||
|
||
public SamsungTvAppWatchService(String host, RemoteControllerWebSocket remoteControllerWebSocket) { | ||
this.host = host; | ||
this.remoteControllerWebSocket = remoteControllerWebSocket; | ||
} | ||
|
||
public void start() { | ||
File file = new File(APPS_PATH, APPS_FILE); | ||
if (file.exists() && !getStarted()) { | ||
logger.info("{}: Starting Apps File monitoring service", host); | ||
started = true; | ||
readFileApps(); | ||
} else if (count++ == 0) { | ||
logger.warn("{}: cannot start Apps File monitoring service, file {} does not exist", host, file.toString()); | ||
remoteControllerWebSocket.addKnownAppIds(); | ||
} | ||
} | ||
|
||
public boolean getStarted() { | ||
return started; | ||
} | ||
|
||
/** | ||
* Check file path for existance | ||
* | ||
*/ | ||
public boolean checkFileDir() { | ||
File file = new File(APPS_PATH, APPS_FILE); | ||
return file.exists(); | ||
} | ||
|
||
public void readFileApps() { | ||
processWatchEvent(WatchService.Kind.MODIFY, Paths.get(APPS_PATH, APPS_FILE)); | ||
} | ||
|
||
public boolean watchSubDirectories() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public void processWatchEvent(WatchService.Kind kind, Path path) { | ||
if (path.endsWith(APPS_FILE) && kind != WatchService.Kind.DELETE) { | ||
logger.debug("{}: Updating Apps list from FILE {}", host, path); | ||
try { | ||
@SuppressWarnings("null") | ||
List<String> allLines = Files.lines(path).filter(line -> !line.trim().startsWith("#")) | ||
.collect(Collectors.toList()); | ||
logger.debug("{}: Updated Apps list, {} apps in list", host, allLines.size()); | ||
remoteControllerWebSocket.updateAppList(allLines); | ||
} catch (IOException e) { | ||
logger.debug("{}: Cannot read apps file: {}", host, e.getMessage()); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file modified
0
...rc/main/java/org/openhab/binding/samsungtv/internal/SamsungTvTlsTrustManagerProvider.java
100644 → 100755
Empty file.
114 changes: 114 additions & 0 deletions
114
...openhab.binding.samsungtv/src/main/java/org/openhab/binding/samsungtv/internal/Utils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/** | ||
* Copyright (c) 2010-2023 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.binding.samsungtv.internal; | ||
|
||
import java.io.IOException; | ||
import java.io.StringReader; | ||
import java.util.Base64; | ||
import java.util.Optional; | ||
|
||
import javax.xml.parsers.DocumentBuilderFactory; | ||
import javax.xml.parsers.ParserConfigurationException; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
import org.jupnp.model.meta.RemoteDevice; | ||
import org.openhab.core.types.Command; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.w3c.dom.Document; | ||
import org.xml.sax.InputSource; | ||
import org.xml.sax.SAXException; | ||
|
||
/** | ||
* The {@link Utils} is a collection of static utilities | ||
* | ||
* @author Nick Waterton - Initial contribution | ||
*/ | ||
@NonNullByDefault | ||
public class Utils { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(Utils.class); | ||
public static DocumentBuilderFactory factory = getDocumentBuilder(); | ||
|
||
private static DocumentBuilderFactory getDocumentBuilder() { | ||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); | ||
try { | ||
// see https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html | ||
factory.setFeature("http://xml.org/sax/features/external-general-entities", false); | ||
factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false); | ||
factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); | ||
factory.setXIncludeAware(false); | ||
factory.setExpandEntityReferences(false); | ||
} catch (ParserConfigurationException e) { | ||
LOGGER.debug("XMLParser Configuration Error: {}", e.getMessage()); | ||
} | ||
return Optional.ofNullable(factory).orElse(DocumentBuilderFactory.newInstance()); | ||
} | ||
|
||
/** | ||
* Build {@link Document} from {@link String} which contains XML content. | ||
* | ||
* @param xml | ||
* {@link String} which contains XML content. | ||
* @return {@link Optional Document} or empty if convert has failed. | ||
*/ | ||
public static Optional<Document> loadXMLFromString(String xml, String host) { | ||
try { | ||
return Optional.ofNullable(factory.newDocumentBuilder().parse(new InputSource(new StringReader(xml)))); | ||
} catch (ParserConfigurationException | SAXException | IOException e) { | ||
LOGGER.debug("{}: Error loading XML: {}", host, e.getMessage()); | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
public static boolean isSoundChannel(String name) { | ||
return (name.contains("Volume") || name.contains("Mute")); | ||
} | ||
|
||
public static String b64encode(String str) { | ||
return Base64.getUrlEncoder().encodeToString(str.getBytes()); | ||
} | ||
|
||
public static String truncCmd(Command command) { | ||
String cmd = command.toString(); | ||
return (cmd.length() <= 80) ? cmd : cmd.substring(0, 80) + "..."; | ||
} | ||
|
||
public static String getModelName(@Nullable RemoteDevice device) { | ||
return Optional.ofNullable(device).map(a -> a.getDetails()).map(a -> a.getModelDetails()) | ||
.map(a -> a.getModelName()).orElse(""); | ||
} | ||
|
||
public static String getManufacturer(@Nullable RemoteDevice device) { | ||
return Optional.ofNullable(device).map(a -> a.getDetails()).map(a -> a.getManufacturerDetails()) | ||
.map(a -> a.getManufacturer()).orElse(""); | ||
} | ||
|
||
public static String getFriendlyName(@Nullable RemoteDevice device) { | ||
return Optional.ofNullable(device).map(a -> a.getDetails()).map(a -> a.getFriendlyName()).orElse(""); | ||
} | ||
|
||
public static String getUdn(@Nullable RemoteDevice device) { | ||
return Optional.ofNullable(device).map(a -> a.getIdentity()).map(a -> a.getUdn()) | ||
.map(a -> a.getIdentifierString()).orElse(""); | ||
} | ||
|
||
public static String getHost(@Nullable RemoteDevice device) { | ||
return Optional.ofNullable(device).map(a -> a.getIdentity()).map(a -> a.getDescriptorURL()) | ||
.map(a -> a.getHost()).orElse(""); | ||
} | ||
|
||
public static String getType(@Nullable RemoteDevice device) { | ||
return Optional.ofNullable(device).map(a -> a.getType()).map(a -> a.getType()).orElse(""); | ||
} | ||
} |
Oops, something went wrong.