Plugwise Stretch 2.0 – Schakelen met scripts (HTTP API)
Plugwise Stretch 2.0 API
Met een packet sniffer zoals bijvoorbeeld WireShark, kan je zien wat voor gegevens verstuurd worden naar de Stretch 2.0, zo kun je achterhalen hoe de circles geschakeld worden vanuit de Stretch 2.0
Firmware 2.0.x
Sinds de nieuwe firmware 2.0.x van plugwise is de API iets aangepast, met wat uitelkaar halen van de Plugwise desktop en gebruik van Wireshark heb ik het commando achterhaald:
Dit keer is het ook een POST commando (tov 1.0.x), alleen nu moet je XML data meezenden:
API HTTPÂ adres:Â
http://[stretchip]/core/appliances;id=[applianceid]/relay
API POST gegevens:Â
<relay><state>[on/off]</state></relay>
Firmware 1.0.x
1 2 3 4 5 6 7 8 9 10 11 12 |
POST /minirest/appliances;id=71abf031c54f400ca77c8ae6957ad7cf/power_state=on HTTP/1.1 X-Requested-With: XMLHttpRequest Accept: */* Referer: http://192.168.1.102/html_interface/ Accept-Language: nl Accept-Encoding: gzip, deflate User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0) Host: 192.168.1.102 Content-Length: 0 Connection: Keep-Alive Cache-Control: no-cache Authorization: Basic [BASE64 ENCODED WACHTWOORD] |
Hierbij moet je een HTTP POST commando versturen.
API HTTP adres:Â [stretchip]/minirest/appliances;id=[applianceid]/power_state=[on/off]
Plugwise Stretch 2.0 circles schakelen met PHP
Hier is een API voorbeeld om Circles te schakelen via de Stretch 2.0 met behulp van een PHP script.
Firmware 2.0.x
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
<?php // A PHP script to switch Plugwise Circles with a Stretch 2.0 with firmware 2.X.X // Version 1.0, 2014-11-18 by Sebastiaan Ebeltjes // Stretch 2.0 setup $StretchIp = "192.168.1.102"; //The IP adres of the Stretch 2.0, like: 192.168.1.X $StretchId = "STRETCHID"; //The 8 letters of the Stretch 2.0 ID // Switch setup (circle) $ApplianceId = "97b7844d4da34fe7b60eda3d4bc823da"; // The ID of the circle, look in http://[STRETCHIP]/core/appliances $SwitchStatus = "on"; // on/off // Kernel $AuthBase64 = base64_encode("stretch:".$StretchId); $Postdata = "<relay><state>".$SwitchStatus."</state></relay>"; $HTTPHandle = fsockopen($StretchIp, 80, $errno, $errstr, 30); if (!$HTTPHandle) { echo "$errstr ($errno)<br />\n"; } else { $Data = "POST /core/appliances;id=".$ApplianceId."/relay HTTP/1.1\r\n"; $Data .= "Host: ".$StretchIp."\r\n"; $Data .= "Content-Type: application/xml\r\n"; $Data .= "Content-length: ".strlen($Postdata)."\r\n"; $Data .= "Authorization: Basic ".$AuthBase64."\r\n"; $Data .= "Connection: Close\r\n\r\n"; $Data .= $Postdata; fwrite($HTTPHandle, $Data); while (!feof($HTTPHandle)) { echo fgets($HTTPHandle, 128); } fclose($HTTPHandle); } ?> |
Commandline:
Je kan de ApplianceID en Switchstatus ook opgeven via de PHP commandline, verander dan deze regels in:
1 2 |
$ApplianceId = $argv[1]; // The ID of the circle, look in http://[STRETCHIP]/minirest/appliances $SwitchStatus = $argv[2]; // on/off |
Zo kun je schakelen via een Linux systeem:Â php /volume1/web/schakel.php 97b7844d4da34fe7b60eda3d4bc823da on
Firmware 1.0.x
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<?php // A PHP script to switch Plugwise Circles with a Stretch 2.0 (tested on firmware 1.0.40) // Version 1.0, 2013-04-28 by Sebastiaan Ebeltjes // Stretch 2.0 setup $StretchIp = "192.168.1.102"; //The IP adres of the Stretch 2.0, like: 192.168.1.X $StretchId = "STRETCHID"; //The 8 letters of the Stretch 2.0 ID // Switch setup (circle) $ApplianceId = "71abf031c54f400ca77c8ae6957ad7cf"; // The ID of the circle, look in http://[STRETCHIP]/minirest/appliances $SwitchStatus = "on"; // on/off // Kernel $AuthBase64 = base64_encode("stretch:".$StretchId); $HTTPHandle = fsockopen($StretchIp, 80, $errno, $errstr, 30); if (!$HTTPHandle) { echo "$errstr ($errno)<br />\n"; } else { $Data = "POST /minirest/appliances;id=".$ApplianceId."/power_state=".$SwitchStatus." HTTP/1.1\r\n"; $Data .= "Host: ".$StretchIp."\r\n"; $Data .= "Authorization: Basic ".$AuthBase64."\r\n"; $Data .= "\r\n"; fwrite($HTTPHandle, $Data); while (!feof($HTTPHandle)) { echo fgets($HTTPHandle, 128); } fclose($HTTPHandle); } ?> |
Plugwise Stretch 2.0 circles schakelen met Python
Hier is een API voorbeeld om Circles te schakelen via de Stretch 2.0 met behulp van een Python script.
Firmware 2.0.x
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# A Python script to switch Plugwise Circles with a Stretch 2.0 with firmware 2.X.X # Version 1.0, 2015-05-13 by Sebastiaan Ebeltjes # Importeren van bibliotheken import urllib2 # Bibliotheek om data te ontvangen en te versturen via het HTTP protocol. import base64 # Bibliotheek voor base64 coderingen. # Stretch 2.0 setup stretchip = "http://192.168.102X" # The IP adres of the Stretch 2.0, like: 192.168.1.X stretchid = "STRETCHID" # The 8 letters of the Stretch 2.0 ID # Switch setup (circle) applianceid = "750a4e07ed8840528f8ceb5c313eff3d" # The ID of the circle, look in http://[STRETCHIP]/core/appliances switchstatus = "on" # on/off # Kernel postadres = stretchip + "/core/appliances;id=" + applianceid + "/relay" postgegevens = "<relay><state>" + switchstatus + "</state></relay>" httpdata = urllib2.Request(postadres) httpdata.add_header("Host", stretchip) httpdata.add_header("Content-Type", "application/xml") httpdata.add_header("Content-length", str(len(postgegevens))) httpdata.add_header("Authorization", "Basic " + base64.b64encode("stretch:" + stretchid)) httpdata.add_header("Connection", "Close") httpdata.add_data(postgegevens) httphandle = urllib2.urlopen(httpdata) if httphandle.code != 200: print "error:" + httphandle.read() |
Firmware 2.0.x + commandolijn parameters
Voorbeeld: sudo python schakelen.py 750a4e07ed8840528f8ceb5c313eff3d on
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# A Python script to switch Plugwise Circles with a Stretch 2.0 with firmware 2.X.X + commandline parameters # Version 1.0, 2015-05-13 by Sebastiaan Ebeltjes # Importeren van bibliotheken import urllib2 # Bibliotheek om data te ontvangen en te versturen via het HTTP protocol. import base64 # Bibliotheek voor base64 coderingen. import sys # Bibliotheek voor systeem handelingen. if len(sys.argv) < 3: print "Usage: script.py [Appliance ID] [on/off]" exit() # Stretch 2.0 setup stretchip = "http://192.168.0.102" # The IP adres of the Stretch 2.0, like: 192.168.1.X stretchid = "STRETCHID" # The 8 letters of the Stretch 2.0 ID # Switch setup (circle) applianceid = sys.argv[1] # Commandolijn parameter 1 switchstatus = sys.argv[2] # Commandolijn parameter 2 # Kernel postadres = stretchip + "/core/appliances;id=" + applianceid + "/relay" postgegevens = "<relay><state>" + switchstatus + "</state></relay>" httpdata = urllib2.Request(postadres) httpdata.add_header("Host", stretchip) httpdata.add_header("Content-Type", "application/xml") httpdata.add_header("Content-length", str(len(postgegevens))) httpdata.add_header("Authorization", "Basic " + base64.b64encode("stretch:" + stretchid)) httpdata.add_header("Connection", "Close") httpdata.add_data(postgegevens) httphandle = urllib2.urlopen(httpdata) if httphandle.code != 200: print "error:" + httphandle.read() |
Plugwise Stretch 2.0 circles schakelen met Autoit3
Hier is een API voorbeeld om Circles te schakelen via de Stretch 2.0, bij een foutmelding (geen HTTP 200 OK) wordt er een response (foutmelding) weergegeven in een messagebox.
Firmware 2.0.x
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
; ------------------------------------------------------------------------------ ; Autoit3 script to switch Plugwise Circles with a Stretch 2.0 with firmware 2.0.x) ; Version 1.0, 2013-10-07 by Sebastiaan Ebeltjes ; ------------------------------------------------------------------------------ ; Stretch 2.0 setup Global $StretchIp = "192.168.1.102" ;The IP adres of the Stretch 2.0, like: 192.168.1.X Global $StretchId = "STRETCHID" ; The 8 letters of the Stretch 2.0 ID ; Switch setup (circle) Global $ApplianceId = "71abf031c54f400ca77c8ae6957ad7cf" ;The ID of the circle, look in http://[STRETCHIP]/core/appliances Global $SwitchStatus = "on" ;on/off ; Kernel Global $PostAdres = "http://" & $StretchIp & "/core/appliances;id=" & $ApplianceId & "/relay" Global $PostGegevens = "<relay><state>" & $SwitchStatus & "</state></relay>" $oHTTP = ObjCreate("winhttp.winhttprequest.5.1") $oHTTP.Open("POST", $PostAdres, False) $oHTTP.SetRequestHeader("Host", $StretchIp) $oHTTP.SetRequestHeader("Connection", "Close") $oHTTP.SetRequestHeader("Content-Type", "application/xml") $oHTTP.SetRequestHeader("Authorization", "Basic " & _Base64Encode("stretch:" & $StretchId)) $oHTTP.Send($PostGegevens) $PostResponse = $oHTTP.ResponseText If $oHTTP.Status <> "200" Then MsgBox(64, "Foutmelding", $PostResponse) Exit Func _Base64Encode($sData) Local $oXml = ObjCreate("Msxml2.DOMDocument") If Not IsObj($oXml) Then SetError(1, 1, 0) EndIf Local $oElement = $oXml.createElement("b64") If Not IsObj($oElement) Then SetError(2, 2, 0) EndIf $oElement.dataType = "bin.base64" $oElement.nodeTypedValue = Binary($sData) Local $sReturn = $oElement.Text If StringLen($sReturn) = 0 Then SetError(3, 3, 0) EndIf Return $sReturn EndFunc ;_Base64Encode |
Firmware 1.0.x
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
------------------------------------------------------------------------------ ; Autoit3 script to switch Plugwise Circles with a Stretch 2.0 with firmware 1.0.x) ; Version 1.0, 2012-04-28 by Sebastiaan Ebeltjes ; ------------------------------------------------------------------------------ ; Stretch 2.0 setup Global $StretchIp = "192.168.1.102" ;The IP adres of the Stretch 2.0, like: 192.168.1.X Global $StretchId = "STRETCHID" ; The 8 letters of the Stretch 2.0 ID ; Switch setup (circle) Global $ApplianceId = "71abf031c54f400ca77c8ae6957ad7cf" ;The ID of the circle, look in http://[STRETCHIP]/minirest/appliances Global $SwitchStatus = "on" ;on/off ; Kernel Global $PostAdres = "http://" & $StretchIp & "/minirest/appliances;id=" & $ApplianceId & "/power_state=" & $SwitchStatus $oHTTP = ObjCreate("winhttp.winhttprequest.5.1") $oHTTP.Open("POST", $PostAdres, False) $oHTTP.SetRequestHeader("Host", $StretchIp) $oHTTP.SetRequestHeader("Authorization", "Basic " & _Base64Encode("stretch:" & $StretchId)) $oHTTP.Send() Global $PostResponse = $oHTTP.ResponseText If $oHTTP.Status <> "200" Then MsgBox(64, "Foutmelding", $PostResponse) Exit Func _Base64Encode($sData) Local $oXml = ObjCreate("Msxml2.DOMDocument") If Not IsObj($oXml) Then SetError(1, 1, 0) EndIf Local $oElement = $oXml.createElement("b64") If Not IsObj($oElement) Then SetError(2, 2, 0) EndIf $oElement.dataType = "bin.base64" $oElement.nodeTypedValue = Binary($sData) Local $sReturn = $oElement.Text If StringLen($sReturn) = 0 Then SetError(3, 3, 0) EndIf Return $sReturn EndFunc |