hunting/middleware/AppManager/src/AppManager.cpp
2024-03-06 05:49:11 -08:00

89 lines
3.0 KiB
C++

/*
* Copyright (c) 2023 Fancy Code.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "AppManager.h"
#include "AppManagerMakePtr.h"
// #include "FxHttpServer.h"
#include "IHalCpp.h"
#include "ILog.h"
#include "WebServer.h"
AppManager::AppManager()
{
// mHttpServerRuning = false;
mProtocolHandle = std::make_shared<IAppProtocolHandle>();
}
const StatusCode AppManager::Init(const AppParam &param)
{
std::shared_ptr<VWifiHal> wifi;
IHalCpp::GetInstance()->GetWifiHal(wifi);
if (!wifi) {
LogError("Get wifi hal failed.\n");
return CreateStatusCode(STATUS_CODE_NOT_OK);
}
wifi->OpenApMode();
AppManagerMakePtr::GetInstance()->CreateProtocolHandle(mProtocolHandle);
HttpServerStart(param);
return CreateStatusCode(STATUS_CODE_OK);
}
const StatusCode AppManager::UnInit(void)
{
HttpServerStop();
mProtocolHandle.reset();
return CreateStatusCode(STATUS_CODE_OK);
}
const StatusCode AppManager::SetAppMonitor(std::shared_ptr<VAppMonitor> &monitor)
{
mProtocolHandle->SetAppMonitor(monitor);
return CreateStatusCode(STATUS_CODE_OK);
}
void AppManager::AppRequestHandle(const char *url, const unsigned int urlLength, ResponseHandle responseHandle,
void *context)
{
//
mProtocolHandle->RequestHandle(url, urlLength, responseHandle, context);
}
void AppManager::HttpServerStart(const AppParam &param)
{
auto httpServerThread = [=](std::shared_ptr<AppManager> app) {
LogInfo("AppManager httpServer started.\n");
app->HttpServerThread(param);
};
mHttpSever = std::thread(httpServerThread, shared_from_this());
}
void AppManager::HttpServerStop(void)
{
// FxHttpServerExit();
WebServerExit();
if (mHttpSever.joinable()) {
mHttpSever.join();
}
}
void AppManager::HttpServerThread(const AppParam &param)
{
std::shared_ptr<AppManager> app = shared_from_this();
auto httpHandle =
[](const char *url, const unsigned int urlLength, ResponseHandle responseHandle, void *context) -> void {
// LogInfo("url = %s\n", url);
std::shared_ptr<IAppManager> app = IAppManager::GetInstance();
std::shared_ptr<AppManager> appImpl = std::dynamic_pointer_cast<AppManager>(app);
if (appImpl) {
appImpl->AppRequestHandle(url, urlLength, responseHandle, context);
}
};
// FxHttpServerInit(httpHandle, 8080);
// FxHttpServerUnInit();
WebServerParam web = {.mIp = param.mIP, .mPort = param.mPort, .mHttpRequestHandle = httpHandle};
WebServerInit(web);
WebServerUnInit();
}