Refactor and enhance timelapse capture system

- Removed obsolete script `script_SANSDEMARRAGE.sh`.
- Added new `Camera.py` and `Connexion.py` files for camera handling and socket communication.
- Implemented `First_Try.py` for initial camera preview testing.
- Created `Humidity.py` for humidity sensor data acquisition.
- Developed `Send_data_stocked.py` for managing and sending stored data.
- Introduced `Time_Lapse_Connection.py` and `Time_Lapse_NoConnection.py` for connected and offline modes.
- Added `get_from_server.py` for retrieving camera status from the server.
- Updated `sync_offline_data.py` for synchronizing offline data.
- Created `timelapse.service` for managing the timelapse service on Raspberry Pi.
- Established package structure with `__init__.py` and `api_client.py` for API interactions.
- Enhanced `capture.py` for managing image captures and data storage.
- Configured `config.py` for centralized configuration management.
- Developed `sensors.py` for handling environmental sensors and camera operations.
- Implemented `timelapse_offline.py` and `timelapse_online.py` for capturing images in offline and online modes.
This commit is contained in:
2025-04-27 16:45:00 +02:00
parent bebfb8adb3
commit 610d220c3f
20 changed files with 951 additions and 152 deletions

View File

@@ -1,148 +1,72 @@
import time
from datetime import datetime
import picamera2 as pc
import smbus2
#!/usr/bin/env python3
# coding: utf-8
"""
Script d'automatisation pour la gestion du système timelapse
Ce script vérifie le statut du serveur et configure le système en conséquence.
"""
import os
import json
import shutil
import requests
import sys
import time
import logging
import subprocess
from timelapse.config import config
from timelapse.api_client import api_client
from timelapse.sensors import micro_controller
class MicroControler:
def __init__(self):
pass
def set_data(self, t):
bus = smbus2.SMBus(1)
ans = bus.write_byte(0x28, t)
#print(ans)
time.sleep(0.015)
ans=smbus2.i2c_msg.read(0x28,3)
print("I get that : ",ans)
def main():
"""
Fonction principale d'automatisation
"""
logging.info("==================== AUTOMATISATION TIMELAPSE ====================")
bus.i2c_rdwr(ans)
data = list(ans)
print(data)
try:
# Récupérer le statut de la caméra depuis l'API
camera_status = api_client.get_camera_status()
def set_data_2_octets(self, value):
value_16b = format(value, "016b")
print(value_16b)
#print(value_16b>>8)
high_address = (value>>8) & 0xFF
low_address = value & 0xFF
print(high_address)
print(low_address)
"""High Address"""
self.set_data(high_address)
"""Low Address"""
self.set_data(low_address)
class Server:
def __init__(self):
self.url_requete = "https://timelapse.kerboul.me/api/camera/status"
self.dic = { "set_config":False,
"maintenance": False,
"stop current config":False,
"timelapse":3,
"conf nb_images":1,
"nb_images restantes":1
}
def get_request(self):
try:
response = requests.get(self.url_requete)
print("Here is the answer from the server : ",response.json())
if response.status_code == 200:
camera_status = response.json()
#print(camera_status)
self.dic["maintenance"] = camera_status["maintenance"]
self.dic["timelapse"] = camera_status["interval"]
self.dic["conf nb_images"] = camera_status["nb_images"]
self.dic["set_config"] = camera_status["idle"]
self.dic["stop current config"] = camera_status["stop_flag"]
else:
print("mauvais code")
print(response.status_code)
except requests.exceptions.RequestException as e:
print("erreur API ou internet")
if camera_status is None:
logging.error("Impossible d'obtenir le statut de la caméra depuis l'API")
return
def get_dic_data(self):
return self.dic
def create_Json(self):
self.filename = "/home/timelapse/Documents/Time_Lapse/CONFIG/config.json"
with open(self.filename, "w") as file:
json.dump(self.get_dic_data(), file)
# Mettre à jour la configuration locale
api_client.update_camera_config(camera_status)
# Vérifier l'état de maintenance
if camera_status.get("maintenance", False):
logging.info("Caméra en mode maintenance, aucune action nécessaire")
return
# Vérifier si un arrêt de la procédure est demandé
if camera_status.get("stop_flag", False):
logging.info("Arrêt de la procédure en cours...")
def get_existing_Json(self):
self.filename = "/home/timelapse/Documents/Time_Lapse/CONFIG/config.json"
with open(self.filename, "r", encoding='utf-8') as file:
datas = json.load(file)
return datas
def create_this_Json(self, dic):
self.dic = { "set_config":dic["set_config"],
"maintenance": dic["maintenance"],
"stop current config":dic["stop current config"],
"timelapse":dic["timelapse"],
"conf nb_images":dic["conf nb_images"],
"nb_images restantes":dic["nb_images restantes"]
}
self.filename = "/home/timelapse/Documents/Time_Lapse/CONFIG/config.json"
with open(self.filename, "w") as file:
json.dump(dic, file)
# Supprimer le fichier de configuration s'il existe
config.delete_config_file()
# Confirmer l'arrêt
api_client.confirm_stop()
return
# Vérifier s'il y a une configuration à appliquer
if not camera_status.get("idle", True):
logging.info("Configuration active détectée")
# Obtenir les paramètres
interval = camera_status.get("interval", 3)
# Envoyer l'intervalle au microcontrôleur
logging.info(f"Envoi de l'intervalle au microcontrôleur: {interval}s")
micro_controller.send_interval(interval)
# Éteindre le système après configuration
logging.info("Configuration terminée, arrêt du système")
subprocess.run(["sudo", "shutdown", "now"])
except Exception as e:
logging.error(f"Erreur dans le script d'automatisation: {e}")
if __name__ == "__main__":
MC = MicroControler()
server = Server()
filename = "/home/timelapse/Documents/Time_Lapse/CONFIG/config.json"
server.get_request()
datas = server.get_dic_data()
#datas = {'set_config': False, 'maintenance': True, 'stop current config': True, 'timelapse': 7, 'conf nb_images': 12, 'nb_images restantes': 12}
print("Here are the datas loaded : ",datas)
if (datas["maintenance"]):
print("- Maintenance")
else:
print("- No Maintenance")
if (datas["stop current config"]):
if (os.path.exists(filename)):
print("- Stopping current config") #suppresion fichier json
os.remove(filename)
if (datas["set_config"]==False): #eddition fichier config
print("- Working on Raspberry config")
if (os.path.exists(filename)):
print("- Existing Config : -1 on Images")
datas = server.get_existing_Json()
datas["nb_images restantes"] = datas["nb_images restantes"] - 1
os.remove(filename)
if (datas["nb_images restantes"]==0):
pass
else:
server.create_this_Json(datas)
else:
datas = {'set_config': False, 'maintenance': False, 'stop current config': False, 'timelapse': 3, 'conf nb_images': 1, 'nb_images restantes': 1}
server.create_this_Json(datas)
else:
server.create_this_Json(datas)
print("- New Config")
print("- Shut Down")
print("TimeLapse sent is : ", datas["timelapse"])
MC.set_data_2_octets(datas["timelapse"])
time.sleep(1)
os.system("sudo shutdown now")
"""
#MC.set_data_2_octets(1)
#MC .set_data_2_octets(datas["timelapse"])
#le mot de passe c'est motdepasse
#nmcli dev wifi "le mot de passe c'est motdepasse" password "motdepasse"
"""
main()