/* * 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(); } const StatusCode AppManager::Init(const AppParam ¶m) { std::shared_ptr 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 &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 ¶m) { auto httpServerThread = [=](std::shared_ptr 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 ¶m) { std::shared_ptr 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 app = IAppManager::GetInstance(); std::shared_ptr appImpl = std::dynamic_pointer_cast(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(); }