forked from PDXostc/rvi_alexa_demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
179 lines (133 loc) · 4.31 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* Copyright (C) 2016, Jaguar Land Rover. All Rights Reserved.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
var https = require('https');
var fs = require('fs');
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var knex = require('knex')({
client: 'mysql',
connection: {
host : 'localhost',
user : 'username',
password : 'password',
}
});
var nodeName = "f-type";
var placesAPIKey = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
var places = require('googleplaces-promises')(placesAPIKey);
//Provide valid SSL certificates for your server in the 'certs' directory.
var sslOptions = {
key: fs.readFileSync('certs/private-key.pem'),
cert: fs.readFileSync('certs/certificate.pem')
};
var httpsServer = https.createServer(sslOptions,app);
httpsServer.listen(443,function(){console.log("Server listening.")});
app.use(bodyParser.json());
app.post('/',handleRequest);
function handleRequest(req,res){
if(req.body.request.intent.name == undefined){
alexaResponse.response.outputSpeech.text = "There was an error with Vehicle interaction";
res.json(alexaResponse);
}
switch(req.body.request.intent.name){
case "CarLocation":
returnLocation(req,res);
break;
case "CarHVAC":
manageClimate(req,res);
break;
}
}
function manageClimate(req,res){
//add to the end of the endpoint: temp_right, temp_left
var nodeEndpoint = 'jlr.com/vin/'+nodeName+'/hvac/';
//Add the value to set to at the end of this
var nodeVal = "value=";
nodeVal += req.body.request.intent.slots.temperature.value;
var PythonShell = require('python-shell');
var options = {
mode: 'text',
scriptPath: '/home/rvi/rvi_0.4.0/python/',
args: [nodeEndpoint+"temp_left",nodeVal]
};
PythonShell.run('rvi_call.py', options, function (err, results) {
if (err) throw err;
// results is an array consisting of messages collected during execution
console.log('results: %j', results);
});
options.args = [nodeEndpoint+"temp_right",nodeVal];
PythonShell.run('rvi_call.py', options, function (err, results) {
if (err) throw err;
// results is an array consisting of messages collected during execution
console.log('results: %j', results);
});
alexaResponse.response.outputSpeech.text = "I have set your vehicle's cabin temperature to "+req.body.request.intent.slots.temperature.value + " degrees";
res.json(alexaResponse);
}
function returnLocation(req,res){
var bigData;
var placeInfo;
//Get data from BigData DB
var query = knex.select('loc_time','loc_latitude','loc_longitude','loc_speed')
.from('rvi.tracking_location')
.orderBy('loc_time','desc')
.limit(1);
query.then(function(bd){
bigData = bd;
var location_ops = {
location:[
bigData[0].loc_latitude,
bigData[0].loc_longitude
],
radius : 305,
rankby: "prominence"
}
return places.placeSearch(location_ops);
}).then(function(place_info){
console.log(place_info);
alexaResponse.response.outputSpeech.text = generateResponse(bigData[0],place_info.results);
res.json(alexaResponse);
});
}
//Replaces '&' with 'and', as '&' triggers SSML interpreting.
function clearAmps(input){
return input.replace("&","and");
}
function generateResponse(bdData, mapsData){
var text = "Your vehicle is currently";
text += " in "+mapsData[0].name + " near";
text += " " + mapsData[1].name + ", " + mapsData[2].name + ", and " + mapsData[3].name + ".";
if(bdData.loc_speed > 0){
text += " It is currently driving.";
}else{
text += " It is currently stopped.";
}
return text;
}
var alexaResponse = {
"version": "1.0",
"sessionAttributes": {},
"response": {
"outputSpeech": {
"type": "PlainText",
"text": "Your vehicle currently at "
},
"card": {
"type": "Simple",
"title": "Jaguar Land Rover RVI Demo",
"content": "The JLR RVI Demo was called. "
},
"reprompt": {
"outputSpeech": {
"type": "PlainText",
"text": "Can I help you with anything else?"
}
},
"shouldEndSession": true
}
}