/* * 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 { public: OtherSideSend(std::shared_ptr &mcuManager, const unsigned int &serialNumber, const OtherSideSendType &sendType) : mMcuManager(mcuManager), mSendType(sendType) { McuAsk::mSerialNumber = serialNumber; } virtual ~OtherSideSend() = default; protected: std::shared_ptr mMcuManager; public: const OtherSideSendType mSendType; }; template class OtherSideSendWithData : public OtherSideSend { public: OtherSideSendWithData(std::shared_ptr &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 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 &monitor) { std::lock_guard locker(mMutex); mMonitor = monitor; for (auto ask : mMcuAskList) { std::shared_ptr data = std::dynamic_pointer_cast(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 &ask) { std::shared_ptr context = std::make_shared>>(ask); return McuProtocol::GetIpcMission(context); } const StatusCode McuManagerImpl::CutOffPowerSupply(std::shared_ptr &ask) { std::shared_ptr context = std::make_shared>>(ask); return McuProtocol::CutOffPowerSupply(context); } const StatusCode McuManagerImpl::FeedWatchDog(std::shared_ptr &ask) { std::shared_ptr context = std::make_shared>>(ask); return McuProtocol::FeedWatchDog(context); } const StatusCode McuManagerImpl::SetFeedingCycleForWatchDog(std::shared_ptr &ask, const unsigned char &hour, const unsigned char &min, const unsigned char &second) { std::shared_ptr context = std::make_shared>>(ask); return McuProtocol::SetFeedingCycleForWatchDog(hour, min, second, context); } const StatusCode McuManagerImpl::SetDateTime(std::shared_ptr &ask, const McuAskDateTime &value) { std::shared_ptr context = std::make_shared>>(ask); return McuProtocol::McuSetDateTime( value.mYear, value.mMon, value.mDay, value.mHour, value.mMin, value.mSecond, context); } const StatusCode McuManagerImpl::SetPirSensitivity(std::shared_ptr &ask, const unsigned char &sensitivity) { std::shared_ptr context = std::make_shared>>(ask); return McuProtocol::SetPirSensitivity(sensitivity, context); } const StatusCode McuManagerImpl::ContorlInfraredLight(std::shared_ptr &ask, const ControlLight &control) { std::shared_ptr context = std::make_shared>>(ask); return McuProtocol::ContorlInfraredLight(static_cast(control), context); } const StatusCode McuManagerImpl::GetPhotosensitivityValue(std::shared_ptr &ask) { std::shared_ptr context = std::make_shared>>(ask); return McuProtocol::GetPhotosensitivityValue(context); } std::shared_ptr 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 { public: OtherSideSendV2(std::shared_ptr &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 monitor = GetMcuMonitor(); std::shared_ptr manager = std::dynamic_pointer_cast(SharedFromThis()); std::shared_ptr ask = std::make_shared(manager, serialNumber, OtherSideSendType::SEND_IPC_MISSION, mission); if (monitor) { monitor->RecvIpcMissionEvent(ask, static_cast(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 &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 monitor = GetMcuMonitor(); std::shared_ptr manager = std::dynamic_pointer_cast(SharedFromThis()); std::shared_ptr ask = std::make_shared(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(result), serialNumber); } void McuManagerImpl::ReplyOtherSideSendHeartBeat(const unsigned int &serialNumber) { LogInfo("ReplyOtherSideSendHeartBeat\n"); McuProtocol::ReplyOtherSideSendHearBeat(static_cast(ASK_RESULT::SUCCEED), serialNumber); } void McuManagerImpl::AddMcuAsk(std::shared_ptr &ask) { std::lock_guard locker(mMutex); mMcuAskList.push_back(ask); } void McuManagerImpl::McuAskSendIpcMissionHandle(std::shared_ptr &ask) { std::shared_ptr monitor = GetMcuMonitor(); std::shared_ptr> data = std::dynamic_pointer_cast>(ask); if (monitor) { monitor->RecvIpcMissionEvent(ask, static_cast(data->mOtherSideData)); } } void McuManagerImpl::McuAskSendHeartBeatHandle(std::shared_ptr &ask) { std::shared_ptr monitor = GetMcuMonitor(); if (monitor) { monitor->RecvMcuHeartBeat(ask); } }