set while loop every 10 minutes

This commit is contained in:
Simon Rieger 2024-06-29 16:44:55 +02:00
parent 3a10ed38cc
commit 6a9eedb7ba

View file

@ -71,27 +71,38 @@ cursor.execute("""
) )
""") """)
# Standortdaten abrufen # Endlosschleife für die Abfrage alle 10 Minuten
devices = icloud.devices try:
while True:
# Standortdaten von Geräten abrufen
devices = icloud.devices
for device in devices:
location = device.location()
if location:
device_name = device['name']
latitude = location['latitude']
longitude = location['longitude']
timestamp = datetime.fromtimestamp(location['timeStamp'] / 1000)
for device in devices: # Koordinaten in der Konsole ausgeben
location = device.location() logging.info(f"Gerät: {device_name}, Latitude: {latitude}, Longitude: {longitude}, Zeit: {timestamp}")
if location:
device_name = device['name']
latitude = location['latitude']
longitude = location['longitude']
timestamp = datetime.fromtimestamp(location['timeStamp'] / 1000)
# Koordinaten in der Konsole ausgeben # Daten in die Datenbank einfügen
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))
# Daten in die Datenbank einfügen # Änderungen speichern
cursor.execute(""" db.commit()
INSERT INTO device_locations (device_name, latitude, longitude, timestamp)
VALUES (%s, %s, %s, %s)
""", (device_name, latitude, longitude, timestamp))
# Änderungen speichern und Verbindung schließen # 10 Minuten warten
db.commit() logging.info("Warten auf die nächste Abfrage in 10 Minuten...")
cursor.close() time.sleep(600)
db.close()
except KeyboardInterrupt:
logging.info("Skript manuell beendet.")
finally:
cursor.close()
db.close()