-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnection.js
142 lines (111 loc) · 2.94 KB
/
Connection.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
140
141
142
const express = require('express');
const app = express();
const server = require('http').createServer(app);
const io = require('socket.io')(server);
app.use(express.static(__dirname + '/node_modules'));
app.use("/styles", express.static(__dirname + '/public/stylesheets'));
app.use("/javascript", express.static(__dirname + '/public/javascript'));
app.use("/images", express.static(__dirname + '/public/images'));
app.get('/', function(req, res,next) {
res.sendFile(__dirname + '/public/index.html');
});
let alexa_response = undefined;
app.get('/otto-request', (req, res, next) => {
alexa_response = undefined;
const to_vehicle = send_to_vehicle(req.query);
if(to_vehicle){
broadcastToVehicles(req.query);
}
else {
broadcastToPhones('pong');
}
const promise = new Promise(resolve => {
//Check every 100 ms to see if the request has completed
function check(){
setTimeout(() => {
if(typeof alexa_response !== typeof undefined){
resolve();
}
else{
check();
}
}, 100);
}
check();
});
promise.then(() => {
res.send(alexa_response);
});
});
const phones = [];
const vehicles = [];
io.use((socket, next) => {
const handshake = socket.handshake;
const query = handshake.query;
console.log("query", query);
if(query.hasOwnProperty('vehicle')){
vehicles.push({
connection : {
vin : query.vin,
client : socket
}
});
}
else {
phones.push(socket);
}
next();
});
io.on('connection', function(client) {
console.log('Client connected...');
client.on('otto', data => {
console.log("message from gm received", data);
alexa_response = data;
});
client.on('location_request', data => {
console.log("to vehicles", data);
broadcastToVehicles({
"intent_name" : "NAV",
"address" : data
});
});
client.on('end', () => {
client.disconnect();
});
});
/**
* Broadcast a message to all connected phones
*
* @param data
*/
function broadcastToPhones(data){
console.log("to phones");
phones.forEach(phone => {
phone.emit("location_request", data);
});
}
/**
* Broadcast a message to all connected vehicles
*
* @param data
*/
function broadcastToVehicles(data){
vehicles.forEach(vehicle => {
vehicle.connection.client.emit("request", data);
});
}
/**
* Determine if we need to send a request to the vehicle or the phone.
*
* @param query
* @returns {boolean}
*/
function send_to_vehicle(query) {
const intent = query['intent_name'];
if(intent !== "NAV"){
return true;
}
const slot = JSON.parse(query['slots'])[0];
return slot['value'] !== "Zach";
}
server.listen(4200);