-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.js
172 lines (149 loc) · 3.85 KB
/
app.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
var express = require('express'),
http = require('http'),
axios = require('axios'),
path = require("path");
let FLOW_URL = "REPLACE_ME_WITH_WEBHOOK_URL";
let PORT = 3000;
let API_URL = "https://customer-education.postmanlabs.com/workshops/flows";
let DEMO_API_KEY = 1234;
var app = express();
var server = http.createServer(app);
app.use(express.static('public'));
app.use(express.urlencoded({ extended: true }));
app.get('/', function(req,res){
res.sendFile(path.join(__dirname,'./public/index.html'));
});
//Climatize
app.post('/climatize', async(req,res)=>{
var payload = JSON.stringify({
"vin": req.body.vin,
"temperature": req.body.temp
})
var conf = {
method: 'post',
url: FLOW_URL,
timeout: 8000,
headers: {
'X-API-Key': DEMO_API_KEY
},
data: payload
};
try {
var resp = await axios(conf);
res.send("Vehicle climatization has started!");
} catch (error) {
console.log(error);
if(error.response.status == 404) {
res.send("Vehicle not found.");
} else {
console.log(error);
res.send("Something went wrong");
}
}
});
//Create
app.post('/create', async(req,res)=>{
var payload = JSON.stringify({
"running": req.body.status == "on",
"temperature": req.body.temp
})
var conf = {
method: 'post',
url: API_URL + '/vehicles/',
timeout: 8000,
headers: {
'X-API-Key': DEMO_API_KEY,
'Content-Type': "application/json"
},
data: payload
};
try {
var resp = await axios(conf);
res.send(resp.data);
} catch (error) {
console.log(error);
if(error.response.status == 404) {
res.send("Vehicle was not created.");
} else {
console.log(error);
res.send("Something went wrong");
}
}
});
//Engine
app.post('/engine', async(req,res) => {
var conf = {
method: 'get',
url: API_URL + '/vehicles/' + req.body.vin + '/engine/status',
timeout: 8000,
headers: {
'X-API-Key': DEMO_API_KEY
}
};
try {
var resp = await axios(conf);
res.send(resp.data);
} catch (error) {
console.log(error);
if(error.response.status == 404) {
res.send("Vehicle not found.");
} else {
console.log(error);
res.send("Something went wrong");
}
}
});
//Climate
app.post('/climate', async(req,res) => {
var conf = {
method: 'get',
url: API_URL + '/vehicles/' + req.body.vin + '/interior/climate',
timeout: 8000,
headers: {
'X-API-Key': DEMO_API_KEY
}
};
try {
var resp = await axios(conf);
res.send(resp.data);
} catch (error) {
console.log(error);
if(error.response.status == 404) {
res.send("Vehicle not found.");
} else {
console.log(error);
res.send("Something went wrong");
}
}
});
//Engine On
app.post('/engine_status', async(req,res) => {
if(req.body.status == "on") {
var URL_END = '/engine/start';
} else {
var URL_END = '/engine/stop';
}
var conf = {
method: 'get',
url: API_URL + '/vehicles/' + req.body.vin + URL_END,
timeout: 8000,
headers: {
'X-API-Key': DEMO_API_KEY
}
};
try {
var resp = await axios(conf);
res.send(resp.data);
} catch (error) {
console.log(error);
if(error.response.status == 404) {
res.send("Vehicle not found.");
} else {
console.log(error);
res.send("Something went wrong");
}
}
});
server.listen(PORT, () => {
console.log("Server listening on port: " + PORT);
});