-
Notifications
You must be signed in to change notification settings - Fork 1
/
ScheduleItem.cpp
162 lines (137 loc) · 4.34 KB
/
ScheduleItem.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
/**
* @brief The scheduleitem class which represents a scheduled event to send to the hue api
* @author Gurkiran Tatla
* @author Jake Schindler
* @author Justine Kim
* @author Paul Salvatore
* @author Timal Peramune
*/
#include "ScheduleItem.hpp"
/**
* ScheduleItem
* @brief constructor for ScheduleItem class
*
* @param none
* @return none
*/
ScheduleItem::ScheduleItem(){
}
/**
* ~ScheduleItem
* @brief deconstructor for ScheduleItem class
*
* @param none
* @return none
*/
ScheduleItem::~ScheduleItem(){
}
/**
* setState
* @brief setter for the state variable
*
* @param state a state variable with the schedule state
* @return none
*/
void ScheduleItem::setState(State* state){
this->state = state;
}
/**
* setTime
* @brief setter for the time variable
*
* @param time a tm variable with the schedule time
* @return none
*/
void ScheduleItem::setTime(tm* time){
this->time = time;
}
/**
* sendSchedule
* @brief makes an api call scheduling the specified light/group id at the stored time with the stored state
*
* @param id an integer representing the id to schedule
* @param group an integer (0 or 1) representing if we are scheduling a light or group, respectively
* @return none
*/
void ScheduleItem::sendSchedule(int id, int group, std::string ip, std::string port){
std::cout << "sending sch" << std::endl;
Wt::WIOService *ioservice = new Wt::WIOService();
ioservice->start();
Wt::Http::Client *client = new Wt::Http::Client(*ioservice);
client->done().connect(boost::bind(&ScheduleItem::handleHttpResponseSchedule, this, _1, _2));
// Build the http message
Wt::Http::Message message = Wt::Http::Message();
message.addHeader("Content-type", "application/json");
message.addBodyText(buildMessage(id, group));
std::cout << buildMessage(id, group) << std::endl;
client->post(ip + ":" + port + "/api/newdeveloper/schedules", message);
ioservice->stop();
delete ioservice;
}
/**
* handleHttpResponseSchedule
* @brief prints the response of sending the schedule
*
* @param err a boost error response variable
* @param response a wt http response variable
* @return none
*/
void ScheduleItem::handleHttpResponseSchedule(boost::system::error_code err, const Wt::Http::Message &response) {
std::cout << "scheduled\nstatus: " + std::to_string(response.status()) << std::endl;
std::cout << "body: " + response.body() << std::endl;
}
/**
* buildMessage
* @brief Takes the time and state and builds them into a message to send for scheduling
*
* @param id an integer representing the id of the scheduled item
* @param group an integer (0 or 1) representing if we are scheduling a light or group, respectively
* @return a string message to be the body of the schedule api call
*/
std::string ScheduleItem::buildMessage(int id, int group){
std::string scheduleTime = "";
scheduleTime += std::to_string(this->time->tm_year) + "-";
scheduleTime += std::to_string(this->time->tm_mon) + "-";
scheduleTime += std::to_string(this->time->tm_mday) + "T";
if(std::to_string(this->time->tm_hour).size() == 1){
scheduleTime += "0";
}
scheduleTime += std::to_string(this->time->tm_hour) + ":";
if(std::to_string(this->time->tm_min).size() == 1){
scheduleTime += "0";
}
scheduleTime += std::to_string(this->time->tm_min);
scheduleTime += ":00";
std::string onFlag = "false";
if(this->state->isON()){
onFlag = "true";
}
std::string buildName = "";
if(group == 1){
buildName += "G";
} else{
buildName += "L";
}
buildName += std::to_string(id) + " ";
buildName += scheduleTime + " ";
buildName += onFlag + " ";
std::string dest = "lights";
if(group == 1){
dest = "groups";
}
return (
"{\"name\": \"" + buildName + "\", " +
"\"command\": {" +
"\"address\": \"/api/newdeveloper/" + dest + "/" + std::to_string(id)+ "/action\", " +
"\"method\": \"PUT\", " +
"\"body\": {" +
"\"on\": " + onFlag + ", " +
"\"bri\": " + std::to_string(this->state->getBrightness()) + ", " +
"\"hue\": " + std::to_string(this->state->getHue()) + ", " +
"\"transitiontime\": " + std::to_string(this->state->getTransitionTime()) +
"}" +
"}, " +
"\"time\": \"" + scheduleTime + "\"" +
"}"
);
}