This commit is contained in:
stephb9959
2021-06-13 23:17:51 -07:00
parent 2ad6cadea1
commit c423a06076
34 changed files with 1620 additions and 466 deletions

View File

@@ -0,0 +1,126 @@
//
// License type: BSD 3-Clause License
// License copy: https://github.com/Telecominfraproject/wlan-cloud-ucentralgw/blob/master/LICENSE
//
// Created by Stephane Bourque on 2021-03-04.
// Arilia Wireless Inc.
//
#include "RESTAPI_system_command.h"
#include "Poco/Exception.h"
#include "Poco/JSON/Parser.h"
#include "Daemon.h"
#include "RESTAPI_protocol.h"
/*
SystemCommandDetails:
type: object
properties:
command:
type: string
enum:
- setloglevel
- getloglevel
- stats
parameters:
type: array
items:
properties:
name:
type: string
value:
type: string
SystemCommandResults:
type: object
properties:
command:
type: string
result:
type: integer
resultTxt:
type: array
items:
type: string
*/
namespace uCentral {
void RESTAPI_system_command::handleRequest(Poco::Net::HTTPServerRequest &Request,
Poco::Net::HTTPServerResponse &Response) {
if (!ContinueProcessing(Request, Response))
return;
if (!IsAuthorized(Request, Response))
return;
try {
if (Request.getMethod() == Poco::Net::HTTPRequest::HTTP_POST) {
Poco::JSON::Parser parser;
auto Obj = parser.parse(Request.stream()).extract<Poco::JSON::Object::Ptr>();
if (Obj->has(uCentral::RESTAPI::Protocol::COMMAND)) {
auto Command = Poco::toLower(Obj->get(uCentral::RESTAPI::Protocol::COMMAND).toString());
if (Command == uCentral::RESTAPI::Protocol::SETLOGLEVEL) {
if (Obj->has(uCentral::RESTAPI::Protocol::PARAMETERS) &&
Obj->isArray(uCentral::RESTAPI::Protocol::PARAMETERS)) {
auto ParametersBlock = Obj->getArray(uCentral::RESTAPI::Protocol::PARAMETERS);
for (const auto &i:*ParametersBlock) {
Poco::JSON::Parser pp;
auto InnerObj = pp.parse(i).extract<Poco::JSON::Object::Ptr>();
if (InnerObj->has(uCentral::RESTAPI::Protocol::TAG) &&
InnerObj->has(uCentral::RESTAPI::Protocol::VALUE)) {
auto Name = GetS(uCentral::RESTAPI::Protocol::TAG, InnerObj);
auto Value = GetS(uCentral::RESTAPI::Protocol::VALUE, InnerObj);
Daemon()->SetSubsystemLogLevel(Name, Value);
Logger_.information(Poco::format("Setting log level for %s at %s", Name, Value));
}
}
OK(Request, Response);
return;
}
} else if (Command == uCentral::RESTAPI::Protocol::GETLOGLEVELS) {
auto CurrentLogLevels = Daemon()->GetLogLevels();
Poco::JSON::Object Result;
Poco::JSON::Array Array;
for(auto &[Name,Level]:CurrentLogLevels) {
Poco::JSON::Object Pair;
Pair.set( uCentral::RESTAPI::Protocol::TAG,Name);
Pair.set(uCentral::RESTAPI::Protocol::VALUE,Level);
Array.add(Pair);
}
Result.set(uCentral::RESTAPI::Protocol::TAGLIST,Array);
ReturnObject(Request,Result,Response);
return;
} else if (Command == uCentral::RESTAPI::Protocol::GETLOGLEVELNAMES) {
Poco::JSON::Object Result;
Poco::JSON::Array LevelNamesArray;
const Types::StringVec & LevelNames = Daemon()->GetLogLevelNames();
for(const auto &i:LevelNames)
LevelNamesArray.add(i);
Result.set(uCentral::RESTAPI::Protocol::LIST,LevelNamesArray);
ReturnObject(Request,Result,Response);
return;
} else if (Command == uCentral::RESTAPI::Protocol::GETSUBSYSTEMNAMES) {
Poco::JSON::Object Result;
Poco::JSON::Array LevelNamesArray;
const Types::StringVec & SubSystemNames = Daemon()->GetSubSystems();
for(const auto &i:SubSystemNames)
LevelNamesArray.add(i);
Result.set(uCentral::RESTAPI::Protocol::LIST,LevelNamesArray);
ReturnObject(Request,Result,Response);
return;
} else if (Command == uCentral::RESTAPI::Protocol::STATS) {
}
}
}
} catch (const Poco::Exception &E) {
Logger_.log(E);
}
BadRequest(Request, Response);
}
}