gpt4 book ai didi

c# - 发现设备 mono.upnp

转载 作者:太空宇宙 更新时间:2023-11-03 10:40:57 25 4
gpt4 key购买 nike

我正在尝试通过 upnp 与我的新 wemo 交换机通信。在 Windows 上这很好用。现在我正在尝试使用 mono.upnp 库在 android 上做同样的事情。一切看起来都一样,但我不知道如何在 mono.upnp 上发现设备。

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;
}

我已经将设备类更改为 mono.upnp,但我似乎无法在 mono.upnp 中找到 UPnPDeviceFinder 的等效项。

最佳答案

好吧,终于成功了。这是我用来打开和关闭 wemo 的代码:

    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 the packet and payload to send to the endpoint to get the switch to process the 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 we don't abort the result the device holds on to the connection sometimes and prevents other commands from being received

request.Abort ();
}

public void GetDevice (string Name, wemoAction action)
{

try {

Client client = new Client ();

client.BrowseAll (); //Browse all available upnp devices

client.DeviceAdded += (sender, e) => { //do something when a device is 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);
}

}

发送打开和关闭数据包的代码来自这个视频:http://www.youtube.com/watch?v=ifzmJFdvNEE

关于c# - 发现设备 mono.upnp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25354283/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com