/* * 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" 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(); return CreateStatusCode(STATUS_CODE_OK); } const StatusCode McuManagerImpl::SetMcuMonitor(std::shared_ptr &monitor) { mMonitor = monitor; 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 OtherSideSend : public UartRecvAsk, public McuAsk { public: OtherSideSend(std::shared_ptr &mcuManager, const unsigned int &serialNumber) : mMcuManager(mcuManager) { mSerialNumber = serialNumber; } ~OtherSideSend() = default; void ReplyFinished(const bool result) override { if (result) { mMcuManager->ReplyOtherSideSendIpcMission(mDataReply, mSerialNumber); } } std::shared_ptr mMcuManager; }; std::shared_ptr monitor = GetMcuMonitor(); if (monitor) { std::shared_ptr manager = std::dynamic_pointer_cast(SharedFromThis()); std::shared_ptr ask = std::make_shared(manager, serialNumber); monitor->RecvIpcMissionEvent(ask, static_cast(mission)); } } void McuManagerImpl::ReplyOtherSideSendIpcMission(const ASK_RESULT &result, const unsigned int &serialNumber) { LogInfo("ReplyOtherSideSendIpcMission\n"); return McuProtocol::ReplyOtherSideSendIpcMission(static_cast(result), serialNumber); }