/razpubs/test_src/com/razie/upnp/tv/TvDevice.java

http://razpub.googlecode.com/ · Java · 329 lines · 222 code · 58 blank · 49 comment · 38 complexity · 8c1370fcc588756781b08665a5f83ada MD5 · raw file

  1. /******************************************************************
  2. *
  3. * CyberUPnP for Java
  4. *
  5. * Copyright (C) Satoshi Konno 2002
  6. *
  7. * File : ClockDevice.java
  8. *
  9. ******************************************************************/
  10. package com.razie.upnp.tv;
  11. import java.awt.Component;
  12. import org.cybergarage.upnp.Action;
  13. import org.cybergarage.upnp.Argument;
  14. import org.cybergarage.upnp.ControlPoint;
  15. import org.cybergarage.upnp.Device;
  16. import org.cybergarage.upnp.Service;
  17. import org.cybergarage.upnp.ServiceList;
  18. import org.cybergarage.upnp.StateVariable;
  19. import org.cybergarage.upnp.control.ActionListener;
  20. import org.cybergarage.upnp.control.QueryListener;
  21. import org.cybergarage.upnp.device.InvalidDescriptionException;
  22. import org.cybergarage.upnp.device.NotifyListener;
  23. import org.cybergarage.upnp.device.SearchResponseListener;
  24. import org.cybergarage.upnp.event.EventListener;
  25. import org.cybergarage.upnp.ssdp.SSDPPacket;
  26. import com.razie.pub.FileUtils;
  27. public class TvDevice implements ActionListener, QueryListener, NotifyListener, EventListener, SearchResponseListener
  28. {
  29. private final static String DESCRIPTION_FILE_NAME = "com/razie/upnp/tv/description/description.xml";
  30. private final static String CLOCK_DEVICE_TYPE = "urn:schemas-upnp-org:device:clock:1";
  31. private final static String CLOCK_SERVICE_TYPE = "urn:schemas-upnp-org:service:timer:1";
  32. private final static String LIGHT_DEVICE_TYPE = "urn:schemas-upnp-org:device:light:1";
  33. private final static String LIGHT_SERVICE_TYPE = "urn:schemas-upnp-org:service:power:1";
  34. private final static String AIRCON_DEVICE_TYPE = "urn:schemas-upnp-org:device:aircon:1";
  35. private final static String AIRCON_SERVICE_TYPE = "urn:schemas-upnp-org:service:temp:1";
  36. private final static String WASHER_DEVICE_TYPE = "urn:schemas-upnp-org:device:washer:1";
  37. private final static String WASHER_SERVICE_TYPE = "urn:schemas-upnp-org:service:state:1";
  38. private ControlPoint ctrlPoint;
  39. private Device tvDev;
  40. public TvDevice()
  41. {
  42. //////////////////////////////////////
  43. // Control Ponit
  44. //////////////////////////////////////
  45. ctrlPoint = new ControlPoint();
  46. ctrlPoint.addNotifyListener(this);
  47. ctrlPoint.addSearchResponseListener(this);
  48. ctrlPoint.addEventListener(this);
  49. //////////////////////////////////////
  50. // Device
  51. //////////////////////////////////////
  52. try {
  53. tvDev = new Device(FileUtils.fileFromUrl(TvDevice.class.getClassLoader().getResource(DESCRIPTION_FILE_NAME)));
  54. }
  55. catch (InvalidDescriptionException e) {}
  56. Action getPowerAction = tvDev.getAction("GetPower");
  57. getPowerAction.setActionListener(this);
  58. Action setPowerAction = tvDev.getAction("SetPower");
  59. setPowerAction.setActionListener(this);
  60. ServiceList serviceList = tvDev.getServiceList();
  61. Service service = serviceList.getService(0);
  62. service.setQueryListener(this);
  63. on();
  64. }
  65. public void finalize()
  66. {
  67. off();
  68. }
  69. ////////////////////////////////////////////////
  70. // Component
  71. ////////////////////////////////////////////////
  72. private Component comp;
  73. public void setComponent(Component comp)
  74. {
  75. this.comp = comp;
  76. }
  77. public Component getComponent()
  78. {
  79. return comp;
  80. }
  81. ////////////////////////////////////////////////
  82. // on/off
  83. ////////////////////////////////////////////////
  84. private boolean onFlag = false;
  85. public void on()
  86. {
  87. ctrlPoint.search();
  88. onFlag = true;
  89. }
  90. public void off()
  91. {
  92. ctrlPoint.unsubscribe();
  93. onFlag = false;
  94. }
  95. public boolean isOn()
  96. {
  97. return onFlag;
  98. }
  99. public void setPowerState(String state)
  100. {
  101. if (state == null) {
  102. off();
  103. return;
  104. }
  105. if (state.compareTo("1") == 0) {
  106. on();
  107. return;
  108. }
  109. if (state.compareTo("0") == 0) {
  110. off();
  111. return;
  112. }
  113. }
  114. public String getPowerState()
  115. {
  116. if (onFlag == true)
  117. return "1";
  118. return "0";
  119. }
  120. ////////////////////////////////////////////////
  121. // Clock
  122. ////////////////////////////////////////////////
  123. private String clockTime = "";
  124. public String getClockTime()
  125. {
  126. return clockTime;
  127. }
  128. ////////////////////////////////////////////////
  129. // Aircon
  130. ////////////////////////////////////////////////
  131. private String airconTemp = "";
  132. public String getAirconTempture()
  133. {
  134. return airconTemp;
  135. }
  136. ////////////////////////////////////////////////
  137. // Message
  138. ////////////////////////////////////////////////
  139. private String message = "";
  140. public void setMessage(String msg)
  141. {
  142. message = msg;
  143. }
  144. public String getMessage()
  145. {
  146. return message;
  147. }
  148. ////////////////////////////////////////////////
  149. // Device (Common)
  150. ////////////////////////////////////////////////
  151. public boolean isDevice(SSDPPacket packet, String deviceType)
  152. {
  153. String usn = packet.getUSN();
  154. if (usn.endsWith(deviceType))
  155. return true;
  156. return false;
  157. }
  158. public Service getDeviceService(String deviceType, String serviceType)
  159. {
  160. Device dev = ctrlPoint.getDevice(deviceType);
  161. if (dev == null)
  162. return null;
  163. Service service = dev.getService(serviceType);
  164. if (service == null)
  165. return null;
  166. return service;
  167. }
  168. public boolean subscribeService(SSDPPacket packet, String deviceType, String serviceType)
  169. {
  170. Service service = getDeviceService(deviceType, serviceType);
  171. if (service == null)
  172. return false;
  173. return ctrlPoint.subscribe(service);
  174. }
  175. ////////////////////////////////////////////////
  176. // SSDP Listener
  177. ////////////////////////////////////////////////
  178. public void checkNewDevices(SSDPPacket packet)
  179. {
  180. subscribeService(packet, CLOCK_DEVICE_TYPE, CLOCK_SERVICE_TYPE);
  181. subscribeService(packet, AIRCON_DEVICE_TYPE, AIRCON_SERVICE_TYPE);
  182. subscribeService(packet, LIGHT_DEVICE_TYPE, LIGHT_SERVICE_TYPE);
  183. subscribeService(packet, WASHER_DEVICE_TYPE, WASHER_SERVICE_TYPE);
  184. }
  185. public void checkRemoveDevices(SSDPPacket packet)
  186. {
  187. if (isDevice(packet, CLOCK_DEVICE_TYPE) == true)
  188. clockTime = "";
  189. if (isDevice(packet, AIRCON_DEVICE_TYPE) == true)
  190. airconTemp = "";
  191. }
  192. ////////////////////////////////////////////////
  193. // Control Point Listener
  194. ////////////////////////////////////////////////
  195. public void deviceNotifyReceived(SSDPPacket packet)
  196. {
  197. if (packet.isAlive() == true)
  198. checkNewDevices(packet);
  199. if (packet.isByeBye() == true)
  200. checkRemoveDevices(packet);
  201. }
  202. public void deviceSearchResponseReceived(SSDPPacket packet)
  203. {
  204. checkNewDevices(packet);
  205. }
  206. public void eventNotifyReceived(String uuid, long seq, String name, String value)
  207. {
  208. //System.out.println("Notify = " + uuid + ", " + seq + "," + name + "," + value);
  209. Service service = ctrlPoint.getSubscriberService(uuid);
  210. if (service == null)
  211. return;
  212. if (service.isService(CLOCK_SERVICE_TYPE) == true)
  213. clockTime = value;
  214. else if (service.isService(AIRCON_SERVICE_TYPE) == true)
  215. airconTemp = value;
  216. else {
  217. if (value != null && 0 < value.length()) {
  218. Device dev = service.getDevice();
  219. String fname = dev.getFriendlyName();
  220. message = fname + ":" + value;
  221. }
  222. }
  223. comp.repaint();
  224. }
  225. ////////////////////////////////////////////////
  226. // ActionListener
  227. ////////////////////////////////////////////////
  228. public boolean actionControlReceived(Action action)
  229. {
  230. String actionName = action.getName();
  231. if (actionName.equals("GetPower") == true) {
  232. String state = getPowerState();
  233. Argument powerArg = action.getArgument("Power");
  234. powerArg.setValue(state);
  235. comp.repaint();
  236. return true;
  237. }
  238. if (actionName.equals("SetPower") == true) {
  239. Argument powerArg = action.getArgument("Power");
  240. String state = powerArg.getValue();
  241. setPowerState(state);
  242. state = getPowerState();
  243. Argument resultArg = action.getArgument("Result");
  244. resultArg.setValue(state);
  245. comp.repaint();
  246. return true;
  247. }
  248. return false;
  249. }
  250. ////////////////////////////////////////////////
  251. // QueryListener
  252. ////////////////////////////////////////////////
  253. public boolean queryControlReceived(StateVariable stateVar)
  254. {
  255. stateVar.setValue(getPowerState());
  256. return true;
  257. }
  258. ////////////////////////////////////////////////
  259. // start/stop
  260. ////////////////////////////////////////////////
  261. public void start()
  262. {
  263. ctrlPoint.start();
  264. tvDev.start();
  265. }
  266. public void stop()
  267. {
  268. ctrlPoint.stop();
  269. tvDev.stop();
  270. }
  271. }