hunting/middleware/McuManager/src/McuManagerImpl.cpp
2024-05-14 13:49:33 +08:00

227 lines
8.9 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 "McuManagerImpl.h"
#include "ILog.h"
#include "UartRecvAsk.h"
class OtherSideSend : public UartRecvAsk, public McuAsk<ASK_RESULT>
{
public:
OtherSideSend(std::shared_ptr<McuManagerImpl> &mcuManager, const unsigned int &serialNumber,
const OtherSideSendType &sendType)
: mMcuManager(mcuManager), mSendType(sendType)
{
McuAsk::mSerialNumber = serialNumber;
}
virtual ~OtherSideSend() = default;
protected:
std::shared_ptr<McuManagerImpl> mMcuManager;
public:
const OtherSideSendType mSendType;
};
template <typename T>
class OtherSideSendWithData : public OtherSideSend
{
public:
OtherSideSendWithData(std::shared_ptr<McuManagerImpl> &mcuManager, const unsigned int &serialNumber,
const OtherSideSendType &sendType, const T &otherSideData)
: OtherSideSend(mcuManager, serialNumber, sendType), mOtherSideData(otherSideData)
{
}
virtual ~OtherSideSendWithData() = default;
public:
const T mOtherSideData;
};
McuManagerImpl::McuManagerImpl()
{
mMcuAskHandle[OtherSideSendType::SEND_IPC_MISSION] =
std::bind(&McuManagerImpl::McuAskSendIpcMissionHandle, this, _1);
mMcuAskHandle[OtherSideSendType::SEND_HEART_BEAT] = std::bind(&McuManagerImpl::McuAskSendHeartBeatHandle, this, _1);
}
std::shared_ptr<VProtocolBase> McuManagerImpl::SharedFromThis(void)
{
return shared_from_this();
}
const StatusCode McuManagerImpl::Init(void)
{
McuDevice::Init();
McuProtocol::Init();
return CreateStatusCode(STATUS_CODE_OK);
}
const StatusCode McuManagerImpl::UnInit(void)
{
McuDevice::UnInit();
McuProtocol::UnInit();
mMcuAskList.clear();
return CreateStatusCode(STATUS_CODE_OK);
}
const StatusCode McuManagerImpl::SetMcuMonitor(std::shared_ptr<VMcuMonitor> &monitor)
{
std::lock_guard<std::mutex> locker(mMutex);
mMonitor = monitor;
for (auto ask : mMcuAskList) {
std::shared_ptr<OtherSideSend> data = std::dynamic_pointer_cast<OtherSideSend>(ask);
auto handle = mMcuAskHandle.find(data->mSendType);
if (handle != mMcuAskHandle.end()) {
handle->second(ask);
}
}
mMcuAskList.clear();
return CreateStatusCode(STATUS_CODE_OK);
}
const StatusCode McuManagerImpl::GetIpcMission(std::shared_ptr<VMcuAsk> &ask)
{
std::shared_ptr<VProtocolContext> context = std::make_shared<ProtocolContext<std::shared_ptr<VMcuAsk>>>(ask);
return McuProtocol::GetIpcMission(context);
}
const StatusCode McuManagerImpl::CutOffPowerSupply(std::shared_ptr<VMcuAsk> &ask)
{
std::shared_ptr<VProtocolContext> context = std::make_shared<ProtocolContext<std::shared_ptr<VMcuAsk>>>(ask);
return McuProtocol::CutOffPowerSupply(context);
}
const StatusCode McuManagerImpl::FeedWatchDog(std::shared_ptr<VMcuAsk> &ask)
{
std::shared_ptr<VProtocolContext> context = std::make_shared<ProtocolContext<std::shared_ptr<VMcuAsk>>>(ask);
return McuProtocol::FeedWatchDog(context);
}
const StatusCode McuManagerImpl::SetFeedingCycleForWatchDog(std::shared_ptr<VMcuAsk> &ask, const unsigned char &hour,
const unsigned char &min, const unsigned char &second)
{
std::shared_ptr<VProtocolContext> context = std::make_shared<ProtocolContext<std::shared_ptr<VMcuAsk>>>(ask);
return McuProtocol::SetFeedingCycleForWatchDog(hour, min, second, context);
}
const StatusCode McuManagerImpl::SetDateTime(std::shared_ptr<VMcuAsk> &ask, const McuAskDateTime &value)
{
std::shared_ptr<VProtocolContext> context = std::make_shared<ProtocolContext<std::shared_ptr<VMcuAsk>>>(ask);
return McuProtocol::McuSetDateTime(
value.mYear, value.mMon, value.mDay, value.mHour, value.mMin, value.mSecond, context);
}
const StatusCode McuManagerImpl::SetPirSensitivity(std::shared_ptr<VMcuAsk> &ask, const unsigned char &sensitivity)
{
std::shared_ptr<VProtocolContext> context = std::make_shared<ProtocolContext<std::shared_ptr<VMcuAsk>>>(ask);
return McuProtocol::SetPirSensitivity(sensitivity, context);
}
const StatusCode McuManagerImpl::ContorlInfraredLight(std::shared_ptr<VMcuAsk> &ask, const ControlLight &control)
{
std::shared_ptr<VProtocolContext> context = std::make_shared<ProtocolContext<std::shared_ptr<VMcuAsk>>>(ask);
return McuProtocol::ContorlInfraredLight(static_cast<const unsigned char>(control), context);
}
const StatusCode McuManagerImpl::GetPhotosensitivityValue(std::shared_ptr<VMcuAsk> &ask)
{
std::shared_ptr<VProtocolContext> context = std::make_shared<ProtocolContext<std::shared_ptr<VMcuAsk>>>(ask);
return McuProtocol::GetPhotosensitivityValue(context);
}
std::shared_ptr<VMcuMonitor> McuManagerImpl::GetMcuMonitor(void)
{
auto monitor = mMonitor.lock();
if (!monitor) {
LogWarning("mMonitor is nullptr.\n");
return nullptr;
}
if (mMonitor.expired()) {
LogWarning("mMonitor shared ptr expired failed.\n");
return nullptr;
}
return monitor;
}
void McuManagerImpl::OtherSideSendIpcMission(const unsigned int &serialNumber, const unsigned char &mission)
{
class OtherSideSendV2 : public OtherSideSendWithData<unsigned char>
{
public:
OtherSideSendV2(std::shared_ptr<McuManagerImpl> &mcuManager, const unsigned int &serialNumber,
const OtherSideSendType &sendType, const unsigned char &mission)
: OtherSideSendWithData(mcuManager, serialNumber, sendType, mission)
{
}
~OtherSideSendV2() = default;
void ReplyFinished(const bool result) override
{
mMcuManager->ReplyOtherSideSendIpcMission(McuAsk::mDataReply, VMcuAsk::mSerialNumber);
}
};
std::shared_ptr<VMcuMonitor> monitor = GetMcuMonitor();
std::shared_ptr<McuManagerImpl> manager = std::dynamic_pointer_cast<McuManagerImpl>(SharedFromThis());
std::shared_ptr<VMcuAsk> ask =
std::make_shared<OtherSideSendV2>(manager, serialNumber, OtherSideSendType::SEND_IPC_MISSION, mission);
if (monitor) {
monitor->RecvIpcMissionEvent(ask, static_cast<IpcMission>(mission));
}
else {
LogWarning("mMonitor is nullptr, AddMcuAsk.\n");
AddMcuAsk(ask);
}
}
void McuManagerImpl::OtherSideSendHearBeat(const unsigned int &serialNumber)
{
class OtherSideSendV2 : public OtherSideSend
{
public:
OtherSideSendV2(std::shared_ptr<McuManagerImpl> &mcuManager, const unsigned int &serialNumber,
const OtherSideSendType &sendType)
: OtherSideSend(mcuManager, serialNumber, sendType)
{
}
~OtherSideSendV2() = default;
void ReplyFinished(const bool result) override
{
mMcuManager->ReplyOtherSideSendHeartBeat(mSerialNumber);
}
};
std::shared_ptr<VMcuMonitor> monitor = GetMcuMonitor();
std::shared_ptr<McuManagerImpl> manager = std::dynamic_pointer_cast<McuManagerImpl>(SharedFromThis());
std::shared_ptr<VMcuAsk> ask =
std::make_shared<OtherSideSendV2>(manager, serialNumber, OtherSideSendType::SEND_HEART_BEAT);
if (monitor) {
monitor->RecvMcuHeartBeat(ask);
}
else {
LogWarning("mMonitor is nullptr, AddMcuAsk.\n");
AddMcuAsk(ask);
}
}
void McuManagerImpl::ReplyOtherSideSendIpcMission(const ASK_RESULT &result, const unsigned int &serialNumber)
{
LogInfo("ReplyOtherSideSendIpcMission\n");
McuProtocol::ReplyOtherSideSendIpcMission(static_cast<ReplyResult>(result), serialNumber);
}
void McuManagerImpl::ReplyOtherSideSendHeartBeat(const unsigned int &serialNumber)
{
LogInfo("ReplyOtherSideSendHeartBeat\n");
McuProtocol::ReplyOtherSideSendHearBeat(static_cast<ReplyResult>(ASK_RESULT::SUCCEED), serialNumber);
}
void McuManagerImpl::AddMcuAsk(std::shared_ptr<VMcuAsk> &ask)
{
std::lock_guard<std::mutex> locker(mMutex);
mMcuAskList.push_back(ask);
}
void McuManagerImpl::McuAskSendIpcMissionHandle(std::shared_ptr<VMcuAsk> &ask)
{
std::shared_ptr<VMcuMonitor> monitor = GetMcuMonitor();
std::shared_ptr<OtherSideSendWithData<unsigned char>> data =
std::dynamic_pointer_cast<OtherSideSendWithData<unsigned char>>(ask);
if (monitor) {
monitor->RecvIpcMissionEvent(ask, static_cast<IpcMission>(data->mOtherSideData));
}
}
void McuManagerImpl::McuAskSendHeartBeatHandle(std::shared_ptr<VMcuAsk> &ask)
{
std::shared_ptr<VMcuMonitor> monitor = GetMcuMonitor();
if (monitor) {
monitor->RecvMcuHeartBeat(ask);
}
}