Implement OwnTracks Webhook and remove manual tracking
This commit is contained in:
@@ -27,7 +27,6 @@
|
||||
'views/fleet_handover_views.xml',
|
||||
'views/fuel_index_views.xml',
|
||||
'views/home_views.xml',
|
||||
'views/driver_tracker_template.xml',
|
||||
],
|
||||
'installable': True,
|
||||
'application': True,
|
||||
|
||||
@@ -390,16 +390,52 @@ class MymachFleetController(http.Controller):
|
||||
'vehicles_json': map_data
|
||||
})
|
||||
|
||||
@http.route('/mymach/driver/tracker', type='http', auth='user')
|
||||
def driver_tracker(self, **kwargs):
|
||||
@http.route('/mymach/api/tracker', type='http', auth='public', methods=['GET', 'POST'], csrf=False)
|
||||
def api_tracker_webhook(self, **kwargs):
|
||||
"""
|
||||
Sürücülerin mobil cihazlarından konum paylaşabilmeleri için web arayüzü.
|
||||
Otonom GPS Takip Webhook'u (OwnTracks / Traccar / OsmAnd Formatları)
|
||||
Bu endpoint arka plan takip uygulamasından gelen verileri (GET veya POST) karşılar.
|
||||
"""
|
||||
vehicles = request.env['fleet.vehicle'].sudo().search([])
|
||||
return request.render('mymach_fleet_intelligence.driver_tracker_template', {
|
||||
'vehicles': vehicles,
|
||||
try:
|
||||
data = {}
|
||||
if request.httprequest.method == 'POST':
|
||||
try:
|
||||
data = json.loads(request.httprequest.data.decode('utf-8'))
|
||||
except:
|
||||
data = request.httprequest.form.to_dict()
|
||||
else:
|
||||
data = request.httprequest.args.to_dict()
|
||||
|
||||
# Traccar / OsmAnd formatları query params (id, lat, lon) olarak gönderir.
|
||||
# OwnTracks JSON içinde (tid, lat, lon) olarak gönderir.
|
||||
|
||||
device_id = data.get('id') or data.get('device') or data.get('tid') or kwargs.get('id')
|
||||
lat = data.get('lat') or kwargs.get('lat')
|
||||
lon = data.get('lon') or data.get('lng') or kwargs.get('lon')
|
||||
speed = data.get('speed') or data.get('vel') or kwargs.get('speed') or 0.0
|
||||
|
||||
if not device_id or not lat or not lon:
|
||||
return request.make_response(json.dumps({'error': 'Missing device_id or coordinates'}), headers=[('Content-Type', 'application/json')], status=400)
|
||||
|
||||
# Cihaz ID'si ile eşleşen aracı bul (Sıfır Güven - Sadece kayıtlı cihazlar)
|
||||
vehicle = request.env['fleet.vehicle'].sudo().search([('tracker_device_id', '=', device_id)], limit=1)
|
||||
|
||||
if not vehicle:
|
||||
return request.make_response(json.dumps({'error': 'Device not registered to any vehicle'}), headers=[('Content-Type', 'application/json')], status=404)
|
||||
|
||||
from odoo import fields
|
||||
# Araç Konumunu Güncelle
|
||||
vehicle.sudo().write({
|
||||
'latitude': float(lat),
|
||||
'longitude': float(lon),
|
||||
'gps_speed': float(speed),
|
||||
'last_gps_update': fields.Datetime.now()
|
||||
})
|
||||
|
||||
return request.make_response(json.dumps({'status': 'success', 'vehicle': vehicle.license_plate}), headers=[('Content-Type', 'application/json')])
|
||||
except Exception as e:
|
||||
return request.make_response(json.dumps({'error': str(e)}), headers=[('Content-Type', 'application/json')], status=500)
|
||||
|
||||
@http.route('/mymach/fix_db', type='http', auth='none')
|
||||
def fix_db(self, db='mymach-odoodev2-2026', **kwargs):
|
||||
import odoo
|
||||
|
||||
@@ -51,6 +51,9 @@ class FleetVehicle(models.Model):
|
||||
index_currency_id = fields.Many2one('res.currency', string="Amortisman Endeks Dövizi",
|
||||
default=lambda self: self.env.ref('base.TRY').id if self.env.ref('base.TRY', raise_if_not_found=False) else False)
|
||||
|
||||
# === Otonom GPS / Takip Cihazı (Faz 9) ===
|
||||
tracker_device_id = fields.Char(string="Takip Uygulaması ID", help="Traccar veya OwnTracks uygulamasındaki benzersiz kimlik (Device ID).")
|
||||
|
||||
# 3 Para Birimli Amortisman Maliyet Alanları ve Ortalama Değer
|
||||
depreciation_cost_try = fields.Float(string="Amortisman Maliyeti (Ana Döviz)", default=0.0)
|
||||
depreciation_cost_usd = fields.Float(string="Amortisman Maliyeti (Döviz 1)", default=0.0)
|
||||
|
||||
@@ -216,14 +216,7 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- DEV SEFERE BAŞLA BUTONU -->
|
||||
<a href="/mymach/driver/tracker" target="_blank" class="start-trip-btn">
|
||||
<i class="fa-solid fa-location-crosshairs fa-spin-pulse"></i>
|
||||
<div>
|
||||
<strong>SEFERE BAŞLA / KONUM PAYLAŞ</strong>
|
||||
<small>Sürücü Mobil Takip Ekranını Aç (Yeni Sekme)</small>
|
||||
</div>
|
||||
</a>
|
||||
|
||||
|
||||
<!-- METRİKLER (HAREKETLİ, PARK, ÇEVRİMDIŞI) -->
|
||||
<div class="metrics-row" style="display: flex; gap: 10px;">
|
||||
|
||||
@@ -1,299 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<odoo>
|
||||
<template id="driver_tracker_template" name="MyMach Sürücü Mobil Takip">
|
||||
<t t-call="web.html_container">
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8"/>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"/>
|
||||
<title>Şoför Konum Takibi</title>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet"/>
|
||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css"/>
|
||||
<style>
|
||||
body {
|
||||
background-color: #0f172a;
|
||||
color: #f8fafc;
|
||||
font-family: 'Inter', -apple-system, sans-serif;
|
||||
height: 100vh;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
margin: 0;
|
||||
overflow: hidden;
|
||||
}
|
||||
.header {
|
||||
background-color: #1e293b;
|
||||
padding: 20px;
|
||||
text-align: center;
|
||||
border-bottom: 1px solid #334155;
|
||||
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
.header h2 {
|
||||
margin: 0;
|
||||
font-size: 20px;
|
||||
font-weight: 600;
|
||||
color: #e2e8f0;
|
||||
}
|
||||
.main-container {
|
||||
flex: 1;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
padding: 20px;
|
||||
}
|
||||
.vehicle-selector {
|
||||
width: 100%;
|
||||
max-width: 350px;
|
||||
margin-bottom: 30px;
|
||||
}
|
||||
.form-select {
|
||||
background-color: #1e293b;
|
||||
color: #f8fafc;
|
||||
border: 1px solid #475569;
|
||||
border-radius: 12px;
|
||||
padding: 15px;
|
||||
font-size: 16px;
|
||||
box-shadow: inset 0 2px 4px 0 rgba(0, 0, 0, 0.06);
|
||||
}
|
||||
.form-select:focus {
|
||||
background-color: #1e293b;
|
||||
color: white;
|
||||
border-color: #3b82f6;
|
||||
box-shadow: 0 0 0 0.25rem rgba(59, 130, 246, 0.25);
|
||||
}
|
||||
.btn-track {
|
||||
width: 220px;
|
||||
height: 220px;
|
||||
border-radius: 50%;
|
||||
font-size: 24px;
|
||||
font-weight: 700;
|
||||
border: none;
|
||||
color: white;
|
||||
background: linear-gradient(135deg, #3b82f6 0%, #2563eb 100%);
|
||||
box-shadow: 0 10px 25px -5px rgba(59, 130, 246, 0.5), inset 0 4px 6px -1px rgba(255, 255, 255, 0.2);
|
||||
transition: all 0.3s ease;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
gap: 15px;
|
||||
}
|
||||
.btn-track:active {
|
||||
transform: scale(0.95);
|
||||
}
|
||||
.btn-track.active {
|
||||
background: linear-gradient(135deg, #ef4444 0%, #dc2626 100%);
|
||||
box-shadow: 0 10px 25px -5px rgba(239, 68, 68, 0.5), inset 0 4px 6px -1px rgba(255, 255, 255, 0.2);
|
||||
animation: pulse-red 2s infinite;
|
||||
}
|
||||
@keyframes pulse-red {
|
||||
0% { box-shadow: 0 0 0 0 rgba(239, 68, 68, 0.7); }
|
||||
70% { box-shadow: 0 0 0 20px rgba(239, 68, 68, 0); }
|
||||
100% { box-shadow: 0 0 0 0 rgba(239, 68, 68, 0); }
|
||||
}
|
||||
.btn-track i {
|
||||
font-size: 50px;
|
||||
}
|
||||
.status-panel {
|
||||
margin-top: 40px;
|
||||
text-align: center;
|
||||
width: 100%;
|
||||
max-width: 350px;
|
||||
background-color: #1e293b;
|
||||
padding: 15px;
|
||||
border-radius: 12px;
|
||||
border: 1px solid #334155;
|
||||
}
|
||||
.status-text {
|
||||
font-size: 16px;
|
||||
font-weight: 500;
|
||||
color: #94a3b8;
|
||||
margin-bottom: 5px;
|
||||
}
|
||||
.coords-text {
|
||||
font-size: 14px;
|
||||
color: #cbd5e1;
|
||||
font-family: monospace;
|
||||
}
|
||||
.last-ping {
|
||||
font-size: 12px;
|
||||
color: #10b981;
|
||||
margin-top: 10px;
|
||||
display: none;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="header">
|
||||
<h2><i class="fa-solid fa-truck-fast"></i> Sürücü Konum Paneli</h2>
|
||||
</div>
|
||||
|
||||
<div class="main-container">
|
||||
<div class="vehicle-selector">
|
||||
<label class="form-label" style="color: #94a3b8; font-size: 14px; font-weight: 500; margin-bottom: 8px;">Kullanılan Araç:</label>
|
||||
<select id="vehicle_select" class="form-select">
|
||||
<t t-foreach="vehicles" t-as="v">
|
||||
<option t-att-value="v.id"><t t-esc="v.license_plate"/> <t t-if="v.name">- <t t-esc="v.name"/></t></option>
|
||||
</t>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<button id="toggleTrackBtn" class="btn-track">
|
||||
<i class="fa-solid fa-location-crosshairs" id="trackIcon"></i>
|
||||
<span id="trackLabel">SEFERE BAŞLA</span>
|
||||
</button>
|
||||
|
||||
<div class="status-panel">
|
||||
<div class="status-text" id="statusText">Konum takibi kapalı.</div>
|
||||
<div class="coords-text" id="coordsText">-- / --</div>
|
||||
<div class="last-ping" id="lastPingText"><i class="fa-solid fa-check-circle"></i> Son Gönderim: <span></span></div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script type="text/javascript">
|
||||
document.addEventListener("DOMContentLoaded", function() {
|
||||
const btn = document.getElementById('toggleTrackBtn');
|
||||
const trackLabel = document.getElementById('trackLabel');
|
||||
const trackIcon = document.getElementById('trackIcon');
|
||||
const statusText = document.getElementById('statusText');
|
||||
const coordsText = document.getElementById('coordsText');
|
||||
const lastPingText = document.getElementById('lastPingText');
|
||||
const lastPingSpan = lastPingText.querySelector('span');
|
||||
const vehicleSelect = document.getElementById('vehicle_select');
|
||||
|
||||
let isTracking = false;
|
||||
let watchId = null;
|
||||
const API_TOKEN = 'MYMACH_SECURE_TOKEN_2026';
|
||||
|
||||
function formatTime(date) {
|
||||
return date.toLocaleTimeString('tr-TR', { hour: '2-digit', minute: '2-digit', second: '2-digit' });
|
||||
}
|
||||
|
||||
function sendPing(lat, lng) {
|
||||
const vehicleId = parseInt(vehicleSelect.value);
|
||||
if (!vehicleId) return;
|
||||
|
||||
const payload = {
|
||||
jsonrpc: "2.0",
|
||||
method: "call",
|
||||
params: {
|
||||
vehicle_id: vehicleId,
|
||||
latitude: lat,
|
||||
longitude: lng,
|
||||
api_token: API_TOKEN
|
||||
}
|
||||
};
|
||||
|
||||
fetch('/mymach/fleet/ping', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(payload)
|
||||
}).then(res => res.json())
|
||||
.then(data => {
|
||||
console.log('Ping Success:', data);
|
||||
lastPingText.style.display = 'block';
|
||||
lastPingSpan.textContent = formatTime(new Date());
|
||||
}).catch(err => {
|
||||
console.error('Ping Error:', err);
|
||||
});
|
||||
}
|
||||
|
||||
function successPosition(position) {
|
||||
const lat = position.coords.latitude;
|
||||
const lng = position.coords.longitude;
|
||||
coordsText.textContent = lat.toFixed(6) + " / " + lng.toFixed(6);
|
||||
sendPing(lat, lng);
|
||||
}
|
||||
|
||||
function errorPosition(err) {
|
||||
console.warn(`ERROR(${err.code}): ${err.message}`);
|
||||
if (err.code === 1) { // PERMISSION_DENIED
|
||||
statusText.textContent = "HATA: Konum izni reddedildi! Lütfen tarayıcı ayarlarından konum izni verin.";
|
||||
statusText.style.color = "#ef4444"; // Kırmızı
|
||||
stopTracking(); // İzin yoksa devam edemeyiz
|
||||
} else if (err.code === 2) {
|
||||
statusText.textContent = "GPS sinyali yok. Açık alana çıkın...";
|
||||
statusText.style.color = "#fbbf24";
|
||||
} else if (err.code === 3) {
|
||||
statusText.textContent = "Konum bulma zaman aşımına uğradı, tekrar deneniyor...";
|
||||
statusText.style.color = "#fbbf24";
|
||||
} else {
|
||||
statusText.textContent = "Konum aranıyor... Lütfen bekleyin.";
|
||||
statusText.style.color = "#fbbf24";
|
||||
}
|
||||
}
|
||||
|
||||
function startTracking() {
|
||||
if (!navigator.geolocation) {
|
||||
alert("Tarayıcınız konum servisini desteklemiyor.");
|
||||
return;
|
||||
}
|
||||
|
||||
isTracking = true;
|
||||
btn.classList.add('active');
|
||||
trackLabel.textContent = "SEFERİ BİTİR";
|
||||
trackIcon.className = "fa-solid fa-stop";
|
||||
statusText.textContent = "GPS Sinyali Bekleniyor... (Dışarıda Test Edin)";
|
||||
statusText.style.color = "#10b981";
|
||||
vehicleSelect.disabled = true;
|
||||
|
||||
// watchPosition keeps getting location updates
|
||||
watchId = navigator.geolocation.watchPosition(successPosition, errorPosition, {
|
||||
enableHighAccuracy: true,
|
||||
timeout: 10000,
|
||||
maximumAge: 0
|
||||
});
|
||||
}
|
||||
|
||||
function stopTracking() {
|
||||
isTracking = false;
|
||||
btn.classList.remove('active');
|
||||
trackLabel.textContent = "SEFERE BAŞLA";
|
||||
trackIcon.className = "fa-solid fa-location-crosshairs";
|
||||
statusText.textContent = "Konum takibi durduruldu.";
|
||||
statusText.style.color = "#94a3b8";
|
||||
vehicleSelect.disabled = false;
|
||||
lastPingText.style.display = 'none';
|
||||
coordsText.textContent = "-- / --";
|
||||
|
||||
if (watchId !== null) {
|
||||
navigator.geolocation.clearWatch(watchId);
|
||||
watchId = null;
|
||||
}
|
||||
}
|
||||
|
||||
btn.addEventListener('click', function(e) {
|
||||
e.preventDefault();
|
||||
// Disable button temporarily to prevent double clicking
|
||||
btn.style.pointerEvents = 'none';
|
||||
setTimeout(() => { btn.style.pointerEvents = 'auto'; }, 1000);
|
||||
|
||||
if (isTracking) {
|
||||
stopTracking();
|
||||
} else {
|
||||
startTracking();
|
||||
}
|
||||
});
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
</t>
|
||||
</template>
|
||||
|
||||
<!-- Odoo Menüsü İçin URL Aksiyonu ve Menü Öğesi -->
|
||||
<record id="action_mymach_driver_tracker_url" model="ir.actions.act_url">
|
||||
<field name="name">Sürücü Mobil Takip Ekranı</field>
|
||||
<field name="url">/mymach/driver/tracker</field>
|
||||
<field name="target">new</field>
|
||||
</record>
|
||||
|
||||
<menuitem id="menu_mymach_driver_tracker"
|
||||
name="Sürücü Mobil Takip"
|
||||
parent="hr.menu_hr_root"
|
||||
action="action_mymach_driver_tracker_url"
|
||||
sequence="15"/>
|
||||
</odoo>
|
||||
|
||||
@@ -30,6 +30,7 @@
|
||||
<xpath expr="//field[@name='location']" position="replace">
|
||||
<field name="mymach_vehicle_type" />
|
||||
<field name="mymach_location_id" />
|
||||
<field name="tracker_device_id" />
|
||||
</xpath>
|
||||
<xpath expr="//notebook" position="inside">
|
||||
<page string="Finans & Gider Analizi (KM / Aylık / Yıllık)">
|
||||
|
||||
@@ -130,11 +130,7 @@ action = {
|
||||
<i class="fa fa-bell-o mb-2"></i><br/> Bekleyen Rota Uyarılarım
|
||||
</button>
|
||||
</div>
|
||||
<div class="col-md mb-2">
|
||||
<a href="/mymach/driver/tracker" target="_blank" class="btn mymach_zara_btn" style="width: 100%; border-color: #10b981; color: #10b981; background: rgba(16, 185, 129, 0.1); display: block; padding-top: 15px;">
|
||||
<i class="fa fa-location-arrow fa-spin-pulse mb-2"></i><br/> 📍 Sefere Başla
|
||||
</a>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user