-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.cpp
158 lines (128 loc) · 4.5 KB
/
routes.cpp
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
#include <HTTPClient.h>
#include <Arduino.h>
#include <WiFi.h>
#include "routes.h"
#include <Arduino_JSON.h>
String Routes::request(String url) {
http.begin(url);
int httpCode = http.GET();
if (httpCode > 0) {
if (httpCode == HTTP_CODE_OK) {
return http.getString();
}
}
return "Error" + String(httpCode) ;
}
// Retrieve all radio stations
String Routes::AllStations() {
String payload;
payload = request(baseURL + "stations");
JSONVar getRadioInfo = JSON.parse(payload);
String radioArr = JSON.stringify(getRadioInfo["data"]);
return radioArr;
}
// Retrieve data of a station
String Routes::GetRadioInfo(const int stationID) {
String payload;
payload = request(baseURL + "stations/" + stationID);
Serial.print(payload);
JSONVar output = JSON.parse(payload);
Serial.println("output");
Serial.print(output);
String name = output["data"]["output"];
return name;
}
// Retrieve data of a station
String Routes::GetPlayingStation() {
Serial.print("test");
String payload;
payload = request(baseURL + "stations?is_playing=1" );
Serial.print(payload);
JSONVar output = JSON.parse(payload);
Serial.println(output["data"][0]);
String name = output["data"][0]["output"];
return name;
}
// Retrieve data of a station
String Routes::GetRandomRadio() {
String payload;
payload = request(baseURL + "stations/discover");
JSONVar output = JSON.parse(payload);
String name = output["data"]["output"];
return name;
}
// Send data to front-end about currently playing station
String Routes::PutRadioInfo(const int stationID) {
http.begin(baseURL + "stations/" + stationID);
int httpCode = http.PUT("playing=1");
if (httpCode > 0) {
if (httpCode == HTTP_CODE_OK) {
return http.getString();
}
}
return "Error";
}
// Get all favorites
String Routes::GetFavorites(const int user_id) {
String payload;
payload = request(baseURL + "users/" + user_id + "/favourites");
JSONVar parsedFavorites= JSON.parse(payload);
String favoritesArr = JSON.stringify(parsedFavorites["data"]);
return favoritesArr;
}
// Add favorite to database
String Routes::PostNewFavorite(const int user_id, const int station_id) {
http.begin(baseURL + "users/" + user_id + "/favourites");
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
String payload = "station=";
payload.concat(station_id);
payload.concat("favourite=");
payload.concat(1);
Serial.print(payload);
int httpCode = http.PUT(payload);
if (httpCode > 0) {
if (httpCode == HTTP_CODE_OK) {
Serial.print("Added favorite");
return http.getString();
}
}
return "Error toevoegen niet gelukt";
}
// Delete favorite from database
String Routes::DeleteFavorite(const int user_id, const int favorite_id) {
http.begin(baseURL + "users/" + user_id + "/favourites/" + favorite_id);
int httpCode = http.sendRequest("DELETE");
if (httpCode > 0) {
if (httpCode == HTTP_CODE_OK) {
return http.getString();
}
}
return "Error verwijderen niet gelukt";
}
// Get all settings for a user
int Routes::GetSettings(const int user_id) {
String payload;
// String name;
payload = request(baseURL + "users/" + user_id + "/settings");
JSONVar output = JSON.parse(payload);
int parsed = output["data"]["volume"];
return parsed;
}
// PUT /api/users/{user_id}/settings?volume={volume}
// Update the volume
String Routes::UpdateVolume(const int user_id, const int volume) {
http.begin(baseURL + "users/" + user_id + "/settings");
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
String payload = "volume=";
payload.concat(volume);
Serial.println(payload);
int httpCode = http.POST(payload);
if (httpCode > 0) {
if (httpCode == HTTP_CODE_OK) {
Serial.println(http.getString());
return http.getString();
}
}
Serial.println("ik ben hier");
return "Error";
}