cscore: Add JSON for source settings (#1423)

This allows save and restore of camera settings.  The restore is a bit
smarter than the save.

* Fix mime types in mjpeg server

* wpiutil: WPI_LOG: Make sure level is an unsigned int
This commit is contained in:
Peter Johnson
2018-11-10 20:30:02 -08:00
committed by GitHub
parent 43d188a429
commit 9bc998f4b0
14 changed files with 501 additions and 5 deletions

View File

@@ -67,7 +67,8 @@ static const char* startRootPage =
"</head><body>\n"
"<div class=\"stream\">\n"
"<img src=\"/stream.mjpg\" /><p />\n"
"<a href=\"/settings.json\">Settings JSON</a>\n"
"<a href=\"/settings.json\">Settings JSON</a> |\n"
"<a href=\"/config.json\">Source Config JSON</a>\n"
"</div>\n"
"<div class=\"settings\">\n";
static const char* endRootPage = "</div></body></html>";
@@ -340,7 +341,7 @@ void MjpegServerImpl::ConnThread::SendHTMLHeadTitle(
// Send the root html file with controls for all the settable properties.
void MjpegServerImpl::ConnThread::SendHTML(wpi::raw_ostream& os,
SourceImpl& source, bool header) {
if (header) SendHeader(os, 200, "OK", "application/x-javascript");
if (header) SendHeader(os, 200, "OK", "text/html");
SendHTMLHeadTitle(os);
os << startRootPage;
@@ -453,7 +454,7 @@ void MjpegServerImpl::ConnThread::SendHTML(wpi::raw_ostream& os,
// Send a JSON file which is contains information about the source parameters.
void MjpegServerImpl::ConnThread::SendJSON(wpi::raw_ostream& os,
SourceImpl& source, bool header) {
if (header) SendHeader(os, 200, "OK", "application/x-javascript");
if (header) SendHeader(os, 200, "OK", "application/json");
os << "{\n\"controls\": [\n";
wpi::SmallVector<int, 32> properties_vec;
@@ -728,7 +729,7 @@ void MjpegServerImpl::ConnThread::ProcessRequest() {
return;
}
enum { kCommand, kStream, kGetSettings, kRootPage } kind;
enum { kCommand, kStream, kGetSettings, kGetSourceConfig, kRootPage } kind;
wpi::StringRef parameters;
size_t pos;
@@ -748,6 +749,9 @@ void MjpegServerImpl::ConnThread::ProcessRequest() {
} else if (req.find("GET /settings") != wpi::StringRef::npos &&
req.find(".json") != wpi::StringRef::npos) {
kind = kGetSettings;
} else if (req.find("GET /config") != wpi::StringRef::npos &&
req.find(".json") != wpi::StringRef::npos) {
kind = kGetSourceConfig;
} else if (req.find("GET /input") != wpi::StringRef::npos &&
req.find(".json") != wpi::StringRef::npos) {
kind = kGetSettings;
@@ -806,6 +810,17 @@ void MjpegServerImpl::ConnThread::ProcessRequest() {
else
SendError(os, 404, "Resource not found");
break;
case kGetSourceConfig:
SDEBUG("request for JSON file");
if (auto source = GetSource()) {
SendHeader(os, 200, "OK", "application/json");
CS_Status status = CS_OK;
os << source->GetConfigJson(&status);
os.flush();
} else {
SendError(os, 404, "Resource not found");
}
break;
case kRootPage:
SDEBUG("request for root page");
SendHeader(os, 200, "OK", "text/html");