From 5a2756d39f10ba2af32bb65f6eded7938eec0adc Mon Sep 17 00:00:00 2001 From: Christoph Weitkamp Date: Thu, 8 Feb 2018 10:32:53 +0100 Subject: [PATCH] Added model for Kodi specific entities Signed-off-by: Christoph Weitkamp --- .../binding/kodi/handler/KodiHandler.java | 8 +- .../kodi/internal/model/KodiPVRChannel.java | 12 +++ .../internal/model/KodiPVRChannelGroups.java | 44 --------- .../kodi/internal/model/KodiPVRChannels.java | 44 --------- .../internal/protocol/KodiConnection.java | 95 ++++++------------- 5 files changed, 43 insertions(+), 160 deletions(-) delete mode 100644 addons/binding/org.openhab.binding.kodi/src/main/java/org/openhab/binding/kodi/internal/model/KodiPVRChannelGroups.java delete mode 100644 addons/binding/org.openhab.binding.kodi/src/main/java/org/openhab/binding/kodi/internal/model/KodiPVRChannels.java diff --git a/addons/binding/org.openhab.binding.kodi/src/main/java/org/openhab/binding/kodi/handler/KodiHandler.java b/addons/binding/org.openhab.binding.kodi/src/main/java/org/openhab/binding/kodi/handler/KodiHandler.java index 486190bb4931a..3b4259f58d71a 100644 --- a/addons/binding/org.openhab.binding.kodi/src/main/java/org/openhab/binding/kodi/handler/KodiHandler.java +++ b/addons/binding/org.openhab.binding.kodi/src/main/java/org/openhab/binding/kodi/handler/KodiHandler.java @@ -253,8 +253,8 @@ public void playURI(Command command) { public void playPVRChannel(final Command command, final String channelType, final String channelId) { KodiChannelConfig config = getThing().getChannel(channelId).getConfiguration().as(KodiChannelConfig.class); - int channelID = connection.getChannelID(getPVRChannelGroupID(channelType, config.getGroup()), - command.toString()); + int channelGroupID = getPVRChannelGroupID(channelType, config.getGroup()); + int channelID = connection.getChannelID(channelGroupID, command.toString()); if (channelID > 0) { connection.playPVRChannel(channelID); } else { @@ -317,8 +317,8 @@ public void initialize() { private void updatePVRChannelStateDescription(final String channelType, final String channelId) { if (isLinked(channelId)) { KodiChannelConfig config = getThing().getChannel(channelId).getConfiguration().as(KodiChannelConfig.class); - List channels = connection - .getChannelsAsList(getPVRChannelGroupID(channelType, config.getGroup())); + int channelGroupID = getPVRChannelGroupID(channelType, config.getGroup()); + List channels = connection.getChannels(channelGroupID); List options = new ArrayList<>(); for (KodiPVRChannel channel : channels) { options.add(new StateOption(channel.getLabel(), channel.getLabel())); diff --git a/addons/binding/org.openhab.binding.kodi/src/main/java/org/openhab/binding/kodi/internal/model/KodiPVRChannel.java b/addons/binding/org.openhab.binding.kodi/src/main/java/org/openhab/binding/kodi/internal/model/KodiPVRChannel.java index 2e4f550d49984..2d07ada26e9cc 100644 --- a/addons/binding/org.openhab.binding.kodi/src/main/java/org/openhab/binding/kodi/internal/model/KodiPVRChannel.java +++ b/addons/binding/org.openhab.binding.kodi/src/main/java/org/openhab/binding/kodi/internal/model/KodiPVRChannel.java @@ -18,6 +18,10 @@ public class KodiPVRChannel extends KodiBaseItem { * The PVR channel id */ private int channelId; + /** + * The PVR channel group id + */ + private int channelGroupId; public int getId() { return channelId; @@ -26,4 +30,12 @@ public int getId() { public void setId(int channelId) { this.channelId = channelId; } + + public int getChannelGroupId() { + return channelGroupId; + } + + public void setChannelGroupId(int channelGroupId) { + this.channelGroupId = channelGroupId; + } } diff --git a/addons/binding/org.openhab.binding.kodi/src/main/java/org/openhab/binding/kodi/internal/model/KodiPVRChannelGroups.java b/addons/binding/org.openhab.binding.kodi/src/main/java/org/openhab/binding/kodi/internal/model/KodiPVRChannelGroups.java deleted file mode 100644 index 0b1fd01ba6cae..0000000000000 --- a/addons/binding/org.openhab.binding.kodi/src/main/java/org/openhab/binding/kodi/internal/model/KodiPVRChannelGroups.java +++ /dev/null @@ -1,44 +0,0 @@ -/** - * Copyright (c) 2010-2018 by the respective copyright holders. - * - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - */ -package org.openhab.binding.kodi.internal.model; - -import java.util.List; - -/** - * Class representing a Kodi PVR channel group list - * - * @author Christoph Weitkamp - Initial contribution - */ -public class KodiPVRChannelGroups { - /** - * The PVR channel type - */ - private String channelType; - - /** - * A list of {@link KodiPVRChannelGroup}s - */ - private List channelGroups; - - public String getChannelType() { - return channelType; - } - - public void setChannelType(final String channelType) { - this.channelType = channelType; - } - - public List getChannelgroups() { - return channelGroups; - } - - public void setChannelgroups(List channelGroups) { - this.channelGroups = channelGroups; - } -} diff --git a/addons/binding/org.openhab.binding.kodi/src/main/java/org/openhab/binding/kodi/internal/model/KodiPVRChannels.java b/addons/binding/org.openhab.binding.kodi/src/main/java/org/openhab/binding/kodi/internal/model/KodiPVRChannels.java deleted file mode 100644 index 58650d3ef833e..0000000000000 --- a/addons/binding/org.openhab.binding.kodi/src/main/java/org/openhab/binding/kodi/internal/model/KodiPVRChannels.java +++ /dev/null @@ -1,44 +0,0 @@ -/** - * Copyright (c) 2010-2018 by the respective copyright holders. - * - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - */ -package org.openhab.binding.kodi.internal.model; - -import java.util.List; - -/** - * Class representing a Kodi PVR channel list - * - * @author Christoph Weitkamp - Initial contribution - */ -public class KodiPVRChannels { - /** - * The PVR channel group id of the related {@link KodiPVRChannelGroup} - */ - private int channelGroupId; - - /** - * A list of {@link KodiPVRChannel}s - */ - private List channels; - - public int getChannelGroupId() { - return channelGroupId; - } - - public void setChannelGroupId(int channelGroupId) { - this.channelGroupId = channelGroupId; - } - - public List getChannels() { - return channels; - } - - public void setChannels(List channels) { - this.channels = channels; - } -} diff --git a/addons/binding/org.openhab.binding.kodi/src/main/java/org/openhab/binding/kodi/internal/protocol/KodiConnection.java b/addons/binding/org.openhab.binding.kodi/src/main/java/org/openhab/binding/kodi/internal/protocol/KodiConnection.java index c7978148a4577..8c5dcb3ac90a2 100644 --- a/addons/binding/org.openhab.binding.kodi/src/main/java/org/openhab/binding/kodi/internal/protocol/KodiConnection.java +++ b/addons/binding/org.openhab.binding.kodi/src/main/java/org/openhab/binding/kodi/internal/protocol/KodiConnection.java @@ -14,13 +14,11 @@ import java.net.URLEncoder; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collections; import java.util.List; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import org.apache.commons.lang.StringUtils; -import org.eclipse.jdt.annotation.Nullable; import org.eclipse.smarthome.core.cache.ExpiringCacheMap; import org.eclipse.smarthome.core.library.types.RawType; import org.eclipse.smarthome.io.net.http.HttpUtil; @@ -28,13 +26,9 @@ import org.openhab.binding.kodi.internal.KodiEventListener.KodiState; import org.openhab.binding.kodi.internal.model.KodiPVRChannel; import org.openhab.binding.kodi.internal.model.KodiPVRChannelGroup; -import org.openhab.binding.kodi.internal.model.KodiPVRChannelGroups; -import org.openhab.binding.kodi.internal.model.KodiPVRChannels; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; @@ -68,7 +62,6 @@ public class KodiConnection implements KodiClientSocketEventListener { private KodiState currentState = KodiState.Stop; private final KodiEventListener listener; - private static final Gson GSON = new GsonBuilder().create(); public KodiConnection(KodiEventListener listener) { this.listener = listener; @@ -575,9 +568,7 @@ public synchronized void playURI(String uri) { socket.callMethod("Player.Open", params); } - @Nullable - // public synchronized KodiPVRChannelGroups getChannelGroups(final String channelType) { - private synchronized JsonArray getChannelGroups(final String channelType) { + public synchronized List getChannelGroups(final String channelType) { String method = "PVR.GetChannelGroups"; String hash = method + "#channeltype=" + channelType; JsonElement response = REQUEST_CACHE.putIfAbsentAndGet(hash, () -> { @@ -586,42 +577,32 @@ private synchronized JsonArray getChannelGroups(final String channelType) { return socket.callMethod(method, params); }); - // if( response instanceof JsonElement) { - // KodiPVRChannelGroups channelGroups = GSON.fromJson(response, KodiPVRChannelGroups.class); - // channelGroups.setChannelType(channelType); - // return channelGroups; + List channelGroups = new ArrayList<>(); if (response instanceof JsonObject) { JsonObject result = response.getAsJsonObject(); - if (result.has("channelgroups")) { - return result.get("channelgroups").getAsJsonArray(); + for (JsonElement element : result.get("channelgroups").getAsJsonArray()) { + JsonObject object = (JsonObject) element; + KodiPVRChannelGroup channelGroup = new KodiPVRChannelGroup(); + channelGroup.setId(object.get("channelgroupid").getAsInt()); + channelGroup.setLabel(object.get("label").getAsString()); + channelGroup.setChannelType(channelType); + channelGroups.add(channelGroup); } } - return null; + return channelGroups; } public int getChannelGroupID(final String channelType, final String channelGroupName) { - // KodiPVRChannelGroups channelGroups = getChannelGroups(channelType); - // for (KodiPVRChannelGroup channelGroup : channelGroups.getChannelgroups()) { - // if (StringUtils.equalsIgnoreCase(channelGroup.getLabel(), channelGroupName)) { - // return channelGroup.getId(); - // } - // } - JsonArray channelGroups = getChannelGroups(channelType); - if (channelGroups instanceof JsonArray) { - for (JsonElement element : channelGroups) { - JsonObject channelGroup = (JsonObject) element; - String label = channelGroup.get("label").getAsString(); - if (StringUtils.equalsIgnoreCase(label, channelGroupName)) { - return channelGroup.get("channelgroupid").getAsInt(); - } + List channelGroups = getChannelGroups(channelType); + for (KodiPVRChannelGroup channelGroup : channelGroups) { + if (StringUtils.equalsIgnoreCase(channelGroup.getLabel(), channelGroupName)) { + return channelGroup.getId(); } } return 0; } - @Nullable - // public synchronized KodiPVRChannels getChannels(final int channelGroupID) { - private synchronized JsonArray getChannels(final int channelGroupID) { + public synchronized List getChannels(final int channelGroupID) { String method = "PVR.GetChannels"; String hash = method + "#channelgroupid=" + channelGroupID; JsonElement response = REQUEST_CACHE.putIfAbsentAndGet(hash, () -> { @@ -630,48 +611,26 @@ private synchronized JsonArray getChannels(final int channelGroupID) { return socket.callMethod(method, params); }); - // if (response instanceof JsonElement) { - // KodiPVRChannels channels = GSON.fromJson(response, KodiPVRChannels.class); - // channels.setChannelGroupId(channelGroupID); - // return channels; + List channels = new ArrayList<>(); if (response instanceof JsonObject) { JsonObject result = response.getAsJsonObject(); - if (result.has("channels")) { - return result.get("channels").getAsJsonArray(); - } - } - return null; - } - - public List getChannelsAsList(final int channelGroupID) { - JsonArray array = getChannels(channelGroupID); - if (array instanceof JsonArray) { - List channels = new ArrayList<>(); - for (JsonElement element : array) { - KodiPVRChannel channel = GSON.fromJson(element, KodiPVRChannel.class); + for (JsonElement element : result.get("channels").getAsJsonArray()) { + JsonObject object = (JsonObject) element; + KodiPVRChannel channel = new KodiPVRChannel(); + channel.setId(object.get("channelid").getAsInt()); + channel.setLabel(object.get("label").getAsString()); + channel.setChannelGroupId(channelGroupID); channels.add(channel); } - return Collections.unmodifiableList(channels); - } else { - return Collections.emptyList(); } + return channels; } public int getChannelID(final int channelGroupID, final String channelName) { - // KodiPVRChannels channels = getChannels(channelGroupID); - // for (KodiPVRChannel channel : channels.getChannels()) { - // if (StringUtils.equalsIgnoreCase(channel.getLabel(), channelName)) { - // return channel.getId(); - // } - // } - JsonArray channels = getChannels(channelGroupID); - if (channels instanceof JsonArray) { - for (JsonElement element : channels) { - JsonObject channel = (JsonObject) element; - String label = channel.get("label").getAsString(); - if (StringUtils.equalsIgnoreCase(label, channelName)) { - return channel.get("channelid").getAsInt(); - } + List channels = getChannels(channelGroupID); + for (KodiPVRChannel channel : channels) { + if (StringUtils.equalsIgnoreCase(channel.getLabel(), channelName)) { + return channel.getId(); } } return 0;