teltonika

This commit is contained in:
Pringgosutono
2025-10-30 10:33:55 +07:00
parent 84a9ce3f4c
commit fd304e1f61
7 changed files with 1355 additions and 492 deletions

View File

@ -1,113 +1,121 @@
const moment = require("moment");
const Validator = require("validatorjs");
const Helper = require("../library/LibHelper");
const response = require("../config/response");
const LibMail = require("../library/LibMail");
const LibDevice = require("../library/LibDevice");
const DanaModels = require("../models/DanaModels");
const GpsTracksModels = require("../models/GpsTracksModels");
const { nanoid } = require("nanoid");
const VhcModels = require("../models/VhcModels");
const ZoneModels = require("../models/ZoneModels");
const LibHelper = require("../library/LibHelper");
const moment = require("moment")
const Validator = require("validatorjs")
const Helper = require("../library/LibHelper")
const response = require("../config/response")
const LibMail = require("../library/LibMail")
const LibDevice = require("../library/LibDevice")
const DanaModels = require("../models/DanaModels")
const GpsTracksModels = require("../models/GpsTracksModels")
const { nanoid } = require("nanoid")
const VhcModels = require("../models/VhcModels")
const ZoneModels = require("../models/ZoneModels")
const LibHelper = require("../library/LibHelper")
Validator.useLang("en");
Validator.useLang("en")
const devices = [];
const devices = []
async function commitMessage(now, logDevice) {
try {
if (!logDevice.original_hex) {
return false;
return false
}
const vhc = await VhcModels.getVhcByDeviceId(logDevice.device_id);
const lastTrack = await GpsTracksModels.get2LastLocByDeviceId(logDevice.device_id);
const vhc = await VhcModels.getVhcByDeviceId(logDevice.device_id)
const lastTrack = await GpsTracksModels.get2LastLocByDeviceId(logDevice.device_id)
if (["heartbeat", "alarm"].includes(logDevice.action)) {
if (logDevice.ignition == GpsTracksModels.STTS_IGNITION_HIGH) {
if (logDevice.speed) {
logDevice.stts_engine = GpsTracksModels.STTS_EN_MOVING;
logDevice.stts_engine = GpsTracksModels.STTS_EN_MOVING
} else {
if (lastTrack.length > 0) {
const checkLastHeartbeat = await GpsTracksModels.getLastHeartbeatToDeterminIdling(logDevice.device_id, lastTrack[0].crt, now);
const checkLastHeartbeat = await GpsTracksModels.getLastHeartbeatToDeterminIdling(
logDevice.device_id,
lastTrack[0].crt,
now
)
if (checkLastHeartbeat.length >= 3) {
logDevice.stts_engine = GpsTracksModels.STTS_EN_IDLING;
logDevice.stts_engine = GpsTracksModels.STTS_EN_IDLING
} else {
logDevice.stts_engine = lastTrack[0].stts_engine;
logDevice.stts_engine = lastTrack[0].stts_engine
}
}
}
} else {
logDevice.stts_engine = GpsTracksModels.STTS_EN_STOPING;
logDevice.stts_engine = GpsTracksModels.STTS_EN_STOPING
}
}
if (["location"].includes(logDevice.action)) {
console.log("logDevice:", logDevice);
// console.log("logDevice:", logDevice)
const lastHeartbeatOrAlarm = await GpsTracksModels.get2LastHeartbeatOrAlarm(logDevice.device_id);
const lastHeartbeatOrAlarm = await GpsTracksModels.get2LastHeartbeatOrAlarm(logDevice.device_id)
if (lastHeartbeatOrAlarm.length > 0) {
logDevice.ignition = lastHeartbeatOrAlarm[0].ignition;
logDevice.stts_gps = lastHeartbeatOrAlarm[0].stts_gps;
logDevice.stts_gsm = lastHeartbeatOrAlarm[0].stts_gsm;
logDevice.stts_oil_electricity = lastHeartbeatOrAlarm[0].stts_oil_electricity;
logDevice.stts_alarm = lastHeartbeatOrAlarm[0].stts_alarm;
logDevice.stts_charge = lastHeartbeatOrAlarm[0].stts_charge;
logDevice.stts_acc = lastHeartbeatOrAlarm[0].stts_acc;
logDevice.stts_volt = lastHeartbeatOrAlarm[0].stts_volt;
logDevice.stts_switch = lastHeartbeatOrAlarm[0].stts_switch;
logDevice.ignition = lastHeartbeatOrAlarm[0].ignition
logDevice.stts_gps = lastHeartbeatOrAlarm[0].stts_gps
logDevice.stts_gsm = lastHeartbeatOrAlarm[0].stts_gsm
logDevice.stts_oil_electricity = lastHeartbeatOrAlarm[0].stts_oil_electricity
logDevice.stts_alarm = lastHeartbeatOrAlarm[0].stts_alarm
logDevice.stts_charge = lastHeartbeatOrAlarm[0].stts_charge
logDevice.stts_acc = lastHeartbeatOrAlarm[0].stts_acc
logDevice.stts_volt = lastHeartbeatOrAlarm[0].stts_volt
logDevice.stts_switch = lastHeartbeatOrAlarm[0].stts_switch
} else {
logDevice.ignition = null;
logDevice.stts_gps = null;
logDevice.stts_gsm = null;
logDevice.stts_oil_electricity = null;
logDevice.stts_alarm = null;
logDevice.stts_charge = null;
logDevice.stts_acc = null;
logDevice.stts_volt = null;
logDevice.stts_switch = null;
logDevice.ignition = logDevice.ignition || 0
logDevice.stts_gps = logDevice.stts_gps || 0
logDevice.stts_gsm = logDevice.stts_gsm || 0
logDevice.stts_oil_electricity = logDevice.stts_oil_electricity || 0
logDevice.stts_alarm = logDevice.stts_alarm || 0
logDevice.stts_charge = logDevice.stts_charge || 0
logDevice.stts_acc = logDevice.stts_acc || 0
logDevice.stts_volt = logDevice.stts_volt || 0
logDevice.stts_switch = logDevice.stts_switch || 0
}
// set engine stts moving,idling,stopped
if (logDevice.ignition == GpsTracksModels.STTS_IGNITION_HIGH) {
if (logDevice.speed) {
logDevice.stts_engine = GpsTracksModels.STTS_EN_MOVING;
logDevice.stts_engine = GpsTracksModels.STTS_EN_MOVING
} else {
if (lastTrack.length > 0) {
const checkLastHeartbeat = await GpsTracksModels.getLastHeartbeatToDeterminIdling(logDevice.device_id, lastTrack[0].crt, now);
const checkLastHeartbeat = await GpsTracksModels.getLastHeartbeatToDeterminIdling(
logDevice.device_id,
lastTrack[0].crt,
now
)
if (checkLastHeartbeat.length >= 3) {
logDevice.stts_engine = GpsTracksModels.STTS_EN_IDLING;
logDevice.stts_engine = GpsTracksModels.STTS_EN_IDLING
} else {
logDevice.stts_engine = lastTrack[0].stts_engine;
logDevice.stts_engine = lastTrack[0].stts_engine
}
}
}
// get stts_alarm, stts_charge, stts_acc, stts_volt, stts_switch
} else {
logDevice.stts_engine = GpsTracksModels.STTS_EN_STOPING;
logDevice.stts_engine = GpsTracksModels.STTS_EN_STOPING
}
logDevice.stts_oil_electricity = lastHeartbeatOrAlarm[0].stts_oil_electricity;
logDevice.stts_alarm = lastHeartbeatOrAlarm[0].stts_alarm;
logDevice.stts_charge = lastHeartbeatOrAlarm[0].stts_charge;
logDevice.stts_acc = lastHeartbeatOrAlarm[0].stts_acc;
logDevice.stts_volt = lastHeartbeatOrAlarm[0].stts_volt;
logDevice.stts_switch = lastHeartbeatOrAlarm[0].stts_switch;
logDevice.stts_oil_electricity = lastHeartbeatOrAlarm[0]?.stts_oil_electricity || 0
logDevice.stts_alarm = lastHeartbeatOrAlarm[0]?.stts_alarm || 0
logDevice.stts_charge = lastHeartbeatOrAlarm[0]?.stts_charge || 0
logDevice.stts_acc = lastHeartbeatOrAlarm[0]?.stts_acc || 0
logDevice.stts_volt = lastHeartbeatOrAlarm[0]?.stts_volt || 0
logDevice.stts_switch = lastHeartbeatOrAlarm[0]?.stts_switch || 0
}
// sekarang heartbeat diinject data lokasi juga dari lokasi terakhir
if (["heartbeat"].includes(logDevice.action)) {
if (lastTrack.length > 0) {
logDevice.latitude = lastTrack[0].latitude;
logDevice.longitude = lastTrack[0].longitude;
logDevice.speed = lastTrack[0].speed;
logDevice.orientation = lastTrack[0].orientation;
logDevice.latitude = lastTrack[0].latitude
logDevice.longitude = lastTrack[0].longitude
logDevice.speed = lastTrack[0].speed
logDevice.orientation = lastTrack[0].orientation
if (logDevice.latitude) {
logDevice.stts_reverse_geo = GpsTracksModels.STTS_REVERSE_GEO_NOT;
logDevice.stts_reverse_geo = GpsTracksModels.STTS_REVERSE_GEO_NOT
}
logDevice.length_gps = lastTrack[0].length_gps;
logDevice.pos_stlt_gps = lastTrack[0].pos_stlt_gps;
logDevice.pos_type_gps = lastTrack[0].pos_type_gps;
logDevice.is_pos_gps = lastTrack[0].is_pos_gps;
logDevice.length_gps = lastTrack[0].length_gps
logDevice.pos_stlt_gps = lastTrack[0].pos_stlt_gps
logDevice.pos_type_gps = lastTrack[0].pos_type_gps
logDevice.is_pos_gps = lastTrack[0].is_pos_gps
// jika gapengen dimunculin di last movement
// logDevice.crt = lastTrack[0].crt;
@ -115,27 +123,33 @@ async function commitMessage(now, logDevice) {
// logDevice.crt_d = lastTrack[0].crt_d;
// logDevice.crt_d_format = lastTrack[0].crt_d_format;
// jika pengen di munculin di last movement
logDevice.crt = now;
logDevice.crt_format = moment.unix(now).format("YYYY-MM-DD HH:mm:ss");
logDevice.crt_d = now;
logDevice.crt_d_format = moment.unix(now).format("YYYY-MM-DD HH:mm:ss");
logDevice.crt = now
logDevice.crt_format = moment.unix(now).format("YYYY-MM-DD HH:mm:ss")
logDevice.crt_d = now
logDevice.crt_d_format = moment.unix(now).format("YYYY-MM-DD HH:mm:ss")
}
}
// count milleage
if (logDevice.latitude != null && logDevice.longitude != null && lastTrack.length > 0) {
const distance = LibHelper.haversineGreatCircleDistance(lastTrack[0].latitude, lastTrack[0].longitude, logDevice.latitude, logDevice.longitude, LibHelper.EARTH_RADIUS_KM);
const distance_km = LibHelper.kmToKm(distance, 100000000);
const distance = LibHelper.haversineGreatCircleDistance(
lastTrack[0].latitude,
lastTrack[0].longitude,
logDevice.latitude,
logDevice.longitude,
LibHelper.EARTH_RADIUS_KM
)
const distance_km = LibHelper.kmToKm(distance, 100000000)
// console.log(`GPS TRACKER UP LOCATION => device_id:${logDevice.device_id} vhc_id:${(vhc[0]) ? vhc[0].vid : 0}, distance_km:${distance_km}`);
// validasi jika lebih dari 3km, ga disimpan
if (distance_km >= 3) {
GpsTracksModels.bundleCreate2(logDevice, logDevice); // jika tidak disimpan malah jadi bug, jadi akan update lokasi terus dengan kalkulasi jarak sebelumnya jadi makan lama makin lebar, mending disimpen terus milleagenya jadi 0 aja
return false;
GpsTracksModels.bundleCreate2(logDevice, logDevice) // jika tidak disimpan malah jadi bug, jadi akan update lokasi terus dengan kalkulasi jarak sebelumnya jadi makan lama makin lebar, mending disimpen terus milleagenya jadi 0 aja
return false
}
logDevice.pre_milleage = distance_km;
logDevice.sum_milleage = (lastTrack[0].sum_milleage + logDevice.pre_milleage).toFixed(8);
logDevice.pre_milleage = distance_km
logDevice.sum_milleage = (lastTrack[0].sum_milleage + logDevice.pre_milleage).toFixed(8)
if (vhc.length > 0) {
logDevice.vhc_id = vhc[0] ? vhc[0].vid : 0;
logDevice.vhc_id = vhc[0] ? vhc[0].vid : 0
// if (lastTrack[0].vhc_id == logDevice.vhc_id) {
// logDevice.vhc_milleage = (lastTrack[0].vhc_milleage + logDevice.pre_milleage).toFixed(8);
// } else {
@ -147,9 +161,9 @@ async function commitMessage(now, logDevice) {
// logDevice.vhc_milleage = ((vhc[0] ? vhc[0].sum_milleage : 0) + logDevice.pre_milleage).toFixed(8);
// }
// force mileage dari vechile
logDevice.vhc_milleage = ((vhc[0]?.sum_milleage || 0) + logDevice.pre_milleage).toFixed(8);
logDevice.vhc_milleage = ((vhc[0]?.sum_milleage || 0) + logDevice.pre_milleage).toFixed(8)
VhcModels.update(vhc[0] ? vhc[0].vid : 0, { sum_milleage: logDevice.vhc_milleage });
VhcModels.update(vhc[0] ? vhc[0].vid : 0, { sum_milleage: logDevice.vhc_milleage })
}
}
@ -157,16 +171,16 @@ async function commitMessage(now, logDevice) {
if (logDevice.latitude != null && logDevice.longitude != null) {
// && vhc.length > 0
// log tracking
const currTrack = await GpsTracksModels.bundleCreate2(logDevice, logDevice);
const currTrack = await GpsTracksModels.bundleCreate2(logDevice, logDevice)
// console.log('GT06 HAS LOCATION AND CHECK ZONE');
const inCircle = await ZoneModels.getInCircle(logDevice.latitude, logDevice.longitude);
const inShape = await ZoneModels.getInShape(logDevice.latitude, logDevice.longitude);
const insideSpawnZone = [];
const inCircle = await ZoneModels.getInCircle(logDevice.latitude, logDevice.longitude)
const inShape = await ZoneModels.getInShape(logDevice.latitude, logDevice.longitude)
const insideSpawnZone = []
for (let zone of inCircle) {
insideSpawnZone.push(zone);
insideSpawnZone.push(zone)
}
for (let zone of inShape) {
insideSpawnZone.push(zone);
insideSpawnZone.push(zone)
}
/**
@ -181,12 +195,12 @@ async function commitMessage(now, logDevice) {
leave_at_d: 0,
order_by: "ORDER BY id DESC",
limit: 1,
});
})
if (lastSpawn.length > 0) {
let is_leave_zone = 1;
let is_leave_zone = 1
for (const insideZone of insideSpawnZone) {
if (insideZone.zid === lastSpawn[0].zone_id) is_leave_zone = 0;
if (insideZone.zid === lastSpawn[0].zone_id) is_leave_zone = 0
}
if (is_leave_zone === 1 && lastSpawn[0].device_id == logDevice.device_id) {
GpsTracksModels.updt2SpawnZone(
@ -201,7 +215,7 @@ async function commitMessage(now, logDevice) {
updt_format: moment.unix(now).format("YYYY-MM-DD HH:mm:ss"),
},
lastSpawn[0].id
);
)
}
} else {
for (let zone of insideSpawnZone) {
@ -213,13 +227,17 @@ async function commitMessage(now, logDevice) {
z_workflow_name: zone.workflow_type_name,
shiptocode: zone.shiptocode,
z_fulladdress: zone.fulladdress,
v_nopol: (vhc[0] ? vhc[0].nopol1 : 0 || "") + (vhc[0] ? vhc[0].nopol2 : 0 || "") + (vhc[0] ? vhc[0].nopol3 : 0 || ""),
v_nopol:
(vhc[0] ? vhc[0].nopol1 : 0 || "") +
(vhc[0] ? vhc[0].nopol2 : 0 || "") +
(vhc[0] ? vhc[0].nopol3 : 0 || ""),
da_name: vhc[0] ? vhc[0].da_name : 0 || "",
da_phone: "+" + (vhc[0] ? vhc[0].da_phone_code : 0 || "") + (vhc[0] ? vhc[0].da_phone : 0 || ""),
};
da_phone:
"+" + (vhc[0] ? vhc[0].da_phone_code : 0 || "") + (vhc[0] ? vhc[0].da_phone : 0 || ""),
}
// LibMail.sendVhcSpawnZoneMail(`${(vhc[0]) ? vhc[0].nopol1 : 0 || ''}${(vhc[0]) ? vhc[0].nopol2 : 0 || ''}${(vhc[0]) ? vhc[0].nopol3 : 0 || ''} entering zone ${mailData.z_name}`, mailData.pic_mail, mailData);
if (logDevice.device_id === "0865784052395871") console.log(1234567890);
if (logDevice.device_id === "0865784052395871") console.log(1234567890)
GpsTracksModels.create2SpawnZone({
device_id: logDevice.device_id,
master_id: Number(currTrack.result.insertId),
@ -235,27 +253,27 @@ async function commitMessage(now, logDevice) {
source: GpsTracksModels.SOURCE_GPS_TRACKER,
crt: now,
crt_format: moment.unix(now).format("YYYY-MM-DD HH:mm:ss"),
});
if (logDevice.device_id === "0865784052395871") console.log("DISINI");
})
if (logDevice.device_id === "0865784052395871") console.log("DISINI")
}
}
} else {
// log tracking
GpsTracksModels.bundleCreate2(logDevice, logDevice);
GpsTracksModels.bundleCreate2(logDevice, logDevice)
}
} catch (e) {
console.error(e);
console.error(e)
}
}
class ListenController {
async gps(req, res) {
let apiRes = {};
let apiRes = {}
try {
const c = { id: req.body._id };
let buffer_req = Buffer.from(req.body.data, "base64");
const now = moment().unix();
const me = LibDevice.identifyProtocolFromBuffer(buffer_req);
const c = { id: req.body._id }
let buffer_req = Buffer.from(req.body.data, "base64")
const now = moment().unix()
const me = LibDevice.identifyProtocolFromBuffer(buffer_req)
const logDevice = {
original_hex: me.ori_string,
@ -291,100 +309,228 @@ class ListenController {
crt_d_format: null,
crt_s: now,
crt_s_format: moment.unix(now).format("YYYY-MM-DD HH:mm:ss"),
};
}
if (me.protocol_name == "gt06") {
let dvc_id = devices[c.id];
const act = LibDevice.gt06Action(me, dvc_id || null);
let dvc_id = devices[c.id]
const act = LibDevice.gt06Action(me, dvc_id || null)
if (act.action_type == "login") {
logDevice.action = act.action_type;
logDevice.device_id = act.device_id;
devices[c.id] = act.device_id;
logDevice.action = act.action_type
logDevice.device_id = act.device_id
devices[c.id] = act.device_id
} else if (act.action_type == "location") {
logDevice.action = act.action_type;
logDevice.device_id = act.device_id;
logDevice.latitude = act.gps_data.latitude || null;
logDevice.longitude = act.gps_data.longitude || null;
logDevice.speed = act.gps_data.speed;
logDevice.orientation = act.gps_data.orientation;
logDevice.action = act.action_type
logDevice.device_id = act.device_id
logDevice.latitude = act.gps_data.latitude || null
logDevice.longitude = act.gps_data.longitude || null
logDevice.speed = act.gps_data.speed
logDevice.orientation = act.gps_data.orientation
if (logDevice.latitude) {
logDevice.stts_reverse_geo = GpsTracksModels.STTS_REVERSE_GEO_NOT;
logDevice.stts_reverse_geo = GpsTracksModels.STTS_REVERSE_GEO_NOT
}
logDevice.length_gps = act.gps_data.quantity_pos_satellites_c;
logDevice.pos_stlt_gps = act.gps_data.quantity_pos_satellites_b;
logDevice.pos_type_gps = Number(act.gps_data.realtime_dif_gps) === 0 ? GpsTracksModels.STTS_POS_TYPE_GPS_RLTM : GpsTracksModels.STTS_POS_TYPE_GPS_DIFF;
logDevice.is_pos_gps = Number(act.gps_data.positioning_gps) ? GpsTracksModels.STTS_IS_POS_GPS_HAS : GpsTracksModels.STTS_IS_POS_GPS_NOT;
logDevice.crt = moment(act.gps_data.date).unix();
logDevice.crt_format = moment(act.gps_data.date).format("YYYY-MM-DD HH:mm:ss");
logDevice.crt_d = moment(act.gps_data.date).unix();
logDevice.crt_d_format = moment(act.gps_data.date).format("YYYY-MM-DD HH:mm:ss");
logDevice.length_gps = act.gps_data.quantity_pos_satellites_c
logDevice.pos_stlt_gps = act.gps_data.quantity_pos_satellites_b
logDevice.pos_type_gps =
Number(act.gps_data.realtime_dif_gps) === 0
? GpsTracksModels.STTS_POS_TYPE_GPS_RLTM
: GpsTracksModels.STTS_POS_TYPE_GPS_DIFF
logDevice.is_pos_gps = Number(act.gps_data.positioning_gps)
? GpsTracksModels.STTS_IS_POS_GPS_HAS
: GpsTracksModels.STTS_IS_POS_GPS_NOT
logDevice.crt = moment(act.gps_data.date).unix()
logDevice.crt_format = moment(act.gps_data.date).format("YYYY-MM-DD HH:mm:ss")
logDevice.crt_d = moment(act.gps_data.date).unix()
logDevice.crt_d_format = moment(act.gps_data.date).format("YYYY-MM-DD HH:mm:ss")
} else if (act.action_type == "heartbeat") {
logDevice.action = act.action_type;
logDevice.device_id = act.device_id;
logDevice.ignition = Number(act.stts_data.terminal_info.acc) ? GpsTracksModels.STTS_IGNITION_HIGH : GpsTracksModels.STTS_IGNITION_LOW;
logDevice.stts_gps = act.stts_data.terminal_info.gps_tracking ? GpsTracksModels.STTS_GPS_ON : GpsTracksModels.STTS_GPS_OFF;
logDevice.stts_gsm = Number(act.stts_data.gsm_signal_strength) + 1;
logDevice.stts_oil_electricity = Number(act.stts_data.terminal_info.oil_electricity) === 0 ? GpsTracksModels.STTS_OIL_ELECTRIC_ON : GpsTracksModels.STTS_OIL_ELECTRIC_OFF;
logDevice.action = act.action_type
logDevice.device_id = act.device_id
logDevice.ignition = Number(act.stts_data.terminal_info.acc)
? GpsTracksModels.STTS_IGNITION_HIGH
: GpsTracksModels.STTS_IGNITION_LOW
logDevice.stts_gps = act.stts_data.terminal_info.gps_tracking
? GpsTracksModels.STTS_GPS_ON
: GpsTracksModels.STTS_GPS_OFF
logDevice.stts_gsm = Number(act.stts_data.gsm_signal_strength) + 1
logDevice.stts_oil_electricity =
Number(act.stts_data.terminal_info.oil_electricity) === 0
? GpsTracksModels.STTS_OIL_ELECTRIC_ON
: GpsTracksModels.STTS_OIL_ELECTRIC_OFF
let stts_alarm = Number(act.stts_data.terminal_info.stts);
logDevice.stts_alarm = stts_alarm === 1 ? GpsTracksModels.STTS_ALARM_SHOCK : stts_alarm === 2 ? GpsTracksModels.STTS_ALARM_POWER_CUT : stts_alarm === 3 ? GpsTracksModels.STTS_ALARM_LOW_BATTERY : stts_alarm === 4 ? GpsTracksModels.STTS_ALARM_SOS : GpsTracksModels.STTS_ALARM_NORMAL;
let stts_alarm = Number(act.stts_data.terminal_info.stts)
logDevice.stts_alarm =
stts_alarm === 1
? GpsTracksModels.STTS_ALARM_SHOCK
: stts_alarm === 2
? GpsTracksModels.STTS_ALARM_POWER_CUT
: stts_alarm === 3
? GpsTracksModels.STTS_ALARM_LOW_BATTERY
: stts_alarm === 4
? GpsTracksModels.STTS_ALARM_SOS
: GpsTracksModels.STTS_ALARM_NORMAL
logDevice.stts_charge = Number(act.stts_data.terminal_info.charge) ? GpsTracksModels.STTS_CHARGE_ON : GpsTracksModels.STTS_CHARGE_OFF;
logDevice.stts_acc = Number(act.stts_data.terminal_info.acc) ? GpsTracksModels.STTS_ACC_HIGH : GpsTracksModels.STTS_ACC_LOW;
logDevice.stts_switch = Number(act.stts_data.terminal_info.is_active) ? GpsTracksModels.STTS_SWITCH_ON : GpsTracksModels.STTS_SWITCH_OFF;
logDevice.stts_volt = Number(act.stts_data.voltage_level) + 1;
logDevice.stts_charge = Number(act.stts_data.terminal_info.charge)
? GpsTracksModels.STTS_CHARGE_ON
: GpsTracksModels.STTS_CHARGE_OFF
logDevice.stts_acc = Number(act.stts_data.terminal_info.acc)
? GpsTracksModels.STTS_ACC_HIGH
: GpsTracksModels.STTS_ACC_LOW
logDevice.stts_switch = Number(act.stts_data.terminal_info.is_active)
? GpsTracksModels.STTS_SWITCH_ON
: GpsTracksModels.STTS_SWITCH_OFF
logDevice.stts_volt = Number(act.stts_data.voltage_level) + 1
} else if (act.action_type == "alarm") {
logDevice.action = act.action_type;
logDevice.device_id = act.device_id;
logDevice.latitude = act.gps_data.latitude || null;
logDevice.longitude = act.gps_data.longitude || null;
logDevice.speed = act.gps_data.speed;
logDevice.orientation = act.gps_data.orientation;
logDevice.action = act.action_type
logDevice.device_id = act.device_id
logDevice.latitude = act.gps_data.latitude || null
logDevice.longitude = act.gps_data.longitude || null
logDevice.speed = act.gps_data.speed
logDevice.orientation = act.gps_data.orientation
if (logDevice.latitude) {
logDevice.stts_reverse_geo = GpsTracksModels.STTS_REVERSE_GEO_NOT;
logDevice.stts_reverse_geo = GpsTracksModels.STTS_REVERSE_GEO_NOT
}
logDevice.length_gps = act.gps_data.quantity_pos_satellites_c;
logDevice.pos_stlt_gps = act.gps_data.quantity_pos_satellites_b;
logDevice.pos_type_gps = Number(act.gps_data.realtime_dif_gps) === 0 ? GpsTracksModels.STTS_POS_TYPE_GPS_RLTM : GpsTracksModels.STTS_POS_TYPE_GPS_DIFF;
logDevice.is_pos_gps = Number(act.gps_data.positioning_gps) ? GpsTracksModels.STTS_IS_POS_GPS_HAS : GpsTracksModels.STTS_IS_POS_GPS_NOT;
logDevice.ignition = Number(act.stts_data.terminal_info.acc) ? GpsTracksModels.STTS_IGNITION_HIGH : GpsTracksModels.STTS_IGNITION_LOW;
logDevice.stts_gps = act.stts_data.terminal_info.gps_tracking ? GpsTracksModels.STTS_GPS_ON : GpsTracksModels.STTS_GPS_OFF;
logDevice.stts_gsm = Number(act.stts_data.gsm_signal_strength) + 1;
logDevice.stts_oil_electricity = Number(act.stts_data.terminal_info.oil_electricity) === 0 ? GpsTracksModels.STTS_OIL_ELECTRIC_ON : GpsTracksModels.STTS_OIL_ELECTRIC_OFF;
logDevice.length_gps = act.gps_data.quantity_pos_satellites_c
logDevice.pos_stlt_gps = act.gps_data.quantity_pos_satellites_b
logDevice.pos_type_gps =
Number(act.gps_data.realtime_dif_gps) === 0
? GpsTracksModels.STTS_POS_TYPE_GPS_RLTM
: GpsTracksModels.STTS_POS_TYPE_GPS_DIFF
logDevice.is_pos_gps = Number(act.gps_data.positioning_gps)
? GpsTracksModels.STTS_IS_POS_GPS_HAS
: GpsTracksModels.STTS_IS_POS_GPS_NOT
logDevice.ignition = Number(act.stts_data.terminal_info.acc)
? GpsTracksModels.STTS_IGNITION_HIGH
: GpsTracksModels.STTS_IGNITION_LOW
logDevice.stts_gps = act.stts_data.terminal_info.gps_tracking
? GpsTracksModels.STTS_GPS_ON
: GpsTracksModels.STTS_GPS_OFF
logDevice.stts_gsm = Number(act.stts_data.gsm_signal_strength) + 1
logDevice.stts_oil_electricity =
Number(act.stts_data.terminal_info.oil_electricity) === 0
? GpsTracksModels.STTS_OIL_ELECTRIC_ON
: GpsTracksModels.STTS_OIL_ELECTRIC_OFF
let stts_alarm = Number(act.stts_data.terminal_info.stts);
logDevice.stts_alarm = stts_alarm === 1 ? GpsTracksModels.STTS_ALARM_SHOCK : stts_alarm === 2 ? GpsTracksModels.STTS_ALARM_POWER_CUT : stts_alarm === 3 ? GpsTracksModels.STTS_ALARM_LOW_BATTERY : stts_alarm === 4 ? GpsTracksModels.STTS_ALARM_SOS : GpsTracksModels.STTS_ALARM_NORMAL;
let stts_alarm = Number(act.stts_data.terminal_info.stts)
logDevice.stts_alarm =
stts_alarm === 1
? GpsTracksModels.STTS_ALARM_SHOCK
: stts_alarm === 2
? GpsTracksModels.STTS_ALARM_POWER_CUT
: stts_alarm === 3
? GpsTracksModels.STTS_ALARM_LOW_BATTERY
: stts_alarm === 4
? GpsTracksModels.STTS_ALARM_SOS
: GpsTracksModels.STTS_ALARM_NORMAL
logDevice.stts_charge = Number(act.stts_data.terminal_info.charge) ? GpsTracksModels.STTS_CHARGE_ON : GpsTracksModels.STTS_CHARGE_OFF;
logDevice.stts_acc = Number(act.stts_data.terminal_info.acc) ? GpsTracksModels.STTS_ACC_HIGH : GpsTracksModels.STTS_ACC_LOW;
logDevice.stts_switch = Number(act.stts_data.terminal_info.is_active) ? GpsTracksModels.STTS_SWITCH_ON : GpsTracksModels.STTS_SWITCH_OFF;
logDevice.stts_volt = Number(act.stts_data.voltage_level) + 1;
logDevice.crt = moment(act.gps_data.date).unix();
logDevice.crt_format = moment(act.gps_data.date).format("YYYY-MM-DD HH:mm:ss");
logDevice.crt_d = moment(act.gps_data.date).unix();
logDevice.crt_d_format = moment(act.gps_data.date).format("YYYY-MM-DD HH:mm:ss");
logDevice.stts_charge = Number(act.stts_data.terminal_info.charge)
? GpsTracksModels.STTS_CHARGE_ON
: GpsTracksModels.STTS_CHARGE_OFF
logDevice.stts_acc = Number(act.stts_data.terminal_info.acc)
? GpsTracksModels.STTS_ACC_HIGH
: GpsTracksModels.STTS_ACC_LOW
logDevice.stts_switch = Number(act.stts_data.terminal_info.is_active)
? GpsTracksModels.STTS_SWITCH_ON
: GpsTracksModels.STTS_SWITCH_OFF
logDevice.stts_volt = Number(act.stts_data.voltage_level) + 1
logDevice.crt = moment(act.gps_data.date).unix()
logDevice.crt_format = moment(act.gps_data.date).format("YYYY-MM-DD HH:mm:ss")
logDevice.crt_d = moment(act.gps_data.date).unix()
logDevice.crt_d_format = moment(act.gps_data.date).format("YYYY-MM-DD HH:mm:ss")
} else {
logDevice.action = act.action_type;
logDevice.device_id = act.device_id;
logDevice.action = act.action_type
logDevice.device_id = act.device_id
}
}
await commitMessage(now, logDevice);
await commitMessage(now, logDevice)
apiRes = JSON.parse(JSON.stringify(response[200]));
apiRes.meta.message = "success";
apiRes.data = logDevice;
return res.status(200).json(apiRes);
apiRes = JSON.parse(JSON.stringify(response[200]))
apiRes.meta.message = "success"
apiRes.data = logDevice
return res.status(200).json(apiRes)
} catch (e) {
console.log("error", e);
apiRes = JSON.parse(JSON.stringify(response[500]));
apiRes.meta.message += Helper.setErrMsg(": " + e.message);
return res.status(500).json(apiRes);
console.log("error", e)
apiRes = JSON.parse(JSON.stringify(response[500]))
apiRes.meta.message += Helper.setErrMsg(": " + e.message)
return res.status(500).json(apiRes)
}
}
async teltonika(req, res) {
let apiRes = {}
try {
const imei = req.body.imei.toString().padStart(16, "0")
const records = req.body.records
// for (const rec of records) {
// console.log(
// `→ ${rec.timestamp} ${imei} | GNSS: ${JSON.stringify(rec.gnss)} || IO: ${JSON.stringify(rec.io)}`
// )
// }
// -- Commit each record to DB --
// records.forEach(async (rec) => {
for (const rec of records) {
// console.log("rec ====> ", rec)
const now = moment().unix()
const logDevice = {
original_hex: "0xTELTONIKA",
protocol: "teltonika8e",
action: "location",
device_id: imei,
latitude: rec.gnss.latitude || null,
longitude: rec.gnss.longitude || null,
speed: rec.gnss.speed || 0,
orientation: rec.gnss.angle || 0,
ignition: rec.io.Ignition ? GpsTracksModels.STTS_IGNITION_HIGH : GpsTracksModels.STTS_IGNITION_LOW,
stts_engine: 0,
stts_gps: rec.gnss.satellites || 0,
length_gps: 0,
pos_stlt_gps: 0,
pos_type_gps: 0,
is_pos_gps: 0,
stts_gsm: 0,
stts_oil_electricity: 0,
stts_alarm: 0,
stts_charge: 0,
stts_acc: 0,
stts_volt: 0,
stts_switch: 0,
stts_reverse_geo: 0,
pre_milleage: 0,
source: GpsTracksModels.SOURCE_GPS_TRACKER,
vhc_id: 0,
drv_id: 0,
drv_milleage: rec.io["Trip Odometer"] || 0,
crt: now,
crt_format: moment.unix(now).format("YYYY-MM-DD HH:mm:ss"),
crt_d: rec.gnss.timestamp / 1000,
crt_d_format: moment.unix(rec.gnss.timestamp / 1000).format("YYYY-MM-DD HH:mm:ss"),
crt_s: now,
crt_s_format: moment.unix(now).format("YYYY-MM-DD HH:mm:ss"),
fuel_curr: rec.io["Fuel Level"] || 0,
fuel_count: rec.io["Fuel Consumed (counted)"] || 0,
}
await commitMessage(now, logDevice)
}
// IO: {"Ignition":0,"Movement":1,"GSM Signal":5,"Sleep Mode":0,"GNSS Status":1,"Accelerator Pedal Position":0,"External Voltage":13087,"Battery Voltage":4100,"Fuel Level":144,"Engine RPM":0,"Engine Temperature":950,"Trip Odometer":48250,"Total Odometer":288320,"Total Mileage":125662000,"Fuel Consumed (counted)":229}
apiRes = JSON.parse(JSON.stringify(response[200]))
apiRes.meta.message = "success"
// apiRes.data = "logDevice"
return res.status(200).json(apiRes)
} catch (err) {
console.error("❌ error:", err.message)
apiRes = JSON.parse(JSON.stringify(response[500]))
apiRes.meta.message += Helper.setErrMsg(": " + err.message)
return res.status(500).json(apiRes)
}
}
}
const object = new ListenController();
const object = new ListenController()
module.exports = object;
module.exports = object