Go to the documentation of this file.00001
00009 package org.rosbuilding.common.media;
00010
00011 import java.net.URI;
00012 import java.util.Arrays;
00013 import java.util.List;
00014
00015 import org.ros.node.ConnectedNode;
00016 import org.rosbuilding.common.IModule;
00017
00018 import com.google.common.base.Joiner;
00019
00020 import smarthome_media_msgs.MediaAction;
00021 import smarthome_comm_msgs.Command;
00022
00028 public class CommandUtil {
00029
00030 public enum Action {
00031 SAY("say"),
00032 SHOW("show"),
00033 VIEW("mediamovie" + IPlayer.OP_OPEN);
00034
00035 private String label;
00036 private Action(String label) {
00037 this.label = label;
00038 }
00039
00040 public String getValue() {
00041 return this.label;
00042 }
00043 }
00044
00045 public static MediaAction toMediaAction(ConnectedNode node, Command command) {
00046 MediaAction mediaAction = null;
00047
00048 if (node != null) {
00049 mediaAction = node.getTopicMessageFactory()
00050 .newFromType(MediaAction._TYPE);
00051
00052 String[] action = command.getAction().split(IModule.SEP);
00053
00054 if (action.length > 1) {
00055 mediaAction.setType(action[0]);
00056 mediaAction.setMethod(action[1]);
00057 } else {
00058 mediaAction.setMethod(action[0]);
00059 }
00060
00061 String subject = command.getSubject();
00062 if (subject.startsWith(IModule.URI_DATA)) {
00063 List<String> data = Arrays.asList(
00064 subject.substring(IModule.URI_DATA.length())
00065 .split(IModule.SEP));
00066
00067 mediaAction.setData(data);
00068 } else {
00069 mediaAction.setUri(command.getSubject());
00070 }
00071 }
00072 return mediaAction;
00073 }
00074
00075 public static Command toCommand(ConnectedNode node, MediaAction mediaAction) {
00076 Command command = null;
00077
00078 if (node != null) {
00079 command = node.getTopicMessageFactory().newFromType(Command._TYPE);
00080 command.setAction(mediaAction.getType() + IModule.SEP + mediaAction.getMethod());
00081 command.setSubject(mediaAction.getUri());
00082 }
00083
00084 return command;
00085 }
00086
00087 public static Command toCommand(ConnectedNode node, String iplayer, URI uri, String type) {
00088 Command command = null;
00089
00090 if (node != null) {
00091 command = node.getTopicMessageFactory().newFromType(Command._TYPE);
00092 command.setAction(type + IModule.SEP + iplayer);
00093 command.setSubject(uri.toString());
00094 }
00095
00096 return command;
00097 }
00098
00099 public static Command toCommand(ConnectedNode node, String iplayer, String...data) {
00100 Command command = node.getTopicMessageFactory()
00101 .newFromType(Command._TYPE);
00102
00103 command.setAction(IModule.SEP + iplayer);
00104 command.setSubject(IModule.URI_DATA + Joiner.on(IModule.SEP).join(data));
00105
00106 return command;
00107 }
00108 }