-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_interface.js
86 lines (69 loc) · 1.85 KB
/
api_interface.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
const baseURL = "http://localhost:8080/"
async function stationExists(stat_id){
const data = await fetch(baseURL + "exists/" + stat_id,
{
method: 'GET'
});
// console.log(data);
const info = await data.json()
return info.exists;
}
async function deleteStation(stat_id){
const response = await fetch(baseURL + "delete/" + stat_id,
{
method: 'DELETE'
})
// console.log(response);
}
async function getWaitingTime(stat_id){
const data = await fetch(baseURL + "read/" + stat_id,
{
method: 'GET'
});
// console.log(data);
const info = await data.json()
if(info.waitingTime){
return info.waitingTime
}else{
return 0
}
// res.send(data);
}
async function updateWaitingTime(stat_id){
var chargingTime = EV.getChargingTime();
var currWaitingTime = await getWaitingTime(stat_id);
const curr_timestamp = Date.now()/1000
if (currWaitingTime < curr_timestamp){
var new_waiting_time = chargingTime + curr_timestamp;
EV.waitingTime = 100;
}else{
new_waiting_time = chargingTime + currWaitingTime;
EV.waitingTime = currWaitingTime - curr_timestamp;
}
// curr_stat.waitingTime = new_waiting_time - curr_timestamp;
// EV.waitingTime = new_waiting_time - curr_timestamp;
const res = await fetch(baseURL + 'update/' + stat_id, {
method: 'POST',
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
waitingTime: new_waiting_time
})
})
}
async function addStation(station){
const res = await fetch(baseURL + 'create', {
method: 'POST',
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
id: station.id,
name: station.name,
lat: station.lat,
lng: station.lng,
waitingTime: Date.now()/1000 + station.waitingTime
})
})
}