Go to the documentation of this file.00001
00009 package org.rosmultimedia.player.xbmc;
00010
00011 import org.ros.exception.ServiceException;
00012 import org.ros.node.ConnectedNode;
00013 import org.ros.node.Node;
00014 import org.ros.node.service.ServiceResponseBuilder;
00015 import org.rosbuilding.common.BaseNodeMain;
00016 import org.rosbuilding.common.media.MediaMessageConverter;
00017 import org.rosbuilding.common.media.MediaStateDataComparator;
00018 import org.rosmultimedia.player.xbmc.internal.XbmcLibrary;
00019 import org.rosmultimedia.player.xbmc.internal.XbmcMonitor;
00020 import org.rosmultimedia.player.xbmc.internal.XbmcPlayer;
00021 import org.rosmultimedia.player.xbmc.internal.XbmcSpeaker;
00022 import org.rosmultimedia.player.xbmc.internal.XbmcSystem;
00023 import org.rosmultimedia.player.xbmc.jsonrpc.XbmcJson;
00024 import org.xbmc.android.jsonrpc.api.call.JSONRPC.Ping;
00025 import org.xbmc.android.jsonrpc.api.call.JSONRPC.Version;
00026
00027 import smarthome_media_msgs.MediaAction;
00028 import smarthome_media_msgs.MediaGetItem;
00029 import smarthome_media_msgs.MediaGetItemRequest;
00030 import smarthome_media_msgs.MediaGetItemResponse;
00031 import smarthome_media_msgs.MediaGetItems;
00032 import smarthome_media_msgs.MediaGetItemsRequest;
00033 import smarthome_media_msgs.MediaGetItemsResponse;
00034 import smarthome_media_msgs.StateData;
00035 import smarthome_media_msgs.ToggleMuteSpeaker;
00036 import smarthome_media_msgs.ToggleMuteSpeakerRequest;
00037 import smarthome_media_msgs.ToggleMuteSpeakerResponse;
00038
00045 public class XbmcNode extends BaseNodeMain<XbmcConfig, StateData, MediaAction> implements IXbmcNode {
00046
00047 public static final String SRV_MUTE_SPEAKER_TOGGLE = "speaker_mute_toggle";
00048 public static final String SRV_MEDIA_GET_ITEM = "get_item";
00049 public static final String SRV_MEDIA_GET_ITEMS = "get_items";
00050
00051 private XbmcJson xbmcJson;
00052
00053 private XbmcLibrary library;
00054 private XbmcSpeaker speaker;
00055
00056 public XbmcNode() {
00057 super("xbmc",
00058 new MediaStateDataComparator(),
00059 new MediaMessageConverter(),
00060 MediaAction._TYPE,
00061 StateData._TYPE);
00062 }
00063
00064 @Override
00065 public void onStart(final ConnectedNode connectedNode) {
00066 super.onStart(connectedNode);
00067 this.startFinal();
00068 }
00069
00070 @Override
00071 public void onShutdown(Node node) {
00072 super.onShutdown(node);
00073 }
00074
00075 @Override
00076 protected void onConnected() {
00077 this.getStateData().setState(StateData.ENABLE);
00078 }
00079
00080 @Override
00081 protected void onDisconnected() {
00082 this.getStateData().setState(StateData.UNKNOWN);
00083 }
00084
00085 @Override
00086 public void onNewMessage(MediaAction message) {
00087 if (message != null) {
00088 this.logI(String.format("Command \"%s\"... for %s",
00089 message.getMethod(),
00090 message.getUri()));
00091
00092 super.onNewMessage(message);
00093 }
00094 }
00095
00096 @Override
00097 protected void initialize() {
00098 super.initialize();
00099
00100 String url = String.format("http://%s:%d/jsonrpc",
00101 this.configuration.getHost(),
00102 this.configuration.getPort());
00103
00104 this.xbmcJson = new XbmcJson(
00105 url, this.configuration.getUser(), this.configuration.getPassword());
00106
00107 this.library = new XbmcLibrary(this.xbmcJson, this);
00108 this.speaker = new XbmcSpeaker(this.xbmcJson, this);
00109
00110 this.addModule(new XbmcMonitor());
00111 this.addModule(new XbmcPlayer(this.xbmcJson, this));
00112 this.addModule(new XbmcSystem(this.xbmcJson, this));
00113 this.addModule(this.speaker);
00114 }
00115
00116 @Override
00117 protected void initServices() {
00118 super.initServices();
00119
00120 this.getConnectedNode().newServiceServer(
00121 this.configuration.getPrefix() + SRV_MUTE_SPEAKER_TOGGLE,
00122 ToggleMuteSpeaker._TYPE,
00123 new ServiceResponseBuilder<ToggleMuteSpeakerRequest, ToggleMuteSpeakerResponse>() {
00124 @Override
00125 public void build(ToggleMuteSpeakerRequest request,
00126 ToggleMuteSpeakerResponse response) throws ServiceException {
00127 XbmcNode.this.speaker.handleSpeakerMuteToggle(request, response);
00128 }
00129 });
00130
00131 this.getConnectedNode().newServiceServer(
00132 this.configuration.getPrefix() + SRV_MEDIA_GET_ITEM,
00133 MediaGetItem._TYPE,
00134 new ServiceResponseBuilder<MediaGetItemRequest, MediaGetItemResponse>() {
00135 @Override
00136 public void build(MediaGetItemRequest request,
00137 MediaGetItemResponse response) throws ServiceException {
00138 XbmcNode.this.library.handleMediaGetItem(request, response);
00139 }
00140 });
00141
00142 this.getConnectedNode().newServiceServer(
00143 this.configuration.getPrefix() + SRV_MEDIA_GET_ITEMS,
00144 MediaGetItems._TYPE,
00145 new ServiceResponseBuilder<MediaGetItemsRequest, MediaGetItemsResponse>() {
00146 @Override
00147 public void build(MediaGetItemsRequest request,
00148 MediaGetItemsResponse response) throws ServiceException {
00149 XbmcNode.this.library.handleMediaGetItems(request, response);
00150 }
00151 });
00152 }
00153
00154 @Override
00155 protected boolean connect() {
00156 boolean result = false;
00157
00158 this.logI(String.format("Connecting to %s:%s...",
00159 this.configuration.getHost(),
00160 this.configuration.getPort()));
00161
00162 if (this.pingXbmc()) {
00163 this.getStateData().setState(StateData.INIT);
00164
00165 result = true;
00166 Version.VersionResult version = this.xbmcJson.getResult(new Version());
00167
00168 if (version.major >= 6) {
00169 this.logI(String.format(
00170 "\tDetected XBMC JSON-RPC version : %d.%d.%d . Done",
00171 version.major,
00172 version.minor,
00173 version.patch));
00174 } else {
00175 this.logI(String.format(
00176 "\tDetected XBMC JSON-RPC version : %d.%d.%d . Upgrade your XBMC ! this drivers is only available for >= 6.x.x JSON-RPC",
00177 version.major,
00178 version.minor,
00179 version.patch));
00180 }
00181
00182 this.logI("\tConnected done.");
00183 } else {
00184 this.getStateData().setState(StateData.SHUTDOWN);
00185
00186 try {
00187 Thread.sleep(10000 / this.configuration.getRate());
00188 } catch (InterruptedException e) {
00189 this.logE(e);
00190 }
00191 }
00192
00193 return result;
00194 }
00195
00196 @Override
00197 protected XbmcConfig getConfig() {
00198 return new XbmcConfig(this.getConnectedNode());
00199 }
00200
00201 private boolean pingXbmc() {
00202 String ping = this.xbmcJson.getResult(new Ping());
00203 return ping != null && ping.equals("pong");
00204 }
00205 }