From bc1f312e815424eea1c25b9fa94bbb3f80643f0d Mon Sep 17 00:00:00 2001 From: Luiz Costa Date: Tue, 21 Nov 2023 12:18:03 +0000 Subject: [PATCH] change doctype names --- fleet_management/traccar_api.py | 55 ++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 22 deletions(-) diff --git a/fleet_management/traccar_api.py b/fleet_management/traccar_api.py index b190c8c..1b6b0f8 100644 --- a/fleet_management/traccar_api.py +++ b/fleet_management/traccar_api.py @@ -10,12 +10,12 @@ def traccar_auth(ts): data = { 'email': ts.traccar_username, 'password': frappe.utils.password.get_decrypted_password( - 'TraccarServer', + 'Traccar Settings', ts.name, 'traccar_password' ) } - ) + ) if response.status_code == 200: return response.cookies else: @@ -28,7 +28,7 @@ def get_devices(doc=None): should also be able to be called frequently to update the status""" # get informations for authentication on traccar server - ts = frappe.get_last_doc('TraccarServer') + ts = frappe.get_last_doc('Traccar Settings') # get all devices from traccar server try: @@ -62,30 +62,42 @@ def get_devices(doc=None): frappe.db.commit() +@frappe.whitelist() +def get_trips_for_device(device_id, start=None, end=None): + """ + Fetches all trips for a device, stores them in Trip Doctype, and links them to the vehicle assigned to the device. + """ + # get information for authentication on traccar server + ts = frappe.get_last_doc('Traccar Settings') -def get_trips_for_device(device, start = None, end = None): - """getches all trip for a device, stores them Trip Doctype and links them to the vehicle which is assigned to the device""" - # get informations for authentication on traccar server - ts = frappe.get_last_doc('TraccarServer') + # Define default start and end datetime if not provided + if not start: + start = datetime.now().strftime('%Y-%m-%dT%H:%M:%SZ') # Adjust format as required by your Traccar API + if not end: + end = datetime.now().strftime('%Y-%m-%dT%H:%M:%SZ') # Adjust format as required by your Traccar API # get all trips from traccar server try: - trips = requests.get( + trips_response = requests.get( f"http://{ts.traccar_server}:{ts.traccar_port}/api/reports/trips", - cookies = traccar_auth(ts), - params = { - 'deviceId': device, + cookies=traccar_auth(ts), + params={ + 'deviceId': device_id, 'from': start, 'to': end } ) - except: - frappe.throw("Could not fetch trips from traccar server") + trips_response.raise_for_status() # This will raise an error for HTTP error codes + except requests.HTTPError as e: + frappe.throw(f"HTTP error occurred: {e}") + except Exception as e: + frappe.throw(f"Error fetching trips: {e}") # Insert trips into Trip Doctype - for trip in trips.json(): - if not frappe.db.exists('Trip', trip['id']): - frappe.get_doc({ + for trip in trips_response.json(): + # Assuming 'id' is unique identifier for the trip from Traccar API response + if not frappe.db.exists('Trip', {'portal_id': trip['id']}): + trip_doc = frappe.get_doc({ 'doctype': 'Trip', 'portal_id': trip['id'], 'device_id': trip['deviceId'], @@ -99,11 +111,10 @@ def get_trips_for_device(device, start = None, end = None): 'end_address': trip['endAddress'], 'duration': trip['duration'], 'driver_unique_id': trip['driverUniqueId'], - 'vehicle': frappe.get_value('Tracker', trip['deviceId'], 'vehicle'), - }).insert() - - # update db frappe + 'vehicle': frappe.get_value('Tracker', trip['deviceId'], 'vehicle') + }) + trip_doc.insert(ignore_permissions=True) # Use ignore_permissions if needed + + # commit the transaction frappe.db.commit() - -