Add position change threshold to reduce redundant database entries
This commit is contained in:
parent
6a9eedb7ba
commit
9235e647ff
2 changed files with 24 additions and 8 deletions
|
@ -32,7 +32,7 @@ services:
|
|||
- mariadb
|
||||
volumes:
|
||||
- ./keyring_data:/root/.local/share/python_keyring # Persistenter Speicher für den Keyring
|
||||
#restart: always
|
||||
restart: always
|
||||
networks:
|
||||
default:
|
||||
dns:
|
||||
|
|
22
script.py
22
script.py
|
@ -14,6 +14,9 @@ logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(
|
|||
# Keyring auf Dateisystem setzen
|
||||
keyring.set_keyring(PlaintextKeyring())
|
||||
|
||||
# Schwellenwert für Positionsänderung (in Dezimalgrad)
|
||||
CHANGE_THRESHOLD = 0.0001
|
||||
|
||||
# Datenbankverbindung prüfen
|
||||
def connect_to_database():
|
||||
while True:
|
||||
|
@ -32,6 +35,10 @@ def connect_to_database():
|
|||
logging.info("Erneuter Verbindungsversuch in 5 Sekunden...")
|
||||
time.sleep(5)
|
||||
|
||||
# Funktion zur Überprüfung der Positionsänderung
|
||||
def has_significant_change(last_lat, last_lon, new_lat, new_lon):
|
||||
return abs(last_lat - new_lat) > CHANGE_THRESHOLD or abs(last_lon - new_lon) > CHANGE_THRESHOLD
|
||||
|
||||
# iCloud-Verbindung herstellen
|
||||
icloud = PyiCloudService(
|
||||
os.getenv('ICLOUD_EMAIL'),
|
||||
|
@ -84,14 +91,23 @@ try:
|
|||
longitude = location['longitude']
|
||||
timestamp = datetime.fromtimestamp(location['timeStamp'] / 1000)
|
||||
|
||||
# Koordinaten in der Konsole ausgeben
|
||||
logging.info(f"Gerät: {device_name}, Latitude: {latitude}, Longitude: {longitude}, Zeit: {timestamp}")
|
||||
# Letzten Eintrag abrufen
|
||||
cursor.execute("""
|
||||
SELECT latitude, longitude FROM device_locations
|
||||
WHERE device_name = %s
|
||||
ORDER BY timestamp DESC LIMIT 1
|
||||
""", (device_name,))
|
||||
last_entry = cursor.fetchone()
|
||||
|
||||
# Daten in die Datenbank einfügen
|
||||
# Nur einfügen, wenn sich die Position signifikant geändert hat
|
||||
if last_entry is None or has_significant_change(last_entry[0], last_entry[1], latitude, longitude):
|
||||
logging.info(f"Gerät: {device_name}, Latitude: {latitude}, Longitude: {longitude}, Zeit: {timestamp}")
|
||||
cursor.execute("""
|
||||
INSERT INTO device_locations (device_name, latitude, longitude, timestamp)
|
||||
VALUES (%s, %s, %s, %s)
|
||||
""", (device_name, latitude, longitude, timestamp))
|
||||
else:
|
||||
logging.info(f"Keine signifikante Positionsänderung für {device_name}. Eintrag übersprungen.")
|
||||
|
||||
# Änderungen speichern
|
||||
db.commit()
|
||||
|
|
Loading…
Reference in a new issue