/* 
 * ROMAN DIGITAL - Autor: Américo Patetta
 */
/**
 * Author:  pc
 * Created: 23 set. 2022
 */

SELECT 
    u.unit_id, u.nombre_comun,
    p.lat, p.lng, p.speed, p.heading, p.timestamp, p.ignition, p.external_voltage as bateria, p.total_mileage, p.current_mileage, u.max_allowed_speed, u.gauge_max_speed, u.km_offset,p.total_hours, p.current_hours, u.hour_offset,
    i.icon_url, i.base_x, i.base_y, i.width, i.height, u.matricula, current_position_timestamp, last_heartbeat_timestamp, current_inf_timestamp
    FROM units u 
    LEFT JOIN log_position p ON u.current_position = p.log_id 
    LEFT JOIN icons i ON u.icon_id = i.icon_id 
    WHERE id_flota IN (%s) AND 
    u.activo=%d
    ORDER BY IF((%d-p.timestamp)<%d,0,1), u.matricula

SELECT machine_id, machine_name, p.lat, p.lng, p.speed, p.azimuth, p.gps_timestamp, p.external_power as bateria, p.mileage, 15 as max_allowed_speed, 20 as gauge_max_speed, mileage_offset, p.hours, hours_offset, 
    "iconUrl" as icon_url, 0 as base_x, 0 as base_y, 32 as width, 32 as height, machine_name as matricula, gps_timestamp, 