PageRenderTime 54ms CodeModel.GetById 17ms app.highlight 31ms RepoModel.GetById 1ms app.codeStats 0ms

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