Files
gps-backend/app.js.gps
2025-05-29 08:59:40 +00:00

98 lines
3.3 KiB
Plaintext
Executable File

require('dotenv').config({ path: '.env' });
const gps = require('gps-tracking');
const express = require('express');
const LibBullAdapter = require('./library/LibBullAdapter');
const routes = require('./routes/routes');
const app = express();
// start for gps-tracking
const options = {
'debug': true,
'port': process.env.PORT,
'device_adapter': "ET200"
}
const server = gps.server(options, function (device, connection) {
/* Available device variables:
----------------------------
device.uid -> Set when the first packet is parsed
device.name -> You can set a custon name for this device.
device.ip -> IP of the device
device.port --> Device port
*/
// console.log('start: 0');
// console.log(device);
// console.log('end: 0');
/****************************** LOGIN ******************************/
device.on("login_request", function (device_id, msg_parts) {
// console.log('start: login_request');
// console.log({device_id, msg_parts});
//Do some stuff before authenticate the device...
// This way you can prevent from anyone to send their position without your consent
this.login_authorized(true, msg_parts); //Accept the login request.
// console.log('end: login_request');
});
device.on("login", function () {
// console.log('start: login');
// console.log("Hi! i'm " + device.uid);
// console.log('end: login');
});
device.on("login_rejected", function () {
console.log('start: login_rejected');
console.log('end: login_rejected');
});
/****************************** PING - When the gps sends their position ******************************/
device.on("ping", function (data) {
// //After the ping is received
// console.log('start: ping');
// console.log(data);
// // console.log("I'm here now: " + gps_data.latitude + ", " + gps_data.longitude);
// console.log('end: ping');
return data;
});
/****************************** ALARM - When the gps sends and alarm ******************************/
device.on("alarm", function (alarm_code, alarm_data, msg_data) {
// console.log('start: alarm');
// console.log(msg_data);
// console.log("Help! Something happend: " + alarm_code + " (" + alarm_data.msg + ")");
// console.log('end: alarm');
//call_me();
});
/****************************** MISC ******************************/
device.on("handshake", function () {
// console.log('start: handshake');
// console.log('end: handshake');
});
});
server.setDebug(true);
// end for gps-tracking
// start for normal http request
app.use(express.json({ limit: '1mb' })); // parsing application/json
app.use(express.urlencoded({ extended: true })); // parsing application/x-www-form-urlencoded
app.use(async function (req, res, next) {
// set control allowed headers
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();
});
app.use(process.env.PATH_URL + '/bull/monitor', LibBullAdapter.getRouter());
routes.use(app);
app.listen(process.env.PORT_EXPRESS, () => {
console.log('Express server running at port ' + process.env.PORT_EXPRESS);
});
// end for normal http request