#! /usr/bin/env python3
#-*- coding: utf-8 -*-

# python /home/scripts_python_tests_capteurs/UDP_server/AQCM05_UDP_scanner.py

import socket
import time  

# Create a UDP socket at client side
UDPClientSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
UDPClientSocket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
UDPClientSocket.settimeout(1.0) 
bufferSize          = 1024

msgFromClient       = "AQCM05"
bytesToSend         = str.encode(msgFromClient)

master_failed = 0
timout_failed = 0

sondes_adress_port   = ("192.168.1.255", 31559)

try :
	# Send to server using created UDP socket
	UDPClientSocket.sendto(bytesToSend, sondes_adress_port)
	print("la requête de scan est diffusée...")
	
	timestamp_1 = time.time()
	
	while ( time.time() < timestamp_1 + 5 ) :
		try :
			msgFromServer = UDPClientSocket.recvfrom(bufferSize)
			message = msgFromServer[0].decode('utf-8') 
			print(message)
		except :
			timout_failed += 1
		

except :
	master_failed += 1

UDPClientSocket.close()

print("master_failed = ", master_failed)
print("timout_failed = ", timout_failed)
