verion which did not include the handling in RTService wth creating the
Json file
This commit is contained in:
86
vrpmdvaotserver/src/App.cpp
Normal file
86
vrpmdvaotserver/src/App.cpp
Normal file
@@ -0,0 +1,86 @@
|
||||
|
||||
#include "AppComponent.hpp"
|
||||
|
||||
#include "controller/UserController.hpp"
|
||||
#include "controller/MonitoringController.hpp"
|
||||
#include "controller/StaticController.hpp"
|
||||
|
||||
#include "oatpp-swagger/Controller.hpp"
|
||||
|
||||
#include "oatpp/network/Server.hpp"
|
||||
#include "oatpp/web/server/interceptor/AllowCorsGlobal.hpp"
|
||||
#include <iostream>
|
||||
|
||||
|
||||
void run() {
|
||||
|
||||
AppComponent components; // Create scope Environment components
|
||||
|
||||
/* Get router component */
|
||||
OATPP_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, router);
|
||||
|
||||
oatpp::web::server::api::Endpoints docEndpoints;
|
||||
|
||||
docEndpoints.append(router->addController(UserController::createShared())->getEndpoints());
|
||||
docEndpoints.append(router->addController(MonitoringController::createShared())->getEndpoints());
|
||||
|
||||
router->addController(oatpp::swagger::Controller::createShared(docEndpoints));
|
||||
router->addController(StaticController::createShared());
|
||||
|
||||
/* Get connection handler component */
|
||||
//OATPP_COMPONENT(std::shared_ptr<oatpp::network::ConnectionHandler>, connectionHandler);
|
||||
auto connectionHandler = oatpp::web::server::HttpConnectionHandler::createShared(router);
|
||||
|
||||
/* Add CORS-enabling interceptors */
|
||||
|
||||
// AllowCorsGlobal(const oatpp::String &origin = "*",
|
||||
// const oatpp::String &methods = "GET, POST, OPTIONS",
|
||||
// const oatpp::String &headers = "DNT, User-Agent, X-Requested-With, If-Modified-Since, Cache-Control, Content-Type, Range, Authorization",
|
||||
// const oatpp::String &maxAge = "1728000");
|
||||
|
||||
|
||||
connectionHandler->addRequestInterceptor(std::make_shared<oatpp::web::server::interceptor::AllowOptionsGlobal>());
|
||||
|
||||
auto corsGlob = std::make_shared<oatpp::web::server::interceptor::AllowCorsGlobal>("*",
|
||||
"GET, POST, PUT, PATCH, DELETE, OPTIONS",
|
||||
"DNT, User-Agent, X-Requested-With, If-Modified-Since, Cache-Control, Content-Type, Range, Authorization",
|
||||
"1728000");
|
||||
|
||||
connectionHandler->addResponseInterceptor(corsGlob);
|
||||
|
||||
/* Get connection provider component */
|
||||
OATPP_COMPONENT(std::shared_ptr<oatpp::network::ServerConnectionProvider>, connectionProvider);
|
||||
|
||||
/* create server */
|
||||
oatpp::network::Server server(connectionProvider, connectionHandler);
|
||||
|
||||
|
||||
OATPP_LOGD("Server", "Running on port %s...", connectionProvider->getProperty("port").toString()->c_str());
|
||||
|
||||
server.run();
|
||||
|
||||
/* stop db connection pool */
|
||||
OATPP_COMPONENT(std::shared_ptr<oatpp::provider::Provider<oatpp::sqlite::Connection>>, dbConnectionProvider);
|
||||
dbConnectionProvider->stop();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* main
|
||||
*/
|
||||
int main(int argc, const char * argv[]) {
|
||||
|
||||
oatpp::base::Environment::init();
|
||||
|
||||
run();
|
||||
|
||||
/* Print how many objects were created during app running, and what have left-probably leaked */
|
||||
/* Disable object counting for release builds using '-D OATPP_DISABLE_ENV_OBJECT_COUNTERS' flag for better performance */
|
||||
std::cout << "\nEnvironment:\n";
|
||||
std::cout << "objectsCount = " << oatpp::base::Environment::getObjectsCount() << "\n";
|
||||
std::cout << "objectsCreated = " << oatpp::base::Environment::getObjectsCreated() << "\n\n";
|
||||
|
||||
oatpp::base::Environment::destroy();
|
||||
|
||||
return 0;
|
||||
}
|
||||
74
vrpmdvaotserver/src/AppComponent.hpp
Normal file
74
vrpmdvaotserver/src/AppComponent.hpp
Normal file
@@ -0,0 +1,74 @@
|
||||
|
||||
#ifndef VRPMDV_APPCOMPONENT_HPP
|
||||
#define VRPMDV_APPCOMPONENT_HPP
|
||||
|
||||
#include "SwaggerComponent.hpp"
|
||||
#include "DatabaseComponent.hpp"
|
||||
|
||||
#include "ErrorHandler.hpp"
|
||||
|
||||
#include "oatpp/web/server/HttpConnectionHandler.hpp"
|
||||
#include "oatpp/web/server/HttpRouter.hpp"
|
||||
#include "oatpp/network/tcp/server/ConnectionProvider.hpp"
|
||||
|
||||
#include "oatpp/parser/json/mapping/ObjectMapper.hpp"
|
||||
|
||||
#include "oatpp/core/macro/component.hpp"
|
||||
|
||||
/**
|
||||
* Class which creates and holds Application components and registers components in oatpp::base::Environment
|
||||
* Order of components initialization is from top to bottom
|
||||
*/
|
||||
class AppComponent {
|
||||
public:
|
||||
|
||||
/**
|
||||
* Swagger component
|
||||
*/
|
||||
SwaggerComponent swaggerComponent;
|
||||
|
||||
/**
|
||||
* Database component
|
||||
*/
|
||||
DatabaseComponent databaseComponent;
|
||||
|
||||
/**
|
||||
* Create ObjectMapper component to serialize/deserialize DTOs in Controller's API
|
||||
*/
|
||||
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::data::mapping::ObjectMapper>, apiObjectMapper)([] {
|
||||
auto objectMapper = oatpp::parser::json::mapping::ObjectMapper::createShared();
|
||||
objectMapper->getDeserializer()->getConfig()->allowUnknownFields = false;
|
||||
return objectMapper;
|
||||
}());
|
||||
|
||||
/**
|
||||
* Create ConnectionProvider component which listens on the port
|
||||
*/
|
||||
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::ServerConnectionProvider>, serverConnectionProvider)([] {
|
||||
return oatpp::network::tcp::server::ConnectionProvider::createShared({"0.0.0.0", 8000, oatpp::network::Address::IP_4});
|
||||
}());
|
||||
|
||||
/**
|
||||
* Create Router component
|
||||
*/
|
||||
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, httpRouter)([] {
|
||||
return oatpp::web::server::HttpRouter::createShared();
|
||||
}());
|
||||
|
||||
/**
|
||||
* Create ConnectionHandler component which uses Router component to route requests
|
||||
*/
|
||||
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::ConnectionHandler>, serverConnectionHandler)([] {
|
||||
|
||||
OATPP_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, router); // get Router component
|
||||
OATPP_COMPONENT(std::shared_ptr<oatpp::data::mapping::ObjectMapper>, objectMapper); // get ObjectMapper component
|
||||
|
||||
auto connectionHandler = oatpp::web::server::HttpConnectionHandler::createShared(router);
|
||||
connectionHandler->setErrorHandler(std::make_shared<ErrorHandler>(objectMapper));
|
||||
return connectionHandler;
|
||||
|
||||
}());
|
||||
|
||||
};
|
||||
|
||||
#endif /* AppComponent_hpp */
|
||||
61
vrpmdvaotserver/src/DatabaseComponent.hpp
Normal file
61
vrpmdvaotserver/src/DatabaseComponent.hpp
Normal file
@@ -0,0 +1,61 @@
|
||||
|
||||
#ifndef VRPMDV_DATABASECOMPONENT_HPP
|
||||
#define VRPMDV_DATABASECOMPONENT_HPP
|
||||
|
||||
#include "db/UserDb.hpp"
|
||||
#include "db/MonitoringDb.hpp"
|
||||
|
||||
class DatabaseComponent {
|
||||
public:
|
||||
|
||||
/**
|
||||
* Create database connection provider component
|
||||
*/
|
||||
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::provider::Provider<oatpp::sqlite::Connection>>, dbConnectionProvider)([] {
|
||||
|
||||
/* Create database-specific ConnectionProvider */
|
||||
auto connectionProvider = std::make_shared<oatpp::sqlite::ConnectionProvider>(MONDATABASE_FILE);
|
||||
|
||||
/* Create database-specific ConnectionPool */
|
||||
return oatpp::sqlite::ConnectionPool::createShared(connectionProvider,
|
||||
10 /* max-connections */,
|
||||
std::chrono::seconds(5) /* connection TTL */);
|
||||
|
||||
}());
|
||||
|
||||
// /**
|
||||
// * Create database client
|
||||
// */
|
||||
// OATPP_CREATE_COMPONENT(std::shared_ptr<UserDb>, userDb)([] {
|
||||
|
||||
// /* Get database ConnectionProvider component */
|
||||
// OATPP_COMPONENT(std::shared_ptr<oatpp::provider::Provider<oatpp::sqlite::Connection>>, connectionProvider);
|
||||
|
||||
// /* Create database-specific Executor */
|
||||
// auto executor = std::make_shared<oatpp::sqlite::Executor>(connectionProvider);
|
||||
|
||||
// /* Create MyClient database client */
|
||||
// return std::make_shared<UserDb>(executor);
|
||||
|
||||
// }());
|
||||
|
||||
|
||||
/**
|
||||
* Create database client for monitorings
|
||||
*/
|
||||
OATPP_CREATE_COMPONENT(std::shared_ptr<MonitoringDb>, monitoringDb)([] {
|
||||
|
||||
/* Get database ConnectionProvider component */
|
||||
OATPP_COMPONENT(std::shared_ptr<oatpp::provider::Provider<oatpp::sqlite::Connection>>, connectionProvider);
|
||||
|
||||
/* Create database-specific Executor */
|
||||
auto executor = std::make_shared<oatpp::sqlite::Executor>(connectionProvider);
|
||||
|
||||
/* Create MyClient database client */
|
||||
return std::make_shared<MonitoringDb>(executor);
|
||||
|
||||
}());
|
||||
|
||||
};
|
||||
|
||||
#endif //CRUD_DATABASECOMPONENT_HPP
|
||||
24
vrpmdvaotserver/src/ErrorHandler.cpp
Normal file
24
vrpmdvaotserver/src/ErrorHandler.cpp
Normal file
@@ -0,0 +1,24 @@
|
||||
|
||||
#include "ErrorHandler.hpp"
|
||||
|
||||
ErrorHandler::ErrorHandler(const std::shared_ptr<oatpp::data::mapping::ObjectMapper>& objectMapper)
|
||||
: m_objectMapper(objectMapper)
|
||||
{}
|
||||
|
||||
std::shared_ptr<ErrorHandler::OutgoingResponse>
|
||||
ErrorHandler::handleError(const Status& status, const oatpp::String& message, const Headers& headers) {
|
||||
|
||||
auto error = StatusDto::createShared();
|
||||
error->status = "ERROR";
|
||||
error->code = status.code;
|
||||
error->message = message;
|
||||
|
||||
auto response = ResponseFactory::createResponse(status, error, m_objectMapper);
|
||||
|
||||
for(const auto& pair : headers.getAll()) {
|
||||
response->putHeader(pair.first.toString(), pair.second.toString());
|
||||
}
|
||||
|
||||
return response;
|
||||
|
||||
}
|
||||
27
vrpmdvaotserver/src/ErrorHandler.hpp
Normal file
27
vrpmdvaotserver/src/ErrorHandler.hpp
Normal file
@@ -0,0 +1,27 @@
|
||||
|
||||
#ifndef VRPMDV_ERRORHANDLER_HPP
|
||||
#define VRPMDV_ERRORHANDLER_HPP
|
||||
|
||||
#include "dto/StatusDto.hpp"
|
||||
|
||||
#include "oatpp/web/server/handler/ErrorHandler.hpp"
|
||||
#include "oatpp/web/protocol/http/outgoing/ResponseFactory.hpp"
|
||||
|
||||
class ErrorHandler : public oatpp::web::server::handler::ErrorHandler {
|
||||
private:
|
||||
typedef oatpp::web::protocol::http::outgoing::Response OutgoingResponse;
|
||||
typedef oatpp::web::protocol::http::Status Status;
|
||||
typedef oatpp::web::protocol::http::outgoing::ResponseFactory ResponseFactory;
|
||||
private:
|
||||
std::shared_ptr<oatpp::data::mapping::ObjectMapper> m_objectMapper;
|
||||
public:
|
||||
|
||||
ErrorHandler(const std::shared_ptr<oatpp::data::mapping::ObjectMapper>& objectMapper);
|
||||
|
||||
std::shared_ptr<OutgoingResponse>
|
||||
handleError(const Status& status, const oatpp::String& message, const Headers& headers) override;
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif //CRUD_ERRORHANDLER_HPP
|
||||
298
vrpmdvaotserver/src/RTSMonitoring/RTSCoproHelper.cpp
Normal file
298
vrpmdvaotserver/src/RTSMonitoring/RTSCoproHelper.cpp
Normal file
@@ -0,0 +1,298 @@
|
||||
/*
|
||||
*/
|
||||
|
||||
#include "RTSCoproHelper.h"
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <termios.h>
|
||||
#include <fcntl.h>
|
||||
#include <inttypes.h>
|
||||
#include <time.h>
|
||||
#include <pthread.h>
|
||||
#include <unistd.h> // for usleep
|
||||
#include <math.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/eventfd.h>
|
||||
#include <sys/poll.h>
|
||||
#include <regex.h>
|
||||
#include <sched.h>
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <error.h>
|
||||
|
||||
|
||||
|
||||
#define MAX_BUF 80
|
||||
|
||||
#define TTY_CTRL_OPTS (CS8 | CLOCAL | CREAD)
|
||||
#define TTY_INPUT_OPTS IGNPAR
|
||||
#define TTY_OUTPUT_OPTS 0
|
||||
#define TTY_LOCAL_OPTS 0
|
||||
|
||||
|
||||
|
||||
RTSCoproHelper::RTSCoproHelper(/* args */)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
RTSCoproHelper::~RTSCoproHelper()
|
||||
{
|
||||
}
|
||||
|
||||
int RTSCoproHelper::Init(std::string fwPath, std::string fwName) {
|
||||
if (this->Copro_isFwRunning() != 0){
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/********************************************************************************
|
||||
Copro functions allowing to manage a virtual TTY over RPMSG
|
||||
*********************************************************************************/
|
||||
int RTSCoproHelper::Copro_isFwRunning(void)
|
||||
{
|
||||
int fd;
|
||||
size_t byte_read;
|
||||
bool result = 0;
|
||||
unsigned char bufRead[MAX_BUF];
|
||||
char *user = getenv("USER");
|
||||
if (user && strncmp(user, "root", 4)) {
|
||||
system("XTERM=xterm su root -c 'cat /sys/class/remoteproc/remoteproc0/state' > /tmp/remoteproc0_state");
|
||||
fd = open("/tmp/remoteproc0_state", O_RDONLY);
|
||||
} else {
|
||||
fd = open("/sys/class/remoteproc/remoteproc0/state", O_RDONLY);
|
||||
}
|
||||
if (fd < 0) {
|
||||
printf("CA7 : Error opening remoteproc0/state, err=-%d\n", errno);
|
||||
return (errno * -1);
|
||||
}
|
||||
byte_read = (size_t) read (fd, bufRead, MAX_BUF);
|
||||
if (byte_read >= strlen("running")) {
|
||||
char* pos = strstr((char*)bufRead, "running");
|
||||
if(pos) {
|
||||
result = 1;
|
||||
}
|
||||
}
|
||||
close(fd);
|
||||
return result;
|
||||
}
|
||||
|
||||
int RTSCoproHelper::Copro_stopFw(void)
|
||||
{
|
||||
int fd;
|
||||
char *user = getenv("USER");
|
||||
if (user && strncmp(user, "root",4)) {
|
||||
system("su root -c 'echo stop > /sys/class/remoteproc/remoteproc0/state'");
|
||||
return 0;
|
||||
}
|
||||
fd = open("/sys/class/remoteproc/remoteproc0/state", O_RDWR);
|
||||
if (fd < 0) {
|
||||
printf("CA7 : Error opening remoteproc0/state, err=-%d\n", errno);
|
||||
return (errno * -1);
|
||||
}
|
||||
write(fd, "stop", strlen("stop"));
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int RTSCoproHelper::Copro_startFw(void)
|
||||
{
|
||||
int fd;
|
||||
char *user = getenv("USER");
|
||||
if (user && strncmp(user, "root",4)) {
|
||||
system("su root -c 'echo start > /sys/class/remoteproc/remoteproc0/state'");
|
||||
return 0;
|
||||
}
|
||||
fd = open("/sys/class/remoteproc/remoteproc0/state", O_RDWR);
|
||||
if (fd < 0) {
|
||||
printf("CA7 : Error opening remoteproc0/state, err=-%d\n", errno);
|
||||
return (errno * -1);
|
||||
}
|
||||
write(fd, "start", strlen("start"));
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int RTSCoproHelper::Copro_getFwPath(char* pathStr)
|
||||
{
|
||||
int fd;
|
||||
int byte_read;
|
||||
char *user = getenv("USER");
|
||||
if (user && strncmp(user, "root",4)) {
|
||||
system("XTERM=xterm su root -c 'cat /sys/module/firmware_class/parameters/path' > /tmp/parameters_path");
|
||||
fd = open("/tmp/parameters_path", O_RDONLY);
|
||||
} else {
|
||||
fd = open("/sys/module/firmware_class/parameters/path", O_RDONLY);
|
||||
}
|
||||
if (fd < 0) {
|
||||
printf("CA7 : Error opening firmware_class/parameters/path, err=-%d\n", errno);
|
||||
return (errno * -1);
|
||||
}
|
||||
byte_read = read (fd, pathStr, MAX_BUF);
|
||||
close(fd);
|
||||
return byte_read;
|
||||
}
|
||||
|
||||
int RTSCoproHelper::Copro_setFwPath(char* pathStr)
|
||||
{
|
||||
int fd;
|
||||
int result = 0;
|
||||
char *user = getenv("USER");
|
||||
if (user && strncmp(user, "root",4)) {
|
||||
char cmd[1024];
|
||||
snprintf(cmd, 1024, "su root -c 'echo %s > /sys/module/firmware_class/parameters/path'", pathStr);
|
||||
system(cmd);
|
||||
return strlen(pathStr);
|
||||
}
|
||||
fd = open("/sys/module/firmware_class/parameters/path", O_RDWR);
|
||||
if (fd < 0) {
|
||||
printf("CA7 : Error opening firmware_class/parameters/path, err=-%d\n", errno);
|
||||
return (errno * -1);
|
||||
}
|
||||
result = write(fd, pathStr, strlen(pathStr));
|
||||
close(fd);
|
||||
return result;
|
||||
}
|
||||
|
||||
int RTSCoproHelper::Copro_getFwName(char* pathStr)
|
||||
{
|
||||
int fd;
|
||||
int byte_read;
|
||||
char *user = getenv("USER");
|
||||
if (user && strncmp(user, "root",4)) {
|
||||
system("XTERM=xterm su root -c 'cat /sys/class/remoteproc/remoteproc0/firmware' > /tmp/remoteproc0_firmware");
|
||||
fd = open("/tmp/remoteproc0_firmware", O_RDONLY);
|
||||
} else {
|
||||
fd = open("/sys/class/remoteproc/remoteproc0/firmware", O_RDWR);
|
||||
}
|
||||
if (fd < 0) {
|
||||
printf("CA7 : Error opening remoteproc0/firmware, err=-%d\n", errno);
|
||||
return (errno * -1);
|
||||
}
|
||||
byte_read = read (fd, pathStr, MAX_BUF);
|
||||
close(fd);
|
||||
return byte_read;
|
||||
}
|
||||
|
||||
int RTSCoproHelper::Copro_setFwName(char* nameStr)
|
||||
{
|
||||
int fd;
|
||||
int result = 0;
|
||||
char *user = getenv("USER");
|
||||
if (user && strncmp(user, "root",4)) {
|
||||
char cmd[1024];
|
||||
snprintf(cmd, 1024, "su root -c 'echo %s > /sys/class/remoteproc/remoteproc0/firmware'", nameStr);
|
||||
system(cmd);
|
||||
return strlen(nameStr);
|
||||
}
|
||||
fd = open("/sys/class/remoteproc/remoteproc0/firmware", O_RDWR);
|
||||
if (fd < 0) {
|
||||
printf("CA7 : Error opening remoteproc0/firmware, err=-%d\n", errno);
|
||||
return (errno * -1);
|
||||
}
|
||||
result = write(fd, nameStr, strlen(nameStr));
|
||||
close(fd);
|
||||
return result;
|
||||
}
|
||||
|
||||
int RTSCoproHelper::Copro_openTtyRpmsg(int ttyNb, int modeRaw)
|
||||
{
|
||||
struct termios tiorpmsg;
|
||||
char devName[50];
|
||||
sprintf(devName, "/dev/ttyRPMSG%d", ttyNb%2);
|
||||
mFdRpmsg[ttyNb%2] = open(devName, O_RDWR | O_NOCTTY | O_NONBLOCK);
|
||||
if (mFdRpmsg[ttyNb%2] < 0) {
|
||||
printf("CA7 : Error opening ttyRPMSG%d, err=-%d\n", ttyNb%2, errno);
|
||||
return (errno * -1);
|
||||
}
|
||||
#if 1
|
||||
/* get current port settings */
|
||||
tcgetattr(mFdRpmsg[ttyNb%2],&tiorpmsg);
|
||||
if (modeRaw) {
|
||||
#if 0
|
||||
tiorpmsg.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP
|
||||
| INLCR | IGNCR | ICRNL | IXON);
|
||||
tiorpmsg.c_oflag &= ~OPOST;
|
||||
tiorpmsg.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
|
||||
tiorpmsg.c_cflag &= ~(CSIZE | PARENB);
|
||||
tiorpmsg.c_cflag |= CS8;
|
||||
#else
|
||||
memset(&tiorpmsg, 0, sizeof(tiorpmsg));
|
||||
tiorpmsg.c_cflag = TTY_CTRL_OPTS;
|
||||
tiorpmsg.c_iflag = TTY_INPUT_OPTS;
|
||||
tiorpmsg.c_oflag = TTY_OUTPUT_OPTS;
|
||||
tiorpmsg.c_lflag = TTY_LOCAL_OPTS;
|
||||
tiorpmsg.c_cc[VTIME] = 0;
|
||||
tiorpmsg.c_cc[VMIN] = 1;
|
||||
cfmakeraw(&tiorpmsg);
|
||||
#endif
|
||||
} else {
|
||||
/* ECHO off, other bits unchanged */
|
||||
tiorpmsg.c_lflag &= ~ECHO;
|
||||
/*do not convert LF to CR LF */
|
||||
tiorpmsg.c_oflag &= ~ONLCR;
|
||||
}
|
||||
if (tcsetattr(mFdRpmsg[ttyNb%2], TCSANOW, &tiorpmsg) < 0) {
|
||||
printf("Error %d in copro_openTtyRpmsg(%d) tcsetattr", errno, ttyNb);
|
||||
return (errno * -1);
|
||||
}
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
int RTSCoproHelper::Copro_closeTtyRpmsg(int ttyNb)
|
||||
{
|
||||
close(mFdRpmsg[ttyNb%2]);
|
||||
mFdRpmsg[ttyNb%2] = -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int RTSCoproHelper::Copro_writeTtyRpmsg(int ttyNb, int len, char* pData)
|
||||
{
|
||||
int result = 0;
|
||||
if (mFdRpmsg[ttyNb%2] < 0) {
|
||||
printf("CA7 : Error writing ttyRPMSG%d, fileDescriptor is not set\n", ttyNb%2);
|
||||
return mFdRpmsg[ttyNb%2];
|
||||
}
|
||||
|
||||
result = write(mFdRpmsg[ttyNb%2], pData, len);
|
||||
return result;
|
||||
}
|
||||
|
||||
int RTSCoproHelper::Copro_readTtyRpmsg(int ttyNb, int len, char* pData)
|
||||
{
|
||||
int byte_rd, byte_avail;
|
||||
int result = 0;
|
||||
if (mFdRpmsg[ttyNb%2] < 0) {
|
||||
printf("CA7 : Error reading ttyRPMSG%d, fileDescriptor is not set\n", ttyNb%2);
|
||||
return mFdRpmsg[ttyNb%2];
|
||||
}
|
||||
ioctl(mFdRpmsg[ttyNb%2], FIONREAD, &byte_avail);
|
||||
if (byte_avail > 0) {
|
||||
if (byte_avail >= len) {
|
||||
byte_rd = read (mFdRpmsg[ttyNb%2], pData, len);
|
||||
} else {
|
||||
byte_rd = read (mFdRpmsg[ttyNb%2], pData, byte_avail);
|
||||
}
|
||||
//printf("CA7 : read successfully %d bytes to %p, [0]=0x%x\n", byte_rd, pData, pData[0]);
|
||||
result = byte_rd;
|
||||
} else {
|
||||
result = 0;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
/********************************************************************************
|
||||
End of Copro functions
|
||||
*********************************************************************************/
|
||||
37
vrpmdvaotserver/src/RTSMonitoring/RTSCoproHelper.h
Normal file
37
vrpmdvaotserver/src/RTSMonitoring/RTSCoproHelper.h
Normal file
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
*/
|
||||
|
||||
#include <string>
|
||||
|
||||
|
||||
class RTSCoproHelper
|
||||
{
|
||||
private:
|
||||
/* data */
|
||||
//lock Element
|
||||
int mFdRpmsg[2] = {-1, -1};
|
||||
|
||||
public:
|
||||
|
||||
RTSCoproHelper(/* args */);
|
||||
~RTSCoproHelper();
|
||||
|
||||
int Init(std::string fwPath, std::string fwName);
|
||||
|
||||
int Copro_isFwRunning(void);
|
||||
|
||||
int Copro_openTtyRpmsg(int ttyNb, int modeRaw);
|
||||
int Copro_closeTtyRpmsg(int ttyNb);
|
||||
int Copro_writeTtyRpmsg(int ttyNb, int len, char* pData);
|
||||
int Copro_readTtyRpmsg(int ttyNb, int len, char* pData);
|
||||
int Copro_stopFw(void);
|
||||
int Copro_startFw(void);
|
||||
|
||||
private:
|
||||
|
||||
int Copro_getFwPath(char* pathStr);
|
||||
int Copro_setFwPath(char* pathStr);
|
||||
int Copro_getFwName(char* pathStr);
|
||||
int Copro_setFwName(char* nameStr);
|
||||
|
||||
};
|
||||
60
vrpmdvaotserver/src/RTSMonitoring/RTSMonFrame.h
Normal file
60
vrpmdvaotserver/src/RTSMonitoring/RTSMonFrame.h
Normal file
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
*/
|
||||
|
||||
#include <string>
|
||||
#include <list>
|
||||
|
||||
|
||||
class RTSMonFrame
|
||||
{
|
||||
private:
|
||||
/* data */
|
||||
int32_t id;
|
||||
int samplerate;
|
||||
int sampleperiod;
|
||||
int downtime;
|
||||
std::list<std::string> items;
|
||||
|
||||
public:
|
||||
|
||||
RTSMonFrame(int32_t id, int samplerate, int sampleperiod, int downtime) {
|
||||
this->id = id;
|
||||
this->samplerate = samplerate;
|
||||
this->sampleperiod = sampleperiod;
|
||||
this->downtime = downtime;
|
||||
}
|
||||
|
||||
RTSMonFrame(int32_t id) {
|
||||
this->id = id;
|
||||
}
|
||||
|
||||
RTSMonFrame(RTSMonFrame& rtsMonFrameCopy) {
|
||||
this->id = rtsMonFrameCopy.GetId();
|
||||
}
|
||||
|
||||
~RTSMonFrame();
|
||||
|
||||
void Add(std::string item){
|
||||
this->items.push_back(item);
|
||||
}
|
||||
int32_t GetId() {
|
||||
return this->id;
|
||||
}
|
||||
|
||||
int GetSampleRate() {
|
||||
return this->samplerate;
|
||||
}
|
||||
|
||||
int GetSamplePeriod(){
|
||||
return this->sampleperiod;
|
||||
}
|
||||
|
||||
int GetDownTime(){
|
||||
return this->downtime;
|
||||
}
|
||||
|
||||
std::list<std::string>& GetItems() {
|
||||
return this->items;
|
||||
}
|
||||
|
||||
};
|
||||
92
vrpmdvaotserver/src/RTSMonitoring/RTSMonitoringTask.cpp
Normal file
92
vrpmdvaotserver/src/RTSMonitoring/RTSMonitoringTask.cpp
Normal file
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
*/
|
||||
|
||||
#include "RTSMonitoringTask.h"
|
||||
//#include "../../include/json.hpp"
|
||||
//#include "../../include/json_fwd.hpp"
|
||||
#include <string>
|
||||
|
||||
|
||||
#define SAMP_SRAM_PACKET_SIZE (256*2)
|
||||
|
||||
|
||||
RTSMonitoringTask::RTSMonitoringTask(/* args */)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
RTSMonitoringTask::~RTSMonitoringTask()
|
||||
{
|
||||
}
|
||||
|
||||
bool RTSMonitoringTask::Init() {
|
||||
//check if the FWIsRunning
|
||||
|
||||
|
||||
if (!isInit) {
|
||||
//if (coproHelper.Init()) {
|
||||
if (pthread_create( &monThread, NULL, &RTSMonitoringTask::Run, this) == 0) {
|
||||
printf("CA7 : virtual_tty_thread creation fails\n");
|
||||
isInit = false;
|
||||
}
|
||||
else {
|
||||
this->coproHelper.Copro_openTtyRpmsg(0,1);
|
||||
isInit = true;
|
||||
}
|
||||
}
|
||||
return isInit;
|
||||
}
|
||||
|
||||
|
||||
bool RTSMonitoringTask::CreateMonitoring(int32_t id, int samplerate, int sampleperiod, int downtime, std::string status){
|
||||
this->rtsMonFrames[id] = new RTSMonFrame(id, samplerate, sampleperiod, downtime);
|
||||
//send to copro for
|
||||
char test[] = "Test";
|
||||
this->coproHelper.Copro_writeTtyRpmsg(0, sizeof(test), test);
|
||||
this->coproHelper.Copro_openTtyRpmsg(id,1);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void* RTSMonitoringTask::Run(void *obj) {
|
||||
|
||||
RTSMonitoringTask* rtsMonTask = static_cast<RTSMonitoringTask*>(obj);
|
||||
std::string newItem;
|
||||
|
||||
char byteBuffer[512];
|
||||
while (1) {
|
||||
// genrate data
|
||||
int i = 1;
|
||||
newItem = "New Item"; //new std::string("New Item: ").concat(new std::string(i));
|
||||
i++;
|
||||
std::map<int32_t, RTSMonFrame*> rtsMonFrame = rtsMonTask->rtsMonFrames;
|
||||
if (rtsMonFrame.size() > 0) {
|
||||
RTSMonFrame* prtsMonFrame = rtsMonFrame.begin()->second;
|
||||
int byte_rd = rtsMonTask->coproHelper.Copro_readTtyRpmsg(prtsMonFrame->GetId(),SAMP_SRAM_PACKET_SIZE, byteBuffer);
|
||||
|
||||
//
|
||||
|
||||
prtsMonFrame->Add(newItem);
|
||||
if (i == 200) {
|
||||
//make a dictonary for the rtsMonFrame id, samplerate, sampleperiod, downtime
|
||||
// boost::python::dict rtsMonFrameDict;
|
||||
// rtsMonFrameDict["id"] = prtsMonFrame->GetId();
|
||||
// rtsMonFrameDict["samplerate"] = prtsMonFrame->GetSampleRate();
|
||||
// rtsMonFrameDict["sampleperiod"] = prtsMonFrame->GetSamplePeriod();
|
||||
// rtsMonFrameDict["downtime"] = prtsMonFrame->GetDownTime();
|
||||
// //make a dictonary for the sampleItems
|
||||
// boost::python::list rtsMonItemsList;
|
||||
// auto items = prtsMonFrame->GetItems();
|
||||
// for (auto iter = items.begin(); iter!= items.end(); ++iter)
|
||||
// {
|
||||
// rtsMonItemsList.append(*iter);
|
||||
// }
|
||||
// rtsMonFrameDict["items"]= rtsMonItemsList;
|
||||
// rtsMonTask->callback();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
42
vrpmdvaotserver/src/RTSMonitoring/RTSMonitoringTask.h
Normal file
42
vrpmdvaotserver/src/RTSMonitoring/RTSMonitoringTask.h
Normal file
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
*/
|
||||
#include "RTSCoproHelper.h"
|
||||
#include "RTSMonFrame.h"
|
||||
#include <pthread.h>
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
|
||||
|
||||
|
||||
class RTSMonitoringTask
|
||||
{
|
||||
private:
|
||||
/* data */
|
||||
//lock Element
|
||||
|
||||
/* The file descriptor used to manage our TTY over RPMSG */
|
||||
int mFdRpmsg[2] = {-1, -1};
|
||||
RTSCoproHelper coproHelper;
|
||||
|
||||
std::map<int32_t, RTSMonFrame*> rtsMonFrames;
|
||||
|
||||
pthread_t monThread;
|
||||
bool isInit = false;
|
||||
|
||||
public:
|
||||
|
||||
RTSMonitoringTask(/* args */);
|
||||
~RTSMonitoringTask();
|
||||
|
||||
//static RTSMonitoringTask& Instance();
|
||||
bool Init();
|
||||
bool CreateMonitoring(int32_t id, int samplerate, int sampleperiod, int downtime, std::string status);
|
||||
bool LoadFW();
|
||||
bool Start();
|
||||
bool Stop();
|
||||
|
||||
private:
|
||||
static void* Run(void *obj);
|
||||
|
||||
};
|
||||
50
vrpmdvaotserver/src/SwaggerComponent.hpp
Normal file
50
vrpmdvaotserver/src/SwaggerComponent.hpp
Normal file
@@ -0,0 +1,50 @@
|
||||
|
||||
#ifndef VRPMDV_SWAGGERCOMPONENT_HPP
|
||||
#define VRPMDV_SWAGGERCOMPONENT_HPP
|
||||
|
||||
#include "oatpp-swagger/Model.hpp"
|
||||
#include "oatpp-swagger/Resources.hpp"
|
||||
#include "oatpp/core/macro/component.hpp"
|
||||
|
||||
/**
|
||||
* Swagger ui is served at
|
||||
* http://host:port/swagger/ui
|
||||
*/
|
||||
class SwaggerComponent {
|
||||
public:
|
||||
|
||||
/**
|
||||
* General API docs info
|
||||
*/
|
||||
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::swagger::DocumentInfo>, swaggerDocumentInfo)([] {
|
||||
|
||||
oatpp::swagger::DocumentInfo::Builder builder;
|
||||
|
||||
builder
|
||||
.setTitle("User entity service")
|
||||
.setDescription("CRUD API Example project with swagger docs")
|
||||
.setVersion("1.0")
|
||||
.setContactName("Ivan Ovsyanochka")
|
||||
.setContactUrl("https://oatpp.io/")
|
||||
|
||||
.setLicenseName("Apache License, Version 2.0")
|
||||
.setLicenseUrl("http://www.apache.org/licenses/LICENSE-2.0")
|
||||
|
||||
.addServer("http://localhost:8000", "server on localhost");
|
||||
|
||||
return builder.build();
|
||||
|
||||
}());
|
||||
|
||||
|
||||
/**
|
||||
* Swagger-Ui Resources (<oatpp-examples>/lib/oatpp-swagger/res)
|
||||
*/
|
||||
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::swagger::Resources>, swaggerResources)([] {
|
||||
// Make sure to specify correct full path to oatpp-swagger/res folder !!!
|
||||
return oatpp::swagger::Resources::loadResources(OATPP_SWAGGER_RES_PATH);
|
||||
}());
|
||||
|
||||
};
|
||||
|
||||
#endif /* SwaggerComponent_hpp */
|
||||
125
vrpmdvaotserver/src/controller/MonitoringController.hpp
Normal file
125
vrpmdvaotserver/src/controller/MonitoringController.hpp
Normal file
@@ -0,0 +1,125 @@
|
||||
|
||||
#ifndef VRPMDV_MONITORINGCONTROLLER_HPP
|
||||
#define VRPMDV_MONITORINGCONTROLLER_HPP
|
||||
|
||||
#include "service/MonitoringService.hpp"
|
||||
|
||||
#include "oatpp/web/server/api/ApiController.hpp"
|
||||
#include "oatpp/parser/json/mapping/ObjectMapper.hpp"
|
||||
#include "oatpp/core/macro/codegen.hpp"
|
||||
|
||||
#include OATPP_CODEGEN_BEGIN(ApiController) //<- Begin Codegen
|
||||
|
||||
/**
|
||||
* Monitoring REST controller.
|
||||
*/
|
||||
class MonitoringController : public oatpp::web::server::api::ApiController {
|
||||
public:
|
||||
MonitoringController(OATPP_COMPONENT(std::shared_ptr<ObjectMapper>, objectMapper))
|
||||
: oatpp::web::server::api::ApiController(objectMapper)
|
||||
{}
|
||||
private:
|
||||
MonitoringService m_MonitoringService; // Create Monitoring service.
|
||||
public:
|
||||
|
||||
static std::shared_ptr<MonitoringController> createShared(
|
||||
OATPP_COMPONENT(std::shared_ptr<ObjectMapper>, objectMapper) // Inject objectMapper component here as default parameter
|
||||
){
|
||||
return std::make_shared<MonitoringController>(objectMapper);
|
||||
}
|
||||
|
||||
ENDPOINT_INFO(createMonitoring) {
|
||||
info->summary = "Create new Monitoring";
|
||||
|
||||
info->addConsumes<Object<InputMonitoringDto>>("application/json");
|
||||
|
||||
info->addResponse<Object<MonitoringDto>>(Status::CODE_200, "application/json");
|
||||
info->addResponse<Object<StatusDto>>(Status::CODE_404, "application/json");
|
||||
info->addResponse<Object<StatusDto>>(Status::CODE_500, "application/json");
|
||||
}
|
||||
ENDPOINT("POST", "monitorings", createMonitoring,
|
||||
BODY_DTO(Object<InputMonitoringDto>, monitoringInputDto))
|
||||
{
|
||||
return createDtoResponse(Status::CODE_200, m_MonitoringService.createMonitoring(monitoringInputDto));
|
||||
}
|
||||
|
||||
|
||||
ENDPOINT_INFO(putMonitoring) {
|
||||
info->summary = "Update Monitoring by id";
|
||||
|
||||
info->addConsumes<Object<MonitoringDto>>("application/json");
|
||||
|
||||
info->addResponse<Object<MonitoringDto>>(Status::CODE_200, "application/json");
|
||||
info->addResponse<Object<StatusDto>>(Status::CODE_404, "application/json");
|
||||
info->addResponse<Object<StatusDto>>(Status::CODE_500, "application/json");
|
||||
|
||||
info->pathParams["id"].description = "Identifier";
|
||||
}
|
||||
ENDPOINT("POST", "monitorings/{id}", putMonitoring,
|
||||
PATH(Int32, id),
|
||||
BODY_DTO(Object<MonitoringDto>, monitoringDto))
|
||||
{
|
||||
return createDtoResponse(Status::CODE_200, m_MonitoringService.updateMonitoring(monitoringDto, id));
|
||||
}
|
||||
|
||||
|
||||
ENDPOINT_INFO(getMonitoringById) {
|
||||
info->summary = "Get one Monitoring by id";
|
||||
|
||||
info->addResponse<Object<MonitoringDto>>(Status::CODE_200, "application/json");
|
||||
info->addResponse<Object<StatusDto>>(Status::CODE_404, "application/json");
|
||||
info->addResponse<Object<StatusDto>>(Status::CODE_500, "application/json");
|
||||
|
||||
info->pathParams["id"].description = "Identifier";
|
||||
}
|
||||
ENDPOINT("GET", "monitorings/{id}", getMonitoringById,
|
||||
PATH(Int32, id))
|
||||
{
|
||||
return createDtoResponse(Status::CODE_200, m_MonitoringService.getMonitoringById(id));
|
||||
}
|
||||
|
||||
ENDPOINT_INFO(getMonitorings) {
|
||||
info->summary = "get all stored monitorings";
|
||||
|
||||
|
||||
info->addResponse<List<Object<MonitoringDto>>>(Status::CODE_200, "application/json");
|
||||
info->addResponse<Object<StatusDto>>(Status::CODE_500, "application/json");
|
||||
}
|
||||
ENDPOINT("GET", "monitorings/", getMonitorings )
|
||||
{
|
||||
return createDtoResponse(Status::CODE_200, m_MonitoringService.getAllMonitorings());
|
||||
}
|
||||
|
||||
// ENDPOINT_INFO(getMonitorings) {
|
||||
// info->summary = "get all stored monitorings";
|
||||
|
||||
// info->addResponse<oatpp::Object<MonitoringsPageDto>>(Status::CODE_200, "application/json");
|
||||
// info->addResponse<Object<StatusDto>>(Status::CODE_500, "application/json");
|
||||
// }
|
||||
// ENDPOINT("GET", "monitorings/offset/{offset}/limit/{limit}", getMonitorings,
|
||||
// PATH(UInt32, offset),
|
||||
// PATH(UInt32, limit))
|
||||
// {
|
||||
// return createDtoResponse(Status::CODE_200, m_MonitoringService.getAllMonitorings(offset, limit));
|
||||
// }
|
||||
|
||||
|
||||
ENDPOINT_INFO(deleteMonitoring) {
|
||||
info->summary = "Delete Monitoring by id";
|
||||
|
||||
info->addResponse<Object<StatusDto>>(Status::CODE_200, "application/json");
|
||||
info->addResponse<Object<StatusDto>>(Status::CODE_500, "application/json");
|
||||
|
||||
info->pathParams["id"].description = "Monitoring Identifier";
|
||||
}
|
||||
ENDPOINT("DELETE", "monitorings/{id}", deleteMonitoring,
|
||||
PATH(Int32, id))
|
||||
{
|
||||
return createDtoResponse(Status::CODE_200, m_MonitoringService.deleteMonitoringById(id));
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#include OATPP_CODEGEN_END(ApiController) //<- End Codegen
|
||||
|
||||
#endif /* MonitoringController_hpp */
|
||||
68
vrpmdvaotserver/src/controller/StaticController.hpp
Normal file
68
vrpmdvaotserver/src/controller/StaticController.hpp
Normal file
@@ -0,0 +1,68 @@
|
||||
|
||||
#ifndef VRPMDV_STATICCONTROLLER_HPP
|
||||
#define VRPMDV_STATICCONTROLLER_HPP
|
||||
|
||||
#include "oatpp/web/server/api/ApiController.hpp"
|
||||
#include "oatpp/parser/json/mapping/ObjectMapper.hpp"
|
||||
#include "oatpp/core/macro/codegen.hpp"
|
||||
#include "oatpp/core/macro/component.hpp"
|
||||
#include <fstream>
|
||||
|
||||
#include OATPP_CODEGEN_BEGIN(ApiController) //<- Begin Codegen
|
||||
|
||||
class StaticController : public oatpp::web::server::api::ApiController {
|
||||
private:
|
||||
oatpp::String m_resDir = "./";
|
||||
|
||||
public:
|
||||
StaticController(const std::shared_ptr<ObjectMapper>& objectMapper)
|
||||
: oatpp::web::server::api::ApiController(objectMapper)
|
||||
{}
|
||||
public:
|
||||
|
||||
static std::shared_ptr<StaticController> createShared(
|
||||
OATPP_COMPONENT(std::shared_ptr<ObjectMapper>, objectMapper) // Inject objectMapper component here as default parameter
|
||||
){
|
||||
return std::make_shared<StaticController>(objectMapper);
|
||||
}
|
||||
|
||||
ENDPOINT("GET", "/", root) {
|
||||
// const char* html =
|
||||
// "<html lang='en'>"
|
||||
// " <head>"
|
||||
// " <meta charset=utf-8/>"
|
||||
// " </head>"
|
||||
// " <body>"
|
||||
// " <p>Hello CRUD example project!</p>"
|
||||
// " <a href='swagger/ui'>Checkout Swagger-UI page</a>"
|
||||
// " </body>"
|
||||
// "</html>";
|
||||
|
||||
oatpp::String html;// = loadFromFile("index.html");
|
||||
const oatpp::String htmlsend = html.loadFromFile("./webui/index.html");
|
||||
auto response = createResponse(Status::CODE_200, htmlsend);
|
||||
response->putHeader(Header::CONTENT_TYPE, "text/html");
|
||||
return response;
|
||||
}
|
||||
|
||||
// oatpp::String loadFromFile(const char* fileName) {
|
||||
|
||||
// auto fullFilename = m_resDir + fileName;
|
||||
|
||||
// std::ifstream file (fullFilename->c_str(), std::ios::in|std::ios::binary|std::ios::ate);
|
||||
|
||||
// if (file.is_open()) {
|
||||
|
||||
// auto result = oatpp::String((v_int32) file.tellg());
|
||||
// file.seekg(0, std::ios::beg);
|
||||
// file.read((char*)result->data(), result->size());
|
||||
// file.close();
|
||||
// return result;
|
||||
|
||||
// }
|
||||
|
||||
};
|
||||
|
||||
#include OATPP_CODEGEN_END(ApiController) //<- End Codegen
|
||||
|
||||
#endif //VRPMDV_STATICCONTROLLER_HPP
|
||||
115
vrpmdvaotserver/src/controller/UserController.hpp
Normal file
115
vrpmdvaotserver/src/controller/UserController.hpp
Normal file
@@ -0,0 +1,115 @@
|
||||
|
||||
#ifndef VRPMDV_USERCONTROLLER_HPP
|
||||
#define VRPMDV_USERCONTROLLER_HPP
|
||||
|
||||
#include "service/UserService.hpp"
|
||||
|
||||
#include "oatpp/web/server/api/ApiController.hpp"
|
||||
#include "oatpp/parser/json/mapping/ObjectMapper.hpp"
|
||||
#include "oatpp/core/macro/codegen.hpp"
|
||||
|
||||
#include OATPP_CODEGEN_BEGIN(ApiController) //<- Begin Codegen
|
||||
|
||||
/**
|
||||
* User REST controller.
|
||||
*/
|
||||
class UserController : public oatpp::web::server::api::ApiController {
|
||||
public:
|
||||
UserController(OATPP_COMPONENT(std::shared_ptr<ObjectMapper>, objectMapper))
|
||||
: oatpp::web::server::api::ApiController(objectMapper)
|
||||
{}
|
||||
private:
|
||||
UserService m_userService; // Create user service.
|
||||
public:
|
||||
|
||||
static std::shared_ptr<UserController> createShared(
|
||||
OATPP_COMPONENT(std::shared_ptr<ObjectMapper>, objectMapper) // Inject objectMapper component here as default parameter
|
||||
){
|
||||
return std::make_shared<UserController>(objectMapper);
|
||||
}
|
||||
|
||||
ENDPOINT_INFO(createUser) {
|
||||
info->summary = "Create new User";
|
||||
|
||||
info->addConsumes<Object<UserDto>>("application/json");
|
||||
|
||||
info->addResponse<Object<UserDto>>(Status::CODE_200, "application/json");
|
||||
info->addResponse<Object<StatusDto>>(Status::CODE_404, "application/json");
|
||||
info->addResponse<Object<StatusDto>>(Status::CODE_500, "application/json");
|
||||
}
|
||||
ENDPOINT("POST", "users", createUser,
|
||||
BODY_DTO(Object<UserDto>, userDto))
|
||||
{
|
||||
return createDtoResponse(Status::CODE_200, m_userService.createUser(userDto));
|
||||
}
|
||||
|
||||
|
||||
ENDPOINT_INFO(putUser) {
|
||||
info->summary = "Update User by userId";
|
||||
|
||||
info->addConsumes<Object<UserDto>>("application/json");
|
||||
|
||||
info->addResponse<Object<UserDto>>(Status::CODE_200, "application/json");
|
||||
info->addResponse<Object<StatusDto>>(Status::CODE_404, "application/json");
|
||||
info->addResponse<Object<StatusDto>>(Status::CODE_500, "application/json");
|
||||
|
||||
info->pathParams["userId"].description = "User Identifier";
|
||||
}
|
||||
ENDPOINT("PUT", "users/{userId}", putUser,
|
||||
PATH(Int32, userId),
|
||||
BODY_DTO(Object<UserDto>, userDto))
|
||||
{
|
||||
userDto->id = userId;
|
||||
return createDtoResponse(Status::CODE_200, m_userService.updateUser(userDto));
|
||||
}
|
||||
|
||||
|
||||
ENDPOINT_INFO(getUserById) {
|
||||
info->summary = "Get one User by userId";
|
||||
|
||||
info->addResponse<Object<UserDto>>(Status::CODE_200, "application/json");
|
||||
info->addResponse<Object<StatusDto>>(Status::CODE_404, "application/json");
|
||||
info->addResponse<Object<StatusDto>>(Status::CODE_500, "application/json");
|
||||
|
||||
info->pathParams["userId"].description = "User Identifier";
|
||||
}
|
||||
ENDPOINT("GET", "users/{userId}", getUserById,
|
||||
PATH(Int32, userId))
|
||||
{
|
||||
return createDtoResponse(Status::CODE_200, m_userService.getUserById(userId));
|
||||
}
|
||||
|
||||
|
||||
ENDPOINT_INFO(getUsers) {
|
||||
info->summary = "get all stored users";
|
||||
|
||||
info->addResponse<oatpp::Object<UsersPageDto>>(Status::CODE_200, "application/json");
|
||||
info->addResponse<Object<StatusDto>>(Status::CODE_500, "application/json");
|
||||
}
|
||||
ENDPOINT("GET", "users/offset/{offset}/limit/{limit}", getUsers,
|
||||
PATH(UInt32, offset),
|
||||
PATH(UInt32, limit))
|
||||
{
|
||||
return createDtoResponse(Status::CODE_200, m_userService.getAllUsers(offset, limit));
|
||||
}
|
||||
|
||||
|
||||
ENDPOINT_INFO(deleteUser) {
|
||||
info->summary = "Delete User by userId";
|
||||
|
||||
info->addResponse<Object<StatusDto>>(Status::CODE_200, "application/json");
|
||||
info->addResponse<Object<StatusDto>>(Status::CODE_500, "application/json");
|
||||
|
||||
info->pathParams["userId"].description = "User Identifier";
|
||||
}
|
||||
ENDPOINT("DELETE", "users/{userId}", deleteUser,
|
||||
PATH(Int32, userId))
|
||||
{
|
||||
return createDtoResponse(Status::CODE_200, m_userService.deleteUserById(userId));
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#include OATPP_CODEGEN_END(ApiController) //<- End Codegen
|
||||
|
||||
#endif /* UserController_hpp */
|
||||
104
vrpmdvaotserver/src/db/MonitoringDb.hpp
Normal file
104
vrpmdvaotserver/src/db/MonitoringDb.hpp
Normal file
@@ -0,0 +1,104 @@
|
||||
|
||||
#ifndef VRPMDV_MONITORINGDB_HPP
|
||||
#define VRPMDV_MONITORINGDB_HPP
|
||||
|
||||
#include "dto/MonitoringDto.hpp"
|
||||
#include "dto/InputMonitoringDto.hpp"
|
||||
#include "dto/UserDto.hpp"
|
||||
#include "oatpp-sqlite/orm.hpp"
|
||||
|
||||
|
||||
#include OATPP_CODEGEN_BEGIN(DbClient) //<- Begin Codegen
|
||||
|
||||
/**
|
||||
* MonitoringDb client definitions.
|
||||
*/
|
||||
class MonitoringDb : public oatpp::orm::DbClient {
|
||||
public:
|
||||
|
||||
MonitoringDb(const std::shared_ptr<oatpp::orm::Executor>& executor)
|
||||
: oatpp::orm::DbClient(executor)
|
||||
{
|
||||
|
||||
oatpp::orm::SchemaMigration migration(executor);
|
||||
migration.addFile(1 /* start from version 1 */, DATABASE_MIGRATIONS "/002_init.sql");
|
||||
// TODO - Add more migrations here.
|
||||
migration.migrate(); // <-- run migrations. This guy will throw on error.
|
||||
|
||||
auto version = executor->getSchemaVersion();
|
||||
OATPP_LOGD("MonitoringDb", "Migration - OK. Version=%ld.", version);
|
||||
|
||||
}
|
||||
|
||||
QUERY(createUser,
|
||||
"INSERT INTO AppUser"
|
||||
"(username, email, password, role) VALUES "
|
||||
"(:user.username, :user.email, :user.password, :user.role);",
|
||||
PARAM(oatpp::Object<UserDto>, user))
|
||||
|
||||
QUERY(updateUser,
|
||||
"UPDATE AppUser "
|
||||
"SET "
|
||||
" username=:user.username, "
|
||||
" email=:user.email, "
|
||||
" password=:user.password, "
|
||||
" role=:user.role "
|
||||
"WHERE "
|
||||
" id=:user.id;",
|
||||
PARAM(oatpp::Object<UserDto>, user))
|
||||
|
||||
QUERY(getUserById,
|
||||
"SELECT * FROM AppUser WHERE id=:id;",
|
||||
PARAM(oatpp::Int32, id))
|
||||
|
||||
QUERY(getAllUsers,
|
||||
"SELECT * FROM AppUser LIMIT :limit OFFSET :offset;",
|
||||
PARAM(oatpp::UInt32, offset),
|
||||
PARAM(oatpp::UInt32, limit))
|
||||
|
||||
QUERY(deleteUserById,
|
||||
"DELETE FROM AppUser WHERE id=:id;",
|
||||
PARAM(oatpp::Int32, id))
|
||||
|
||||
|
||||
QUERY(createMonitoring,
|
||||
"INSERT INTO Monitoring"
|
||||
"(name, samplerate, sampleperiod, downtime, status, created_at) VALUES "
|
||||
"(:monitoring.name, :monitoring.samplerate, :monitoring.sampleperiod, :monitoring.downtime, :monitoring.status, :created_at);",
|
||||
PARAM(oatpp::Object<InputMonitoringDto>, monitoring),
|
||||
PARAM(oatpp::String, created_at))
|
||||
|
||||
QUERY(updateMonitoring,
|
||||
"UPDATE Monitoring "
|
||||
"SET "
|
||||
" name=:monitoring.name, "
|
||||
" samplerate=:monitoring.samplerate, "
|
||||
" sampleperiod=:monitoring.sampleperiod, "
|
||||
" downtime=:monitoring.downtime "
|
||||
" status=:monitoring.status "
|
||||
"WHERE "
|
||||
" id=:id;",
|
||||
PARAM(oatpp::Object<MonitoringDto>, monitoring),
|
||||
PARAM(oatpp::Int32, id))
|
||||
|
||||
QUERY(getMonitoringById,
|
||||
"SELECT * FROM Monitoring WHERE id=:id;",
|
||||
PARAM(oatpp::Int32, id))
|
||||
|
||||
QUERY(getSelectedMonitorings,
|
||||
"SELECT * FROM Monitoring LIMIT :limit OFFSET :offset;",
|
||||
PARAM(oatpp::UInt32, offset),
|
||||
PARAM(oatpp::UInt32, limit))
|
||||
|
||||
QUERY(getAllMonitorings,
|
||||
"SELECT * FROM Monitoring;")
|
||||
|
||||
QUERY(deleteMonitoringById,
|
||||
"DELETE FROM Monitoring WHERE id=:id;",
|
||||
PARAM(oatpp::Int32, id))
|
||||
|
||||
};
|
||||
|
||||
#include OATPP_CODEGEN_END(DbClient) //<- End Codegen
|
||||
|
||||
#endif //CRUD_USERDB_HPP
|
||||
64
vrpmdvaotserver/src/db/UserDb.hpp
Normal file
64
vrpmdvaotserver/src/db/UserDb.hpp
Normal file
@@ -0,0 +1,64 @@
|
||||
|
||||
#ifndef VRPMDV_USERDB_HPP
|
||||
#define VRPMDV_USERDB_HPP
|
||||
|
||||
#include "dto/UserDto.hpp"
|
||||
#include "oatpp-sqlite/orm.hpp"
|
||||
|
||||
#include OATPP_CODEGEN_BEGIN(DbClient) //<- Begin Codegen
|
||||
|
||||
/**
|
||||
* UserDb client definitions.
|
||||
*/
|
||||
class UserDb : public oatpp::orm::DbClient {
|
||||
public:
|
||||
|
||||
UserDb(const std::shared_ptr<oatpp::orm::Executor>& executor)
|
||||
: oatpp::orm::DbClient(executor)
|
||||
{
|
||||
|
||||
oatpp::orm::SchemaMigration migration(executor);
|
||||
migration.addFile(1 /* start from version 1 */, DATABASE_MIGRATIONS "/001_init.sql");
|
||||
// TODO - Add more migrations here.
|
||||
migration.migrate(); // <-- run migrations. This guy will throw on error.
|
||||
|
||||
auto version = executor->getSchemaVersion();
|
||||
OATPP_LOGD("UserDb", "Migration - OK. Version=%ld.", version);
|
||||
|
||||
}
|
||||
|
||||
QUERY(createUser,
|
||||
"INSERT INTO AppUser"
|
||||
"(username, email, password, role) VALUES "
|
||||
"(:user.username, :user.email, :user.password, :user.role);",
|
||||
PARAM(oatpp::Object<UserDto>, user))
|
||||
|
||||
QUERY(updateUser,
|
||||
"UPDATE AppUser "
|
||||
"SET "
|
||||
" username=:user.username, "
|
||||
" email=:user.email, "
|
||||
" password=:user.password, "
|
||||
" role=:user.role "
|
||||
"WHERE "
|
||||
" id=:user.id;",
|
||||
PARAM(oatpp::Object<UserDto>, user))
|
||||
|
||||
QUERY(getUserById,
|
||||
"SELECT * FROM AppUser WHERE id=:id;",
|
||||
PARAM(oatpp::Int32, id))
|
||||
|
||||
QUERY(getAllUsers,
|
||||
"SELECT * FROM AppUser LIMIT :limit OFFSET :offset;",
|
||||
PARAM(oatpp::UInt32, offset),
|
||||
PARAM(oatpp::UInt32, limit))
|
||||
|
||||
QUERY(deleteUserById,
|
||||
"DELETE FROM AppUser WHERE id=:id;",
|
||||
PARAM(oatpp::Int32, id))
|
||||
|
||||
};
|
||||
|
||||
#include OATPP_CODEGEN_END(DbClient) //<- End Codegen
|
||||
|
||||
#endif //CRUD_USERDB_HPP
|
||||
25
vrpmdvaotserver/src/dto/InputMonitoringDto.hpp
Normal file
25
vrpmdvaotserver/src/dto/InputMonitoringDto.hpp
Normal file
@@ -0,0 +1,25 @@
|
||||
#ifndef VRPMDV_INPUTMONITORINGDTO_HPP
|
||||
#define VRPMDV_INPUTMONITORINGDTO_HPP
|
||||
|
||||
#include "oatpp/core/macro/codegen.hpp"
|
||||
#include "oatpp/core/Types.hpp"
|
||||
#include "MonStatusEnum.hpp"
|
||||
|
||||
#include OATPP_CODEGEN_BEGIN(DTO)
|
||||
|
||||
|
||||
class InputMonitoringDto : public oatpp::DTO {
|
||||
|
||||
DTO_INIT(InputMonitoringDto, DTO)
|
||||
|
||||
DTO_FIELD(String, name, "name");
|
||||
DTO_FIELD(String, samplerate, "samplerate");
|
||||
DTO_FIELD(String, sampleperiod, "sampleperiod");
|
||||
DTO_FIELD(String, downtime, "downtime");
|
||||
DTO_FIELD(String, owner, "owner");
|
||||
DTO_FIELD(Enum<MonStatus>::AsString, status, "status");
|
||||
};
|
||||
|
||||
#include OATPP_CODEGEN_END(DTO)
|
||||
|
||||
#endif /* UserDto_hpp */
|
||||
18
vrpmdvaotserver/src/dto/MonStatusEnum.hpp
Normal file
18
vrpmdvaotserver/src/dto/MonStatusEnum.hpp
Normal file
@@ -0,0 +1,18 @@
|
||||
#ifndef VRPMDV_MONSTATUSENUM_HPP
|
||||
#define VRPMDV_MONSTATUSENUM_HPP
|
||||
|
||||
#include "oatpp/core/macro/codegen.hpp"
|
||||
#include "oatpp/core/Types.hpp"
|
||||
|
||||
|
||||
#include OATPP_CODEGEN_BEGIN(DTO)
|
||||
|
||||
ENUM(MonStatus, v_int32,
|
||||
VALUE(DEACTIVATED, 0, "off"),
|
||||
VALUE(STOPPED, 1, "stopped"),
|
||||
VALUE(STARTED, 2, "started")
|
||||
)
|
||||
|
||||
#include OATPP_CODEGEN_END(DTO)
|
||||
|
||||
#endif /* UserDto_hpp */
|
||||
26
vrpmdvaotserver/src/dto/MonitoringDto.hpp
Normal file
26
vrpmdvaotserver/src/dto/MonitoringDto.hpp
Normal file
@@ -0,0 +1,26 @@
|
||||
#ifndef VRPMDV_MONITORINGDTO_HPP
|
||||
#define VRPMDV_MONITORINGDTO_HPP
|
||||
|
||||
#include "oatpp/core/macro/codegen.hpp"
|
||||
#include "oatpp/core/Types.hpp"
|
||||
#include "MonStatusEnum.hpp"
|
||||
|
||||
#include OATPP_CODEGEN_BEGIN(DTO)
|
||||
|
||||
class MonitoringDto : public oatpp::DTO {
|
||||
|
||||
DTO_INIT(MonitoringDto, DTO)
|
||||
|
||||
DTO_FIELD(Int32, id);
|
||||
DTO_FIELD(String, name, "name");
|
||||
DTO_FIELD(String, samplerate, "samplerate");
|
||||
DTO_FIELD(String, sampleperiod, "sampleperiod");
|
||||
DTO_FIELD(String, downtime, "downtime");
|
||||
//DTO_FIELD(String, owner, "owner");
|
||||
DTO_FIELD(Enum<MonStatus>::AsString, status, "status");
|
||||
DTO_FIELD(String, created_at, "created_at");
|
||||
};
|
||||
|
||||
#include OATPP_CODEGEN_END(DTO)
|
||||
|
||||
#endif /* UserDto_hpp */
|
||||
48
vrpmdvaotserver/src/dto/PageDto.hpp
Normal file
48
vrpmdvaotserver/src/dto/PageDto.hpp
Normal file
@@ -0,0 +1,48 @@
|
||||
|
||||
#ifndef VRPMDV_PAGEDTO_HPP
|
||||
#define VRPMDV_PAGEDTO_HPP
|
||||
|
||||
#include "UserDto.hpp"
|
||||
#include "MonitoringDto.hpp"
|
||||
|
||||
#include OATPP_CODEGEN_BEGIN(DTO)
|
||||
|
||||
template<class T>
|
||||
class PageDto : public oatpp::DTO {
|
||||
|
||||
DTO_INIT(PageDto, DTO)
|
||||
|
||||
DTO_FIELD(UInt32, offset);
|
||||
DTO_FIELD(UInt32, limit);
|
||||
DTO_FIELD(UInt32, count);
|
||||
DTO_FIELD(Vector<T>, items);
|
||||
|
||||
};
|
||||
|
||||
template<class T>
|
||||
class MonPageDto : public oatpp::DTO {
|
||||
|
||||
DTO_INIT(MonPageDto, DTO)
|
||||
|
||||
DTO_FIELD(Vector<T>, items);
|
||||
|
||||
};
|
||||
|
||||
|
||||
class UsersPageDto : public PageDto<oatpp::Object<UserDto>> {
|
||||
|
||||
DTO_INIT(UsersPageDto, PageDto<oatpp::Object<UserDto>>)
|
||||
|
||||
};
|
||||
|
||||
|
||||
class MonitoringsPageDto : public MonPageDto<oatpp::Object<MonitoringDto>> {
|
||||
|
||||
DTO_INIT(MonitoringsPageDto, MonPageDto<oatpp::Object<MonitoringDto>>)
|
||||
|
||||
};
|
||||
|
||||
|
||||
#include OATPP_CODEGEN_END(DTO)
|
||||
|
||||
#endif //CRUD_PAGEDTO_HPP
|
||||
33
vrpmdvaotserver/src/dto/StatusDto.hpp
Normal file
33
vrpmdvaotserver/src/dto/StatusDto.hpp
Normal file
@@ -0,0 +1,33 @@
|
||||
|
||||
#ifndef VRPMDV_STATUSDTO_HPP
|
||||
#define VRPMDV_STATUSDTO_HPP
|
||||
|
||||
#include "oatpp/core/macro/codegen.hpp"
|
||||
#include "oatpp/core/Types.hpp"
|
||||
|
||||
#include OATPP_CODEGEN_BEGIN(DTO)
|
||||
|
||||
class StatusDto : public oatpp::DTO {
|
||||
|
||||
DTO_INIT(StatusDto, DTO)
|
||||
|
||||
DTO_FIELD_INFO(status) {
|
||||
info->description = "Short status text";
|
||||
}
|
||||
DTO_FIELD(String, status);
|
||||
|
||||
DTO_FIELD_INFO(code) {
|
||||
info->description = "Status code";
|
||||
}
|
||||
DTO_FIELD(Int32, code);
|
||||
|
||||
DTO_FIELD_INFO(message) {
|
||||
info->description = "Verbose message";
|
||||
}
|
||||
DTO_FIELD(String, message);
|
||||
|
||||
};
|
||||
|
||||
#include OATPP_CODEGEN_END(DTO)
|
||||
|
||||
#endif //CRUD_STATUSDTO_HPP
|
||||
28
vrpmdvaotserver/src/dto/UserDto.hpp
Normal file
28
vrpmdvaotserver/src/dto/UserDto.hpp
Normal file
@@ -0,0 +1,28 @@
|
||||
#ifndef VRPMDV_USERDTO_HPP
|
||||
#define VRPMDV_USERDTO_HPP
|
||||
|
||||
#include "oatpp/core/macro/codegen.hpp"
|
||||
#include "oatpp/core/Types.hpp"
|
||||
|
||||
#include OATPP_CODEGEN_BEGIN(DTO)
|
||||
|
||||
ENUM(Role, v_int32,
|
||||
VALUE(GUEST, 0, "ROLE_GUEST"),
|
||||
VALUE(ADMIN, 1, "ROLE_ADMIN")
|
||||
)
|
||||
|
||||
class UserDto : public oatpp::DTO {
|
||||
|
||||
DTO_INIT(UserDto, DTO)
|
||||
|
||||
DTO_FIELD(Int32, id);
|
||||
DTO_FIELD(String, userName, "username");
|
||||
DTO_FIELD(String, email, "email");
|
||||
DTO_FIELD(String, password, "password");
|
||||
DTO_FIELD(Enum<Role>::AsString, role, "role");
|
||||
|
||||
};
|
||||
|
||||
#include OATPP_CODEGEN_END(DTO)
|
||||
|
||||
#endif /* UserDto_hpp */
|
||||
114
vrpmdvaotserver/src/service/MonitoringService.cpp
Normal file
114
vrpmdvaotserver/src/service/MonitoringService.cpp
Normal file
@@ -0,0 +1,114 @@
|
||||
|
||||
#include "MonitoringService.hpp"
|
||||
|
||||
oatpp::Object<MonitoringDto> MonitoringService::createMonitoring(const oatpp::Object<InputMonitoringDto>& dto) {
|
||||
|
||||
time_t _tm =time(NULL );
|
||||
struct tm * curtime = localtime ( &_tm );
|
||||
oatpp:: String created_at = asctime(curtime);
|
||||
|
||||
auto dbResult = m_database->createMonitoring(dto, created_at);
|
||||
OATPP_ASSERT_HTTP(dbResult->isSuccess(), Status::CODE_500, dbResult->getErrorMessage());
|
||||
|
||||
auto id = oatpp::sqlite::Utils::getLastInsertRowId(dbResult->getConnection());
|
||||
|
||||
//now init the montask and create the monitoring
|
||||
rtsMonTask.Init();
|
||||
//int isamplerate = dto->samplerate;
|
||||
//rtsMonTask.CreateMonitoring(id, dto->samplerate, dto->sampleperiod, dto->downtime, dto->status);
|
||||
|
||||
return getMonitoringById((v_int32) id);
|
||||
|
||||
}
|
||||
|
||||
oatpp::Object<MonitoringDto> MonitoringService::updateMonitoring(const oatpp::Object<MonitoringDto>& dto, const oatpp::Int32& id) {
|
||||
|
||||
auto dbResult = m_database->updateMonitoring(dto, id);
|
||||
OATPP_ASSERT_HTTP(dbResult->isSuccess(), Status::CODE_500, dbResult->getErrorMessage());
|
||||
return getMonitoringById(id);
|
||||
|
||||
}
|
||||
|
||||
oatpp::Object<MonitoringDto> MonitoringService::getMonitoringById(const oatpp::Int32& id, const oatpp::provider::ResourceHandle<oatpp::orm::Connection>& connection) {
|
||||
|
||||
auto dbResult = m_database->getMonitoringById(id);
|
||||
OATPP_ASSERT_HTTP(dbResult->isSuccess(), Status::CODE_500, dbResult->getErrorMessage());
|
||||
OATPP_ASSERT_HTTP(dbResult->hasMoreToFetch(), Status::CODE_404, "Monitoring not found");
|
||||
|
||||
auto result = dbResult->fetch<oatpp::Vector<oatpp::Object<MonitoringDto>>>();
|
||||
OATPP_ASSERT_HTTP(result->size() == 1, Status::CODE_500, "Unknown error");
|
||||
|
||||
return result[0];
|
||||
|
||||
}
|
||||
|
||||
oatpp::Object<PageDto<oatpp::Object<MonitoringDto>>> MonitoringService::getSelectedMonitorings(const oatpp::UInt32& offset, const oatpp::UInt32& limit) {
|
||||
|
||||
oatpp::UInt32 countToFetch = limit;
|
||||
|
||||
if(limit > 10) {
|
||||
countToFetch = 10;
|
||||
}
|
||||
|
||||
auto dbResult = m_database->getSelectedMonitorings(offset, countToFetch);
|
||||
OATPP_ASSERT_HTTP(dbResult->isSuccess(), Status::CODE_500, dbResult->getErrorMessage());
|
||||
|
||||
auto items = dbResult->fetch<oatpp::Vector<oatpp::Object<MonitoringDto>>>();
|
||||
|
||||
auto page = PageDto<oatpp::Object<MonitoringDto>>::createShared();
|
||||
page->offset = offset;
|
||||
page->limit = countToFetch;
|
||||
page->count = items->size();
|
||||
page->items = items;
|
||||
|
||||
return page;
|
||||
|
||||
}
|
||||
|
||||
|
||||
oatpp::Vector<oatpp::Object<MonitoringDto>> MonitoringService::getAllMonitorings() {
|
||||
|
||||
auto dbResult = m_database->getAllMonitorings();
|
||||
OATPP_ASSERT_HTTP(dbResult->isSuccess(), Status::CODE_500, dbResult->getErrorMessage());
|
||||
|
||||
auto items = dbResult->fetch<oatpp::Vector<oatpp::Object<MonitoringDto>>>();
|
||||
|
||||
return items;
|
||||
|
||||
}
|
||||
|
||||
// oatpp::Vector<oatpp::Object<MonitoringDto>> MonitoringService::getAllMonitorings() {
|
||||
|
||||
// auto dbResult = m_database->getAllMonitorings();
|
||||
// OATPP_ASSERT_HTTP(dbResult->isSuccess(), Status::CODE_500, dbResult->getErrorMessage());
|
||||
|
||||
// auto items = dbResult->fetch<oatpp::Vector<oatpp::Object<MonitoringDto>>>();
|
||||
|
||||
// return items;
|
||||
|
||||
// }
|
||||
|
||||
// oatpp::Object<MonPageDto<oatpp::Object<MonitoringDto>>> MonitoringService::getAllMonitorings() {
|
||||
|
||||
// auto dbResult = m_database->getAllMonitorings();
|
||||
// OATPP_ASSERT_HTTP(dbResult->isSuccess(), Status::CODE_500, dbResult->getErrorMessage());
|
||||
|
||||
// auto items = dbResult->fetch<oatpp::Vector<oatpp::Object<MonitoringDto>>>();
|
||||
|
||||
// auto page = MonPageDto<oatpp::Object<MonitoringDto>>::createShared();
|
||||
// page->items = items;
|
||||
|
||||
// return page;
|
||||
|
||||
// }
|
||||
|
||||
|
||||
oatpp::Object<StatusDto> MonitoringService::deleteMonitoringById(const oatpp::Int32& id) {
|
||||
auto dbResult = m_database->deleteMonitoringById(id);
|
||||
OATPP_ASSERT_HTTP(dbResult->isSuccess(), Status::CODE_500, dbResult->getErrorMessage());
|
||||
auto status = StatusDto::createShared();
|
||||
status->status = "OK";
|
||||
status->code = 200;
|
||||
status->message = "Monitoring was successfully deleted";
|
||||
return status;
|
||||
}
|
||||
34
vrpmdvaotserver/src/service/MonitoringService.hpp
Normal file
34
vrpmdvaotserver/src/service/MonitoringService.hpp
Normal file
@@ -0,0 +1,34 @@
|
||||
|
||||
#ifndef VRPMD_MONITORINGSERVICE_HPP
|
||||
#define VRPMD_MONITORINGSERVICE_HPP
|
||||
|
||||
#include "db/MonitoringDb.hpp"
|
||||
#include "dto/PageDto.hpp"
|
||||
#include "dto/StatusDto.hpp"
|
||||
|
||||
#include "oatpp/web/protocol/http/Http.hpp"
|
||||
#include "oatpp/core/macro/component.hpp"
|
||||
|
||||
#include "../RTSMonitoring/RTSMonitoringTask.h"
|
||||
|
||||
class MonitoringService {
|
||||
private:
|
||||
typedef oatpp::web::protocol::http::Status Status;
|
||||
|
||||
private:
|
||||
OATPP_COMPONENT(std::shared_ptr<MonitoringDb>, m_database); // Inject database component
|
||||
|
||||
RTSMonitoringTask rtsMonTask;
|
||||
public:
|
||||
|
||||
oatpp::Object<MonitoringDto> createMonitoring(const oatpp::Object<InputMonitoringDto>& dto);
|
||||
oatpp::Object<MonitoringDto> updateMonitoring(const oatpp::Object<MonitoringDto>& dto, const oatpp::Int32& id);
|
||||
oatpp::Object<MonitoringDto> getMonitoringById(const oatpp::Int32& id, const oatpp::provider::ResourceHandle<oatpp::orm::Connection>& connection = nullptr);
|
||||
oatpp::Object<PageDto<oatpp::Object<MonitoringDto>>> getSelectedMonitorings(const oatpp::UInt32& offset, const oatpp::UInt32& limit);
|
||||
//oatpp::List<oatpp::Object<MonitoringDto>> getAllMonitorings();
|
||||
oatpp::Vector<oatpp::Object<MonitoringDto>> getAllMonitorings();
|
||||
oatpp::Object<StatusDto> deleteMonitoringById(const oatpp::Int32& id);
|
||||
//oatpp::Object<MonPageDto<oatpp::Object<MonitoringDto>>> getAllMonitorings();
|
||||
};
|
||||
|
||||
#endif //VRPMDV_MONITORINGERVICE_HPP
|
||||
67
vrpmdvaotserver/src/service/UserService.cpp
Normal file
67
vrpmdvaotserver/src/service/UserService.cpp
Normal file
@@ -0,0 +1,67 @@
|
||||
|
||||
#include "UserService.hpp"
|
||||
|
||||
oatpp::Object<UserDto> UserService::createUser(const oatpp::Object<UserDto>& dto) {
|
||||
|
||||
auto dbResult = m_database->createUser(dto);
|
||||
OATPP_ASSERT_HTTP(dbResult->isSuccess(), Status::CODE_500, dbResult->getErrorMessage());
|
||||
|
||||
auto userId = oatpp::sqlite::Utils::getLastInsertRowId(dbResult->getConnection());
|
||||
|
||||
return getUserById((v_int32) userId);
|
||||
|
||||
}
|
||||
|
||||
oatpp::Object<UserDto> UserService::updateUser(const oatpp::Object<UserDto>& dto) {
|
||||
|
||||
auto dbResult = m_database->updateUser(dto);
|
||||
OATPP_ASSERT_HTTP(dbResult->isSuccess(), Status::CODE_500, dbResult->getErrorMessage());
|
||||
return getUserById(dto->id);
|
||||
|
||||
}
|
||||
|
||||
oatpp::Object<UserDto> UserService::getUserById(const oatpp::Int32& id, const oatpp::provider::ResourceHandle<oatpp::orm::Connection>& connection) {
|
||||
|
||||
auto dbResult = m_database->getUserById(id, connection);
|
||||
OATPP_ASSERT_HTTP(dbResult->isSuccess(), Status::CODE_500, dbResult->getErrorMessage());
|
||||
OATPP_ASSERT_HTTP(dbResult->hasMoreToFetch(), Status::CODE_404, "User not found");
|
||||
|
||||
auto result = dbResult->fetch<oatpp::Vector<oatpp::Object<UserDto>>>();
|
||||
OATPP_ASSERT_HTTP(result->size() == 1, Status::CODE_500, "Unknown error");
|
||||
|
||||
return result[0];
|
||||
|
||||
}
|
||||
|
||||
oatpp::Object<PageDto<oatpp::Object<UserDto>>> UserService::getAllUsers(const oatpp::UInt32& offset, const oatpp::UInt32& limit) {
|
||||
|
||||
oatpp::UInt32 countToFetch = limit;
|
||||
|
||||
if(limit > 10) {
|
||||
countToFetch = 10;
|
||||
}
|
||||
|
||||
auto dbResult = m_database->getAllUsers(offset, countToFetch);
|
||||
OATPP_ASSERT_HTTP(dbResult->isSuccess(), Status::CODE_500, dbResult->getErrorMessage());
|
||||
|
||||
auto items = dbResult->fetch<oatpp::Vector<oatpp::Object<UserDto>>>();
|
||||
|
||||
auto page = PageDto<oatpp::Object<UserDto>>::createShared();
|
||||
page->offset = offset;
|
||||
page->limit = countToFetch;
|
||||
page->count = items->size();
|
||||
page->items = items;
|
||||
|
||||
return page;
|
||||
|
||||
}
|
||||
|
||||
oatpp::Object<StatusDto> UserService::deleteUserById(const oatpp::Int32& userId) {
|
||||
auto dbResult = m_database->deleteUserById(userId);
|
||||
OATPP_ASSERT_HTTP(dbResult->isSuccess(), Status::CODE_500, dbResult->getErrorMessage());
|
||||
auto status = StatusDto::createShared();
|
||||
status->status = "OK";
|
||||
status->code = 200;
|
||||
status->message = "User was successfully deleted";
|
||||
return status;
|
||||
}
|
||||
29
vrpmdvaotserver/src/service/UserService.hpp
Normal file
29
vrpmdvaotserver/src/service/UserService.hpp
Normal file
@@ -0,0 +1,29 @@
|
||||
|
||||
#ifndef VRPMDV_USERSERVICE_HPP
|
||||
#define VRPMDV_USERSERVICE_HPP
|
||||
|
||||
//#include "db/UserDb.hpp"
|
||||
#include "db/MonitoringDb.hpp"
|
||||
#include "dto/PageDto.hpp"
|
||||
#include "dto/StatusDto.hpp"
|
||||
|
||||
#include "oatpp/web/protocol/http/Http.hpp"
|
||||
#include "oatpp/core/macro/component.hpp"
|
||||
|
||||
class UserService {
|
||||
private:
|
||||
typedef oatpp::web::protocol::http::Status Status;
|
||||
private:
|
||||
// OATPP_COMPONENT(std::shared_ptr<UserDb>, m_database); // Inject database component
|
||||
OATPP_COMPONENT(std::shared_ptr<MonitoringDb>, m_database); // Inject database component
|
||||
public:
|
||||
|
||||
oatpp::Object<UserDto> createUser(const oatpp::Object<UserDto>& dto);
|
||||
oatpp::Object<UserDto> updateUser(const oatpp::Object<UserDto>& dto);
|
||||
oatpp::Object<UserDto> getUserById(const oatpp::Int32& id, const oatpp::provider::ResourceHandle<oatpp::orm::Connection>& connection = nullptr);
|
||||
oatpp::Object<PageDto<oatpp::Object<UserDto>>> getAllUsers(const oatpp::UInt32& offset, const oatpp::UInt32& limit);
|
||||
oatpp::Object<StatusDto> deleteUserById(const oatpp::Int32& id);
|
||||
|
||||
};
|
||||
|
||||
#endif //CRUD_USERSERVICE_HPP
|
||||
Reference in New Issue
Block a user