forked from thebookins/transpector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
139 lines (115 loc) · 3.81 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
var express = require("express");
var bodyParser = require("body-parser");
const socketIO = require('socket.io');
// const Scanner = require('./scanner');
const Transmitter = require('./transmitter');
const transmitters = [];
var TRANSMITTERS = [
{ id: '400000', nickname: 'G5 1' },
{ id: '400001', nickname: 'G5 2' },
{ id: '400002', nickname: 'G5 3' },
{ id: '400003', nickname: 'G5 4' },
{ id: '400004', nickname: 'G5 5' },
{ id: '400005', nickname: 'G5 6' },
{ id: '400006', nickname: 'G5 7' },
{ id: '400007', nickname: 'G5 8' },
{ id: '400008', nickname: 'G5 9' },
{ id: '400009', nickname: 'G5 10' }
];
// var mongodb = require("mongodb");
// var ObjectID = mongodb.ObjectID;
// var CONTACTS_COLLECTION = "contacts";
var app = express();
app.use(bodyParser.json());
// Create link to Angular build directory
var distDir = __dirname + "/dist/transmitter-inspector/";
app.use(express.static(distDir));
// // Create a database variable outside of the database connection callback to reuse the connection pool in your app.
// var db;
var server = app.listen(process.env.PORT || 8080, function () {
var port = server.address().port;
console.log("App now running on port", port);
});
const io = socketIO(server);
io.on('connection', (socket) => {
console.log('Client connected');
socket.on('disconnect', () => console.log('Client disconnected'));
});
// TransmitterIO(io.of('/cgm')); // TODO: not sure if we need namespaces
//const transmitterIO = TransmitterIO(io); // TODO: not sure if we need namespaces
// Scanner(io);
transmitters.push(Transmitter('41MLX0', io));
transmitters.push(Transmitter('41N7MG', io));
transmitters.push(Transmitter('41QEU1', io));
// API ROUTES BELOW
// Generic error handler used by all endpoints.
function handleError(res, reason, message, code) {
console.log("ERROR: " + reason);
res.status(code || 500).json({"error": message});
}
/* "/api/transmitters"
* GET: finds all transmitters
* POST: creates a new transmitter
*/
app.get("/api/transmitters", function(req, res) {
const status = [];
transmitters.forEach((transmitter) => {
status.push(transmitter.status);
});
res.status(200).json(status);
});
app.post("/api/transmitters", function(req, res) {
var id = req.body.id;
if (!id) {
handleError(res, "Invalid user input", "Must provide an ID.", 400);
} else {
const newTransmitter = Transmitter(id, io)
transmitters.push(newTransmitter);
res.status(201).json(newTransmitter.status);
}
});
/* "/api/transmitters/:id"
* GET: find transmitter by id
* PUT: update transmitter by id
* DELETE: deletes transmitter by id
*/
app.get("/api/transmitters/:id", function(req, res) {
// put this method in TransmitterIO
var transmitter = transmitters.find(function(e) {
return req.params.id === e.id;
});
if (transmitter) {
res.status(200).json(transmitter.status);
} else {
handleError(res, "blah", "Failed to get transmitter");
}
});
app.put("/api/transmitters/:id", function(req, res) {
var command = req.body;
var transmitter = transmitters.find(function(e) {
return req.params.id === e.id;
});
if (transmitter) {
transmitter.sendCommand(command);
res.status(200).json(transmitter.status);
} else {
handleError(res, "blah", "Failed to get transmitter");
}
});
app.delete("/api/transmitters/:id", function(req, res) {
// TODO: handle errors
var transmitter = transmitters.find(function(e) {
return req.params.id === e.id;
});
// stop listening for this transmitter
transmitter.cleanup();
const idx = transmitters.findIndex(e => {
return req.params.id === e.id;
})
transmitters.splice(idx, 1);
res.status(200).send();
});
// prevent error message on reloads as per https://stackoverflow.com/a/35284602
app.get('/*', function(req, res){
res.sendFile(distDir + '/index.html');
});