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
|
- mariadb
|
||||||
volumes:
|
volumes:
|
||||||
- ./keyring_data:/root/.local/share/python_keyring # Persistenter Speicher für den Keyring
|
- ./keyring_data:/root/.local/share/python_keyring # Persistenter Speicher für den Keyring
|
||||||
#restart: always
|
restart: always
|
||||||
networks:
|
networks:
|
||||||
default:
|
default:
|
||||||
dns:
|
dns:
|
||||||
|
|
30
script.py
30
script.py
|
@ -14,6 +14,9 @@ logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(
|
||||||
# Keyring auf Dateisystem setzen
|
# Keyring auf Dateisystem setzen
|
||||||
keyring.set_keyring(PlaintextKeyring())
|
keyring.set_keyring(PlaintextKeyring())
|
||||||
|
|
||||||
|
# Schwellenwert für Positionsänderung (in Dezimalgrad)
|
||||||
|
CHANGE_THRESHOLD = 0.0001
|
||||||
|
|
||||||
# Datenbankverbindung prüfen
|
# Datenbankverbindung prüfen
|
||||||
def connect_to_database():
|
def connect_to_database():
|
||||||
while True:
|
while True:
|
||||||
|
@ -32,6 +35,10 @@ def connect_to_database():
|
||||||
logging.info("Erneuter Verbindungsversuch in 5 Sekunden...")
|
logging.info("Erneuter Verbindungsversuch in 5 Sekunden...")
|
||||||
time.sleep(5)
|
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-Verbindung herstellen
|
||||||
icloud = PyiCloudService(
|
icloud = PyiCloudService(
|
||||||
os.getenv('ICLOUD_EMAIL'),
|
os.getenv('ICLOUD_EMAIL'),
|
||||||
|
@ -84,14 +91,23 @@ try:
|
||||||
longitude = location['longitude']
|
longitude = location['longitude']
|
||||||
timestamp = datetime.fromtimestamp(location['timeStamp'] / 1000)
|
timestamp = datetime.fromtimestamp(location['timeStamp'] / 1000)
|
||||||
|
|
||||||
# Koordinaten in der Konsole ausgeben
|
# Letzten Eintrag abrufen
|
||||||
logging.info(f"Gerät: {device_name}, Latitude: {latitude}, Longitude: {longitude}, Zeit: {timestamp}")
|
|
||||||
|
|
||||||
# Daten in die Datenbank einfügen
|
|
||||||
cursor.execute("""
|
cursor.execute("""
|
||||||
INSERT INTO device_locations (device_name, latitude, longitude, timestamp)
|
SELECT latitude, longitude FROM device_locations
|
||||||
VALUES (%s, %s, %s, %s)
|
WHERE device_name = %s
|
||||||
""", (device_name, latitude, longitude, timestamp))
|
ORDER BY timestamp DESC LIMIT 1
|
||||||
|
""", (device_name,))
|
||||||
|
last_entry = cursor.fetchone()
|
||||||
|
|
||||||
|
# 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
|
# Änderungen speichern
|
||||||
db.commit()
|
db.commit()
|
||||||
|
|
Loading…
Reference in a new issue