-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
94 lines (77 loc) · 3.32 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
from dh_manager import dh_manager
import time
import csv
from datetime import datetime, timedelta
from plot_manager import PlotManager
from drive_manager import Drive
import logging
# Configuration de base du logging
logging.basicConfig(
filename='app.log', # Nom du fichier de log
filemode='w',
level=logging.INFO, # Niveau minimal de log (DEBUG, INFO, WARNING, ERROR, CRITICAL)
format='%(asctime)s - %(levelname)s - %(message)s', # Format des messages
datefmt='%Y-%m-%d %H:%M:%S', # Format de la date
)
def main():
plot = PlotManager()
plot.create_csv()
dh = dh_manager()
end_time = datetime.now() + timedelta(hours=24) # 3h test
last_upload_time = datetime.now()
logging.info(f"Program will run until {end_time}")
try:
while True: #datetime.now() < end_time:
try:
manage(dh)
except Exception as err:
logging.error(f"Error in manage function: {err}")
time.sleep(2) # hardware limitations: 2s after an error
time.sleep(5) # 5s between 2 readings to let the climate change
# Vérification si 24 heures se sont écoulées depuis le dernier upload
if datetime.now() - last_upload_time >= timedelta(hours=24):
logging.info("24h reached, starting file upload.")
upload_files()
last_upload_time = datetime.now() # Mise à jour du dernier upload
except KeyboardInterrupt:
logging.warning("Program interrupted by user.")
except Exception as err:
logging.error(f'An error occured : {err}')
finally:
time.sleep(2) # hardware limitations: 2s after an error
dh.cleanup() # Always clean up GPIO
upload_files()
def manage(dh):
csv_file = 'temperature_humidity_data.csv'
try:
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
temperature, humidity, fan_on, humidifier_on = dh.manage_climate()
while temperature is None or humidity is None:
time.sleep(2) # hardware limitation 1s before reading again
temperature, humidity, fan_on, humidifier_on = dh.manage_climate()
with open(csv_file, mode='a', newline='') as file:
writer = csv.writer(file)
writer.writerow([timestamp, temperature, humidity, fan_on, humidifier_on])
except Exception as err:
raise err
def upload_files():
try:
plot = PlotManager()
logging.info('Uploading files...')
# Files upload
drive = Drive()
service = drive.authenticate()
drive.upload(service, 'app.log', 'text/plain', f'app_{timestamp}.log')
logging.info('File App.log uploaded')
timestamp = datetime.now().strftime('%Y-%m-%d')
drive.upload(service, 'temperature_humidity_data.csv', 'text/csv', f'temperature_humidity_data_{timestamp}.csv')
logging.info('Data file uploaded')
plot_file = plot.generate_plot()
drive.upload(service, plot_file, 'image/png')
logging.info('Plot file uploaded')
logging.info("Finished running and cleaned up resources.")
except Exception as e:
logging.error(f'An error occured while uploading files : {e}')
if __name__ == "__main__":
logging.info('Process starting.')
main()