c# - 发现设备 mono.upnp

标签 c# mono xamarin xamarin.android upnp

我正在尝试通过 upnp 与我的新 wemo 交换机进行通信。在 Windows 上这工作得很好。现在我尝试使用 mono.upnp lib 在 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/

相关文章:

c# - 单击更改 ViewCell 元素

ios - 如何在 Xamarin iOS 中启动由 iBeacon 触发的 GPS 跟踪?

c# - 如何在 WPF 进度条中使用图像?

c# - .Net Core 3.1 上的 AutoMapper

c# - 在特定运行时强制 MonoDevelop 为 'Run Tests'

f# - 在 Mono 2.8 上编译 F# 代码

c# - ASP.NET 核心,Web API : No route matches the supplied values

c# - 使用线程保持 UI 更新

linux - DNU 恢复失败,https GET 超时

android - 如何在 Xamarin Picker 中设置项目列表的样式(在 Android 中)