This repository was archived by the owner on Jun 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
140 lines (114 loc) · 2.8 KB
/
index.js
File metadata and controls
140 lines (114 loc) · 2.8 KB
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
140
/*
Settings
*/
const PORT = process.env.port || 3000;
/*
Modules
*/
const express = require('express');
const http = require('http');
const socketio = require('socket.io');
const fetch = require('node-fetch');
/*
Server Setup
*/
const app = express();
const server = http.Server(app);
const io = socketio(server);
server.listen(PORT, () => {
console.log(`Server started on port ${PORT}`);
});
/*
Express Methods
*/
const latestRates = {};
const userNames = {};
const contacts = {};
app.get('/', (req, res) => {
res.send(latestRates);
});
app.post('/register', (req, res) => {
const { userId, userName } = req.query;
userNames[userId] = userName;
res.sendStatus(200);
});
app.get('/user', (req, res) => {
const { userId } = req.query;
res.send(userNames[userId] || userId);
});
app.get('/contacts', (req, res) => {
const { userId } = req.query;
res.send(contacts[userId] || {});
});
app.post('/contacts', (req, res) => {
const { userId, contactName, contactPhone } = req.query;
if (!contacts[userId]) {
contacts[userId] = [];
}
contacts[userId].push({
name: contactName,
phone: contactPhone
});
res.sendStatus(200);
});
app.get('/bpm', (req, res) => {
const { userId } = req.query;
if (latestRates[userId]) {
res.send(latestRates[userId]);
} else {
res.sendStatus(400);
}
});
app.post('/bpm', (req, res) => {
const { heartRate, userId } = req.query;
if (!heartRate || !userId) {
res.sendStatus(400);
}
// Store heart rate.
latestRates[userId] = heartRate;
// Emit rate.
const socket = io.sockets.connected[connections[userId]];
if (socket) {
socket.emit('heartRate', heartRate);
}
res.sendStatus(200);
});
app.post('/alert', (req, res) => {
const { userId } = req.query;
if (!userId) {
res.sendStatus(400);
}
const name = userNames[userId] || userId;
const number = '6502195319';
const msg = `Hey! ${name} is experiencing withdrawal symptoms and could really use your support! Please call ${name} ASAP!`
fetch(`https://unitingdust.api.stdlib.com/examples-twilio@dev/?tel=${number}&body=${encodeURIComponent(msg)}`, {
method: 'GET',
mode: 'cors',
credentials: 'omit'
})
.then(() => {
console.log('Sent to Twilio.');
res.sendStatus(200);
})
.catch(err => {
console.error(err);
res.sendStatus(400);
});
});
/*
Socket.io Methods
*/
const connections = {}; // Key: User email, Value: Socket ID.
io.on('connection', socket => {
socketLogger(socket, 'Connected.');
// Login user.
socket.on('login', email => {
connections[email] = socket.id;
socketLogger(socket, `Logged in as ${email}`);
// Emit last heart rate immediately.
socket.emit('heartRate', latestRates[email] || 0);
});
});
socketLogger = (socket, message) => {
console.log(`[${socket.id}] ${message}`);
};