Iv'e written a script in Autoit3 that can "monitor" the stretch with a computer, this way you can create your own "event" scripts!
You can use diffrent functions like:
- Get the consumed power of a plug: ProbePower("[name of circle]")
- Get the state of a plug on/off: ProbeState("[name of circle]")
- Switching of a plug: PlugSwitch("[name of circle]", "[on/off]")
In my case...when the webserver is down (power <30 watt) it's getting automaticly rebooted after 6 seconds (see the script on the bottom of this topic)
Autoit3 script for Power & Switch event scripting on a Stretch 2.0 with firmware 2.0.x
Code: Select all
; ------------------------------------------------------------------------------
; 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.x.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
Code: Select all
; ***** 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 *****