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

525
app.js
View File

@ -1,141 +1,158 @@
require("dotenv").config({ path: ".env" });
require("events").EventEmitter.prototype._maxListeners = 30;
const morgan = require("morgan");
require("dotenv").config({ path: ".env" })
require("events").EventEmitter.prototype._maxListeners = 30
const morgan = require("morgan")
const net = require("net");
const dgram = require("dgram");
const crc = require("crc");
const moment = require("moment");
const LibDevice = require("./library/LibDevice");
const GpsTracksModels = require("./models/GpsTracksModels");
const ZoneModels = require("./models/ZoneModels");
const VhcModels = require("./models/VhcModels");
const LibMail = require("./library/LibMail");
const LibHelper = require("./library/LibHelper");
const nanoid = require("nanoid").nanoid;
const express = require("express");
const routes = require("./routes/routes");
const app = express();
const LibWinston = require("./library/LibWinston");
const net = require("net")
const dgram = require("dgram")
const crc = require("crc")
const moment = require("moment")
const LibDevice = require("./library/LibDevice")
const GpsTracksModels = require("./models/GpsTracksModels")
const ZoneModels = require("./models/ZoneModels")
const VhcModels = require("./models/VhcModels")
const LibMail = require("./library/LibMail")
const LibHelper = require("./library/LibHelper")
const nanoid = require("nanoid").nanoid
const LibWinston = require("./library/LibWinston")
const express = require("express")
// const routes = require("./routes/routes")
const listen = require("./routes/listen")
const app = express()
const logName = "libUdp"
const Logger = LibWinston.initialize(logName)
const logName = "libUdp";
const Logger = LibWinston.initialize(logName);
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)) {
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.ignition = lastHeartbeatOrAlarm[0].ignition
logDevice.stts_gps = lastHeartbeatOrAlarm[0].stts_gps
logDevice.stts_gsm = lastHeartbeatOrAlarm[0].stts_gsm
} else {
logDevice.ignition = lastHeartbeatOrAlarm[0].ignition;
logDevice.stts_gps = lastHeartbeatOrAlarm[0].stts_gps;
logDevice.stts_gsm = lastHeartbeatOrAlarm[0].stts_gsm;
logDevice.ignition = lastHeartbeatOrAlarm[0].ignition
logDevice.stts_gps = lastHeartbeatOrAlarm[0].stts_gps
logDevice.stts_gsm = lastHeartbeatOrAlarm[0].stts_gsm
}
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
}
logDevice.stts_oil_electricity = lastHeartbeatOrAlarm[0]?.stts_oil_electricity ?? 0;
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
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
}
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
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")
}
}
// ================= UPDATE SUM MILEAGE DARI t_vehicle ====================
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 = 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_km = LibHelper.kmToKm(distance, 100000000)
if (distance_km >= 3) {
GpsTracksModels.bundleCreate2(logDevice, logDevice);
return false;
GpsTracksModels.bundleCreate2(logDevice, logDevice)
return false
}
logDevice.pre_milleage = distance_km;
logDevice.pre_milleage = distance_km
// 1. Hitung sum_milleage dari log sebelumnya
const logBaseMileage = lastTrack[0]?.sum_milleage ?? 0;
logDevice.sum_milleage = (logBaseMileage + logDevice.pre_milleage).toFixed(8);
const logBaseMileage = lastTrack[0]?.sum_milleage ?? 0
logDevice.sum_milleage = (logBaseMileage + logDevice.pre_milleage).toFixed(8)
// 2. Hitung vhc_milleage dari master vehicle
if (vhc.length > 0) {
const vhcBaseMileage = vhc[0]?.sum_milleage ?? 0;
logDevice.vhc_id = vhc[0]?.vid ?? 0;
logDevice.vhc_milleage = (vhcBaseMileage + logDevice.pre_milleage).toFixed(8);
const vhcBaseMileage = vhc[0]?.sum_milleage ?? 0
logDevice.vhc_id = vhc[0]?.vid ?? 0
logDevice.vhc_milleage = (vhcBaseMileage + logDevice.pre_milleage).toFixed(8)
VhcModels.update(logDevice.vhc_id, {
sum_milleage: logDevice.vhc_milleage,
});
})
}
// 4. Simpan ke DB
GpsTracksModels.bundleCreate2(logDevice, logDevice);
GpsTracksModels.bundleCreate2(logDevice, logDevice)
}
// V1
@ -178,16 +195,16 @@ async function commitMessage(now, logDevice) {
// }
if (logDevice.latitude != null && logDevice.longitude != null) {
const currTrack = await GpsTracksModels.bundleCreate2(logDevice, logDevice);
const currTrack = await GpsTracksModels.bundleCreate2(logDevice, logDevice)
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)
}
/**
@ -202,12 +219,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(
@ -222,7 +239,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) {
@ -234,12 +251,16 @@ 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 || ""),
}
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),
@ -255,20 +276,20 @@ 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 {
GpsTracksModels.bundleCreate2(logDevice, logDevice);
GpsTracksModels.bundleCreate2(logDevice, logDevice)
}
} catch (e) {
console.error(e);
console.error(e)
}
}
const devices = [];
const netConn = require("./config/netConn");
const devices = []
const netConn = require("./config/netConn")
/**
* uniquely identify a socket with Node.js
* https://stackoverflow.com/questions/6805432/how-to-uniquely-identify-a-socket-with-node-js
@ -278,13 +299,13 @@ net.createServer(
allowHalfOpen: true,
},
function (c) {
c.id = nanoid();
c.id = nanoid()
c.pipe(c);
c.pipe(c)
c.on("data", async (buffer_req) => {
const now = moment().unix();
const me = LibDevice.identifyProtocolFromBuffer(buffer_req);
const now = moment().unix()
const me = LibDevice.identifyProtocolFromBuffer(buffer_req)
const logDevice = {
original_hex: me.ori_string,
@ -320,198 +341,240 @@ net.createServer(
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;
logDevice.action = act.action_type
logDevice.device_id = act.device_id
devices[c.id] = act.device_id;
netConn[act.device_id] = c;
devices[c.id] = act.device_id
netConn[act.device_id] = c
if (typeof act.buffer_resp != "undefined") {
c.write(act.buffer_resp);
c.write(act.buffer_resp)
}
} else if (act.action_type == "location") {
logDevice.action = act.action_type;
logDevice.device_id = act.device_id;
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.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.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");
let deviceTime = moment(act.gps_data.date).unix();
let now = moment().unix();
let diff = Math.abs(deviceTime - now);
let deviceTime = moment(act.gps_data.date).unix()
let now = moment().unix()
let diff = Math.abs(deviceTime - now)
// Jika beda waktu terlalu besar (> 300 detik atau 5 menit), pakai waktu server
if (diff > 300) {
console.warn(`[WAKTU TIDAK VALID] Device ${logDevice.device_id} mengirim waktu tidak sinkron, pakai waktu server.`);
deviceTime = now;
console.warn(
`[WAKTU TIDAK VALID] Device ${logDevice.device_id} mengirim waktu tidak sinkron, pakai waktu server.`
)
deviceTime = now
}
logDevice.crt = deviceTime;
logDevice.crt_format = moment.unix(deviceTime).format("YYYY-MM-DD HH:mm:ss");
logDevice.crt_d = deviceTime;
logDevice.crt_d_format = moment.unix(deviceTime).format("YYYY-MM-DD HH:mm:ss");
logDevice.crt_device_raw = moment(act.gps_data.date).unix(); // waktu asli dari device (sebelum validasi)
logDevice.crt = deviceTime
logDevice.crt_format = moment.unix(deviceTime).format("YYYY-MM-DD HH:mm:ss")
logDevice.crt_d = deviceTime
logDevice.crt_d_format = moment.unix(deviceTime).format("YYYY-MM-DD HH:mm:ss")
logDevice.crt_device_raw = moment(act.gps_data.date).unix() // waktu asli dari device (sebelum validasi)
} else if (act.action_type == "heartbeat") {
logDevice.action = act.action_type;
logDevice.device_id = act.device_id;
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.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.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);
let stts_alarm = Number(act.stts_data.terminal_info.stts)
if (stts_alarm == 0) {
logDevice.stts_alarm = GpsTracksModels.STTS_ALARM_NORMAL;
logDevice.stts_alarm = GpsTracksModels.STTS_ALARM_NORMAL
} else if (stts_alarm == 1) {
logDevice.stts_alarm = GpsTracksModels.STTS_ALARM_SHOCK;
logDevice.stts_alarm = GpsTracksModels.STTS_ALARM_SHOCK
} else if (stts_alarm == 2) {
logDevice.stts_alarm = GpsTracksModels.STTS_ALARM_POWER_CUT;
logDevice.stts_alarm = GpsTracksModels.STTS_ALARM_POWER_CUT
} else if (stts_alarm == 3) {
logDevice.stts_alarm = GpsTracksModels.STTS_ALARM_LOW_BATTERY;
logDevice.stts_alarm = GpsTracksModels.STTS_ALARM_LOW_BATTERY
} else if (stts_alarm == 4) {
logDevice.stts_alarm = GpsTracksModels.STTS_ALARM_SOS;
logDevice.stts_alarm = GpsTracksModels.STTS_ALARM_SOS
}
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
if (typeof act.buffer_resp != "undefined") {
c.write(act.buffer_resp);
c.write(act.buffer_resp)
}
} else if (act.action_type == "alarm") {
logDevice.action = act.action_type;
logDevice.device_id = act.device_id;
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.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.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.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.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);
let stts_alarm = Number(act.stts_data.terminal_info.stts)
if (stts_alarm == 0) {
logDevice.stts_alarm = GpsTracksModels.STTS_ALARM_NORMAL;
logDevice.stts_alarm = GpsTracksModels.STTS_ALARM_NORMAL
} else if (stts_alarm == 1) {
logDevice.stts_alarm = GpsTracksModels.STTS_ALARM_SHOCK;
logDevice.stts_alarm = GpsTracksModels.STTS_ALARM_SHOCK
} else if (stts_alarm == 2) {
logDevice.stts_alarm = GpsTracksModels.STTS_ALARM_POWER_CUT;
logDevice.stts_alarm = GpsTracksModels.STTS_ALARM_POWER_CUT
} else if (stts_alarm == 3) {
logDevice.stts_alarm = GpsTracksModels.STTS_ALARM_LOW_BATTERY;
logDevice.stts_alarm = GpsTracksModels.STTS_ALARM_LOW_BATTERY
} else if (stts_alarm == 4) {
logDevice.stts_alarm = GpsTracksModels.STTS_ALARM_SOS;
logDevice.stts_alarm = GpsTracksModels.STTS_ALARM_SOS
}
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
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.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")
if (typeof act.buffer_resp != "undefined") {
c.write(act.buffer_resp);
c.write(act.buffer_resp)
}
} else {
logDevice.action = act.action_type;
logDevice.device_id = act.device_id;
logDevice.action = act.action_type
logDevice.device_id = act.device_id
}
}
commitMessage(now, logDevice);
});
c.on("end", () => {});
c.on("close", () => {});
commitMessage(now, logDevice)
})
c.on("end", () => {})
c.on("close", () => {})
c.on("drain", (a) => {
console.log("client drain", a);
});
console.log("client drain", a)
})
c.on("error", (a) => {
console.error("client error", a);
});
console.error("client error", a)
})
c.on("lookup", (a) => {
console.log("client lookup", a);
});
console.log("client lookup", a)
})
c.on("ready", (a) => {
console.log("client ready", a);
});
console.log("client ready", a)
})
c.on("timeout", (a) => {
console.log("client timeout", a);
});
console.log("client timeout", a)
})
c.pipe(c);
c.pipe(c)
}
).listen(process.env.PORT, () => {
console.log("Server gps tracker running at port " + process.env.PORT);
});
console.log("Server gps tracker running at port " + process.env.PORT)
})
app.use(morgan("combined"));
app.use(express.json({ limit: "10mb" }));
app.use(express.urlencoded({ extended: true, limit: "10mb" }));
// ========= server express =========
app.use(morgan("combined"))
app.use(express.json({ limit: "10mb" }))
app.use(express.urlencoded({ extended: true, limit: "10mb" }))
app.use(async function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "OPTIONS,GET,HEAD,PUT,PATCH,POST,DELETE");
res.header("Access-Control-Allow-Headers", "Accept, Authorization, Content-Type, X-Requested-With, Range, x-api-key, x-forwarded-for");
next();
});
res.header("Access-Control-Allow-Origin", "*")
res.header("Access-Control-Allow-Methods", "OPTIONS,GET,HEAD,PUT,PATCH,POST,DELETE")
res.header(
"Access-Control-Allow-Headers",
"Accept, Authorization, Content-Type, X-Requested-With, Range, x-api-key, x-forwarded-for"
)
next()
})
routes.use(app);
listen.use(app)
app.listen(process.env.PORT_EXPRESS, () => {
console.log("Express server running at port " + process.env.PORT_EXPRESS);
});
console.log("Express server running at port " + process.env.PORT_EXPRESS)
})
const udp = dgram.createSocket("udp4");
const udp = dgram.createSocket("udp4")
const devices1 = [];
const devices1 = []
udp.on("message", (msg, rinfo) => {
const buffer_req = Buffer.from(msg, "utf8");
console.log("20203", buffer_req);
console.log("20203", rinfo);
Logger.log("info", buffer_req.toString("hex"));
const buffer_req = Buffer.from(msg, "utf8")
console.log("20203", buffer_req)
console.log("20203", rinfo)
Logger.log("info", buffer_req.toString("hex"))
const now = moment().unix();
const me = LibDevice.identifyProtocolFromBuffer(buffer_req, { isEelinkCustom: 1 });
console.log("port 20203 ", me);
const now = moment().unix()
const me = LibDevice.identifyProtocolFromBuffer(buffer_req, { isEelinkCustom: 1 })
console.log("port 20203 ", me)
const logDevice = {
original_hex: me.ori_buffer,
@ -545,38 +608,38 @@ udp.on("message", (msg, rinfo) => {
crt_d_format: null,
crt_s: now,
crt_s_format: moment.unix(now).format("YYYY-MM-DD HH:mm:ss"),
};
}
if (me.protocol_name === "eelinkCustom") {
let dvc_id = devices1[`${rinfo.address}:${rinfo.port}`];
const act = LibDevice.eelinkCustomAction(me, dvc_id || null);
console.log("act 20203", act);
let dvc_id = devices1[`${rinfo.address}:${rinfo.port}`]
const act = LibDevice.eelinkCustomAction(me, dvc_id || null)
console.log("act 20203", act)
if (act.action_type == "exist_data") {
logDevice.action = act.action_type;
logDevice.device_id = act.device_id;
logDevice.action = act.action_type
logDevice.device_id = act.device_id
devices1[`${rinfo.address}:${rinfo.port}`] = act.device_id;
devices1[`${rinfo.address}:${rinfo.port}`] = act.device_id
if (typeof act.buffer_resp != "undefined") {
udp.send(act.buffer_resp, 0, act.buffer_resp.length, rinfo.port, rinfo.address, function (err, bytes) {
if (err) throw err;
console.log("UDP message sent to " + rinfo.address + ":" + rinfo.port);
console.log("20203", bytes);
});
if (err) throw err
console.log("UDP message sent to " + rinfo.address + ":" + rinfo.port)
console.log("20203", bytes)
})
}
}
}
});
})
udp.on("error", (err) => {
console.log(`udp error:\n${err.stack}`);
udp.close();
});
console.log(`udp error:\n${err.stack}`)
udp.close()
})
udp.on("close", function () {
console.log("udp socket is closed !");
});
console.log("udp socket is closed !")
})
udp.on("listening", () => {
const address = udp.address();
console.log(`udp listening ${address.address}:${address.port}`);
});
const address = udp.address()
console.log(`udp listening ${address.address}:${address.port}`)
})
udp.bind(process.env.PORT_UDP);
udp.bind(process.env.PORT_UDP)