Skip to content

Commit

Permalink
Added model for Kodi specific entities
Browse files Browse the repository at this point in the history
Signed-off-by: Christoph Weitkamp <[email protected]>
  • Loading branch information
cweitkamp committed Feb 8, 2018
1 parent cddc17f commit 5a2756d
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 160 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<KodiPVRChannel> channels = connection
.getChannelsAsList(getPVRChannelGroupID(channelType, config.getGroup()));
int channelGroupID = getPVRChannelGroupID(channelType, config.getGroup());
List<KodiPVRChannel> channels = connection.getChannels(channelGroupID);
List<StateOption> options = new ArrayList<>();
for (KodiPVRChannel channel : channels) {
options.add(new StateOption(channel.getLabel(), channel.getLabel()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,21 @@
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;
import org.openhab.binding.kodi.internal.KodiEventListener;
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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<KodiPVRChannelGroup> getChannelGroups(final String channelType) {
String method = "PVR.GetChannelGroups";
String hash = method + "#channeltype=" + channelType;
JsonElement response = REQUEST_CACHE.putIfAbsentAndGet(hash, () -> {
Expand All @@ -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<KodiPVRChannelGroup> 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<KodiPVRChannelGroup> 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<KodiPVRChannel> getChannels(final int channelGroupID) {
String method = "PVR.GetChannels";
String hash = method + "#channelgroupid=" + channelGroupID;
JsonElement response = REQUEST_CACHE.putIfAbsentAndGet(hash, () -> {
Expand All @@ -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<KodiPVRChannel> channels = new ArrayList<>();
if (response instanceof JsonObject) {
JsonObject result = response.getAsJsonObject();
if (result.has("channels")) {
return result.get("channels").getAsJsonArray();
}
}
return null;
}

public List<KodiPVRChannel> getChannelsAsList(final int channelGroupID) {
JsonArray array = getChannels(channelGroupID);
if (array instanceof JsonArray) {
List<KodiPVRChannel> 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<KodiPVRChannel> channels = getChannels(channelGroupID);
for (KodiPVRChannel channel : channels) {
if (StringUtils.equalsIgnoreCase(channel.getLabel(), channelName)) {
return channel.getId();
}
}
return 0;
Expand Down

0 comments on commit 5a2756d

Please sign in to comment.