c# - Discover devices mono.upnp -
i'm trying communicate new wemo switch on upnp. on windows works fine. i'm trying same on android using mono.upnp lib. looks same cant figuer out how discover devices on mono.upnp.
here code on windows:
public static list<wemodevice> getdevices () { upnpdevicefinder finder = new upnpdevicefinder (); list<wemodevice> founddevices = new list<wemodevice> (); string devicetype = "upnp:rootdevice"; device devices = finder.findbytype (devicetype, 1); foreach (device device in devices) { if (device.type.startswith ("urn:belkin:")) { switch (getdevicetype (device)) { case wemodevicetype.switch: wemoswitch wemoswitch = new wemoswitch (device); founddevices.add (wemoswitch); break; case wemodevicetype.sensor: wemosensor wemosensor = new wemosensor (device); founddevices.add (wemosensor); break; default: break; } } } return founddevices; }
i changed device class mono.upnp 1 cant seem find equivalent upnpdevicefinder in mono.upnp.
alright got working. here code used switch wemo on , off:
const string command_off = @"<?xml version=""1.0"" encoding=""utf-8""?><s:envelope xmlns:s=""http://schemas.xmlsoap.org/soap/envelope/"" s:encodingstyle=""http://schemas.xmlsoap.org/soap/encoding/""><s:body><u:setbinarystate xmlns:u=""urn:belkin:service:basicevent:1""><binarystate>0</binarystate></u:setbinarystate></s:body></s:envelope>"; const string command_on = @"<?xml version=""1.0"" encoding=""utf-8""?><s:envelope xmlns:s=""http://schemas.xmlsoap.org/soap/envelope/"" s:encodingstyle=""http://schemas.xmlsoap.org/soap/encoding/""><s:body><u:setbinarystate xmlns:u=""urn:belkin:service:basicevent:1""><binarystate>1</binarystate></u:setbinarystate></s:body></s:envelope>"; public void on (string ip, string port) { sendcommand (command_on, ip, port); } public void off (string ip, string port) { sendcommand (command_off, ip, port); } private void sendcommand (string command, string ip, string port) { string targeturl = "http://" + ip + ":" + port + "/upnp/control/basicevent1"; // create packet , payload send endpoint switch process command httpwebrequest request = (httpwebrequest)httpwebrequest.create (targeturl); request.method = "post"; request.headers.add ("soapaction", "\"urn:belkin:service:basicevent:1#setbinarystate\""); request.contenttype = @"text/xml; charset=""utf-8"""; request.keepalive = false; byte[] bytes = utf8encoding.ascii.getbytes (command); request.contentlength = bytes.length; using (stream stream = request.getrequeststream ()) { stream.write (bytes, 0, bytes.length); stream.close (); request.getresponse (); } // hack: if don't abort result device holds on connection , prevents other commands being received request.abort (); } public void getdevice (string name, wemoaction action) { try { client client = new client (); client.browseall (); //browse available upnp devices client.deviceadded += (sender, e) => { //do when device found system.console.writeline ("got one!"); if (e.device.tostring ().contains ("urn:belkin")) { if (e.device.getdevice ().friendlyname.equals (name)) { var url = e.device.getdevice ().services.first ().eventurl; switch (action) { case wemoaction.on: on (url.dnssafehost, url.port.tostring ()); break; case wemoaction.off: off (url.dnssafehost, url.port.tostring ()); break; } } } }; } catch (exception ex) { system.console.writeline (ex.message); } }
the code sending on , off packet video: http://www.youtube.com/watch?v=ifzmjfdvnee
Comments
Post a Comment