首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >发现设备mono.upnp

发现设备mono.upnp
EN

Stack Overflow用户
提问于 2014-08-17 22:34:30
回答 1查看 1.9K关注 0票数 2

我正在试图与我的新的wemo开关的upnp。在窗户上,这个工作得很好。现在,我正尝试使用mono.upnp库在android上做同样的事情。一切看起来都一样,但我想不出如何在mono.upnp上发现设备。

下面是windows上的代码:

代码语言:javascript
复制
    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类,但似乎找不到UPnPDeviceFinder在mono.upnp中的等效类。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-08-18 21:45:31

好吧,终于起作用了。下面是我用来打开和关闭wemo的代码:

代码语言:javascript
复制
    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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25354283

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档