Skip to content

Commit

Permalink
tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
NickWaterton committed Dec 30, 2021
1 parent b29c100 commit 3f66888
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 18 deletions.
1 change: 1 addition & 0 deletions bundles/org.openhab.binding.samsungtv/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ This only works on TV's after 2016, and has some quirks.
* Does not work on hardwired ethernet connected TV's **if you have a soundbar connected via ARC/eARC**
* Works on WiFi connected TV's (with or without soundbar)
* May need to enable this function on the TV
* May have to wait up to 1 minute before turning TV back on, as TV does not power down immediately (and so doesn't respond to WOL)

You will have to experiment to see if it works for you. If not, you can power on the TV using IR (if you have a Harmony Hub, or GC iTach or similar).

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
*/
package org.openhab.binding.samsungtv.internal;

import java.util.Base64;
import java.util.Optional;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.jupnp.model.meta.RemoteDevice;
import org.openhab.core.types.Command;

/**
* The {@link Utils} is a collection of static utilities for getting info from a UpNp remote device
Expand All @@ -26,6 +28,15 @@
@NonNullByDefault
public class Utils {

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("");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
*
* @author Arjan Mels - Initial contribution
* @author Laurent Garnier - Use improvements from the LG webOS binding
* @author Nick Waterton - refactored for efficiency
* @author Nick Waterton - use single ip address as source per interface
*
*/
@NonNullByDefault
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
*
* @author Pauli Anttila - Initial contribution
* @author Arjan Mels - Added MAC Address
* @author Nick Waterton - added Smartthings, fix minor compiler warnings
* @author Nick Waterton - added Smartthings, refactoring
*/

@SuppressWarnings("null")
@NonNullByDefault({})
public class SamsungTvConfiguration {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,14 +366,9 @@ public String fetchPowerState() {
return PowerState;
}

public String truncCmd(Command command) {
String cmd = command.toString();
return (cmd.length() <= 80) ? cmd : cmd.substring(0, 80) + "...";
}

@Override
public void handleCommand(ChannelUID channelUID, Command command) {
logger.debug("{}: Received channel: {}, command: {}", host, channelUID, truncCmd(command));
logger.debug("{}: Received channel: {}, command: {}", host, channelUID, Utils.truncCmd(command));

String channel = channelUID.getId();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* The {@link RemoteController} is the base class for handling remote control keys for the Samsung TV.
*
* @author Arjan Mels - Initial contribution
* @author Nick Waterton - added getArtmodeStatus()
* @author Nick Waterton - added getArtmodeStatus(), sendKeyPress()
*/
@NonNullByDefault
public abstract class RemoteController implements AutoCloseable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand All @@ -32,6 +31,7 @@
import org.eclipse.jetty.util.component.LifeCycle.Listener;
import org.eclipse.jetty.websocket.client.WebSocketClient;
import org.openhab.binding.samsungtv.internal.SamsungTvAppWatchService;
import org.openhab.binding.samsungtv.internal.Utils;
import org.openhab.binding.samsungtv.internal.config.SamsungTvConfiguration;
import org.openhab.binding.samsungtv.internal.service.RemoteControllerService;
import org.openhab.core.io.net.http.WebSocketFactory;
Expand All @@ -47,7 +47,7 @@
*
* @author Arjan Mels - Initial contribution
* @author Arjan Mels - Moved websocket inner classes to standalone classes
* @author Nick Waterton - added Action enum and some refactoring
* @author Nick Waterton - added Action enum, manual app handling and some refactoring
*/
@NonNullByDefault
public class RemoteControllerWebSocket extends RemoteController implements Listener {
Expand Down Expand Up @@ -214,14 +214,10 @@ private void logResult(String msg, Throwable cause) {
}
}

public static String b64encode(String str) {
return Base64.getUrlEncoder().encodeToString(str.getBytes());
}

private void connectWebSockets() {
logger.trace("{}: connectWebSockets()", host);

String encodedAppName = b64encode(appName);
String encodedAppName = Utils.b64encode(appName);

String protocol = (SamsungTvConfiguration.PROTOCOL_SECUREWEBSOCKET
.equals(callback.getConfig(SamsungTvConfiguration.PROTOCOL))) ? "wss" : "ws";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.samsungtv.internal.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -289,7 +290,7 @@ public JSONRemoteControl(String action, String value) {
params.TypeOfRemote = "SendInputEnd";
break;
case "Text":
params.Cmd = RemoteControllerWebSocket.b64encode(value);
params.Cmd = Utils.b64encode(value);
params.DataOfCmd = "base64";
params.TypeOfRemote = "SendInputString";
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.samsungtv.internal.Utils;
import org.openhab.binding.samsungtv.internal.handler.SamsungTvHandler;
import org.openhab.binding.samsungtv.internal.protocol.KeyCode;
import org.openhab.binding.samsungtv.internal.protocol.RemoteController;
Expand Down Expand Up @@ -158,7 +159,7 @@ public boolean isUpnp() {

@Override
public boolean handleCommand(String channel, Command command) {
logger.trace("{}: Received channel: {}, command: {}", host, channel, handler.truncCmd(command));
logger.trace("{}: Received channel: {}, command: {}", host, channel, Utils.truncCmd(command));

boolean result = false;
if (!remoteController.isConnected()) {
Expand Down

0 comments on commit 3f66888

Please sign in to comment.