从Pebble切换到状态500的SOAP请求到Wemo

时间:2015-12-29 20:57:48

标签: soap xmlhttprequest pebble-watch pebble-sdk pebble-js

一直试图为wemo开关编写一个pebble应用程序,目前这是我使用的代码:

function WemoRequest(callback) {
    if (SOAPData === false || SOAPData === undefined) {
        console.log("Invalid SOAP data: " + JSON.stringify(SOAPData));
        return;
    }

var url = "http://192.168.1.230:49153/upnp/control/basicevent1";

try {
    var request = new XMLHttpRequest();
    request.open("POST", url, false);
    request.setRequestHeader("SOAPAction", "urn:Belkin:service:basicevent:1#GetBinaryState");
    request.setRequestHeader("Content-Type",  "text/xml; charset=utf-8");
    request.onreadystatechange = function() {
    if (request.readyState == 4 && request.status === 200 && callback) {
            callback(request, SOAPData);
            }else{console.log("Status: "+request.status + " State: "+request.readyState+" Callback: "+callback);}
    };
    var packet = '<?xml version="1.0" encoding="utf-8"?>'+
            '<s:Envelope xmls:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">'+
        '<s:Body>'+
            '<u:GetBinaryState xmlns:u="urn:Belkin:service:basicevent:1"></u:GetBinaryState>'+
        '</s:Body>'+
    '</s:Envelope>';

    request.send(packet);
} catch (error) {
    console.log("Error in XMLHttpRequest: " + error);
}}

我目前从OnReadyStateChange获得状态500,并且不知道我做错了什么。如果此代码不够,则可在此处找到应用代码:https://github.com/dmf444/Webble

1 个答案:

答案 0 :(得分:0)

所以...我知道这是4年前的大声笑,但是我在Google搜索期间发现了这个问题,并找到了答案,因此我想我会做出回应:我认为您的标头仅需要一个额外的设置“ urn:Belkin:service:basicevent:1#SetBinaryState”周围的引号引起来,以便指定肥皂动作的字符串从字面上开始并以引号结束。

我正在使用Python工作(因为这几天所有孩子似乎都在做),但是我也遇到了500错误,直到我做了非常细微的更改(双引号周围的单引号)并且当我的灯熄灭时,几乎哭泣着喜悦的眼泪:

"SOAPACTION": '"urn:Belkin:service:basicevent:1#SetBinaryState"'

这是代码的工作版本(在Python大声笑中):

import http.client

#Variables (value=on/off, ipaddress=address of your wemo)
value = 0 #1=ON, 0=OFF
ipAddress = "192.168.0.108"

#Build the SOAP Envelope (data)
data = '<?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>' + str(value) + '</BinaryState></u:SetBinaryState></s:Body></s:Envelope>'

#Build the Header (headers)
headers = {"Content-type" : 'text/xml; charset="utf-8"', "SOAPACTION": '"urn:Belkin:service:basicevent:1#SetBinaryState"', "Content-Length": len(data)}

#Send request and check response data (resp_data)
conn = http.client.HTTPConnection(ipAddress, 49153)
conn.request("POST", "/upnp/control/basicevent1", data, headers)
response = conn.getresponse()
resp_data = response.read()

if response.status == 200:
    conn.close()
    print("SUCCESS!")
elif response.status == 403:
    print("ERROR: 403 (FORBIDDEN)")
else:
    print("ERROR: " + str(response.status))