[Help] Optimisation de mon script

Aide et conseils concernant AutoIt et ses outils.
Règles du forum
.
Répondre
Tabango
Niveau 1
Niveau 1
Messages : 1
Enregistré le : sam. 01 avr. 2017 10:12
Status : Hors ligne

[Help] Optimisation de mon script

#1

Message par Tabango »

Bonjours à tous !

Débutant avec Autoit j'ai écris un petit script qui me permet de commander un affichage LED à partir d'un programme qui gère un autre type d'écran ancien.
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Outfile=..\Chronos_LedDisp.exe
#AutoIt3Wrapper_Outfile_x64=..\Chronos_LedDispX64.exe
#AutoIt3Wrapper_Compile_Both=y
#AutoIt3Wrapper_Res_Language=4108
#AutoIt3Wrapper_Add_Constants=n
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
Global $l_IP = @IPAddress2 ;  Set the listening IP
Global $conned = False
Global $ConnectedSocket = ""
Global $iRet
Global $hWs2_32
Global $MainSocket
Global $RecvChronosData
Global $hSocket
Global $UDPbind

TCPShutdown()
TCPCloseSocket($MainSocket)
TCPStartup() ;  Starts TCP services

$MainSocket = TCPListen($l_IP, 6969, 100) ;  start a listening socket and Port
If $MainSocket = -1 Then reconnect()

Func reconnect()
   Do
      $ConnectedSocket = TCPAccept($MainSocket)
   Until $ConnectedSocket = -1
   ;Sleep(200)
   $remIP = SocketToIP($ConnectedSocket)
   Return $remIP
EndFunc   ;==>reconnect

;  if a socket can't be created, exit the server

_waitForConn() ;  Start the loop that makes the program look for clients.

Func _waitForConn()

   While $conned = False ;  look for client connection loop
      $ConnectedSocket = TCPAccept($MainSocket) ;  check if somebody wants to connect


      If $ConnectedSocket >= 0 Then ;  if a client tries to connect, let him do so.
         $conned = True ;  set the conned var to true (really no use for this... Used it in an older version. will be removed soon.)
         _iAmConned() ;  start the connected loop.

      EndIf
   WEnd


EndFunc   ;==>_waitForConn

Func _iAmConned()


   While $conned = True
      $ChronosData = ""
      $ChronosData = TCPRecv($ConnectedSocket, 100)
      ConsoleWrite(@CRLF & $ChronosData & @CRLF)
      BinaryToString($ChronosData)
      If $ChronosData = StringInStr($ChronosData, "0x0002305203") Or $ChronosData = StringInStr($ChronosData, "0x0002305603") Then _CommandLedDisplay($ChronosData)
   WEnd

EndFunc   ;==>_iAmConned

Func _CommandLedDisplay($RecvChronosData)

   If StringInStr($RecvChronosData, "0x0002305203") Or StringInStr($RecvChronosData, "0x023030303") <> 0 Then ;  If the package contained the message "0x0002305203" (Chrono command for stoping chrono) then
      _ConnectDisplayLedUDP()
      ConsoleWrite(@CRLF & "Arret CHRONO en cours" & @CRLF)
      $RECV = ""
      While $RECV <> "# 5E00"
         _WinsockSend($hSocket, "*#5TIMR1,0000")
         $RECV = UDPRecv($UDPbind, 6, 0)
         ConsoleWrite("RECV stop: " & $RECV & @CRLF)
      WEnd
      $RECV = ""
      ConsoleWrite(@CRLF & "CHRONO Stoper" & @CRLF)
      While $RECV <> "# 5E00"
         ;Sleep(600)
         _WinsockSend($hSocket, "*#5PRGC31,0000")
         $RECV = UDPRecv($UDPbind, 6, 0)
         ConsoleWrite("RECV PRG1: " & $RECV & @CRLF)
      WEnd
      $RECV = ""
      ConsoleWrite(@CRLF & "LedDisp: Heure + Logo AFM" & @CRLF)
      _DisconnectDisplayLedUDP()

   Else
      If StringInStr($RecvChronosData, "0x0002305603") Then
         _ConnectDisplayLedUDP()
         ConsoleWrite(@CRLF & "Affichage Chrono LedDisplay" & @CRLF)
         $RECV = ""
         While $RECV <> "# 5E00"
            _WinsockSend($hSocket, "*#5PRGC30,0000")
            $RECV = UDPRecv($UDPbind, 6, 0)
            ConsoleWrite("RECV PRG0: " & $RECV & @CRLF)
         WEnd
         $RECV = TCPRecv($ConnectedSocket, 2048)
         While $RECV <> "# 5E00"
            ;Sleep(600)
            _WinsockSend($hSocket, "*#5TIMS1,0000")
            $RECV = UDPRecv($UDPbind, 6, 0)
            ConsoleWrite("RECV Run: " & $RECV & @CRLF)
         WEnd
         $RECV = ""
         ConsoleWrite(@CRLF & "CHRONO LedDisplay lancer" & @CRLF)
         _DisconnectDisplayLedUDP()
      EndIf
   EndIf
   $RecvChronosData = ""
   Sleep(50)
   $ChronosData = ""
   Sleep(50)
   $RECV = ""
   Sleep(50)
EndFunc   ;==>_CommandLedDisplay

Func _ConnectDisplayLedUDP()


   $ChronosData = ""
   UDPStartup()
   $hWs2_32 = DllOpen("ws2_32.dll")
   $UDPbind = UDPBind(@IPAddress2, 6969) ; Ip and len buff. local wifi PC
   UDPCloseSocket($UDPbind)
   $hSocket = _WinsockCreateSocket(2, 2, 17)
   $UDPbind[1] = $hSocket
   _WinsockConnect($hSocket, "192.168.0.252", 5959); IP and Port LedDisplay.

EndFunc   ;==>_ConnectDisplayLedUDP

Func _DisconnectDisplayLedUDP()

   UDPCloseSocket($hSocket)
   UDPCloseSocket($UDPbind)
   UDPShutdown()
   $RecvChronosData = ""
   $ChronosData = ""

EndFunc   ;==>_DisconnectDisplayLedUDP

Func _resetConnection()
   TCPCloseSocket($ConnectedSocket) ;  Closes the socket
   $ConnectedSocket = -1 ;  Resets the ConnectedSocket placeholder
   $conned = False ;  set the conned var to false (really no use for this... Used it in an older version. will be removed soon.)
   _waitForConn() ;  go back to the "searching for a connection" function.
EndFunc   ;==>_resetConnection
Func _WinsockCreateSocket($Address_Family, $Socket_Type, $Protocol)
   $iRet = DllCall($hWs2_32, "int", "socket", "int", $Address_Family, "int", $Socket_Type, "int", $Protocol)
   Return $iRet[0]
EndFunc   ;==>_WinsockCreateSocket

Func _WinsockConnect($hSocket, $IP, $Port)
   $hAddr = _SocketAddr($IP, $Port)
   $iRet = DllCall($hWs2_32, "int", "connect", "uint", $hSocket, "ptr", DllStructGetPtr($hAddr), "int", DllStructGetSize($hAddr))
EndFunc   ;==>_WinsockConnect

Func _WinsockSend($hSocket, $data)
   $hBuf = DllStructCreate("byte[" & BinaryLen($data) & "]")
   DllStructSetData($hBuf, 1, $data)
   $iRet = DllCall($hWs2_32, "int", "send", "uint", $hSocket, "ptr", DllStructGetPtr($hBuf), "int", DllStructGetSize($hBuf), "int", 0)
EndFunc   ;==>_WinsockSend

Func _SocketAddr($IP, $Port, $Address_Family = 2)
   $stAddress = DllStructCreate("short; ushort; uint; char[8]")
   DllStructSetData($stAddress, 1, $Address_Family)
   $iRet = DllCall($hWs2_32, "ushort", "htons", "ushort", $Port)
   DllStructSetData($stAddress, 2, $iRet[0])
   $iRet = DllCall($hWs2_32, "uint", "inet_addr", "str", $IP)
   DllStructSetData($stAddress, 3, $iRet[0])
   Return $stAddress
EndFunc   ;==>_SocketAddr

Func _WSAGetLastError()
   $iRet = DllCall($hWs2_32, "int", "WSAGetLastError")
   Return $iRet[0]
EndFunc   ;==>_WSAGetLastError

Func closeServer()
   TCPShutdown()
   Exit
EndFunc   ;==>closeServer

Func SocketToIP($SHOCKET)
   Local $sockaddr, $aRet

   $sockaddr = DllStructCreate("short;ushort;uint;char[8]")

   $aRet = DllCall("Ws2_32.dll", "int", "getpeername", "int", $SHOCKET, _
         "ptr", DllStructGetPtr($sockaddr), "ptr", DllStructGetSize($sockaddr))
   If Not @error And $aRet[0] = 0 Then
      $aRet = DllCall("Ws2_32.dll", "str", "inet_ntoa", "int", DllStructGetData($sockaddr, 3))
      If Not @error Then $aRet = $aRet[0]
   Else
      $aRet = 0
   EndIf

   $sockaddr = 0

   Return $aRet
EndFunc   ;==>SocketToIP
Mon script fonctionne avec quelques aléas et j'aurai désiré votre aide afin d'améliorer celui-ci de manière optimal.

Merci d'avance à vous de prendre la temps pour ce coup de pouce

P.S. Je pense que mon code va en faire rire pas mal mais au moins j'ai eu le mérite de mettre un peu les doigts dans la vase pour réaliser cela.
Merci de ne pas vous retenir de rire ... c'est bon pour la santé
Répondre