mirror of
https://github.com/Telecominfraproject/wlan-cloud-userportal.git
synced 2025-11-02 03:27:48 +00:00
Adding device command: blink, reboot, etc...
This commit is contained in:
@@ -75,7 +75,7 @@ add_executable(owsub
|
||||
src/RESTAPI/RESTAPI_wifiClients_handler.cpp src/RESTAPI/RESTAPI_wifiClients_handler.h
|
||||
src/RESTObjects/RESTAPI_SubObjects.cpp src/RESTObjects/RESTAPI_SubObjects.h
|
||||
src/RESTAPI/RESTAPI_subscriber_handler.cpp src/RESTAPI/RESTAPI_subscriber_handler.h
|
||||
src/RESTAPI/RESTAPI_oauth2_handler.cpp src/RESTAPI/RESTAPI_oauth2_handler.h)
|
||||
src/RESTAPI/RESTAPI_oauth2_handler.cpp src/RESTAPI/RESTAPI_oauth2_handler.h src/RESTAPI/RESTAPI_action_handler.cpp src/RESTAPI/RESTAPI_action_handler.h)
|
||||
|
||||
target_link_libraries(owsub PUBLIC
|
||||
${Poco_LIBRARIES} ${MySQL_LIBRARIES}
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include "RESTAPI//RESTAPI_wiredClients_handler.h"
|
||||
#include "RESTAPI/RESTAPI_subscriber_handler.h"
|
||||
#include "RESTAPI/RESTAPI_oauth2_handler.h"
|
||||
#include "RESTAPI/RESTAPI_action_handler.h"
|
||||
|
||||
namespace OpenWifi {
|
||||
|
||||
@@ -17,6 +18,7 @@ namespace OpenWifi {
|
||||
RESTAPI_wiredClients_handler,
|
||||
RESTAPI_subscriber_handler,
|
||||
RESTAPI_oauth2_handler,
|
||||
RESTAPI_action_handler,
|
||||
RESTAPI_system_command>(Path, Bindings, L, S);
|
||||
}
|
||||
|
||||
|
||||
137
src/RESTAPI/RESTAPI_action_handler.cpp
Normal file
137
src/RESTAPI/RESTAPI_action_handler.cpp
Normal file
@@ -0,0 +1,137 @@
|
||||
//
|
||||
// Created by stephane bourque on 2021-11-30.
|
||||
//
|
||||
|
||||
#include "RESTAPI_action_handler.h"
|
||||
#include "SubscriberCache.h"
|
||||
|
||||
namespace OpenWifi {
|
||||
|
||||
void RESTAPI_action_handler::DoPost() {
|
||||
auto Command = GetParameter("action","");
|
||||
if(Command.empty()) {
|
||||
return BadRequest(RESTAPI::Errors::MissingOrInvalidParameters);
|
||||
}
|
||||
|
||||
auto Body = ParseStream();
|
||||
std::string Mac, ImageName,Pattern{"blink"};
|
||||
AssignIfPresent(Body,"mac",Mac);
|
||||
uint64_t When=0, Duration = 30;
|
||||
bool keepRedirector=true;
|
||||
AssignIfPresent(Body, "when",When);
|
||||
AssignIfPresent(Body, "duration", Duration);
|
||||
AssignIfPresent(Body, "uri", ImageName);
|
||||
AssignIfPresent(Body, "pattern", Pattern);
|
||||
AssignIfPresent(Body, "keepRedirector",keepRedirector);
|
||||
|
||||
if(Mac.empty()) {
|
||||
return BadRequest(RESTAPI::Errors::MissingOrInvalidParameters);
|
||||
}
|
||||
|
||||
Poco::SharedPtr<SubObjects::SubscriberInfo> SubInfo;
|
||||
auto UserFound = SubscriberCache()->GetSubInfo(UserInfo_.userinfo.Id,SubInfo);
|
||||
if(!UserFound) {
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
for(const auto &i:SubInfo->accessPoints.list) {
|
||||
if(i.macAddress == Mac) {
|
||||
if(Command == "reboot") {
|
||||
Reboot(Mac, When);
|
||||
} else if(Command == "leds") {
|
||||
LEDs(Mac, When, Duration, Pattern);
|
||||
} else if(Command == "upgrade") {
|
||||
Upgrade(Mac, When, ImageName, keepRedirector);
|
||||
} else if(Command == "factory") {
|
||||
Factory(Mac, When, keepRedirector);
|
||||
} else if(Command == "refresh") {
|
||||
Refresh(Mac, When);
|
||||
} else {
|
||||
return BadRequest(RESTAPI::Errors::MissingOrInvalidParameters);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
void RESTAPI_action_handler::Reboot(const std::string & Mac, uint64_t When) {
|
||||
std::string EndPoint = "/device/" + Mac + "/reboot";
|
||||
Poco::JSON::Object ObjRequest;
|
||||
|
||||
ObjRequest.set("serialNumber", Mac);
|
||||
ObjRequest.set("when",0);
|
||||
|
||||
PerformCommand(EndPoint, ObjRequest);
|
||||
}
|
||||
|
||||
void RESTAPI_action_handler::LEDs(const std::string & Mac, uint64_t When, uint64_t Duration, const std::string & Pattern) {
|
||||
std::string EndPoint = "/device/" + Mac + "/leds";
|
||||
Poco::JSON::Object ObjRequest;
|
||||
|
||||
ObjRequest.set("serialNumber", Mac);
|
||||
ObjRequest.set("when",When);
|
||||
ObjRequest.set("duration",Duration);
|
||||
ObjRequest.set("pattern", Pattern);
|
||||
PerformCommand(EndPoint, ObjRequest);
|
||||
}
|
||||
|
||||
void RESTAPI_action_handler::Factory(const std::string & Mac, uint64_t When, bool KeepRedirector) {
|
||||
std::string EndPoint = "/device/" + Mac + "/factory";
|
||||
Poco::JSON::Object ObjRequest;
|
||||
|
||||
ObjRequest.set("serialNumber", Mac);
|
||||
ObjRequest.set("when",When);
|
||||
ObjRequest.set("keepRedirector",KeepRedirector);
|
||||
PerformCommand(EndPoint, ObjRequest);
|
||||
}
|
||||
|
||||
void RESTAPI_action_handler::Upgrade(const std::string & Mac, uint64_t When, const std::string & ImageName, bool KeepRedirector) {
|
||||
std::string EndPoint = "/device/" + Mac + "/upgrade";
|
||||
Poco::JSON::Object ObjRequest;
|
||||
|
||||
ObjRequest.set("serialNumber", Mac);
|
||||
ObjRequest.set("when",When);
|
||||
ObjRequest.set("uri",ImageName);
|
||||
PerformCommand(EndPoint, ObjRequest);
|
||||
}
|
||||
|
||||
void RESTAPI_action_handler::Refresh(const std::string & Mac, uint64_t When) {
|
||||
std::string EndPoint = "/device/" + Mac + "/refresh";
|
||||
Poco::JSON::Object ObjRequest;
|
||||
|
||||
ObjRequest.set("serialNumber", Mac);
|
||||
ObjRequest.set("when",When);
|
||||
PerformCommand(EndPoint, ObjRequest);
|
||||
}
|
||||
|
||||
void RESTAPI_action_handler::PerformCommand(const std::string & EndPoint, Poco::JSON::Object & CommandRequest) {
|
||||
auto API = OpenAPIRequestPost(uSERVICE_GATEWAY, EndPoint, Types::StringPairVec{}, CommandRequest, 20000);
|
||||
Poco::JSON::Object::Ptr CallResponse;
|
||||
|
||||
auto ResponseStatus = API.Do(CallResponse);
|
||||
|
||||
if(ResponseStatus == Poco::Net::HTTPServerResponse::HTTP_GATEWAY_TIMEOUT) {
|
||||
Poco::JSON::Object ResponseObject;
|
||||
ResponseObject.set("ErrorCode",Poco::Net::HTTPServerResponse::HTTP_GATEWAY_TIMEOUT);
|
||||
ResponseObject.set("ErrorDetails","Command could not complete in time.");
|
||||
ResponseObject.set("ErrorDescription","Command could not complete, you may want to retry this operation later.");
|
||||
Response->setStatus(ResponseStatus);
|
||||
std::stringstream SS;
|
||||
Poco::JSON::Stringifier::condense(ResponseObject,SS);
|
||||
Response->setContentLength(SS.str().size());
|
||||
Response->setContentType("application/json");
|
||||
auto & os = Response->send();
|
||||
os << SS.str();
|
||||
} else {
|
||||
Response->setStatus(ResponseStatus);
|
||||
std::stringstream SS;
|
||||
Poco::JSON::Stringifier::condense(CallResponse,SS);
|
||||
Response->setContentLength(SS.str().size());
|
||||
Response->setContentType("application/json");
|
||||
auto & os = Response->send();
|
||||
os << SS.str();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
45
src/RESTAPI/RESTAPI_action_handler.h
Normal file
45
src/RESTAPI/RESTAPI_action_handler.h
Normal file
@@ -0,0 +1,45 @@
|
||||
//
|
||||
// Created by stephane bourque on 2021-11-30.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "framework/MicroService.h"
|
||||
|
||||
namespace OpenWifi {
|
||||
class RESTAPI_action_handler : public RESTAPIHandler {
|
||||
public:
|
||||
RESTAPI_action_handler(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, RESTAPI_GenericServer & Server, bool Internal)
|
||||
: RESTAPIHandler(bindings, L,
|
||||
std::vector<std::string>{
|
||||
Poco::Net::HTTPRequest::HTTP_GET,
|
||||
Poco::Net::HTTPRequest::HTTP_OPTIONS},
|
||||
Server,
|
||||
Internal,true, false, RateLimit{}, true){}
|
||||
|
||||
static const std::list<const char *> PathName() { return std::list<const char *>{"/api/v1/action"}; };
|
||||
|
||||
inline bool RoleIsAuthorized(std::string & Reason) {
|
||||
if(UserInfo_.userinfo.userRole != SecurityObjects::USER_ROLE::SUBSCRIBER) {
|
||||
Reason = "User must be a subscriber";
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void DoGet() final {};
|
||||
void DoPost() final;
|
||||
void DoPut() final {};
|
||||
void DoDelete() final {};
|
||||
private:
|
||||
|
||||
void Reboot(const std::string & Mac, uint64_t When);
|
||||
void LEDs(const std::string & Mac, uint64_t When, uint64_t Duration, const std::string & Pattern);
|
||||
void Factory(const std::string & Mac, uint64_t When, bool KeepRedirector);
|
||||
void Upgrade(const std::string & Mac, uint64_t When, const std::string & ImageName, bool KeepRedirector);
|
||||
void Refresh(const std::string & Mac, uint64_t When);
|
||||
|
||||
void PerformCommand(const std::string & EndPoint, Poco::JSON::Object & CommandRequest);
|
||||
|
||||
};
|
||||
}
|
||||
@@ -2,8 +2,7 @@
|
||||
// Created by stephane bourque on 2021-11-28.
|
||||
//
|
||||
|
||||
#ifndef OWSUB_RESTAPI_OAUTH2_HANDLER_H
|
||||
#define OWSUB_RESTAPI_OAUTH2_HANDLER_H
|
||||
#pragma once
|
||||
|
||||
#include "framework/MicroService.h"
|
||||
|
||||
@@ -30,4 +29,3 @@ namespace OpenWifi {
|
||||
};
|
||||
}
|
||||
|
||||
#endif //OWSUB_RESTAPI_OAUTH2_HANDLER_H
|
||||
|
||||
@@ -2,9 +2,7 @@
|
||||
// Created by stephane bourque on 2021-11-07.
|
||||
//
|
||||
|
||||
#ifndef OWSUB_RESTAPI_SUBSCRIBER_HANDLER_H
|
||||
#define OWSUB_RESTAPI_SUBSCRIBER_HANDLER_H
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "framework/MicroService.h"
|
||||
|
||||
@@ -29,5 +27,3 @@ namespace OpenWifi {
|
||||
private:
|
||||
};
|
||||
}
|
||||
|
||||
#endif //OWSUB_RESTAPI_SUBSCRIBER_HANDLER_H
|
||||
|
||||
@@ -2,8 +2,7 @@
|
||||
// Created by stephane bourque on 2021-10-26.
|
||||
//
|
||||
|
||||
#ifndef OWSUB_RESTAPI_WIFICLIENTS_HANDLER_H
|
||||
#define OWSUB_RESTAPI_WIFICLIENTS_HANDLER_H
|
||||
#pragma once
|
||||
|
||||
#include "framework/MicroService.h"
|
||||
|
||||
@@ -35,5 +34,3 @@ namespace OpenWifi {
|
||||
private:
|
||||
};
|
||||
}
|
||||
|
||||
#endif //OWSUB_RESTAPI_WIFICLIENTS_HANDLER_H
|
||||
|
||||
@@ -2,8 +2,7 @@
|
||||
// Created by stephane bourque on 2021-10-26.
|
||||
//
|
||||
|
||||
#ifndef OWSUB_RESTAPI_WIREDCLIENTS_HANDLER_H
|
||||
#define OWSUB_RESTAPI_WIREDCLIENTS_HANDLER_H
|
||||
#pragma once
|
||||
|
||||
#include "framework/MicroService.h"
|
||||
|
||||
@@ -35,5 +34,3 @@ namespace OpenWifi {
|
||||
private:
|
||||
};
|
||||
}
|
||||
|
||||
#endif //OWSUB_RESTAPI_WIREDCLIENTS_HANDLER_H
|
||||
|
||||
@@ -2042,15 +2042,15 @@ namespace OpenWifi {
|
||||
|
||||
class OpenAPIRequestGet {
|
||||
public:
|
||||
explicit OpenAPIRequestGet( std::string Type,
|
||||
std::string EndPoint,
|
||||
Types::StringPairVec & QueryData,
|
||||
explicit OpenAPIRequestGet( const std::string & Type,
|
||||
const std::string & EndPoint,
|
||||
const Types::StringPairVec & QueryData,
|
||||
uint64_t msTimeout):
|
||||
Type_(std::move(Type)),
|
||||
EndPoint_(std::move(EndPoint)),
|
||||
Type_(Type),
|
||||
EndPoint_(EndPoint),
|
||||
QueryData_(QueryData),
|
||||
msTimeout_(msTimeout) {};
|
||||
inline int Do(Poco::JSON::Object::Ptr &ResponseObject);
|
||||
inline Poco::Net::HTTPServerResponse::HTTPStatus Do(Poco::JSON::Object::Ptr &ResponseObject);
|
||||
private:
|
||||
std::string Type_;
|
||||
std::string EndPoint_;
|
||||
@@ -2060,18 +2060,18 @@ namespace OpenWifi {
|
||||
|
||||
class OpenAPIRequestPut {
|
||||
public:
|
||||
explicit OpenAPIRequestPut( std::string Type,
|
||||
std::string EndPoint,
|
||||
Types::StringPairVec & QueryData,
|
||||
Poco::JSON::Object Body,
|
||||
explicit OpenAPIRequestPut( const std::string & Type,
|
||||
const std::string & EndPoint,
|
||||
const Types::StringPairVec & QueryData,
|
||||
const Poco::JSON::Object & Body,
|
||||
uint64_t msTimeout):
|
||||
Type_(std::move(Type)),
|
||||
EndPoint_(std::move(EndPoint)),
|
||||
Type_(Type),
|
||||
EndPoint_(EndPoint),
|
||||
QueryData_(QueryData),
|
||||
msTimeout_(msTimeout),
|
||||
Body_(std::move(Body)){};
|
||||
Body_(Body){};
|
||||
|
||||
inline int Do(Poco::JSON::Object::Ptr &ResponseObject);
|
||||
inline Poco::Net::HTTPServerResponse::HTTPStatus Do(Poco::JSON::Object::Ptr &ResponseObject);
|
||||
|
||||
private:
|
||||
std::string Type_;
|
||||
@@ -2083,17 +2083,17 @@ namespace OpenWifi {
|
||||
|
||||
class OpenAPIRequestPost {
|
||||
public:
|
||||
explicit OpenAPIRequestPost( std::string Type,
|
||||
std::string EndPoint,
|
||||
Types::StringPairVec & QueryData,
|
||||
Poco::JSON::Object Body,
|
||||
explicit OpenAPIRequestPost( const std::string & Type,
|
||||
const std::string & EndPoint,
|
||||
const Types::StringPairVec & QueryData,
|
||||
const Poco::JSON::Object & Body,
|
||||
uint64_t msTimeout):
|
||||
Type_(std::move(Type)),
|
||||
EndPoint_(std::move(EndPoint)),
|
||||
Type_(Type),
|
||||
EndPoint_(EndPoint),
|
||||
QueryData_(QueryData),
|
||||
msTimeout_(msTimeout),
|
||||
Body_(std::move(Body)){};
|
||||
inline int Do(Poco::JSON::Object::Ptr &ResponseObject);
|
||||
Body_(Body){};
|
||||
inline Poco::Net::HTTPServerResponse::HTTPStatus Do(Poco::JSON::Object::Ptr &ResponseObject);
|
||||
private:
|
||||
std::string Type_;
|
||||
std::string EndPoint_;
|
||||
@@ -2293,7 +2293,7 @@ namespace OpenWifi {
|
||||
QueryData,
|
||||
5000);
|
||||
Poco::JSON::Object::Ptr Response;
|
||||
if(Req.Do(Response)==Poco::Net::HTTPResponse::HTTP_OK) {
|
||||
if(Req.Do(Response)==Poco::Net::HTTPServerResponse::HTTP_OK) {
|
||||
if(Response->has("tokenInfo") && Response->has("userInfo")) {
|
||||
UInfo.from_json(Response);
|
||||
if(IsTokenExpired(UInfo.webtoken)) {
|
||||
@@ -3563,7 +3563,7 @@ namespace OpenWifi {
|
||||
void DoDelete() final {};
|
||||
};
|
||||
|
||||
inline int OpenAPIRequestGet::Do(Poco::JSON::Object::Ptr &ResponseObject) {
|
||||
inline Poco::Net::HTTPServerResponse::HTTPStatus OpenAPIRequestGet::Do(Poco::JSON::Object::Ptr &ResponseObject) {
|
||||
try {
|
||||
auto Services = MicroService::instance().GetServices(Type_);
|
||||
for(auto const &Svc:Services) {
|
||||
@@ -3597,10 +3597,10 @@ namespace OpenWifi {
|
||||
{
|
||||
std::cerr << E.displayText() << std::endl;
|
||||
}
|
||||
return -1;
|
||||
return Poco::Net::HTTPServerResponse::HTTP_GATEWAY_TIMEOUT;
|
||||
}
|
||||
|
||||
inline int OpenAPIRequestPut::Do(Poco::JSON::Object::Ptr &ResponseObject) {
|
||||
inline Poco::Net::HTTPServerResponse::HTTPStatus OpenAPIRequestPut::Do(Poco::JSON::Object::Ptr &ResponseObject) {
|
||||
try {
|
||||
auto Services = MicroService::instance().GetServices(Type_);
|
||||
for(auto const &Svc:Services) {
|
||||
@@ -3645,10 +3645,10 @@ namespace OpenWifi {
|
||||
{
|
||||
std::cerr << E.displayText() << std::endl;
|
||||
}
|
||||
return -1;
|
||||
return Poco::Net::HTTPServerResponse::HTTP_GATEWAY_TIMEOUT;
|
||||
}
|
||||
|
||||
int OpenAPIRequestPost::Do(Poco::JSON::Object::Ptr &ResponseObject) {
|
||||
inline Poco::Net::HTTPServerResponse::HTTPStatus OpenAPIRequestPost::Do(Poco::JSON::Object::Ptr &ResponseObject) {
|
||||
try {
|
||||
auto Services = MicroService::instance().GetServices(Type_);
|
||||
for(auto const &Svc:Services) {
|
||||
@@ -3693,7 +3693,7 @@ namespace OpenWifi {
|
||||
{
|
||||
std::cerr << E.displayText() << std::endl;
|
||||
}
|
||||
return -1;
|
||||
return Poco::Net::HTTPServerResponse::HTTP_GATEWAY_TIMEOUT;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user