This commit is contained in:
meusinfirmary
2025-04-23 23:51:31 +07:00
parent b7e852126c
commit 46ec6c875e

241
app.js
View File

@ -1,31 +1,31 @@
require('dotenv').config({ path: '.env' }); require("dotenv").config({ path: ".env" });
require('events').EventEmitter.prototype._maxListeners = 30; require("events").EventEmitter.prototype._maxListeners = 30;
// start for gps-tracking // start for gps-tracking
const net = require('net'); const net = require("net");
const dgram = require('dgram'); const dgram = require("dgram");
const crc = require('crc'); const crc = require("crc");
const moment = require('moment'); const moment = require("moment");
const LibDevice = require('./library/LibDevice'); const LibDevice = require("./library/LibDevice");
const GpsTracksModels = require('./models/GpsTracksModels'); const GpsTracksModels = require("./models/GpsTracksModels");
const ZoneModels = require('./models/ZoneModels'); const ZoneModels = require("./models/ZoneModels");
const VhcModels = require('./models/VhcModels'); const VhcModels = require("./models/VhcModels");
const LibMail = require('./library/LibMail'); const LibMail = require("./library/LibMail");
const LibHelper = require('./library/LibHelper'); const LibHelper = require("./library/LibHelper");
const nanoid = require('nanoid').nanoid; const nanoid = require("nanoid").nanoid;
// end for gps-tracking // end for gps-tracking
// start for normal http request // start for normal http request
const express = require('express'); const express = require("express");
const LibBullAdapter = require('./library/LibBullAdapter'); // const LibBullAdapter = require('./library/LibBullAdapter');
const routes = require('./routes/routes'); const routes = require("./routes/routes");
// const routes = require('./routes/routes_without_scheduler'); // const routes = require('./routes/routes_without_scheduler');
const app = express(); const app = express();
// end for normal http request // end for normal http request
// start for logging // start for logging
const LibWinston = require('./library/LibWinston'); const LibWinston = require("./library/LibWinston");
const logName = 'libUdp'; const logName = "libUdp";
const Logger = LibWinston.initialize(logName); const Logger = LibWinston.initialize(logName);
// end for logging // end for logging
@ -40,7 +40,7 @@ async function commitMessage(now, logDevice) {
const vhc = await VhcModels.getVhcByDeviceId(logDevice.device_id); const vhc = await VhcModels.getVhcByDeviceId(logDevice.device_id);
const lastTrack = await GpsTracksModels.get2LastLocByDeviceId(logDevice.device_id); const lastTrack = await GpsTracksModels.get2LastLocByDeviceId(logDevice.device_id);
if (['heartbeat', 'alarm'].includes(logDevice.action)) { if (["heartbeat", "alarm"].includes(logDevice.action)) {
// set engine stts moving,idling,stopped // set engine stts moving,idling,stopped
if (logDevice.ignition == GpsTracksModels.STTS_IGNITION_HIGH) { if (logDevice.ignition == GpsTracksModels.STTS_IGNITION_HIGH) {
if (logDevice.speed) { if (logDevice.speed) {
@ -60,7 +60,7 @@ async function commitMessage(now, logDevice) {
} }
} }
// get last ignition, stts_engine, stts_alarm, stts_charge, stts_acc, stts_volt, stts_switch // get last ignition, stts_engine, stts_alarm, stts_charge, stts_acc, stts_volt, stts_switch
if (['location'].includes(logDevice.action)) { if (["location"].includes(logDevice.action)) {
// get last ignition // get last ignition
const lastHeartbeatOrAlarm = await GpsTracksModels.get2LastHeartbeatOrAlarm(logDevice.device_id); const lastHeartbeatOrAlarm = await GpsTracksModels.get2LastHeartbeatOrAlarm(logDevice.device_id);
if (lastHeartbeatOrAlarm.length > 0) { if (lastHeartbeatOrAlarm.length > 0) {
@ -99,7 +99,7 @@ async function commitMessage(now, logDevice) {
} }
// sekarang heartbeat diinject data lokasi juga dari lokasi terakhir // sekarang heartbeat diinject data lokasi juga dari lokasi terakhir
if (['heartbeat'].includes(logDevice.action)) { if (["heartbeat"].includes(logDevice.action)) {
if (lastTrack.length > 0) { if (lastTrack.length > 0) {
logDevice.latitude = lastTrack[0].latitude; logDevice.latitude = lastTrack[0].latitude;
logDevice.longitude = lastTrack[0].longitude; logDevice.longitude = lastTrack[0].longitude;
@ -121,9 +121,9 @@ async function commitMessage(now, logDevice) {
// logDevice.crt_d_format = lastTrack[0].crt_d_format; // logDevice.crt_d_format = lastTrack[0].crt_d_format;
// jika pengen di munculin di last movement // jika pengen di munculin di last movement
logDevice.crt = now; logDevice.crt = now;
logDevice.crt_format = moment.unix(now).format('YYYY-MM-DD HH:mm:ss'); logDevice.crt_format = moment.unix(now).format("YYYY-MM-DD HH:mm:ss");
logDevice.crt_d = now; logDevice.crt_d = now;
logDevice.crt_d_format = moment.unix(now).format('YYYY-MM-DD HH:mm:ss'); logDevice.crt_d_format = moment.unix(now).format("YYYY-MM-DD HH:mm:ss");
} }
} }
@ -140,18 +140,19 @@ async function commitMessage(now, logDevice) {
logDevice.pre_milleage = distance_km; logDevice.pre_milleage = distance_km;
logDevice.sum_milleage = (lastTrack[0].sum_milleage + logDevice.pre_milleage).toFixed(8); logDevice.sum_milleage = (lastTrack[0].sum_milleage + logDevice.pre_milleage).toFixed(8);
if (vhc.length > 0) { 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) { if (lastTrack[0].vhc_id == logDevice.vhc_id) {
logDevice.vhc_milleage = (lastTrack[0].vhc_milleage + logDevice.pre_milleage).toFixed(8); logDevice.vhc_milleage = (lastTrack[0].vhc_milleage + logDevice.pre_milleage).toFixed(8);
} else { } else {
logDevice.vhc_milleage = ((vhc[0]) ? vhc[0].sum_milleage : 0 + logDevice.pre_milleage).toFixed(8); logDevice.vhc_milleage = (vhc[0] ? 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 });
} }
} }
// truck zoning spawn // truck zoning spawn
if (logDevice.latitude != null && logDevice.longitude != null) { // && vhc.length > 0 if (logDevice.latitude != null && logDevice.longitude != null) {
// && vhc.length > 0
// log tracking // log tracking
const currTrack = await GpsTracksModels.bundleCreate2(logDevice, logDevice); const currTrack = await GpsTracksModels.bundleCreate2(logDevice, logDevice);
// console.log('GT06 HAS LOCATION AND CHECK ZONE'); // console.log('GT06 HAS LOCATION AND CHECK ZONE');
@ -173,9 +174,9 @@ async function commitMessage(now, logDevice) {
const lastSpawn = await GpsTracksModels.listSpawnZone({ const lastSpawn = await GpsTracksModels.listSpawnZone({
source: GpsTracksModels.SOURCE_GPS_TRACKER, source: GpsTracksModels.SOURCE_GPS_TRACKER,
device_id: logDevice.device_id, device_id: logDevice.device_id,
vhc_id: (vhc[0]) ? vhc[0].vid : 0, vhc_id: vhc[0] ? vhc[0].vid : 0,
leave_at_d: 0, leave_at_d: 0,
order_by: 'ORDER BY id DESC', order_by: "ORDER BY id DESC",
limit: 1, limit: 1,
}); });
@ -185,16 +186,19 @@ async function commitMessage(now, logDevice) {
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) { if (is_leave_zone === 1 && lastSpawn[0].device_id == logDevice.device_id) {
GpsTracksModels.updt2SpawnZone({ GpsTracksModels.updt2SpawnZone(
{
leave_lat: logDevice.latitude, leave_lat: logDevice.latitude,
leave_lng: logDevice.longitude, leave_lng: logDevice.longitude,
leave_at_d: logDevice.crt_d || now, leave_at_d: logDevice.crt_d || now,
leave_at_d_format: moment.unix(logDevice.crt_d || now).format('YYYY-MM-DD HH:mm:ss'), leave_at_d_format: moment.unix(logDevice.crt_d || now).format("YYYY-MM-DD HH:mm:ss"),
leave_at_s: now, leave_at_s: now,
leave_at_s_format: moment.unix(now).format('YYYY-MM-DD HH:mm:ss'), leave_at_s_format: moment.unix(now).format("YYYY-MM-DD HH:mm:ss"),
updt: now, updt: now,
updt_format: moment.unix(now).format('YYYY-MM-DD HH:mm:ss'), updt_format: moment.unix(now).format("YYYY-MM-DD HH:mm:ss"),
}, lastSpawn[0].id); },
lastSpawn[0].id
);
} }
} else { } else {
for (let zone of insideSpawnZone) { for (let zone of insideSpawnZone) {
@ -206,30 +210,30 @@ async function commitMessage(now, logDevice) {
z_workflow_name: zone.workflow_type_name, z_workflow_name: zone.workflow_type_name,
shiptocode: zone.shiptocode, shiptocode: zone.shiptocode,
z_fulladdress: zone.fulladdress, 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_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); // 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({ GpsTracksModels.create2SpawnZone({
device_id: logDevice.device_id, device_id: logDevice.device_id,
master_id: Number(currTrack.result.insertId), master_id: Number(currTrack.result.insertId),
enter_lat: logDevice.latitude, enter_lat: logDevice.latitude,
enter_lng: logDevice.longitude, enter_lng: logDevice.longitude,
enter_at_d: logDevice.crt_d || now, enter_at_d: logDevice.crt_d || now,
enter_at_d_format: moment.unix(logDevice.crt_d || now).format('YYYY-MM-DD HH:mm:ss'), enter_at_d_format: moment.unix(logDevice.crt_d || now).format("YYYY-MM-DD HH:mm:ss"),
enter_at_s: now, enter_at_s: now,
enter_at_s_format: moment.unix(now).format('YYYY-MM-DD HH:mm:ss'), enter_at_s_format: moment.unix(now).format("YYYY-MM-DD HH:mm:ss"),
zone_id: zone.zid, zone_id: zone.zid,
zone_name: zone.name, zone_name: zone.name,
vhc_id: (vhc[0]) ? vhc[0].vid : 0, vhc_id: vhc[0] ? vhc[0].vid : 0,
source: GpsTracksModels.SOURCE_GPS_TRACKER, source: GpsTracksModels.SOURCE_GPS_TRACKER,
crt: now, crt: now,
crt_format: moment.unix(now).format('YYYY-MM-DD HH:mm:ss'), 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 { } else {
@ -244,28 +248,31 @@ async function commitMessage(now, logDevice) {
// start for gps-tracking TCP ONLY // start for gps-tracking TCP ONLY
const devices = []; const devices = [];
const netConn = require('./config/netConn'); const netConn = require("./config/netConn");
/** /**
* uniquely identify a socket with Node.js * uniquely identify a socket with Node.js
* https://stackoverflow.com/questions/6805432/how-to-uniquely-identify-a-socket-with-node-js * https://stackoverflow.com/questions/6805432/how-to-uniquely-identify-a-socket-with-node-js
*/ */
net.createServer({ net.createServer(
{
allowHalfOpen: true, allowHalfOpen: true,
}, function (c) { // c mean connection },
function (c) {
// c mean connection
// save connections // save connections
c.id = nanoid(); c.id = nanoid();
c.pipe(c); c.pipe(c);
c.on('data', async (buffer_req) => { c.on("data", async (buffer_req) => {
const now = moment().unix(); const now = moment().unix();
const me = LibDevice.identifyProtocolFromBuffer(buffer_req); const me = LibDevice.identifyProtocolFromBuffer(buffer_req);
// console.log('app ', buffer_req); // console.log('app ', buffer_req);
const logDevice = { const logDevice = {
original_hex: me.ori_string, original_hex: me.ori_string,
protocol: (me.protocol_name == 'unknown') ? null : me.protocol_name, protocol: me.protocol_name == "unknown" ? null : me.protocol_name,
action: null, action: null,
device_id: null, device_id: null,
latitude: null, latitude: null,
@ -292,29 +299,28 @@ net.createServer({
vhc_id: 0, vhc_id: 0,
drv_id: 0, drv_id: 0,
crt: now, crt: now,
crt_format: moment.unix(now).format('YYYY-MM-DD HH:mm:ss'), crt_format: moment.unix(now).format("YYYY-MM-DD HH:mm:ss"),
crt_d: 0, crt_d: 0,
crt_d_format: null, crt_d_format: null,
crt_s: now, crt_s: now,
crt_s_format: moment.unix(now).format('YYYY-MM-DD HH:mm:ss'), crt_s_format: moment.unix(now).format("YYYY-MM-DD HH:mm:ss"),
}; };
if (me.protocol_name == 'gt06') { if (me.protocol_name == "gt06") {
// const act = LibDevice.gt06Action(me, device.device_id || null); // const act = LibDevice.gt06Action(me, device.device_id || null);
let dvc_id = devices[c.id]; let dvc_id = devices[c.id];
const act = LibDevice.gt06Action(me, dvc_id || null); const act = LibDevice.gt06Action(me, dvc_id || null);
if (act.action_type == 'login') { if (act.action_type == "login") {
logDevice.action = act.action_type; logDevice.action = act.action_type;
logDevice.device_id = act.device_id; logDevice.device_id = act.device_id;
devices[c.id] = act.device_id; devices[c.id] = act.device_id;
netConn[act.device_id] = c; netConn[act.device_id] = c;
if (typeof act.buffer_resp != 'undefined') { if (typeof act.buffer_resp != "undefined") {
c.write(act.buffer_resp); c.write(act.buffer_resp);
} }
} } else if (act.action_type == "location") {
else if (act.action_type == 'location') {
logDevice.action = act.action_type; logDevice.action = act.action_type;
logDevice.device_id = act.device_id; logDevice.device_id = act.device_id;
@ -328,23 +334,22 @@ net.createServer({
logDevice.length_gps = act.gps_data.quantity_pos_satellites_c; logDevice.length_gps = act.gps_data.quantity_pos_satellites_c;
logDevice.pos_stlt_gps = act.gps_data.quantity_pos_satellites_b; 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.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.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 = moment(act.gps_data.date).unix();
logDevice.crt_format = moment(act.gps_data.date).format('YYYY-MM-DD HH:mm:ss'); 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 = moment(act.gps_data.date).unix();
logDevice.crt_d_format = moment(act.gps_data.date).format('YYYY-MM-DD HH:mm:ss'); logDevice.crt_d_format = moment(act.gps_data.date).format("YYYY-MM-DD HH:mm:ss");
} } else if (act.action_type == "heartbeat") {
else if (act.action_type == 'heartbeat') {
logDevice.action = act.action_type; logDevice.action = act.action_type;
logDevice.device_id = act.device_id; logDevice.device_id = act.device_id;
logDevice.ignition = Number(act.stts_data.terminal_info.acc) ? GpsTracksModels.STTS_IGNITION_HIGH : GpsTracksModels.STTS_IGNITION_LOW; 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; // 1=>on, 2=>off logDevice.stts_gps = act.stts_data.terminal_info.gps_tracking ? GpsTracksModels.STTS_GPS_ON : GpsTracksModels.STTS_GPS_OFF; // 1=>on, 2=>off
logDevice.stts_gsm = Number(act.stts_data.gsm_signal_strength) + 1; // 1=>no signal, n>1=>get signal logDevice.stts_gsm = Number(act.stts_data.gsm_signal_strength) + 1; // 1=>no signal, n>1=>get signal
logDevice.stts_oil_electricity = (Number(act.stts_data.terminal_info.oil_electricity) === 0) ? GpsTracksModels.STTS_OIL_ELECTRIC_ON : GpsTracksModels.STTS_OIL_ELECTRIC_OFF; // 1=>on, 2=>off logDevice.stts_oil_electricity = Number(act.stts_data.terminal_info.oil_electricity) === 0 ? GpsTracksModels.STTS_OIL_ELECTRIC_ON : GpsTracksModels.STTS_OIL_ELECTRIC_OFF; // 1=>on, 2=>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) { if (stts_alarm == 0) {
@ -364,11 +369,10 @@ net.createServer({
logDevice.stts_switch = Number(act.stts_data.terminal_info.is_active) ? GpsTracksModels.STTS_SWITCH_ON : GpsTracksModels.STTS_SWITCH_OFF; 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_volt = Number(act.stts_data.voltage_level) + 1;
if (typeof act.buffer_resp != 'undefined') { if (typeof act.buffer_resp != "undefined") {
c.write(act.buffer_resp); c.write(act.buffer_resp);
} }
} } else if (act.action_type == "alarm") {
else if (act.action_type == 'alarm') {
logDevice.action = act.action_type; logDevice.action = act.action_type;
logDevice.device_id = act.device_id; logDevice.device_id = act.device_id;
@ -382,14 +386,14 @@ net.createServer({
logDevice.length_gps = act.gps_data.quantity_pos_satellites_c; logDevice.length_gps = act.gps_data.quantity_pos_satellites_c;
logDevice.pos_stlt_gps = act.gps_data.quantity_pos_satellites_b; 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.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.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.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; // 1=>on, 2=>off logDevice.stts_gps = act.stts_data.terminal_info.gps_tracking ? GpsTracksModels.STTS_GPS_ON : GpsTracksModels.STTS_GPS_OFF; // 1=>on, 2=>off
logDevice.stts_gsm = Number(act.stts_data.gsm_signal_strength) + 1; // 1=>no signal, n>1=>get signal logDevice.stts_gsm = Number(act.stts_data.gsm_signal_strength) + 1; // 1=>no signal, n>1=>get signal
logDevice.stts_oil_electricity = (Number(act.stts_data.terminal_info.oil_electricity) === 0) ? GpsTracksModels.STTS_OIL_ELECTRIC_ON : GpsTracksModels.STTS_OIL_ELECTRIC_OFF; // 1=>on, 2=>off logDevice.stts_oil_electricity = Number(act.stts_data.terminal_info.oil_electricity) === 0 ? GpsTracksModels.STTS_OIL_ELECTRIC_ON : GpsTracksModels.STTS_OIL_ELECTRIC_OFF; // 1=>on, 2=>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) { if (stts_alarm == 0) {
@ -410,11 +414,11 @@ net.createServer({
logDevice.stts_volt = Number(act.stts_data.voltage_level) + 1; logDevice.stts_volt = Number(act.stts_data.voltage_level) + 1;
logDevice.crt = moment(act.gps_data.date).unix(); 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_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 = moment(act.gps_data.date).unix();
logDevice.crt_d_format = moment(act.gps_data.date).format('YYYY-MM-DD HH:mm:ss'); logDevice.crt_d_format = moment(act.gps_data.date).format("YYYY-MM-DD HH:mm:ss");
if (typeof act.buffer_resp != 'undefined') { if (typeof act.buffer_resp != "undefined") {
c.write(act.buffer_resp); c.write(act.buffer_resp);
} }
} }
@ -427,26 +431,24 @@ net.createServer({
commitMessage(now, logDevice); commitMessage(now, logDevice);
}); });
c.on('end', () => { c.on("end", () => {});
}); c.on("close", () => {});
c.on('close', () => {
});
// unused // unused
c.on('drain', (a) => { c.on("drain", (a) => {
console.log('client drain', a); console.log("client drain", a);
}); });
c.on('error', (a) => { c.on("error", (a) => {
console.error('client error', a); console.error("client error", a);
}); });
c.on('lookup', (a) => { c.on("lookup", (a) => {
console.log('client lookup', a); console.log("client lookup", a);
}); });
c.on('ready', (a) => { c.on("ready", (a) => {
console.log('client ready', a); console.log("client ready", a);
}); });
c.on('timeout', (a) => { c.on("timeout", (a) => {
console.log('client timeout', a); console.log("client timeout", a);
}); });
// not work for gps tracker // not work for gps tracker
@ -459,45 +461,46 @@ net.createServer({
// c.end(); // c.end();
c.pipe(c); c.pipe(c);
}).listen(process.env.PORT, () => { }
console.log('Server gps tracker running at port ' + process.env.PORT); ).listen(process.env.PORT, () => {
console.log("Server gps tracker running at port " + process.env.PORT);
}); });
// end for gps-tracking TCP ONLY // end for gps-tracking TCP ONLY
// start for normal http request // start for normal http request
app.use(express.json({ limit: '10mb' })); // parsing application/json app.use(express.json({ limit: "10mb" })); // parsing application/json
app.use(express.urlencoded({ extended: true, limit: '10mb' })); // parsing application/x-www-form-urlencoded app.use(express.urlencoded({ extended: true, limit: "10mb" })); // parsing application/x-www-form-urlencoded
app.use(async function (req, res, next) { app.use(async function (req, res, next) {
// set control allowed headers // set control allowed headers
res.header('Access-Control-Allow-Origin', '*'); res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Methods', 'OPTIONS,GET,HEAD,PUT,PATCH,POST,DELETE'); 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'); res.header("Access-Control-Allow-Headers", "Accept, Authorization, Content-Type, X-Requested-With, Range, x-api-key, x-forwarded-for");
next(); next();
}); });
app.use(process.env.PATH_URL + '/bull/monitor', LibBullAdapter.getRouter()); app.use(process.env.PATH_URL + "/bull/monitor", LibBullAdapter.getRouter());
routes.use(app); routes.use(app);
app.listen(process.env.PORT_EXPRESS, () => { 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);
}); });
// end for normal http request // end for normal http request
// start for gps-tracking UDP ONLY // start for gps-tracking UDP ONLY
const udp = dgram.createSocket('udp4'); const udp = dgram.createSocket("udp4");
const devices1 = []; const devices1 = [];
udp.on('message', (msg, rinfo) => { udp.on("message", (msg, rinfo) => {
const buffer_req = Buffer.from(msg, 'utf8'); const buffer_req = Buffer.from(msg, "utf8");
console.log('20203', buffer_req); console.log("20203", buffer_req);
console.log('20203', rinfo); console.log("20203", rinfo);
Logger.log('info', buffer_req.toString('hex')); Logger.log("info", buffer_req.toString("hex"));
const now = moment().unix(); const now = moment().unix();
const me = LibDevice.identifyProtocolFromBuffer(buffer_req, {isEelinkCustom: 1}); const me = LibDevice.identifyProtocolFromBuffer(buffer_req, { isEelinkCustom: 1 });
console.log('port 20203 ', me); console.log("port 20203 ", me);
const logDevice = { const logDevice = {
original_hex: me.ori_buffer, original_hex: me.ori_buffer,
protocol: (me.protocol_name == 'unknown') ? null : me.protocol_name, protocol: me.protocol_name == "unknown" ? null : me.protocol_name,
action: null, action: null,
device_id: null, device_id: null,
latitude: null, latitude: null,
@ -522,43 +525,41 @@ udp.on('message', (msg, rinfo) => {
pre_milleage: 0, pre_milleage: 0,
source: GpsTracksModels.SOURCE_GPS_TRACKER, source: GpsTracksModels.SOURCE_GPS_TRACKER,
crt: now, crt: now,
crt_format: moment.unix(now).format('YYYY-MM-DD HH:mm:ss'), crt_format: moment.unix(now).format("YYYY-MM-DD HH:mm:ss"),
crt_d: 0, crt_d: 0,
crt_d_format: null, crt_d_format: null,
crt_s: now, crt_s: now,
crt_s_format: moment.unix(now).format('YYYY-MM-DD HH:mm:ss'), crt_s_format: moment.unix(now).format("YYYY-MM-DD HH:mm:ss"),
}; };
if (me.protocol_name === 'eelinkCustom') { if (me.protocol_name === "eelinkCustom") {
let dvc_id = devices1[`${rinfo.address}:${rinfo.port}`]; let dvc_id = devices1[`${rinfo.address}:${rinfo.port}`];
const act = LibDevice.eelinkCustomAction(me, dvc_id || null); const act = LibDevice.eelinkCustomAction(me, dvc_id || null);
console.log('act 20203', act); console.log("act 20203", act);
if (act.action_type == 'exist_data') { if (act.action_type == "exist_data") {
logDevice.action = act.action_type; logDevice.action = act.action_type;
logDevice.device_id = act.device_id; 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') { if (typeof act.buffer_resp != "undefined") {
udp.send(act.buffer_resp, 0, act.buffer_resp.length, rinfo.port, rinfo.address, function(err, bytes) { udp.send(act.buffer_resp, 0, act.buffer_resp.length, rinfo.port, rinfo.address, function (err, bytes) {
if (err) throw err; if (err) throw err;
console.log('UDP message sent to ' + rinfo.address +':'+ rinfo.port); console.log("UDP message sent to " + rinfo.address + ":" + rinfo.port);
console.log('20203', bytes); console.log("20203", bytes);
}); });
} }
} }
} }
}); });
udp.on('error', (err) => { udp.on("error", (err) => {
console.log(`udp error:\n${err.stack}`); console.log(`udp error:\n${err.stack}`);
udp.close(); udp.close();
}); });
udp.on('close',function(){ udp.on("close", function () {
console.log('udp socket is closed !'); console.log("udp socket is closed !");
}); });
udp.on('listening', () => { udp.on("listening", () => {
const address = udp.address(); const address = udp.address();
console.log(`udp listening ${address.address}:${address.port}`); console.log(`udp listening ${address.address}:${address.port}`);
}); });