Plugwise Stretch 2.0 – Event scripting (AutoIt3)
Ik heb hier een script in Autoit3 geschreven dat de Stretch kan “monitoren” via een computer, zodoende kun je je eigen “events” script bouwen.
Je kan verschillende functies gebruiken zoals:
– Het detecteren van het opgenomen verbruik van een plug: ProbePower(“[name of circle]”)
– Het detecteren of een plug uit staat: ProbeState(“[name of circle]”)
– Het schakelen van een plug: PlugSwitch(“[name of circle]”, “[on/off]”)
In mijn geval…als de webserver uitvalt (power < 30 watt) wordt deze automatisch ge-reboot na 6 seconden:
1 2 3 4 5 6 7 8 9 10 11 |
; ***** BEGIN - Webserver auto reboot script ***** ; ; Please set in BIOS op CPU -> startup on AC power! ; This will start the system after the circle is being switched on! ; If ProbePower("webserver") < 30 Then PlugSwitch("webserver", "off") ; Turn plug OFF Sleep(6000) PlugSwitch("webserver", "on") ; Turn plug ON EndIf ; ***** END - Webserver auto reboot script ***** |
Autoit3 script voor Power & Switch event scripting op een Stretch 2.0 met 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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
; ------------------------------------------------------------------------------ ; Autoit3 script for Power & Switch event scripting on a Stretch 2.0 with firmware 2.0.x) ; v2.1, 2013-10-16 by Sebastiaan Ebeltjes ; Greetings from Deventer, The Netherlands ; ; Examples: ; ; View consumed power (W) of a circle: ; Function: ProbePower("[name of circle]") ; Return: Value/Number ; ; View state (on/off) of a circle: ; Function: ProbeState("[name of circle]") ; Return: String/(on/off) ; ; Switch a circle (on/off): ; Function: PlugSwitch("[name of circle]", "[on/off]") ; Will switch the first found circle with that name on/off ; ------------------------------------------------------------------------------ #include <array.au3> #include <String.au3> Global $ProbeTime = 300000; 300 sec (5 min) Global $ApplianceID, $SwitchStatus, $GetResponse, $GetStatus, $NameToSearch ;Stretch 2.0 setup Global $StretchIP = "192.168.0.x" ;The IP adres of the Stretch 2.0, like: 192.168.x.x Global $StretchID = "abcdfghj" ; The 8 letters of the Stretch 2.0 ID While 1 ; PUT you script(s) here! Sleep($ProbeTime) Wend Func ProbePower($NameToSearch) GetData($StretchIP, $StretchID) If $GetStatus = "200" Then ; OK...reading values! $GivenName = _StringBetween($GetResponse, "<name>", "</name>") $ApplianceID = _StringBetween($GetResponse, "<appliance id='", "'>") For $loop = 1 to Ubound($GivenName) -1 If $GivenName[$loop] = $NameToSearch Then $PowerUsage_temp1 = _StringBetween($GetResponse, "<type>electricity_consumed</type>" & @CRLF & @TAB & @TAB & @TAB & @TAB & "<unit>W</unit>" , "rement>") $PowerUsage_temp2 = _StringBetween($PowerUsage_temp1[$loop], "<measurement log_date=", "/measu") $PowerUsage = _StringBetween($PowerUsage_temp2[0], ">", "<") Return $PowerUsage[0] EndIf Next EndIf EndFunc ;ProbePower Func ProbeState($NameToSearch) GetData($StretchIP, $StretchID) If $GetStatus = "200" Then ; OK...reading values! $GivenName = _StringBetween($GetResponse, "<name>", "</name>") $SwitchState = _StringBetween($GetResponse, "<state>", "</state>") For $loop = 1 to Ubound($GivenName) -1 If $GivenName[$loop] = $NameToSearch Then Return $SwitchState[$loop] Next EndIf EndFunc ;ProbeState Func PlugSwitch($NameToSearch, $SwitchStatus) GetData($StretchIP, $StretchID) If $GetStatus = "200" Then ; OK...reading values! $GivenName = _StringBetween($GetResponse, "<name>", "</name>") $ApplianceID = _StringBetween($GetResponse, "<appliance id='", "'>") For $loop = 1 to Ubound($GivenName) -1 If $GivenName[$loop] = $NameToSearch Then Global $PostAdres = "http://" & $StretchIP & "/core/appliances;id=" & $ApplianceID[$loop] & "/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 ExitLoop EndIf Next EndIf EndFunc ;PlugSwitch Func GetData($StretchIP, $StretchID) Global $GetAdres = "http://" & $StretchIP & "/core/appliances" $oHTTP = ObjCreate("winhttp.winhttprequest.5.1") $oHTTP.Open("GET", $GetAdres, False) $oHTTP.SetRequestHeader("Host", $StretchIP) $oHTTP.SetRequestHeader("Authorization", "Basic " & _Base64Encode("stretch:" & $StretchID)) $oHTTP.Send() $GetResponse = $oHTTP.ResponseText $GetStatus = $oHTTP.Status EndFunc ;GetData 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 |
Autoit3 script voor Power & Switch event scripting op een Stretch 2.0 met 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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
; ------------------------------------------------------------------------------ ; Autoit3 script for Power & Switch event scripting on a Stretch 2.0 with firmware 1.0.x) ; v1.0, 2013-10-15 by Sebastiaan Ebeltjes ; Greetings from Deventer, The Netherlands ; ; Examples: ; ; View consumed power (W) of a circle: ; Function: ProbePower("[name of circle]") ; Return: Value/Number ; ; View state (on/off) of a circle: ; Function: ProbeState("[name of circle]") ; Return: String/(on/off) ; ; Switch a circle (on/off): ; Function: PlugSwitch("[name of circle]", "[on/off]") ; Will switch the first found circle with that name on/off ; ------------------------------------------------------------------------------ #include <array.au3> #include <String.au3> Global $ProbeTime = 300000; 300 sec (5 min) Global $ApplianceID, $SwitchStatus, $GetResponse, $GetStatus, $NameToSearch ;Stretch 2.0 setup Global $StretchIP = "192.168.x.x" ;The IP adres of the Stretch 2.0, like: 192.168.x.x Global $StretchID = "abcdefgj" ; The 8 letters of the Stretch 2.0 ID While 1 ; PUT you script(s) here! ;Webserver reboot If ProbePower("webserver") < 30 Then InetRead("http://mail.phoenixinteractive.mine.nu:8080/mail.php?to=phoenixinteractive@hotmail.com&subject=server_uitval&message=server_is_uitgevallen_om_" & @HOUR & "_" & @MIN & "_" & @SEC, 1) PlugSwitch("webserver", "off") ; Turn plug OFF Sleep(6000) PlugSwitch("webserver", "on") ; Turn plug ON EndIf Sleep($ProbeTime) Wend Func ProbePower($NameToSearch) GetData($StretchIP, $StretchID) If $GetStatus = "200" Then ; OK...reading values! $GivenName = _StringBetween($GetResponse, "<name>", "</name>") $PowerUsage = _StringBetween($GetResponse, "<current_power_usage>", "</current_power_usage>") For $loop = 1 to Ubound($GivenName) -1 If $GivenName[$loop] = $NameToSearch Then Return $PowerUsage[$loop] Next EndIf EndFunc ;ProbePower Func ProbeState($NameToSearch) GetData($StretchIP, $StretchID) If $GetStatus = "200" Then ; OK...reading values! $GivenName = _StringBetween($GetResponse, "<name>", "</name>") $SwitchState = _StringBetween($GetResponse, "<power_state>", "</power_state>") For $loop = 1 to Ubound($GivenName) -1 If $GivenName[$loop] = $NameToSearch Then Return $SwitchState[$loop] Next EndIf EndFunc ;ProbeState Func PlugSwitch($NameToSearch, $SwitchStatus) GetData($StretchIP, $StretchID) If $GetStatus = "200" Then ; OK...reading values! $GivenName = _StringBetween($GetResponse, "<name>", "</name>") $ApplianceID = _StringBetween($GetResponse, "<appliance id=" & Chr(34), Chr(34) & ">") For $loop = 1 to Ubound($GivenName) -1 If $GivenName[$loop] = $NameToSearch Then Global $PostAdres = "http://" & $StretchIP & "/minirest/appliances;id=" & $ApplianceID[$loop] & "/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() $PostResponse = $oHTTP.ResponseText $PostStatus = $oHTTP.Status ExitLoop EndIf Next EndIf EndFunc ;PlugSwitch Func GetData($StretchIP, $StretchID) Global $GetAdres = "http://" & $StretchIP & "/minirest/appliances" $oHTTP = ObjCreate("winhttp.winhttprequest.5.1") $oHTTP.Open("GET", $GetAdres, False) $oHTTP.SetRequestHeader("Host", $StretchIP) $oHTTP.SetRequestHeader("Authorization", "Basic " & _Base64Encode("stretch:" & $StretchID)) $oHTTP.Send() $GetResponse = $oHTTP.ResponseText $GetStatus = $oHTTP.Status EndFunc ;GetData 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 |