Beispiel #1
0
        /// <summary>
        /// Invokes the service action.
        /// </summary>
        /// <param name="device">The Wemo device.</param>
        /// <param name="serviceName">Name of the service.</param>
        /// <param name="actionName">Name of the action.</param>
        /// <param name="arguments">The optional arguments.</param>
        /// <returns>XML document</returns>
        public static XmlDocument InvokeServiceAction(this WemoDevice device, string serviceName, string actionName, Dictionary <string, string> arguments = null)
        {
            HttpWebRequest req = WebRequest.Create($"{device.Location.Scheme}://{device.Location.Host}:{device.Location.Port}/upnp/control/{serviceName}1") as HttpWebRequest;

            // Set headers
            req.Headers.Add($"SOAPACTION:\"urn:Belkin:service:{serviceName}:1#{actionName}\"");
            req.ContentType = "text/xml; charset=\"utf-8\"";
            req.Method      = "POST";
            req.Timeout     = 2000;
            // Build request content
            string reqContent = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";

            reqContent += "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">";
            reqContent += "<s:Body>";
            reqContent += $"<u:{actionName} xmlns:u=\"urn:Belkin:service:{serviceName}:1\">";
            if (arguments != null)
            {
                foreach (var arg in arguments)
                {
                    reqContent += $"<{arg.Key}>{arg.Value}</{arg.Key}>";
                }
            }
            reqContent += $"</u:{actionName}>";
            reqContent += "</s:Body>";
            reqContent += "</s:Envelope>";
            // Write the request coontent
            using (Stream requestStream = req.GetRequestStream())
            {
                requestStream.Write(Encoding.UTF8.GetBytes(reqContent), 0, Encoding.UTF8.GetByteCount(reqContent));
            }
            // Execute request
            var xmlResponse = new XmlDocument();

            try
            {
                HttpWebResponse response = req.GetResponse() as HttpWebResponse;
                using (Stream rspStm = response.GetResponseStream())
                {
                    using (StreamReader reader = new StreamReader(rspStm))
                    {
                        xmlResponse.LoadXml(reader.ReadToEnd());
                    }
                }
                Debug.WriteLine("Command '{0}' result = {1}", actionName, response.StatusCode.ToString());
            }
            catch (WebException ex)
            {
                Debug.WriteLine(ex.Message);
            }
            // Return the XML response
            return(xmlResponse);
        }
Beispiel #2
0
 /// <summary>
 /// Changes the type of the device.
 /// </summary>
 /// <typeparam name="TWemoDevice">The type of the wemo device.</typeparam>
 /// <param name="device">The device.</param>
 /// <returns></returns>
 public static TWemoDevice ChangeDeviceType <TWemoDevice>(this WemoDevice device) where TWemoDevice : WemoDevice, new()
 {
     return(new TWemoDevice()
     {
         Location = device.Location,
         FriendlyName = device.FriendlyName,
         DeviceType = device.DeviceType,
         Manufacturer = device.Manufacturer,
         ModelDescription = device.ModelDescription,
         ModelName = device.ModelName,
         ModelNumber = device.ModelNumber,
         SerialNumber = device.SerialNumber,
         UDN = device.UDN,
         UPC = device.UPC,
         MacAddress = device.MacAddress,
         FirmwareVersion = device.FirmwareVersion
     });
 }
Beispiel #3
0
 private void sniffer_OnPacket(object sender, string Packet, IPEndPoint Local, IPEndPoint From)
 {
     Debug.WriteLine("* UPnP device on [{0}]", From);
     if (Packet.StartsWith("HTTP/1.1"))
     {
         // Get the location uri
         string location = "";
         int    idx      = Packet.IndexOf("\r\nLOCATION:");
         if (idx == -1)
         {
             idx = Packet.IndexOf("\r\nusn:");
         }
         if (idx > 0)
         {
             int pos2 = Packet.IndexOf("\r\n", idx + 11);
             location = Packet.Substring(idx + 11, pos2 - (idx + 11));
         }
         location = location.Trim();
         // Check device
         try
         {
             var locationUri = new Uri(location);
             using (var wc = new WebClient())
             {
                 wc.DownloadStringCompleted += (s, e) =>
                 {
                     if (!e.Cancelled && e.Error == null && e.Result.Contains(BELKIN_DEVICE_URN)) // TODO: Parse upnp device data and determine device
                     {
                         lock (knowDevices)
                         {
                             if (!knowDevices.Contains(location))
                             {
                                 knowDevices.Add(location);
                                 // Reading the setup.xml
                                 XmlDocument         deviceInfo = new XmlDocument();
                                 XmlNamespaceManager ns         = new XmlNamespaceManager(deviceInfo.NameTable);
                                 ns.AddNamespace("x", BELKIN_DEVICE_URN);
                                 deviceInfo.LoadXml(e.Result);
                                 var device = new WemoDevice()
                                 {
                                     Location         = locationUri,
                                     FriendlyName     = deviceInfo.SelectSingleNode("//x:device/x:friendlyName", ns).InnerText,
                                     DeviceType       = deviceInfo.SelectSingleNode("//x:device/x:deviceType", ns).InnerText,
                                     Manufacturer     = deviceInfo.SelectSingleNode("//x:device/x:manufacturer", ns).InnerText,
                                     ModelDescription = deviceInfo.SelectSingleNode("//x:device/x:modelDescription", ns).InnerText,
                                     ModelName        = deviceInfo.SelectSingleNode("//x:device/x:modelName", ns).InnerText,
                                     ModelNumber      = deviceInfo.SelectSingleNode("//x:device/x:modelNumber", ns).InnerText,
                                     SerialNumber     = deviceInfo.SelectSingleNode("//x:device/x:serialNumber", ns).InnerText,
                                     UDN             = deviceInfo.SelectSingleNode("//x:device/x:UDN", ns).InnerText,
                                     UPC             = deviceInfo.SelectSingleNode("//x:device/x:UPC", ns).InnerText,
                                     MacAddress      = deviceInfo.SelectSingleNode("//x:device/x:macAddress", ns).InnerText,
                                     FirmwareVersion = deviceInfo.SelectSingleNode("//x:device/x:firmwareVersion", ns).InnerText,
                                 };
                                 // Raise event
                                 WemoDeviceFound?.Invoke(this, new WemoDeviceFoundEventArgs()
                                 {
                                     Device = device
                                 });
                             }
                         }
                     }
                 };
                 wc.DownloadStringAsync(locationUri);
             }
         }
         catch { }
     }
 }