mirror of
https://github.com/itsdave-de/fusionpbx_connect.git
synced 2025-05-06 23:55:21 +02:00
change doctype names
This commit is contained in:
parent
c0978a63e7
commit
bc1f312e81
@ -10,7 +10,7 @@ def traccar_auth(ts):
|
|||||||
data = {
|
data = {
|
||||||
'email': ts.traccar_username,
|
'email': ts.traccar_username,
|
||||||
'password': frappe.utils.password.get_decrypted_password(
|
'password': frappe.utils.password.get_decrypted_password(
|
||||||
'TraccarServer',
|
'Traccar Settings',
|
||||||
ts.name,
|
ts.name,
|
||||||
'traccar_password'
|
'traccar_password'
|
||||||
)
|
)
|
||||||
@ -28,7 +28,7 @@ def get_devices(doc=None):
|
|||||||
should also be able to be called frequently to update the status"""
|
should also be able to be called frequently to update the status"""
|
||||||
|
|
||||||
# get informations for authentication on traccar server
|
# 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
|
# get all devices from traccar server
|
||||||
try:
|
try:
|
||||||
@ -62,30 +62,42 @@ def get_devices(doc=None):
|
|||||||
frappe.db.commit()
|
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):
|
# Define default start and end datetime if not provided
|
||||||
"""getches all trip for a device, stores them Trip Doctype and links them to the vehicle which is assigned to the device"""
|
if not start:
|
||||||
# get informations for authentication on traccar server
|
start = datetime.now().strftime('%Y-%m-%dT%H:%M:%SZ') # Adjust format as required by your Traccar API
|
||||||
ts = frappe.get_last_doc('TraccarServer')
|
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
|
# get all trips from traccar server
|
||||||
try:
|
try:
|
||||||
trips = requests.get(
|
trips_response = requests.get(
|
||||||
f"http://{ts.traccar_server}:{ts.traccar_port}/api/reports/trips",
|
f"http://{ts.traccar_server}:{ts.traccar_port}/api/reports/trips",
|
||||||
cookies = traccar_auth(ts),
|
cookies=traccar_auth(ts),
|
||||||
params = {
|
params={
|
||||||
'deviceId': device,
|
'deviceId': device_id,
|
||||||
'from': start,
|
'from': start,
|
||||||
'to': end
|
'to': end
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
except:
|
trips_response.raise_for_status() # This will raise an error for HTTP error codes
|
||||||
frappe.throw("Could not fetch trips from traccar server")
|
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
|
# Insert trips into Trip Doctype
|
||||||
for trip in trips.json():
|
for trip in trips_response.json():
|
||||||
if not frappe.db.exists('Trip', trip['id']):
|
# Assuming 'id' is unique identifier for the trip from Traccar API response
|
||||||
frappe.get_doc({
|
if not frappe.db.exists('Trip', {'portal_id': trip['id']}):
|
||||||
|
trip_doc = frappe.get_doc({
|
||||||
'doctype': 'Trip',
|
'doctype': 'Trip',
|
||||||
'portal_id': trip['id'],
|
'portal_id': trip['id'],
|
||||||
'device_id': trip['deviceId'],
|
'device_id': trip['deviceId'],
|
||||||
@ -99,11 +111,10 @@ def get_trips_for_device(device, start = None, end = None):
|
|||||||
'end_address': trip['endAddress'],
|
'end_address': trip['endAddress'],
|
||||||
'duration': trip['duration'],
|
'duration': trip['duration'],
|
||||||
'driver_unique_id': trip['driverUniqueId'],
|
'driver_unique_id': trip['driverUniqueId'],
|
||||||
'vehicle': frappe.get_value('Tracker', trip['deviceId'], 'vehicle'),
|
'vehicle': frappe.get_value('Tracker', trip['deviceId'], 'vehicle')
|
||||||
}).insert()
|
})
|
||||||
|
trip_doc.insert(ignore_permissions=True) # Use ignore_permissions if needed
|
||||||
|
|
||||||
# update db frappe
|
# commit the transaction
|
||||||
frappe.db.commit()
|
frappe.db.commit()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user