From c89a35827821664aab26573bf2183549f541887d Mon Sep 17 00:00:00 2001 From: cerenX9 Date: Thu, 23 Jul 2026 21:11:21 +0300 Subject: [PATCH] Rewrite dashboard to use live tracking data --- .../controllers/main.py | 267 ++---------------- 1 file changed, 29 insertions(+), 238 deletions(-) diff --git a/addons/mymach_fleet_intelligence/controllers/main.py b/addons/mymach_fleet_intelligence/controllers/main.py index 6c90ad8..51c3eb6 100644 --- a/addons/mymach_fleet_intelligence/controllers/main.py +++ b/addons/mymach_fleet_intelligence/controllers/main.py @@ -86,260 +86,51 @@ class MymachFleetController(http.Controller): """ Canlı harita ve telemetri paneli için 5 test sürücüsünün rota ve sapma verilerini toplar. """ - # 5 Farklı Sürücü İçin Gerçek Cadde/Sokak/Otoban Kıvrımlı Rota Verileri (Sürücü A - E) - routes_db = { - '34-AHM-101': { - 'driver_tag': 'Sürücü A', - 'start_location': 'Genel Merkez Ofis (Levent Kampüs)', - 'destination': 'İstanbul Havalimanı (Proje Bazlı Görev)', - 'planned_km': 15.2, - 'driven_km': 15.4, - 'planned_time': '35 dk (Planlanan Rota)', - 'deviation_label': '0.2 km Uyumlu', - 'green_route': [ - [41.0766, 29.0124], [41.0815, 29.0065], [41.0850, 28.9980], [41.0925, 28.9850], - [41.1000, 28.9720], [41.1050, 28.9650], [41.1180, 28.9500], [41.1350, 28.9300], - [41.1500, 28.9100], [41.1680, 28.8950], [41.1800, 28.8700], [41.1950, 28.8550], - [41.2100, 28.8350], [41.2300, 28.8100], [41.2500, 28.7850], [41.2650, 28.7650], [41.2752, 28.7519] - ], - 'red_route': [], - 'alt1': [ - [41.0766, 29.0124], [41.0650, 29.0010], [41.0500, 28.9900], [41.0620, 28.9450], - [41.0800, 28.9200], [41.1050, 28.9050], [41.1200, 28.8800], [41.1600, 28.8500], - [41.2100, 28.8200], [41.2500, 28.7800], [41.2752, 28.7519] - ], - 'alt2': [ - [41.0766, 29.0124], [41.0900, 29.0300], [41.1100, 29.0500], [41.1350, 29.0400], - [41.1600, 29.0200], [41.1850, 28.9850], [41.2000, 28.9500], [41.2300, 28.9000], - [41.2500, 28.8500], [41.2752, 28.7519] - ] - }, - '34-AYS-202': { - 'driver_tag': 'Sürücü B', - 'start_location': 'Genel Merkez Ofis (Levent Kampüs)', - 'destination': 'Kadıköy Şantiye Sahası (Şantiye Rotası)', - 'planned_km': 12.0, - 'driven_km': 12.3, - 'planned_time': '25 dk (Planlanan Rota)', - 'deviation_label': '0.3 km Uyumlu', - 'green_route': [ - [41.0766, 29.0124], [41.0680, 29.0145], [41.0600, 29.0120], [41.0520, 29.0080], - [41.0450, 29.0060], [41.0440, 29.0200], [41.0455, 29.0330], [41.0450, 29.0400], - [41.0380, 29.0450], [41.0250, 29.0380], [41.0150, 29.0320], [41.0050, 29.0280], - [40.9950, 29.0260], [40.9900, 29.0250] - ], - 'red_route': [], - 'alt1': [ - [41.0766, 29.0124], [41.0720, 29.0300], [41.0600, 29.0450], [41.0480, 29.0520], - [41.0350, 29.0480], [41.0180, 29.0350], [40.9900, 29.0250] - ], - 'alt2': [ - [41.0766, 29.0124], [41.0900, 29.0600], [41.0750, 29.0800], [41.0500, 29.0750], - [41.0300, 29.0600], [41.0100, 29.0450], [40.9900, 29.0250] - ] - }, - '34-MHM-303': { - 'driver_tag': 'Sürücü C', - 'start_location': 'Genel Merkez Ofis (Levent Kampüs)', - 'destination': 'Maslak Proje Sahası (Saha Görevi)', - 'planned_km': 14.0, - 'driven_km': 19.5, - 'planned_time': '35 dk (Planlanan Rota)', - 'deviation_label': '5.5 km Sapma (İhlal)', - 'green_route': [ - [41.0766, 29.0124], [41.0820, 29.0140], [41.0890, 29.0160], [41.0950, 29.0180], [41.1080, 29.0220] - ], - 'red_route': [ - [41.1080, 29.0220], [41.1120, 29.0350], [41.1150, 29.0480], [41.1220, 29.0580], - [41.1350, 29.0620], [41.1450, 29.0600], [41.1380, 29.0480] - ], - 'alt1': [ - [41.0766, 29.0124], [41.0850, 29.0100], [41.0950, 29.0120], [41.1080, 29.0220] - ], - 'alt2': [ - [41.0766, 29.0124], [41.0800, 28.9950], [41.0950, 28.9850], [41.1080, 29.0220] - ] - }, - '34-CNN-404': { - 'driver_tag': 'Sürücü D', - 'start_location': 'Genel Merkez Ofis (Levent Kampüs)', - 'destination': 'Gebze Lojistik Merkezi (Depo Rotası)', - 'planned_km': 25.0, - 'driven_km': 35.0, - 'planned_time': '45 dk (Planlanan Rota)', - 'deviation_label': '10.0 km Sapma (İhlal)', - 'green_route': [ - [41.0766, 29.0124], [41.0850, 29.0350], [41.0900, 29.0600], [41.0850, 29.0850], - [41.0600, 29.1100], [41.0200, 29.1350], [40.9800, 29.1500] - ], - 'red_route': [ - [40.9800, 29.1500], [40.9600, 29.1750], [40.9400, 29.2100], [40.9250, 29.2450], - [40.9100, 29.2800], [40.8900, 29.3150], [40.8700, 29.3500], [40.8400, 29.3900], [40.8000, 29.4300] - ], - 'alt1': [ - [41.0766, 29.0124], [41.0450, 29.0400], [41.0000, 29.1000], [40.9200, 29.2500], [40.8000, 29.4300] - ], - 'alt2': [ - [41.0766, 29.0124], [41.1000, 29.1000], [41.1200, 29.2000], [40.9800, 29.3500], [40.8000, 29.4300] - ] - }, - '34-BRK-505': { - 'driver_tag': 'Sürücü E', - 'start_location': 'Genel Merkez Ofis (Levent Kampüs)', - 'destination': 'Personel Ev Adresi (Kadıköy/Moda - Eve İzinli)', - 'planned_km': 10.0, - 'driven_km': 14.0, - 'planned_time': '20 dk (Planlanan Rota)', - 'deviation_label': 'Serbest İzinli', - 'green_route': [ - [41.0766, 29.0124], [41.0800, 29.0220], [41.0820, 29.0340], [41.0750, 29.0430], - [41.0650, 29.0440], [41.0480, 29.0250], [41.0450, 29.0060], [41.0550, 29.0110], [41.0766, 29.0124] - ], - 'red_route': [], - 'alt1': [ - [41.0766, 29.0124], [41.0600, 29.0100], [41.0480, 29.0250] - ], - 'alt2': [ - [41.0766, 29.0124], [41.0700, 28.9900], [41.0480, 29.0250] - ] - } - } - + # Gerçek Zamanlı GPS Verilerini Çek (Takip cihazı olanlar veya koordinatı olanlar) vehicles = request.env['fleet.vehicle'].sudo().search([ - ('license_plate', 'in', list(routes_db.keys())) + '|', ('tracker_device_id', '!=', False), + ('latitude', '!=', False) ]) - # Eğer veri tabanında bu 5 plaka yoksa veya eksikse, eksikleri dummy veri olarak oluştur: - existing_plates = vehicles.mapped('license_plate') - for plate in routes_db.keys(): - if plate not in existing_plates: - # Veritabanında yoksa fallback kullanması için en azından döngüde bulunması gerekir, ancak - # araç model vs oluşturmak uzun sürer. - pass - vehicle_list = [] for v in vehicles: - score = 100 - commute_allowed = 'forbidden' - policy = 'forbidden' - policy_desc = 'Yasaklı (Şahsi Kullanım Dışı)' - contract = None + lat = v.latitude or 41.0 + lon = v.longitude or 28.9 + speed = v.gps_speed or 0.0 + + route_color = '#10b981' if speed > 3 else '#ef4444' + deviation_label = f"Hız: {speed} km/s" if speed > 3 else "Park Halinde" - if v.driver_id: - contract = request.env['mymach.fleet.contract'].sudo().search([ - ('driver_id', '=', v.driver_id.id) - ], limit=1) - if contract: - score = contract.driver_score - commute_allowed = contract.commute_allowed - policy = contract.personal_use_policy - policy_desc = dict(contract._fields['personal_use_policy'].selection).get(policy, policy) - - # Personel Ev Adresi - driver_home_address = v.driver_id.street or "Personel Ev Adresi (Kadıköy/Moda)" - - rdata = routes_db.get(v.license_plate, { - 'driver_tag': 'Sürücü X', - 'start_location': 'Genel Merkez Ofis (Levent)', - 'destination': 'Genel Merkez Rotası', - 'planned_km': 10.0, - 'driven_km': 10.0, - 'planned_time': '20 dk (Planlanan Rota)', - 'deviation_label': 'Normal', - 'green_route': [[41.0766, 29.0124], [41.0800, 29.0200]], - 'red_route': [], - 'alt1': [[41.0766, 29.0124], [41.0850, 29.0250]], - 'alt2': [[41.0766, 29.0124], [41.0750, 29.0150]] - }) - - # Yönetici Onaylı Görevlendirme / Rota Var mı? (Canlı Harita Otomatik Güncelleme) - approved_res = request.env['mymach.fleet.reservation'].sudo().search([ - ('vehicle_id', '=', v.id), - ('state', '=', 'approved') - ], limit=1) - - destination = rdata['destination'] - is_manager_approved = False - - if approved_res: - is_manager_approved = True - destination = f"✅ Yönetici Onaylı Görev: {approved_res.destination_location or approved_res.purpose}" - - # 1 KM Sapma Kuralı ve Sözleşme KM Sınırı Uyumluluk Hesaplaması - deviation = round(rdata['driven_km'] - rdata['planned_km'], 1) - has_free_use = policy in ('allowed', 'unlimited', 'rental') - - # Sapma 1.0 KM kuralı kontrolü - is_over_1km = deviation > 1.0 - - # Uyumluluk ve İhlal Mantığı - if is_manager_approved: - is_violation = False - deviation_label = "✅ Yönetici Tarafından Onaylı Rota" - elif is_over_1km and policy == 'forbidden': - is_violation = True - deviation_label = f"🚨 UYUMSUZ ({deviation} km Yasaklı Şahsi Kullanım)" - elif is_over_1km and policy == 'limit' and contract and ((contract.personal_km_used + deviation) > (contract.max_km_limit or 0.0)): - is_violation = True - deviation_label = f"🚨 UYUMSUZ ({deviation} km Sözleşme KM Sınırı Aşıldı)" - elif has_free_use: - is_violation = False - deviation_label = "Serbest İzinli Şahsi Kullanım" - elif is_over_1km: - is_violation = True - deviation_label = f"🚨 UYUMSUZ ({deviation} km Sapma - Yöneticiye Alarm)" - else: - is_violation = False - deviation_label = f"{deviation} km Uyumlu Rota" - - # Eğer UYUMSUZ (İhlal) ise yöneticiye güvenlik bildirimi/logu üret - if is_violation and v.driver_id: - log_exists = request.env['mymach.fleet.security.log'].sudo().search([ - ('vehicle_id', '=', v.id), - ('status', '=', 'exception') - ], limit=1) - if not log_exists: - request.env['mymach.fleet.security.log'].sudo().create({ - 'vehicle_id': v.id, - 'driver_id': v.driver_id.id, - 'latitude': rdata['green_route'][-1][0] if rdata['green_route'] else 41.0766, - 'longitude': rdata['green_route'][-1][1] if rdata['green_route'] else 29.0124, - 'status': 'exception', - 'name': f"🚨 UYUMSUZ ROTA SAPMASI ({v.license_plate}): {deviation_label}" - }) - - route_color = '#ef4444' if is_violation else '#10b981' - vehicle_list.append({ 'id': v.id, 'name': v.name, - 'license_plate': v.license_plate, + 'license_plate': v.license_plate or 'Bilinmiyor', 'driver_name': v.driver_id.name or 'Sürücü Atanmamış', - 'driver_home_address': driver_home_address, - 'driver_tag': rdata['driver_tag'], + 'driver_home_address': v.driver_id.street or '', + 'driver_tag': 'Otonom Takip', 'fuel_level': round(v.current_fuel_level, 1), 'tank_capacity': v.tank_capacity, 'needs_predictive_maintenance': v.needs_predictive_maintenance, 'is_maintenance_critical': v.is_maintenance_critical, - 'score': score, - 'commute_allowed': commute_allowed, - 'policy': policy, - 'policy_desc': policy_desc, - 'start_location': rdata['start_location'], - 'destination': destination, - 'planned_km': rdata['planned_km'], - 'driven_km': rdata['driven_km'], - 'planned_time': rdata['planned_time'], + 'score': 100, + 'commute_allowed': 'forbidden', + 'policy': 'forbidden', + 'policy_desc': 'Standart Takip', + 'start_location': 'Canlı GPS Konumu', + 'destination': 'Gerçek Zamanlı Aktif İzleme', + 'planned_km': 0, + 'driven_km': 0, + 'planned_time': '-', 'deviation_label': deviation_label, - 'deviation': deviation, - 'has_free_use': has_free_use, - 'is_manager_approved': is_manager_approved, - 'is_violation': is_violation, + 'deviation': 0, + 'has_free_use': False, + 'is_manager_approved': False, + 'is_violation': False, 'route_color': route_color, - 'green_route': rdata['green_route'], - 'red_route': rdata['red_route'], - 'alt1': rdata['alt1'], - 'alt2': rdata['alt2'] + 'green_route': [[lat, lon]], # Haritada noktayı göstermek için tek koordinat yeterli + 'red_route': [], + 'alt1': [], + 'alt2': [] }) # Son 50 güvenlik logunu çek (Haritada göstermek için)