-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathState.cpp
211 lines (176 loc) · 5.31 KB
/
State.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/**
* @brief The state class which represents the state parameters for a particular light
* @author Gurkiran Tatla
* @author Jake Schindler
* @author Justine Kim
* @author Paul Salvatore
* @author Timal Peramune
*/
#include <iostream>
#include "State.hpp"
/**
* State
* @brief constructor for the state variable
*
* @param isOn: boolean saying if light is on or off
* @param brightness: int representing brightness
* @param hue: long long representing hue
* @param saturation: int representing saturation
* @param light: Light object
* @return N/A
*/
State::State(bool isOn, int brightness, long long hue, int saturation, Light *light) : isOn(isOn), brightness(brightness), hue(hue), saturation(saturation) {
this->light = light;
}
State::State(bool isOn, int brightness, long long hue, int transitionTime): isOn(isOn), brightness(brightness), hue(hue), transitionTime(transitionTime) {
this->isOn = isOn;
this->brightness = brightness;
this->hue = hue;
this->transitionTime = transitionTime;
}
/**
* ~State()
* @brief destructor for state class
*
* @param N/A
* @return N/A
*/
State::~State(){
}
/**
* getON
* @brief retrieves the state and returns it as a bool. it takes in no parameters.
*
* @param N/A
* @return bool representing if the light is on or off
*/
bool State::isON() {
return this->isOn;
}
/**
* getBrightness
* @brief retrieves the brightness and returns it as an int. it takes in no parameters.
*
* @param N/A
* @return int representing brightness of the light
*/
int State::getBrightness() {
return this->brightness;
}
/**
* getHue
* @brief retrieves the hue and returns it as a long long. it takes in no parameters.
*
* @param N/A
* @return long long representing hue of the light
*/
long long State::getHue() {
return this->hue;
}
/**
* getSaturation()
* @brief retrieves the saturation and returns it as an int. it takes in no parameters.
*
* @param N/A
* @return int representing saturation of the light
*/
int State::getSaturation() {
return this->saturation;
}
/**
* getTransitionTime
* @brief retrieves the transition time for the state
*
* @param none
* @return an integer representing the transition time in seconds
*/
int State::getTransitionTime(){
return this->transitionTime;
}
/**
* setON()
* @brief changes the light to turn on. it takes no parameters and has no return value.
*
* @param N/A
* @return N/A
*/
void State::setON(std::string ip, std::string port) {
Wt::WIOService *ioservice = new Wt::WIOService();
ioservice->start();
Wt::Http::Client *client = new Wt::Http::Client(*ioservice);
Wt::Http::Message message = Wt::Http::Message();
message.addHeader("Content-type", "application/json");
message.addBodyText("{\"on\":true}");
client->put(ip + ":" + port + "/api/newdeveloper/lights/" +
this->light->getID() + "/state",
message);
ioservice->stop();
this->isOn = true;
}
/**
* setOFF()
* @brief turn the light off. it takes no parameters and has no return value.
*
* @param N/A
* @return N/A
*/
void State::setOFF(std::string ip, std::string port) {
Wt::WIOService *ioservice = new Wt::WIOService();
ioservice->start();
Wt::Http::Client *client = new Wt::Http::Client(*ioservice);
Wt::Http::Message message = Wt::Http::Message();
message.addHeader("Content-type", "application/json");
message.addBodyText("{\"on\":false}");
client->put(ip + ":" + port + "/api/newdeveloper/lights/" +
this->light->getID() + "/state",
message);
ioservice->stop();
this->isOn = false;
}
/**
* setHue
* @brief sets the hue of a light, it takes in long long hue and has no return value.
*
* @param hue: long long representing the hue
* @return N/A
*/
void State::setHue(long long hue, std::string ip, std::string port) {
Wt::WIOService *ioservice = new Wt::WIOService();
ioservice->start();
Wt::Http::Client *client = new Wt::Http::Client(*ioservice);
// message->addBodyText("{\"name\": "+name+"}");
Wt::Http::Message message = Wt::Http::Message();
message.addHeader("Content-type", "application/json");
// Wt::Json::Object tempObj;
// tempObj["hue"] = hue;
// message.addBodyText(Wt::Json::serialize(tempObj));
std::string tempObj= "{hue: " + std::to_string(hue) + "}";
message.addBodyText(tempObj);
client->put(ip + ":" + port + "/api/newdeveloper/lights/" +
this->light->getID() + "/state",
message);
ioservice->stop();
this->hue = hue;
}
/**
* setBrightness
* @brief changes the brightness of a light, it takes in int brightness and a parameter and has no return type
*
* @param brightness: int representing the brightness
* @return N/A
*/
void State::setBrightness(int brightness, std::string ip, std::string port) {
Wt::WIOService *ioservice = new Wt::WIOService();
ioservice->start();
Wt::Http::Client *client = new Wt::Http::Client(*ioservice);
Wt::Http::Message message = Wt::Http::Message();
message.addHeader("Content-type", "application/json");
std::string tempObj= "{bri: " + std::to_string(brightness) + "}";
message.addBodyText(tempObj);
client->put(ip + ":" + port + "/api/newdeveloper/lights/" + this->light->getID() + "/state", message);
ioservice->stop();
this->brightness = brightness;
}
void State::setTransitionTime(int transitionTime){
this->transitionTime = transitionTime;
}