00001 package edu.stanford.hci.r3.devices;
00002
00003 import java.io.File;
00004 import java.net.InetAddress;
00005 import java.net.MalformedURLException;
00006 import java.net.URL;
00007 import java.net.UnknownHostException;
00008
00009 import edu.stanford.hci.r3.actions.R3Action;
00010 import edu.stanford.hci.r3.actions.remote.ActionReceiver;
00011 import edu.stanford.hci.r3.actions.remote.ActionSender;
00012 import edu.stanford.hci.r3.actions.types.OpenFileAction;
00013 import edu.stanford.hci.r3.actions.types.OpenURLAction;
00014 import edu.stanford.hci.r3.actions.types.PlaySoundAction;
00015 import edu.stanford.hci.r3.actions.types.TextToSpeechAction;
00016 import edu.stanford.hci.r3.devices.channels.ActionChannel;
00017 import edu.stanford.hci.r3.devices.channels.AudioChannel;
00018 import edu.stanford.hci.r3.devices.channels.DisplayChannel;
00019 import edu.stanford.hci.r3.util.DebugUtils;
00020 import edu.stanford.hci.r3.util.networking.ClientServerType;
00021
00039 public class Device {
00040
00041
00042
00043
00044
00045
00046
00050
00051
00052
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00078 private static PlaySoundAction playSoundAction;
00079
00086 public static PlaySoundAction doPlaySound(File soundFile) {
00087 if (playSoundAction != null) {
00088 playSoundAction.stop();
00089 }
00090 playSoundAction = new PlaySoundAction(soundFile);
00091 playSoundAction.invoke();
00092 return playSoundAction;
00093 }
00094
00098 private ActionChannel actionChannel;
00099
00103 private AudioChannel audioChannel;
00104
00108 private DisplayChannel displayChannel;
00109
00113 private String hostNameOrIPAddr;
00114
00118 private String name;
00119
00123 private ActionSender sender;
00124
00128 public Device(String theHostNameOrIPAddr, String descriptiveName) {
00129 hostNameOrIPAddr = theHostNameOrIPAddr;
00130 name = descriptiveName;
00131 }
00132
00136 public void connect() {
00137 sender = new ActionSender(hostNameOrIPAddr, ActionReceiver.DEFAULT_JAVA_PORT,
00138 ClientServerType.JAVA);
00139 }
00140
00144 public void disconnect() {
00145 sender.disconnect();
00146 sender = null;
00147 }
00148
00152 public ActionChannel getActionChannel() {
00153 if (actionChannel == null) {
00154 actionChannel = new ActionChannel(this);
00155 }
00156 return actionChannel;
00157 }
00158
00166 public AudioChannel getAudioChannel() {
00167 if (audioChannel == null) {
00168 audioChannel = new AudioChannel(this);
00169 }
00170 return audioChannel;
00171 }
00172
00178 public DisplayChannel getDisplayChannel() {
00179 if (displayChannel == null) {
00180 displayChannel = new DisplayChannel(this);
00181 }
00182 return displayChannel;
00183 }
00184
00188 public String getName() {
00189 return name;
00190 }
00191
00197 public void invokeAction(R3Action action) {
00198 if (sender != null) {
00199 sender.invokeRemoteAction(action);
00200 } else {
00201 DebugUtils.println("Sender is null.");
00202 }
00203 }
00204
00210 public boolean isAlive() {
00211 try {
00212 DebugUtils.println("Checking if Device is Alive and Reachable...");
00213 final InetAddress address = InetAddress.getByName(hostNameOrIPAddr);
00214 DebugUtils.println("Device Hostname: " + address.getHostName());
00215 DebugUtils.println("Device Address: " + address.getHostAddress());
00216 return true;
00217 } catch (UnknownHostException e) {
00218 DebugUtils.println(e);
00219 }
00220 return false;
00221 }
00222
00226 public static void doSpeakText(String textToSpeak) {
00227 TextToSpeechAction.getInstance().speak(textToSpeak);
00228 }
00229
00233 public static void doOpenURL(String urlString) {
00234 try {
00235 final URL u = new URL(urlString);
00236 new OpenURLAction(u).invoke();
00237 } catch (MalformedURLException e) {
00238 e.printStackTrace();
00239 }
00240 }
00241
00248 public static OpenFileAction doOpenFile(File file) {
00249 final OpenFileAction ofa = new OpenFileAction(file);
00250 ofa.invoke();
00251 return ofa;
00252 }
00253 }