Xbmc.java
Go to the documentation of this file.
00001 
00009 package script.media.xbmc;
00010 
00011 import java.net.URI;
00012 import java.net.URISyntaxException;
00013 import java.util.ArrayList;
00014 import java.util.List;
00015 import java.util.concurrent.Semaphore;
00016 import java.util.concurrent.TimeUnit;
00017 
00018 import org.ros.exception.RemoteException;
00019 import org.ros.exception.ServiceNotFoundException;
00020 import org.ros.node.service.ServiceClient;
00021 import org.ros.node.service.ServiceResponseListener;
00022 import org.rosbuilding.common.ISystem;
00023 import org.rosbuilding.common.media.CommandUtil;
00024 import org.rosbuilding.common.media.IPlayer;
00025 import org.rosbuilding.common.media.ISpeaker;
00026 import org.rosmultimedia.player.media.model.Media;
00027 import org.rosmultimedia.player.media.model.Movie;
00028 import org.rosmultimedia.player.media.model.Song;
00029 import org.rosmultimedia.player.media.model.Tvshow;
00030 
00031 import com.rosalfred.core.ia.CommandPublisher;
00032 import com.rosalfred.core.ia.RsContext;
00033 import com.rosalfred.core.ia.rivescript.BotReply;
00034 import com.rosalfred.core.ia.rivescript.RiveScript;
00035 
00036 import smarthome_media_msgs.MediaGetItems;
00037 import smarthome_media_msgs.MediaGetItemsRequest;
00038 import smarthome_media_msgs.MediaGetItemsResponse;
00039 import smarthome_media_msgs.MediaItem;
00040 import smarthome_media_msgs.MediaType;
00041 
00042 import smarthome_comm_msgs.Command;
00043 
00049 public class Xbmc extends CommandPublisher {
00050 
00051     private final static String nodePath = "/home/salon/xbmc/";
00052     private static ServiceClient<MediaGetItemsRequest, MediaGetItemsResponse> service;
00053 
00054     public Xbmc(RiveScript rivescript) {
00055         super(rivescript);
00056     }
00057 
00058     @Override
00059     protected void initialize() {
00060         super.initialize();
00061 
00062         if (this.node != null) {
00063             try {
00064                 service = this.node.newServiceClient(nodePath + "get_items", MediaGetItems._TYPE);
00065             } catch (ServiceNotFoundException e) {
00066                 this.node.getLog().error("Service Xbmc get_items not found !");
00067             }
00068         }
00069     }
00070 
00071     public void startLibrary() {
00072         // new Onkyo(this.rivescript).startReceiver();
00073         this.publish(ISystem.OP_POWER);
00074     }
00075 
00076     public void shutdownLibrary() {
00077         // new Onkyo(this.rivescript).shutdownReceiver();
00078         this.publish(ISystem.OP_SHUTDOWN);
00079     }
00080 
00081     public void open(String uri) {
00082         this.open(uri, null);
00083     }
00084 
00085     public void open(String uri, String type) {
00086         Command message = null;
00087         try {
00088             message = CommandUtil.toCommand(this.node, IPlayer.OP_OPEN, new URI(uri), type);
00089         } catch (URISyntaxException e) {
00090             this.node.getLog().error("Data no a uri !!", e);
00091         }
00092 
00093         this.publish(message);
00094     }
00095 
00096     public void mute() {
00097         this.publish(ISpeaker.OP_MUTE);
00098     }
00099 
00100     public void play() {
00101         this.publish(IPlayer.OP_PLAY);
00102     }
00103 
00104     public void pause() {
00105         this.publish(IPlayer.OP_PAUSE);
00106     }
00107 
00108     public void stop() {
00109         this.publish(IPlayer.OP_STOP);
00110     }
00111 
00112     public void forward() {
00113         this.publish(IPlayer.OP_RIGHT);
00114     }
00115 
00116     public void backward() {
00117         this.publish(IPlayer.OP_LEFT);
00118     }
00119 
00120     public void up() {
00121         this.publish(IPlayer.OP_UP);
00122     }
00123 
00124     public void down() {
00125         this.publish(IPlayer.OP_DOWN);
00126     }
00127 
00128     public void select() {
00129         this.publish(IPlayer.OP_SELECT);
00130     }
00131 
00132     public void playto() {
00133         // this.publish("power");
00134     }
00135 
00136     public BotReply searchMediaMovie() {
00137         String search = this.getUserParam(RsContext.FIND);
00138         String type = this.getUserParam(RsContext.FIND2);
00139         if (type == null) {
00140             type = "";
00141         }
00142 
00143         List<MediaItem> items;
00144 
00145         if (service != null) {
00146             Movie media = new Movie();
00147             MediaGetItemsRequest request = service.newMessage();
00148             request.getItem().getMediatype().setValue(MediaType.VIDEO_MOVIE);
00149 
00150             switch (type) {
00151             case "cast":
00152                 media.setCast(new ArrayList<String>());
00153                 media.getCast().add(search);
00154                 break;
00155             case "year":
00156                 media.setYear(Integer.parseInt(search));
00157                 break;
00158             case "genre":
00159                 media.setGenre(new ArrayList<String>());
00160                 media.getGenre().add(search);
00161                 break;
00162             default:
00163                 media.setTitle(search);
00164                 break;
00165             }
00166 
00167             request.getItem().setData(media.toJson());
00168             items = this.getMediaItems(request);
00169         } else {
00170             items = new ArrayList<MediaItem>();
00171         }
00172 
00173         this.setUserParam(RsContext.COUNT, String.valueOf(items.size()));
00174 
00175         String titles = "";
00176         List<Media> medias = null;
00177 
00178         if (items.size() == 1) {
00179             MediaItem item = items.get(0);
00180             Media media = Movie.fromJson(item.getData());
00181             titles = media.getTitle();
00182             this.setUserParam(RsContext.LAUNCH, media.getTitle());
00183         } else if (items.size() > 1) {
00184             medias = new ArrayList<Media>();
00185             Media media = null;
00186 
00187             for (MediaItem item : items) {
00188                 media = Movie.fromJson(item.getData());
00189                 String title = RsContext.normalize(media.getTitle());
00190                 titles += title + ".  ";
00191                 medias.add(media);
00192             }
00193         }
00194 
00195         this.setUserParam(RsContext.FINDING, titles);
00196 
00197         BotReply reply = new BotReply(String.valueOf(items.size()));
00198 
00199         if (medias != null) {
00200             for (Media media : medias) {
00201                 reply.getIntents().add(media.toJson());
00202             }
00203         }
00204 
00205         return reply;
00206     }
00207 
00208     public BotReply searchMediaTvshows() {
00209         String show = this.getUserParam(RsContext.FIND);
00210         String season = this.getUserParam(RsContext.FIND2);
00211         String episode = this.getUserParam(RsContext.FIND3);
00212 
00213         MediaGetItemsRequest request = service.newMessage();
00214         MediaItem reqItem = request.getItem();
00215         reqItem.getMediatype().setValue(MediaType.VIDEO_TVSHOW_EPISODE);
00216 
00217         Tvshow media = new Tvshow();
00218         media.setShowtitle(show);
00219         media.setSeason(Integer.valueOf(season));
00220         media.setEpisode(Integer.valueOf(episode));
00221         media.setPlaycount(-1); // force playcount to -1 to ignore it
00222 
00223         reqItem.setData(media.toJson());
00224 
00225         List<MediaItem> items = this.getMediaItems(request);
00226         this.setUserParam(RsContext.COUNT, String.valueOf(items.size()));
00227 
00228         String titles = "";
00229         if (items.size() == 1) {
00230             MediaItem item = items.get(0);
00231             media = Tvshow.fromJson(item.getData());
00232             titles = media.getShowtitle();
00233             this.setUserParam(RsContext.LAUNCH, media.getShowtitle());
00234         } else if (items.size() > 1) {
00235             for (MediaItem item : items) {
00236                 media = Tvshow.fromJson(item.getData());
00237                 String title = RsContext.normalize(media.getShowtitle());
00238                 titles += title + ".  ";
00239             }
00240         }
00241 
00242         this.setUserParam(RsContext.FINDING, titles);
00243 
00244         return new BotReply(String.valueOf(items.size()));
00245     }
00246 
00247     public BotReply searchMediaAudioAlbum() {
00248         String search = this.getUserParam(RsContext.FIND);
00249         String type = this.getUserParam(RsContext.FIND2);
00250         if (type == null) {
00251             type = "";
00252         }
00253 
00254         MediaGetItemsRequest request = service.newMessage();
00255         request.getItem().getMediatype().setValue(MediaType.AUDIO_ALBUM);
00256         Song media = new Song();
00257 
00258         switch (type) {
00259             default:
00260                 media.setAlbum(search);
00261                 break;
00262         }
00263 
00264         List<MediaItem> items = this.getMediaItems(request);
00265 
00266         this.setUserParam(RsContext.COUNT, String.valueOf(items.size()));
00267 
00268         String titles = "";
00269         if (items.size() == 1) {
00270             MediaItem item = items.get(0);
00271             media = Song.fromJson(item.getData());
00272             titles = media.getTitle();
00273             this.setUserParam(RsContext.LAUNCH, media.getAlbum());
00274         } else if (items.size() > 1) {
00275             for (MediaItem item : items) {
00276                 media = Song.fromJson(item.getData());
00277                 String title = RsContext.normalize(media.getAlbum());
00278                 titles += title + ".  ";
00279             }
00280         }
00281 
00282         this.setUserParam(RsContext.FINDING, titles);
00283 
00284         return new BotReply(String.valueOf(items.size()));
00285     }
00286 
00287     public BotReply launchMediaMovie() {
00288         String search = this.getUserParam(RsContext.FIND);
00289         String type = this.getUserParam(RsContext.FIND2);
00290         if (type == null) {
00291             type = "";
00292         }
00293 
00294         String launch = this.getUserParam(RsContext.LAUNCH);
00295         launch = RsContext.normalize(launch).toLowerCase();
00296         String title = null;
00297 
00298         if (service != null) {
00299             MediaGetItemsRequest request = service.newMessage();
00300             request.getItem().getMediatype().setValue(MediaType.VIDEO_MOVIE);
00301             Movie media = new Movie();
00302 
00303             switch (type) {
00304                 case "cast":
00305                     media.setCast(new ArrayList<String>());
00306                     media.getCast().add(search);
00307                     break;
00308                 case "year":
00309                     media.setYear(Integer.parseInt(search));
00310                     break;
00311                 case "genre":
00312                     media.setGenre(new ArrayList<String>());
00313                     media.getGenre().add(search);
00314                     break;
00315                 default:
00316                     media.setTitle(search);
00317                     break;
00318             }
00319 
00320             request.getItem().setData(media.toJson());
00321             List<MediaItem> items = this.getMediaItems(request);
00322 
00323             if (items.size() > 0) {
00324                 for (MediaItem item : items) {
00325                     media = Movie.fromJson(item.getData());
00326                     title = RsContext.normalize(media.getTitle()).toLowerCase();
00327                     if (title.contains(launch)) {
00328                         this.open(
00329                                 "media://" + String.valueOf(item.getMediaid()),
00330                                 "MediaMovie");
00331                         break;
00332                     }
00333                 }
00334             }
00335         }
00336 
00337         return new BotReply(title);
00338     }
00339 
00340     public BotReply launchMediaTvshow() {
00341         String launch = this.getUserParam(RsContext.LAUNCH);
00342         String show = this.getUserParam(RsContext.FIND);
00343         String season = this.getUserParam(RsContext.FIND2);
00344         String episode = this.getUserParam(RsContext.FIND3);
00345         // String resume = this.getUserParam(RsContext.RESUME);
00346         launch = RsContext.normalize(launch).toLowerCase();
00347 
00348         MediaGetItemsRequest request = service.newMessage();
00349         request.getItem().getMediatype().setValue(MediaType.VIDEO_TVSHOW_EPISODE);
00350 
00351         Tvshow media = new Tvshow();
00352         media.setShowtitle(show);
00353         media.setSeason(Integer.valueOf(season));
00354         media.setEpisode(Integer.valueOf(episode));
00355         media.setPlaycount(-1); // force playcount to -1 to ignore it
00356 
00357         List<MediaItem> items = this.getMediaItems(request);
00358         String title = null;
00359 
00360         if (items.size() > 0) {
00361             for (MediaItem item : items) {
00362                 media = Tvshow.fromJson(item.getData());
00363                 title = RsContext.normalize(media.getShowtitle()).toLowerCase();
00364                 if (title.contains(launch)) {
00365                     this.open(
00366                             "media://" + String.valueOf(item.getMediaid()),
00367                             "MediaMovie");
00368                     break;
00369                 }
00370             }
00371         }
00372 
00373         return new BotReply(title);
00374     }
00375 
00376     public BotReply launchMediaLastTvshow() {
00377         String show = this.getUserParam(RsContext.FIND);
00378         // String resume =
00379         // this.rivescript.getUtils().getUserParam(RsContext.RESUME);
00380         show = RsContext.normalize(show).toLowerCase();
00381 
00382         MediaGetItemsRequest request = service.newMessage();
00383         request.getItem().getMediatype().setValue(MediaType.VIDEO_TVSHOW_EPISODE);
00384 
00385         Tvshow media = new Tvshow();
00386         media.setShowtitle(show);
00387         media.setPlaycount(-1);
00388         request.setLimitEnd(1);
00389 
00390         List<MediaItem> items = this.getMediaItems(request);
00391         String title = null;
00392         int result = 0;
00393 
00394         if (items.size() == 0) {
00395             // TODO we need to call info for a show not for specific episode
00396             // service = rospy.ServiceProxy(self.nodePath + 'get_items',
00397             // MediaGetItems)
00398             // msg = MediaItem()
00399             // msg.mediatype = "episode"
00400             // msg.tvshow.showtitle = show
00401             // limits = MediaLimits()
00402             // limits.end = 1
00403             // result = service(msg, limits)
00404             // tvshows = result.items
00405             // resultats_library_count = len( tvshows )
00406             result = 2; // 2 = no tvshow
00407         } else if (items.size() > 0) {
00408             for (MediaItem item : items) {
00409                 media = Tvshow.fromJson(item.getData());
00410                 title = RsContext.normalize(media.getTitle()).toLowerCase();
00411                 if (title.contains(show)) {
00412                     this.open(
00413                             "media://" + String.valueOf(item.getMediaid()),
00414                             "MediaMovie");
00415                     result = 1;
00416                     break;
00417                 }
00418             }
00419         }
00420 
00421         return new BotReply(String.valueOf(result));
00422     }
00423 
00424     public BotReply launchMediaAudioAlbum() {
00425         String find = this.getUserParam(RsContext.FIND);
00426         String launch = this.getUserParam(RsContext.LAUNCH);
00427         launch = RsContext.normalize(launch).toLowerCase();
00428 
00429         MediaGetItemsRequest request = service.newMessage();
00430         request.getItem().getMediatype().setValue(MediaType.AUDIO_ALBUM);
00431         Song media = new Song();
00432         media.setAlbum(find);
00433 
00434         List<MediaItem> items = this.getMediaItems(request);
00435         String title = null;
00436 
00437         if (items.size() > 0) {
00438             for (MediaItem item : items) {
00439                 media = Song.fromJson(item.getData());
00440                 title = RsContext.normalize(media.getAlbum()).toLowerCase();
00441                 if (title.contains(launch)) {
00442                     this.open(
00443                             "media://" + String.valueOf(item.getMediaid()),
00444                             "MediaAudioAlbum");
00445                     break;
00446                 }
00447             }
00448         }
00449 
00450         return new BotReply(title);
00451     }
00452 
00453     private List<MediaItem> getMediaItems(MediaGetItemsRequest request) {
00454         final List<MediaItem> result = new ArrayList<MediaItem>();
00455 
00456         final Semaphore semaphore = new Semaphore(1);
00457 
00458         try {
00459             semaphore.acquire();
00460         } catch (InterruptedException e) {
00461             e.printStackTrace();
00462         }
00463 
00464         service.call(request, new ServiceResponseListener<MediaGetItemsResponse>() {
00465 
00466             @Override
00467             public void onSuccess(MediaGetItemsResponse response) {
00468                 result.addAll(response.getItems());
00469                 semaphore.release();
00470             }
00471 
00472             @Override
00473             public void onFailure(RemoteException arg0) {
00474                 semaphore.release();
00475             }
00476         });
00477 
00478         try {
00479             semaphore.tryAcquire(5000, TimeUnit.MILLISECONDS);
00480         } catch (InterruptedException e) {
00481             e.printStackTrace();
00482         }
00483 
00484         return result;
00485     }
00486 }


alfred_bot
Author(s): Mickael Gaillard , Erwan Le Huitouze
autogenerated on Tue Jun 14 2016 01:34:50