Adding file download.

This commit is contained in:
stephb9959
2022-03-11 14:10:05 -08:00
parent e04988fcac
commit b9b2bf5524
9 changed files with 152 additions and 5 deletions

View File

@@ -112,6 +112,7 @@ add_executable(owprov
src/RESTAPI/RESTAPI_configurations_list_handler.cpp src/RESTAPI/RESTAPI_configurations_list_handler.h
src/RESTAPI/RESTAPI_iptocountry_handler.cpp src/RESTAPI/RESTAPI_iptocountry_handler.h
src/RESTAPI/RESTAPI_signup_handler.h src/RESTAPI/RESTAPI_signup_handler.cpp
src/RESTAPI/RESTAPI_asset_server.cpp src/RESTAPI/RESTAPI_asset_server.h
src/FindCountry.h
src/sdks/SDK_gw.cpp src/sdks/SDK_gw.h
src/sdks/SDK_prov.cpp src/sdks/SDK_prov.h
@@ -128,7 +129,7 @@ add_executable(owprov
src/WebSocketClientServer.cpp src/WebSocketClientServer.h
src/storage/storage_maps.cpp src/storage/storage_maps.h
src/RESTAPI/RESTAPI_map_handler.cpp src/RESTAPI/RESTAPI_map_handler.h
src/RESTAPI/RESTAPI_map_list_handler.cpp src/RESTAPI/RESTAPI_map_list_handler.h src/storage/storage_signup.cpp src/storage/storage_signup.h src/Signup.cpp src/Signup.h src/DeviceTypeCache.h src/storage/storage_variables.cpp src/storage/storage_variables.h src/RESTAPI/RESTAPI_variables_handler.cpp src/RESTAPI/RESTAPI_variables_handler.h src/RESTAPI/RESTAPI_variables_list_handler.cpp src/RESTAPI/RESTAPI_variables_list_handler.h)
src/RESTAPI/RESTAPI_map_list_handler.cpp src/RESTAPI/RESTAPI_map_list_handler.h src/storage/storage_signup.cpp src/storage/storage_signup.h src/Signup.cpp src/Signup.h src/DeviceTypeCache.h src/storage/storage_variables.cpp src/storage/storage_variables.h src/RESTAPI/RESTAPI_variables_handler.cpp src/RESTAPI/RESTAPI_variables_handler.h src/RESTAPI/RESTAPI_variables_list_handler.cpp src/RESTAPI/RESTAPI_variables_list_handler.h src/FileDownloader.cpp src/FileDownloader.h)
target_link_libraries(owprov PUBLIC
${Poco_LIBRARIES} ${MySQL_LIBRARIES}

2
build
View File

@@ -1 +1 @@
47
56

View File

@@ -20,6 +20,7 @@
#include "FindCountry.h"
#include "Signup.h"
#include "DeviceTypeCache.h"
#include "FileDownloader.h"
namespace OpenWifi {
class Daemon *Daemon::instance_ = nullptr;
@@ -40,7 +41,8 @@ namespace OpenWifi {
JobController(),
WebSocketClientServer(),
FindCountryFromIP(),
Signup()
Signup(),
FileDownloader()
});
}
return instance_;
@@ -56,6 +58,16 @@ namespace OpenWifi {
} else {
FWRules_ = ProvObjects::dont_upgrade;
}
AssetDir_ = MicroService::instance().DataDir() + "/wwwassets";
Poco::File DataDir(AssetDir_);
if(!DataDir.exists()) {
try {
DataDir.createDirectory();
} catch (const Poco::Exception &E) {
logger().log(E);
}
}
}
void MicroServicePostInitialization() {

View File

@@ -42,11 +42,12 @@ namespace OpenWifi {
inline OpenWifi::ProvisioningDashboard & GetDashboard() { return DB_; }
Poco::Logger & Log() { return Poco::Logger::get(AppName()); }
ProvObjects::FIRMWARE_UPGRADE_RULES FirmwareRules() const { return FWRules_; }
inline const std::string & AssetDir() { return AssetDir_; }
private:
static Daemon *instance_;
OpenWifi::ProvisioningDashboard DB_{};
ProvObjects::FIRMWARE_UPGRADE_RULES FWRules_{ProvObjects::dont_upgrade};
std::string AssetDir_;
};
inline Daemon * Daemon() { return Daemon::instance(); }

43
src/FileDownloader.cpp Normal file
View File

@@ -0,0 +1,43 @@
//
// Created by stephane bourque on 2022-03-11.
//
#include "FileDownloader.h"
#include "Daemon.h"
namespace OpenWifi {
int FileDownloader::Start() {
TimerCallback_ = std::make_unique<Poco::TimerCallback<FileDownloader>>(*this,&FileDownloader::onTimer);
Timer_.setStartInterval( 20 * 1000); // first run in 20 seconds
Timer_.setPeriodicInterval(2 * 60 * 60 * 1000); // 1 hours
Timer_.start(*TimerCallback_);
return 0;
}
void FileDownloader::Stop() {
Timer_.stop();
Logger().notice("Stopping.");
}
void FileDownloader::onTimer(Poco::Timer &timer) {
const static std::vector<std::pair<std::string,std::string>> Files
{
{"https://raw.githubusercontent.com/blogic/ucentral-schema/main/ucentral.schema.json", "ucentral.schema.json" },
{"https://ucentral.io/ucentral.schema.pretty.json", "ucentral.schema.pretty.json" }
};
for(const auto &[url,filename]:Files) {
try {
std::string FileContent;
if (Utils::wgets(url, FileContent)) {
std::ofstream OF(Daemon()->AssetDir() + "/" + filename,
std::ios_base::out | std::ios_base::trunc);
OF << FileContent;
Logger().warning(Poco::format("File %s was downloaded",url));
}
} catch(...) {
Logger().warning(Poco::format("File %s could not be downloaded",url));
}
}
}
}

34
src/FileDownloader.h Normal file
View File

@@ -0,0 +1,34 @@
//
// Created by stephane bourque on 2022-03-11.
//
#pragma once
#include "framework/MicroService.h"
#include "Poco/Timer.h"
namespace OpenWifi {
class FileDownloader : public SubSystemServer {
public:
static auto instance() {
static auto instance_ = new FileDownloader;
return instance_;
}
int Start() override;
void Stop() override;
void onTimer(Poco::Timer & timer);
private:
Poco::Timer Timer_;
std::unique_ptr<Poco::TimerCallback<FileDownloader>> TimerCallback_;
std::atomic_bool Running_ = false;
FileDownloader() noexcept:
SubSystemServer("FileDownloader", "FILE-DOWNLOADER", "downloader") {
}
};
inline auto FileDownloader() { return FileDownloader::instance(); }
}

View File

@@ -0,0 +1,23 @@
//
// Created by stephane bourque on 2021-07-10.
//
#include "RESTAPI_asset_server.h"
#include "Poco/File.h"
#include "framework/ow_constants.h"
#include "Daemon.h"
namespace OpenWifi {
void RESTAPI_asset_server::DoGet() {
Poco::File AssetFile;
std::string AssetName = GetBinding(RESTAPI::Protocol::ID, "");
AssetFile = Daemon()->AssetDir() + "/" + AssetName;
if(!AssetFile.isFile()) {
return NotFound();
}
SendFile(AssetFile);
}
}

View File

@@ -0,0 +1,31 @@
//
// Created by stephane bourque on 2021-07-10.
//
#pragma once
#include "../framework/MicroService.h"
namespace OpenWifi {
class RESTAPI_asset_server : public RESTAPIHandler {
public:
RESTAPI_asset_server(const RESTAPIHandler::BindingMap &bindings, Poco::Logger &L, RESTAPI_GenericServer &Server, uint64_t TransactionId, bool Internal)
: RESTAPIHandler(bindings, L,
std::vector<std::string>
{
Poco::Net::HTTPRequest::HTTP_GET,
Poco::Net::HTTPRequest::HTTP_OPTIONS},
Server,
TransactionId,
Internal, false) {}
static const std::list<const char *> PathName() { return std::list<const char *>{"/wwwassets/{id}"}; };
void DoGet() final;
void DoPost() final {};
void DoDelete() final {};
void DoPut() final {};
private:
};
}

View File

@@ -26,6 +26,7 @@
#include "RESTAPI/RESTAPI_signup_handler.h"
#include "RESTAPI/RESTAPI_variables_handler.h"
#include "RESTAPI/RESTAPI_variables_list_handler.h"
#include "RESTAPI/RESTAPI_asset_server.h"
namespace OpenWifi {
@@ -54,7 +55,8 @@ namespace OpenWifi {
RESTAPI_iptocountry_handler,
RESTAPI_signup_handler,
RESTAPI_variables_handler,
RESTAPI_variables_list_handler
RESTAPI_variables_list_handler,
RESTAPI_asset_server
>(Path,Bindings,L, S, TransactionId);
}