Repository navigation
Expand file tree
/
Copy pathchangestate.cpp
More file actions
63 lines (54 loc) · 2.27 KB
/
Copy pathchangestate.cpp
File metadata and controls
63 lines (54 loc) · 2.27 KB
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
#include "changestate.h"
#include "changestatetracker.h"
#include <chrono>
#include <sstream>
#include <string>
void ChangeStateResponder::reply(std::ostream& out, cxxtools::http::Request& request, cxxtools::http::Reply& reply)
{
QueryHandler::addHeader(reply);
QueryHandler q("/change-state", request);
if (request.method() == "OPTIONS") {
reply.addHeader("Allow", "GET");
reply.httpReturn(200, "OK");
return;
}
if (request.method() != "GET") {
reply.httpReturn(403, "Only GET method is supported");
return;
}
StreamExtension se(&out);
if (q.isFormat(".json")) {
reply.addHeader("Content-Type", "application/json; charset=utf-8");
replyJson(se);
} else if (q.isFormat(".xml")) {
reply.addHeader("Content-Type", "text/xml; charset=utf-8");
replyXml(se);
} else {
reply.httpReturn(403, "Support formats: json and xml");
}
}
void ChangeStateResponder::replyJson(StreamExtension& se)
{
std::ostringstream json;
json
<< "{"
<< "\"bootID\":\"" << StateChangeTracker::bootID << "\","
<< "\"channelsUpdate\":" << StateChangeTracker::LastChannelsUpdate() << ","
<< "\"recordingsUpdate\":" << StateChangeTracker::LastRecordingsUpdate() << ","
<< "\"timersUpdate\":" << StateChangeTracker::LastTimersUpdate() << ","
<< "\"eventsUpdate\":" << StateChangeTracker::LastEventsUpdate()
<< "}";
std::string payload = json.str();
se.write(payload.c_str());
}
void ChangeStateResponder::replyXml(StreamExtension& se)
{
se.writeXmlHeader();
se.write("<change-state>");
se.write(cString::sprintf("<bootID>%s</bootID>", StateChangeTracker::bootID.c_str()));
se.write(cString::sprintf("<channelsUpdate>%llu</channelsUpdate>", (unsigned long long)StateChangeTracker::LastChannelsUpdate()));
se.write(cString::sprintf("<recordingsUpdate>%llu</recordingsUpdate>", (unsigned long long)StateChangeTracker::LastRecordingsUpdate()));
se.write(cString::sprintf("<timersUpdate>%llu</timersUpdate>", (unsigned long long)StateChangeTracker::LastTimersUpdate()));
se.write(cString::sprintf("<eventsUpdate>%llu</eventsUpdate>", (unsigned long long)StateChangeTracker::LastEventsUpdate()));
se.write("</change-state>");
}