mirror of
https://github.com/itsdave-de/fusionpbx_connect.git
synced 2025-05-06 23:55:21 +02:00
110 lines
3.8 KiB
Python
110 lines
3.8 KiB
Python
from datetime import datetime
|
|
import frappe
|
|
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",
|
|
data = {
|
|
'email': ts.traccar_username,
|
|
'password': frappe.utils.password.get_decrypted_password(
|
|
'TraccarServer',
|
|
ts.name,
|
|
'traccar_password'
|
|
)
|
|
}
|
|
)
|
|
if response.status_code == 200:
|
|
return response.cookies
|
|
else:
|
|
frappe.throw(f"Authentication failed: {response.status_code}: {response.reason}<br />Dict ts: {ts.as_dict()}")
|
|
|
|
|
|
@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"""
|
|
|
|
# get informations for authentication on traccar server
|
|
ts = frappe.get_last_doc('TraccarServer')
|
|
|
|
# get all devices from traccar server
|
|
try:
|
|
devices = requests.get(
|
|
f"http://{ts.traccar_server}:{ts.traccar_port}/api/devices",
|
|
cookies = traccar_auth(ts)
|
|
)
|
|
except:
|
|
frappe.throw("Could not fetch devices from traccar server")
|
|
|
|
# Insert devices into Tracker Doctype
|
|
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()
|
|
|
|
# update db frappe
|
|
frappe.db.commit()
|
|
|
|
|
|
|
|
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')
|
|
|
|
# get all trips from traccar server
|
|
try:
|
|
trips = requests.get(
|
|
f"http://{ts.traccar_server}:{ts.traccar_port}/api/reports/trips",
|
|
cookies = traccar_auth(ts),
|
|
params = {
|
|
'deviceId': device,
|
|
'from': start,
|
|
'to': end
|
|
}
|
|
)
|
|
except:
|
|
frappe.throw("Could not fetch trips from traccar server")
|
|
|
|
# Insert trips into Trip Doctype
|
|
for trip in trips.json():
|
|
if not frappe.db.exists('Trip', trip['id']):
|
|
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'),
|
|
}).insert()
|
|
|
|
# update db frappe
|
|
frappe.db.commit()
|
|
|
|
|
|
|