aus diesem Thread https://www.loxforum.com/forum/faqs-...ausf%C3%BChren
kam die Idee ein Autoitskript zu schreiben, um von Loxone aus Befehle auf Windows-PCs ausführen zu können.
Die Möglichkeiten in Autoit sind nahezu ungebgrenzt. Es kommt also darauf an, welche Befehle man ausführen möchte.
Ich habe mal 3 Beispiele gemacht.
1) Text ausgeben
2) Programm starten (Firefox)
3) Dos-Command ausführen
Autoit könnt ihr hier runterladen: https://www.autoitscript.com/site/autoit/downloads/
Das Skript kann kompiliert werden und als *.exe genutzt werden und dann natürlich auch als Dienst eingebunden werden.
Falls jemand Probleme hat seine Befehle in Autoit umzusetzen, dann bitte einfach melden.
Nutzung des Skripts
In Loxone einen Virtuellen Ausgang anlegen und dazu eine Anzahl von Virteuller Ausgang Befehle. In den einzelnen Befehlen unter Befehl bei EIN den String der gesendet werden soll.
Ich habe es so gemacht, dass bei Verbindungsaufbau
Loxone:DEINBEFEHL
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=connected.ico
#AutoIt3Wrapper_Outfile=LoxoneCommands.exe
#AutoIt3Wrapper_Res_Comment=Receive TCP commands from your Loxone Miniserver and start commands on your Windows-PC.
#AutoIt3Wrapper_Res_Description=Any questions then please go to http://www.loxforum.org
#AutoIt3Wrapper_Res_Language=1031
#AutoIt3Wrapper_Res_Field=Author|Xenobiologist
#AutoIt3Wrapper_Run_Au3Stripper=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
;Communication TCP Loxone - Autoit
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <TrayConstants.au3>
#include <File.au3>
#include <Date.au3>
Opt('TrayAutoPause', 0)
Opt("TrayMenuMode", 3)
Opt("TCPTimeout", 10)
OnAutoItExitRegister('OnAutoItExit')
; ### Your local IP address!
; ### Usally the macro @IPAddress1 works.
; ### If you 've got some virtual network interfaces installed, @IPAddress2 or @IPAddress3 or @IPAddress4 could be the correct one.
Global $ip = @IPAddress1 ; or '192.168.xxx.xxx'
$ip = '192.168.178.52' ; my local IP-address :-)
ConsoleWrite('Listen : ' & $ip & @CRLF)
Global $port = 50001 ; The port has to be the same as defined in your virtual output in your Loxone configuration
Global $MainSocket = -1, $ConnectedSocket = -1
Global $recv = ''
Global $log = True, $path_logfile = @ScriptDir & '\' & @ScriptName & '.log'
; IP-addresses which are allowed to connect to your Windows-PC.
; You can add as much as you want, just append them to the string speprated by a "," - '192.168.178.79, 192.168.178.78, 192.168.178.77'
Global $accpeted_IPs = StringSplit('192.168.178.79, 192.168.178.78, 192.168.178.77', ',', 2)
; Start init function (Listen for incoming connection)
_init()
; main loop (create the menu and check for incoming commands
_main()
; Your functions
Func _command_1($recv)
If $log Then _FileWriteLog($path_logfile, 'Command 1 executed!')
MsgBox($MB_ICONINFORMATION, 'Loxone TCP', $recv, 5)
EndFunc ;==>_command_1
Func _command_2()
If $log Then _FileWriteLog($path_logfile, 'Command 2 executed!')
ShellExecute('Firefox')
EndFunc ;==>_command_2
Func _command_3($recv)
If $log Then _FileWriteLog($path_logfile, 'Command 3 executed!')
ConsoleWrite(_NowTime() & " " & $recv & @CRLF)
Run(@ComSpec & " /k " & $recv, "", @SW_SHOW) ; k=keep, c=close window;
EndFunc ;==>_command_3
Func _command_4()
If $log Then _FileWriteLog($path_logfile, 'Command 4 executed!')
; was auch immmer
EndFunc ;==>_command_4
Func _command_5()
If $log Then _FileWriteLog($path_logfile, 'Command 5 executed!')
; was auch immmer
EndFunc ;==>_command_5
Func _main()
Local $c1 = -1, $c2 = -1, $c3 = -1, $c4 = -1, $c5 = -1
Local $iSettings = TrayCreateMenu('Loxone commands') ; Create a tray menu sub menu with two sub items.
$c1 = TrayCreateItem('1) Show MsgBox', $iSettings)
$c2 = TrayCreateItem('2) Start Firefox', $iSettings)
$c3 = TrayCreateItem('3) Run_in_CMD', $iSettings)
;~ $c4 = TrayCreateItem('4', $iSettings)
;~ $c4 = TrayCreateItem('4', $iSettings)
;~ $c5 = TrayCreateItem('5', $iSettings)
TrayCreateItem('') ; Create a separator line.
Local $tray_exit = TrayCreateItem('Exit')
TraySetState($TRAY_ICONSTATE_SHOW)
While 1
Switch TrayGetMsg()
Case $c1
_command_1($recv)
Case $c2
_command_2()
Case $c3
_command_3($recv)
Case $c4
_command_4()
Case $c5
_command_5()
Case $tray_exit
Exit
EndSwitch
ConsoleWrite(_NowTime() & @CRLF)
_checkTCP() ; ~ Every 250 ms
WEnd
EndFunc ;==>_main
Func _init()
TCPStartup()
$MainSocket = TCPListen($ip, $port, 100)
If @error Then
; Someone is probably already listening on this IP Address and Port (script already running?).
Local $iError = @error
MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), '', 'Could not listen, Error code: ' & $iError)
Else
ConsoleWrite('Listen successful.' & @CRLF)
EndIf
EndFunc ;==>_init
Func _checkTCP()
; If no connection look for one
If $ConnectedSocket = -1 Then
$ConnectedSocket = TCPAccept($MainSocket)
If @error Then
$iError = @error
MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), '', 'Could not accept the incoming connection, Error code: ' & $iError)
EndIf
; Check for valid remote IP-Address
If $ConnectedSocket <> -1 Then
Local $connectedIP = _SocketToIP($ConnectedSocket)
ConsoleWrite('ConnectedIP (try): ' & $connectedIP & @CRLF)
If StringInStr(StringStripWS(_ArrayToString($accpeted_IPs), 8), $connectedIP, 2) = 0 Then
If $log Then _FileWriteLog($path_logfile, 'Invalid connection from : ' & $connectedIP)
TCPCloseSocket($ConnectedSocket)
$ConnectedSocket = -1
Return -1
Else
ConsoleWrite('Connection accepted from : ' & $connectedIP & @CRLF)
If $log Then _FileWriteLog($path_logfile, 'Valid connection from : ' & $connectedIP)
EndIf
EndIf
; If connected try to read some data
Else
$recv = TCPRecv($ConnectedSocket, 512)
$err = @error
If $recv <> '' Then
Switch StringStripWS($recv, $STR_STRIPLEADING + $STR_STRIPTRAILING)
; the string sent by Loxone needs to match the string used in case ...
; ### Change something here to match see "keyword" sent from Loxone Miniserver
; ### The easiest way is to send Loxone:2, Loxone:3 and so on.
; ### You could also use names like in example 1 "Loxone:It works!"
Case 'Loxone:It works!'
_command_1($recv)
Case 'Loxone:2'
_command_2()
Case 'Loxone:3'
_command_3('"ipconfig /all"')
Case 'Loxone:4'
_command_4()
Case 'Loxone:5'
_command_5()
EndSwitch
TCPCloseSocket($ConnectedSocket)
$ConnectedSocket = -1
If $log Then _FileWriteLog($path_logfile, $recv)
ElseIf $err < 0 Then
If $log Then _FileWriteLog($path_logfile, 'Error occurred - closed')
TCPCloseSocket($ConnectedSocket)
$ConnectedSocket = -1
EndIf
EndIf
EndFunc ;==>_checkTCP
Func OnAutoItExit()
TCPCloseSocket($MainSocket)
TCPShutdown()
EndFunc ;==>OnAutoItExit
Func _SocketToIP($iSocket)
Local $tSockAddr = 0, $aRet = 0
$tSockAddr = DllStructCreate("short;ushort;uint;char[8]")
$aRet = DllCall("Ws2_32.dll", "int", "getpeername", "int", $iSocket, "struct*", $tSockAddr, "int*", DllStructGetSize($tSockAddr))
If Not @error And $aRet[0] = 0 Then
$aRet = DllCall("Ws2_32.dll", "str", "inet_ntoa", "int", DllStructGetData($tSockAddr, 3))
If Not @error Then Return $aRet[0]
EndIf
Return 0
EndFunc ;==>_SocketToIP
Mega


Kommentar