-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
82 lines (63 loc) · 2.67 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
Ruptela Uploader
Copyright (C) 2022 Jose Castellanos Molina
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
const express = require('express');
const multer = require("multer");
const events = require('events');
const config = require('config');
const uploader = require('./ruptela_uploader.js');
const app = express();
const port = config.get('server.local_port');
app.use(express.json());
// Procesar el formato x-www-form-urlencoded
app.use(express.urlencoded({
extended:true
}));
const myEmitter = new events.EventEmitter();
const cc = new uploader.RuptelaUploader(config.get('server.traccar_host'),
config.get('server.traccar_port'),
config.get('server.traccar_authorization'),
myEmitter);
// instancia de multer
const upload = multer({
// Dirección de directorio almacenada
dest: config.get('server.local_upload_dir')
});
/**
* Listens for the event scomming from traccar, so we can identify the different states of the device we are updating
*/
app.all('/traccar_events', (req, res) => {
var event = null;
var device = null;
var users = [];
req.body.hasOwnProperty('event') ? event = req.body.event : event = null;
req.body.hasOwnProperty('device') ? device = req.body.device : device = null;
req.body.hasOwnProperty('users') ? users = req.body.users : users = [];
if ( event != null && device != null ) {
//console.log("Event " + event.type );
myEmitter.emit( event.type, req.body );
}
res.send('OK');
})
app.post('/configuration', upload.single("file") , (req, res) => {
cc.sendConfigurationFile(req.body.device, req.file.path );
res.status(202).send({ message: 'Message accepted' });
})
app.get('/configuration', (req, res) => {
cc.readConfigurationFile(req.query.device);
res.status(202).send({ message: 'Message accepted' });
})
app.listen(port, () => {
console.log(`Connector listening at http://localhost:${port}`)
})