init
This commit is contained in:
72
Send_data_stocked.py
Normal file
72
Send_data_stocked.py
Normal file
@@ -0,0 +1,72 @@
|
||||
import os
|
||||
import requests
|
||||
import shutil
|
||||
import json
|
||||
|
||||
class Send_data_stocked:
|
||||
def __init__(self):
|
||||
self.url = "https://timelapse.kerboul.me/api/camera/upload"
|
||||
self.headers = {
|
||||
'accept': 'application/json'
|
||||
}
|
||||
self.project_path = "//home//timelapse//Documents//Time_Lapse//PROJECT//"
|
||||
|
||||
def get_list_folder(self,path = "//home//timelapse//Documents//Time_Lapse//PROJECT"):
|
||||
# Liste des noms de dossiers
|
||||
noms_dossiers = [nom for nom in os.listdir(path) if os.path.isdir(os.path.join(path, nom))]
|
||||
print("Noms des dossiers :", noms_dossiers)
|
||||
return noms_dossiers
|
||||
|
||||
def send_ALL_data_stocked(self):
|
||||
tab_files = self.get_list_folder()
|
||||
for i in tab_files:
|
||||
self.send_data_stocked(i)
|
||||
|
||||
def send_data_stocked(self,file_name):
|
||||
file_path = self.project_path + file_name + "//" + file_name
|
||||
json_file_path = file_path + ".json"
|
||||
img_file_path = file_path + ".jpg"
|
||||
|
||||
datas = self.get_json(json_file_path)
|
||||
print(datas)
|
||||
answer_from_server = self.upload_measurement(img_file_path, datas["timestamp"], datas["temperature"], datas["humidity"])
|
||||
print(answer_from_server)
|
||||
if (answer_from_server["message"]== "Mesure téléchargée avec succès"):
|
||||
self.del_Folder(datas["timestamp"])
|
||||
|
||||
def get_json(self, file_path):
|
||||
with open(file_path, "r", encoding='utf-8') as file:
|
||||
datas = json.load(file)
|
||||
return datas
|
||||
return False
|
||||
|
||||
def del_Folder(self, name):
|
||||
shutil.rmtree(self.project_path + name)
|
||||
|
||||
def upload_measurement(self, image_path, timestamp, temperature, humidity):
|
||||
|
||||
data = {
|
||||
'timestamp': timestamp,
|
||||
'temperature': temperature,
|
||||
'humidity': humidity
|
||||
}
|
||||
files = {
|
||||
'image': (image_path, open(image_path, 'rb'), 'image/jpeg')
|
||||
}
|
||||
try:
|
||||
#print(data)
|
||||
#print(files)
|
||||
response = requests.post(self.url, headers=self.headers, data=data, files=files)
|
||||
print(response)
|
||||
response.raise_for_status()
|
||||
response_data = response.json()
|
||||
return response_data
|
||||
except requests.exceptions.HTTPError as http_err:
|
||||
print(f"HTTP error occurred: {http_err}")
|
||||
except Exception as err:
|
||||
print(f"Other error occurred: {err}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
SDS = Send_data_stocked()
|
||||
#a = SDS.get_list_folder()[0]
|
||||
SDS.send_ALL_data_stocked()
|
||||
Reference in New Issue
Block a user