Hier mal eine schmutzige Implementierung eines UPnP-Device welches folgende Möglichkeiten bietet:
SetHaltestelle
SetOrt
GetHaltestelleData
ACHTUNG, DAS IST NUR DEMOSTUFF
Sonderzeichen werden nicht behandelt etc
Kontrolliert werden kann das Device mit jedem Universal-ControlDevice oder mit dem welches ich als nächstes basteln werde, was aber noch ein wenig dauern dürfte.
man braucht folgende libs:
ClingCoreorg.jsonDVBService.java:
CODE
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package device;
import java.io.*;
import java.util.Date;
import java.net.*;
import org.json.JSONArray;
import org.json.JSONException;
import org.teleal.cling.binding.annotations.*;
@UpnpService(
serviceId = @UpnpServiceId("DVBService"),
serviceType = @UpnpServiceType(value = "DVBService", version = 1)
)
/**
*
* @author kauerauf
*/
public class DVBService {
@UpnpStateVariable(defaultValue = "0")
private String haltestelle = "Sachsenallee";
@UpnpStateVariable(defaultValue = "0")
private String ort = "Dresden";
@UpnpStateVariable(defaultValue = "0")
private String haltestelledata = "";
// @UpnpStateVariable(defaultValue = "0")
// private long letzteakt = 0;
@UpnpAction
public void setHaltestelle(@UpnpInputArgument(name = "SetHaltestelle")
String newHst) {
haltestelle = newHst;
}
@UpnpAction
public void setOrt(@UpnpInputArgument(name = "SetOrt")
String newOrt) {
ort = newOrt;
}
@UpnpAction(out = @UpnpOutputArgument(name = "GetHaltestelleData"))
public String getHaltestelledata() throws MalformedURLException, IOException, JSONException {
return getDVBFromServer(ort, haltestelle).toString();
}
// private long getCurrentTime() {
// Date date = new Date();
// return date.getTime();
// }
private JSONArray getDVBFromServer(String ort, String hst) throws MalformedURLException, IOException, JSONException {
String server = "http://widgets.vvo-online.de/abfahrtsmonitor/Abfahrten.do?";
String params = "ort=" + ort + "&hst=\"" + hst + "\"";
// URL url = new URL(server + params);
URLConnection connection = new URL(server + params).openConnection();
// URLConnection connection = url.openConnection();
BufferedReader input = new BufferedReader(
new InputStreamReader(
connection.getInputStream()));
String inputString;
String DVBDataS = "";
while((inputString = input.readLine()) != null)
DVBDataS = DVBDataS + inputString;
input.close();
JSONArray DVBData = new JSONArray(DVBDataS);
return DVBData;
}
}
Main.java
CODE
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package device;
import java.io.IOException;
import java.net.MalformedURLException;
import org.json.JSONException;
import org.teleal.cling.UpnpService;
import org.teleal.cling.UpnpServiceImpl;
import org.teleal.cling.binding.*;
import org.teleal.cling.binding.annotations.*;
import org.teleal.cling.model.*;
import org.teleal.cling.model.meta.*;
import org.teleal.cling.model.types.*;
/**
*
* @author kauerauf
*/
public class Main implements Runnable{
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws MalformedURLException, IOException, JSONException {
// TODO code application logic here
Thread serverThread = new Thread(new Main());
serverThread.setDaemon(false);
serverThread.start();
}
public void run() {
try {
final UpnpService upnpService = new UpnpServiceImpl();
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
upnpService.shutdown();
}
});
upnpService.getRegistry().addDevice(
createDevice()
);
} catch(Exception e) {
System.err.println("Exception: " +e);
e.printStackTrace(System.err);
System.exit(1);
}
}
LocalDevice createDevice() throws ValidationException {
DeviceIdentity identity = new DeviceIdentity(UDN.uniqueSystemIdentifier("DVB Haltestellenauskunft"));
DeviceType type = new UDADeviceType("Haltestellenauskunft", 1);
DeviceDetails details = new DeviceDetails("Freundliche DVB Auskunft", new ManufacturerDetails("Stefan Kauerauf"), new ModelDetails("DVB Haltestellenauskunft", "gibt aktuelle Auskunft über abgehende Nahverkehrsmittel", "v1"));
LocalService<DVBService> dvbservice = new AnnotationLocalServiceBinder().read(DVBService.class);
dvbservice.setManager(new DefaultServiceManager(dvbservice, DVBService.class));
return new LocalDevice(identity, type, details, dvbservice);
}
}
bevor jetzt alle fragen was sie damit sollen, nein die meisten werdens nicht brauchen, aber vielleicht interessierts jemanden oder er möchte irgendwas damit basteln.
€: falls sich einer wundert, wieso ich den überflüssigen JSON-Schnickschnack mache, das ist gedacht um die Daten im Device noch einfach anzupassen. Leider kann UPnP so einfach kein JSONArray Objekt übergeben, dh der String muss dann auf dem ControlDevice wieder in JSON umgewandelt werden.