Stretch Power & Switch event scripting with Autoit3

Plugwise Forum about Plugwise devices and the Source software.
Post Reply
Phoenix
Starting Member
Starting Member
Posts: 48
Joined: Sun Apr 28, 2013 9:40 pm
Location: Netherlands (Deventer)
Contact:

Stretch Power & Switch event scripting with Autoit3

Post by Phoenix »

Hi All,

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
Autoit3 code for computer reboot script

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 *****
You can always check out my topic here: http://phoenixinteractive.mine.nu/websi ... f=27&t=281
Last edited by Phoenix on Wed Oct 16, 2013 6:16 pm, edited 7 times in total.
Huisautomatisering - Domotica - Elektronica - IT consulting - Software ontwikkeling - 3D printing - Maatwerk
Website: domoticx.nl / Webshop: domoticx.nl/webwinkel / Knowledge Center: http://domoticx.com
Phoenix
Starting Member
Starting Member
Posts: 48
Joined: Sun Apr 28, 2013 9:40 pm
Location: Netherlands (Deventer)
Contact:

Re: Stretch Power & Switch event scripting with Autoit3

Post by Phoenix »

Autoit3 script for Power & Switch event scripting on a Stretch 2.0 with firmware 1.0.x

Code: Select all

; ------------------------------------------------------------------------------
; 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
Huisautomatisering - Domotica - Elektronica - IT consulting - Software ontwikkeling - 3D printing - Maatwerk
Website: domoticx.nl / Webshop: domoticx.nl/webwinkel / Knowledge Center: http://domoticx.com
Post Reply

Return to “Plugwise Forum”