mirror of
https://github.com/itsdave-de/fusionpbx_connect.git
synced 2025-05-06 15:45:15 +02:00
initial api code
This commit is contained in:
parent
9ba7d5df88
commit
1f3dec4afc
68
fleet_management/traccar_api.py
Normal file
68
fleet_management/traccar_api.py
Normal file
@ -0,0 +1,68 @@
|
||||
from datetime import datetime
|
||||
import frappe
|
||||
import requests
|
||||
from requests.auth import HTTPBasicAuth
|
||||
|
||||
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"""
|
||||
pass
|
||||
|
Loading…
x
Reference in New Issue
Block a user