Commit d6fe421e authored by hichbra's avatar hichbra

Passage du bluetooth a ethernet

parent 229b4bd4
# PYTHON 3.5+
import socket
import sys
from datetime import datetime
CHUNK_SIZE = 2048
log = open("log.txt", "w")
file = None
while True:
try:
print("Attente Client")
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(("", 12345))
server.listen(10)
client, addr = server.accept()
print("Client acceptee : ", addr)
name = datetime.now().strftime("%d-%m-%Y_%H.%M.%S")
file = open("data/"+name, "wb")
while True:
data = client.recv(CHUNK_SIZE)
file.write(data)
if len(data) == 0:
print("Fichier recu")
log.write(str(datetime.now())+" => Fichier Recu\n")
file.close()
client.close()
server.close()
break
print(" -> chunk")
except KeyboardInterrupt:
if file is not None:
file.close()
client.close()
print("Deconnexion")
server.close()
log.write(str(datetime.now())+" ==> End Keyboard\n")
log.close()
sys.exit(0)
except Exception as ex:
print("Exception ", type(ex))
log.write(str(datetime.now())+" ==> Exception "+str(type(ex))+" ("+str(ex.args)+")\n")
import glob
import subprocess
import sys
import serial
import time
import os
import psutil
from datetime import datetime
def check_pid(pid):
# Verifie l'existance d'un pid
if psutil.pid_exists(pid):
p = psutil.Process(pid)
if p.status() != psutil.STATUS_ZOMBIE:
return True
return False
process_port = [] # Liste des ports en cours d'utilisation
monitoringPID = {} # Permet de recuperer et tester les processus des ports en cours d'utilisation
log = open("log/logDispacher.txt", "w")
while True:
try:
ports_series = glob.glob("/dev/ttyACM*")
print("Port = ", ports_series)
print("Open = ", process_port)
# Lance la recuperation des port existants
for port in ports_series:
if port not in process_port:
try:
process = subprocess.Popen(["python3", "RecuperationValeur.py", port])
process_port.append(port)
monitoringPID[port] = process.pid
log.write(str(datetime.now())+" => Start "+port+"\n")
except:
log.write(str(datetime.now())+" => Exception au Demmarrage "+port+"\n")
process_port.remove(port)
del monitoringPID[port]
# Test si des processus sont morts
for port in process_port:
if not check_pid(monitoringPID[port]):
log.write(str(datetime.now())+" => Mort de "+port+"\n")
process_port.remove(port)
del monitoringPID[port]
time.sleep(10)
except KeyboardInterrupt:
log.write(str(datetime.now())+" => End Keyboard\n")
log.close()
sys.exit(0)
#except Exception as ex:
# print("Exception ", type(ex))
# log.write(str(datetime.now())+" => Exception "+str(type(ex))+" ("+str(ex.args)+")\n")
# sys.exit(1)
......@@ -5,13 +5,12 @@ import sys
import subprocess
import os
port = sys.argv[1] #"ttyACM0"
port = sys.argv[1].split("/")[2] #"/dev/ttyACM0"
try:
os.makedirs(port)
except OSError:
if not os.path.isdir(port):
Raise
except:
print ("Existing folder")
serial_port = "/dev/"+port
baud_rate = 9600; #In arduino, Serial.begin(baud_rate)
......@@ -20,30 +19,40 @@ file = port+"/"+datetime.now().strftime("%d-%m-%Y_%H.%M.%S")
delai = datetime.now()
output_file = open(file, "w+")
output_file.write("id:"+port+"\n")
ser = serial.Serial(serial_port, baud_rate)
try:
ser = serial.Serial(serial_port, baud_rate)
except:
print("Deconnexion")
sys.exit(1)
while True:
try:
line = ser.readline();
line = line.decode("utf-8") #ser.readline returns a binary, convert to string
line = line.decode("utf-8") #ser.readline returns a binary, convert to string
#temps = time.time()-debut
#temps = datetime.now()-debut
s = str(datetime.now())+" ; "+line
print(s);
output_file.write(s);
if ((datetime.now()-delai).seconds)/60 >= 60:
s = str(datetime.now())+" ; "+line
if (datetime.now()-delai).seconds % 10 == 1:
print(s);
output_file.write(s);
if ((datetime.now()-delai).seconds)/60 >= 1:
print("PROCESS "+port)
output_file.close()
fichierAEnvoyer = file
file = port+"/"+datetime.now().strftime("%d-%m-%Y_%H.%M.%S");
subprocess.Popen(["python", "processusEnvoiBluetooth.py", fichierAEnvoyer])
subprocess.Popen(["python3", "processusEnvoi.py", fichierAEnvoyer])
delai = datetime.now()
output_file = open(file, "w+")
output_file.write("id:"+port+"\n")
else:
print(((datetime.now()-delai).seconds)/60)
if (datetime.now()-delai).seconds % 10 == 1:
print(((datetime.now()-delai).seconds)/60)
except (KeyboardInterrupt, SystemExit):
sys.exit(0)
except serial.SerialException as e:
sys.exit(1)
except:
print("Exception")
pass
# PYTHON 3.5+
from datetime import datetime
import socket
import sys
import os
import os.path
import time
# Verifie qu'il n'y a pas d'autre processus d'envoi en verifiant le fichier lock
if os.path.isfile("./.lockprocess"): print("Locked")
while os.path.isfile("./.lockprocess"): time.sleep(1)
# Creer un nouveau fichier lock pour verrouiller les autres processus
if not os.path.isfile("./.lockprocess"):
open(".lockprocess", "a").close()
else: # Dans l'improbable eventualite ou un processus se serait activee entre la 1ere boucle et la condition
while os.path.isfile("./.lockprocess"): time.sleep(1)
open(".lockprocess", "a").close()
IP_MODULE_4G = "169.254.240.59"
nomFichier = sys.argv[1]
chemin = nomFichier.split("/")
log = open("log/"+chemin[1]+".log", "w")
f = None
try:
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.connect((IP_MODULE_4G, 12345))
print("Connexion")
f = open(nomFichier, "rb")
print("Calcul Taille")
taille = os.path.getsize(nomFichier)
print("Envoi")
totalEnvoyee = 0
with open(nomFichier, 'rb') as f:
totalEnvoyee += server.sendfile(f, 0)
print(" -> chunk %d/%d " % (totalEnvoyee, taille))
log.write(" -> chunk "+str(totalEnvoyee)+"/"+str(taille)+"\n")
print("FIN")
server.close()
except Exception as ex:
print("Exception, voir log")
log.write(str(datetime.now())+" => Exception "+str(type(ex))+" ("+str(ex.args)+")\n")
time.sleep(1)
os.remove(".lockprocess")
print("Fin processus envoi")
if f is not None:
f.close()
server.close()
log.close()
time.sleep(1)
if os.path.isfile("./.lockprocess"):
os.remove(".lockprocess")
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment