update changes

This commit is contained in:
Luiz Costa
2024-04-12 12:09:00 +01:00
parent 9e95b66928
commit 1ef05996f5
24 changed files with 592 additions and 66 deletions

View File

@@ -1,4 +1,5 @@
from datetime import datetime
from datetime import datetime, timedelta
from frappe.utils.password import get_decrypted_password
import frappe
import requests
@@ -6,16 +7,13 @@ import requests
def traccar_auth(ts):
"""authenticates on traccar server and returns cookies"""
response = requests.post(
f"http://{ts.traccar_server}:{ts.traccar_port}/api/session",
f"{ts.traccar_server}/api/session",
data = {
'email': ts.traccar_username,
'password': frappe.utils.password.get_decrypted_password(
'Traccar Settings',
ts.name,
'traccar_password'
)
'password': get_decrypted_password('Traccar Settings', ts.name, 'traccar_password')
}
)
if response.status_code == 200:
return response.cookies
else:
@@ -24,8 +22,7 @@ def traccar_auth(ts):
@frappe.whitelist()
def get_devices(doc=None):
"""fetches all devices the user has access to and stores them in Tracker Doctype
should also be able to be called frequently to update the status"""
"""Fetches all devices the user has access to and stores or updates them in Tracker Doctype."""
# get informations for authentication on traccar server
ts = frappe.get_doc('Traccar Settings')
@@ -33,88 +30,120 @@ def get_devices(doc=None):
# get all devices from traccar server
try:
devices = requests.get(
f"http://{ts.traccar_server}:{ts.traccar_port}/api/devices",
cookies = traccar_auth(ts)
f"{ts.traccar_server}/api/devices",
cookies = traccar_auth(ts) # Assuming traccar_auth is defined elsewhere
)
except:
frappe.throw("Could not fetch devices from traccar server")
except Exception as e:
frappe.throw(f"Could not fetch devices from Traccar server: {str(e)}")
# Insert devices into Tracker Doctype
# Process each device
for dev in devices.json():
if not frappe.db.exists('Tracker', int(dev['id'])):
frappe.get_doc({
'doctype': 'Tracker',
'portal_id': dev['id'],
'portal_name': dev['name'],
'unique_id': dev['uniqueId'],
'status': dev['status'],
'last_update': datetime.fromisoformat(dev['lastUpdate']).strftime('%Y-%m-%d %H:%M:%S'),
'position_id': dev['positionId'],
'group_id': dev['groupId'],
'phone': dev['phone'],
'model': dev['model'],
'contact': dev['contact'],
'categorie': dev['category'],
'disabled': dev['disabled'],
}).insert()
tracker_id = str(dev['id'])
# Check if the Tracker exists
existing_tracker = frappe.db.exists('Tracker', {'portal_id': tracker_id})
# update db frappe
if existing_tracker:
# If exists, get the document and update
tracker_doc = frappe.get_doc('Tracker', existing_tracker)
else:
# If not, create a new Tracker document
tracker_doc = frappe.get_doc({'doctype': 'Tracker', 'portal_id': tracker_id})
# Update or set fields
tracker_doc.portal_name = dev['name']
tracker_doc.unique_id = dev['uniqueId']
tracker_doc.status = dev['status']
tracker_doc.last_update = datetime.fromisoformat(dev['lastUpdate']).strftime('%Y-%m-%d %H:%M:%S')
tracker_doc.position_id = dev['positionId']
tracker_doc.group_id = dev['groupId']
tracker_doc.phone = dev.get('phone', '') # Using .get() in case some fields might be missing
tracker_doc.model = dev.get('model', '')
tracker_doc.contact = dev.get('contact', '')
tracker_doc.categorie = dev.get('category', '')
tracker_doc.disabled = dev['disabled']
# Save changes to the database
tracker_doc.save()
# Commit the transaction
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.
Fetches all trips for a device, stores or updates them in the Trip Doctype,
and links them to the vehicle assigned to the device.
"""
# get information for authentication on traccar server
ts = frappe.get_doc('Traccar Settings')
# 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
start = (datetime.now() - timedelta(days=30)).strftime('%Y-%m-%dT%H:%M:%SZ')
if not end:
end = datetime.now().strftime('%Y-%m-%dT%H:%M:%SZ') # Adjust format as required by your Traccar API
end = datetime.now().strftime('%Y-%m-%dT%H:%M:%SZ')
# get all trips from traccar server
try:
trips_response = requests.get(
f"http://{ts.traccar_server}:{ts.traccar_port}/api/reports/trips",
f"{ts.traccar_server}/api/reports/trips",
cookies=traccar_auth(ts),
params={
'deviceId': device_id,
'from': start,
'to': end
}
params={'deviceId': device_id, 'from': start, 'to': end}
)
trips_response.raise_for_status() # This will raise an error for HTTP error codes
trips_response.raise_for_status()
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_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'],
'start': datetime.fromisoformat(trip['startTime']).strftime('%Y-%m-%d %H:%M:%S'),
'end': datetime.fromisoformat(trip['endTime']).strftime('%Y-%m-%d %H:%M:%S'),
'distance': trip['distance'],
'average_speed': trip['averageSpeed'],
'max_speed': trip['maxSpeed'],
'spent_fuel': trip['spentFuel'],
'start_address': trip['startAddress'],
'end_address': trip['endAddress'],
'duration': trip['duration'],
'driver_unique_id': trip['driverUniqueId'],
'vehicle': frappe.get_value('Tracker', trip['deviceId'], 'vehicle')
})
trip_doc.insert(ignore_permissions=True) # Use ignore_permissions if needed
# Remove all data from device (to renew)
existing_trips = frappe.get_all('Trip', filters={'device_id': device_id}, fields=['portal_id'])
for trip in existing_trips:
frappe.delete_doc('Trip', trip['portal_id'], force=1)
for trip in trips_response.json():
trip_doc = frappe.get_doc({'doctype': 'Trip'})
trip_doc.device_id = trip['deviceId']
trip_doc.start = datetime.fromisoformat(trip['startTime']).strftime('%Y-%m-%d %H:%M:%S')
trip_doc.end = datetime.fromisoformat(trip['endTime']).strftime('%Y-%m-%d %H:%M:%S')
trip_doc.distance = trip['distance']
trip_doc.average_speed = trip['averageSpeed']
trip_doc.max_speed = trip['maxSpeed']
trip_doc.spent_fuel = trip['spentFuel']
trip_doc.start_address = trip['startAddress']
trip_doc.end_address = trip['endAddress']
trip_doc.duration = trip['duration']
trip_doc.driver_unique_id = trip['driverUniqueId']
trip_doc.tracker = frappe.get_value('Tracker', {'portal_id': trip['deviceId']}, 'portal_name')
trip_doc.fleet_vehicle = frappe.get_value('Fleet Vehicle', {'tracker': trip_doc.tracker}, 'name')
trip_doc.save(ignore_permissions=True)
# commit the transaction
frappe.db.commit()
@frappe.whitelist()
def getroutes(name, start=None, end=None):
ts = frappe.get_doc('Traccar Settings')
device_id = frappe.get_value('Trip', {'name': name}, 'device_id')
startdate = datetime.strptime(start, "%Y-%m-%d %H:%M:%S").strftime("%Y-%m-%dT%H:%M:%S.000+00:00")
enddate = datetime.strptime(end, "%Y-%m-%d %H:%M:%S").strftime("%Y-%m-%dT%H:%M:%S.000+00:00")
print(f"startdate: {startdate}")
print(f"enddate: {enddate}")
# get route by filter from traccar server
try:
route_response = requests.get(
f"{ts.traccar_server}/api/reports/route",
cookies=traccar_auth(ts),
headers={'Accept': 'application/json'},
params={'deviceId': device_id, 'from': startdate, 'to': enddate}
)
route_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}")
print(route_response)
return route_response.json()