From 172d38f5e2a0353d13937fcb9424d6283e8feffc Mon Sep 17 00:00:00 2001 From: "Timur A. Fatkhullin" Date: Mon, 9 Jun 2025 22:58:58 +0300 Subject: [PATCH] MccMount is now inheritor of MccFiniteStateMachine --- cxx/CMakeLists.txt | 3 +- cxx/mcc_mount_events_states.h | 29 ++++++ cxx/mount.h | 179 +++------------------------------- 3 files changed, 43 insertions(+), 168 deletions(-) create mode 100644 cxx/mcc_mount_events_states.h diff --git a/cxx/CMakeLists.txt b/cxx/CMakeLists.txt index 85060fd..d9df2dd 100644 --- a/cxx/CMakeLists.txt +++ b/cxx/CMakeLists.txt @@ -126,7 +126,8 @@ set(MOUNT_SERVER_APP_SRC mount.h mount_state.h mount_server.cpp comm_server.h co mount_astrom_default.h mcc_coord.h mount_pz.h mcc_fsm.h mcc_fsm_utils.h) set(MOUNT_SERVER_APP mount_server) add_executable(${MOUNT_SERVER_APP} ${MOUNT_SERVER_APP_SRC} - mcc_finite_state_machine.h) + mcc_finite_state_machine.h + mcc_mount_events_states.h) # target_include_directories(${MOUNT_SERVER_APP} PUBLIC ${ERFA_INCLUDE_DIR}) target_link_libraries(${MOUNT_SERVER_APP} ${CNTR_PROTO_LIB} spdlog::spdlog_header_only ERFA_LIB) diff --git a/cxx/mcc_mount_events_states.h b/cxx/mcc_mount_events_states.h new file mode 100644 index 0000000..ddab3a6 --- /dev/null +++ b/cxx/mcc_mount_events_states.h @@ -0,0 +1,29 @@ +#pragma once + +/* MOUNT CONTROL COMPONENTS LIBRARY */ + +/* + * BASIC EVENTS AND MOUNT STATES DEFINITIONS + */ + + +#include "mount.h" + +namespace mcc +{ + +template CONFIG_TYPE, + std::derived_from> MountT> +struct MccMountInitEvent { + static constexpr std::string_view ID = "MCC-MOUNT-INIT-EVENT"; + + MccMountInitEvent(MountT& mount) : _mount(mount) {} + + MountT mount() const { return _mount; } + +private: + MountT& _mount; +}; + +} // namespace mcc diff --git a/cxx/mount.h b/cxx/mount.h index 2102c65..2f000aa 100644 --- a/cxx/mount.h +++ b/cxx/mount.h @@ -11,6 +11,7 @@ #include "spdlog/sinks/null_sink.h" #include "mcc_coord.h" +#include "mcc_finite_state_machine.h" #include "mcc_spdlog.h" #include "mcc_traits.h" #include "mount_astrom.h" @@ -27,33 +28,6 @@ namespace mcc { -namespace traits -{ - - -// mount state type concept -template -concept mcc_mount_state_c = requires(T t, const T t_const) { - { t_const.ident() } -> std::same_as; - - // requires mcc_is_callable; - // requires mcc_is_callable; - // requires mcc_is_callable; - - { t.enter() } -> std::same_as; - { t.exit() } -> std::same_as; - { t.stop() } -> std::same_as; - - typename T::state_data_t; - - { t.stateData() } -> std::same_as; - - requires std::movable; -}; - -} // namespace traits - - /* SOME BASIC DATA STRUCTURES DEFINITIONS */ @@ -230,7 +204,7 @@ struct MccProhibitedZone1 { // implements a Finite State Machine Pattern template CONFIG_TYPE = MccMountConfig> -class MccMount : public utils::MccSpdlogLogger +class MccMount : public fsm::MccFiniteStateMachine, public utils::MccSpdlogLogger { public: static constexpr MccMountType mountType = MOUNT_TYPE; @@ -248,105 +222,14 @@ public: }; - /* mount state base class */ - - class MccMountState - { - static constexpr std::string_view emptyIdentString{"EMPTY-STRING-MOUNT-STATE-IDENTIFICATOR"}; - - public: - MccMountState(MccMount& mount, traits::mcc_input_char_range auto const& ident) - : _mount(mount), _ident({ident.begin(), ident.end()}) - { - _mount.logTrace("Create MccMountState instance: {}", (void*)this); - - if (!_ident.size()) { - _mount.logWarn("An empty input mount state identificator! Use of default value!"); - _ident = emptyIdentString; - } - } - - - MccMountState(MccMount& mount, const char* ident) : MccMountState(mount, std::string_view(ident)) {} - - MccMountState(MccMountState&&) = default; // movable - MccMountState& operator=(MccMountState&&) = default; - - MccMountState(const MccMountState&) = delete; // non-copyable - MccMountState& operator=(const MccMountState&) = delete; - - - virtual ~MccMountState() - { - _mount.logTrace("Delete MccMountState instance: {}", (void*)this); - }; - - - std::string_view ident() const - { - return _ident; - } - - void enter(this std::derived_from auto&& self) - { - std::forward(self).enterImpl(); - } - - void stop(this std::derived_from auto&& self) - { - std::forward(self).stopImpl(); - } - - void exit(this std::derived_from auto&& self, std::derived_from auto next_state) - { - std::forward(self).exitImpl(std::move(next_state)); - } - - protected: - MccMount& _mount; - std::string_view _ident; - - void enterImpl() - { - _mount.logWarn("call no-op MccMountState::enterImpl()"); - } - - void stopImpl() - { - _mount.logWarn("call no-op MccMountState::stopImpl()"); - } - - - void exitImpl(std::derived_from auto next_state) - { - _mount.setMountState(std::move(next_state)); - } - }; - - // default IDLE state - class MccMountStateIDLE : public MccMountState - { - public: - MccMountStateIDLE(MccMount& mount) : MccMountState(mount, "DEFAULT IDLE STATE") {} - - private: - void enterImpl() - { - this->_mount.logInfo("Enter to defalut IDLE state"); - } - - void stopImpl() - { - this->_mount.logWarn("Ignore stop for default IDLE state!"); - } - }; - /* Constructors and destructor */ - MccMount(traits::mcc_input_char_range auto const& logger_mark = "[MOUNT]", + template + MccMount(InitStateT, + traits::mcc_input_char_range auto const& logger_mark = "[MOUNT]", std::shared_ptr logger = spdlog::null_logger_mt("NULL")) - : utils::MccSpdlogLogger(logger), _exitCurrentState([]() {}), _currentStateName([]() {}) + : fsm::MccFiniteStateMachine(InitStateT{}), utils::MccSpdlogLogger(logger) { std::istringstream strst; @@ -361,42 +244,17 @@ public: updateIERSDatabase(IERS_DB_EARTH_ORIENT); } - virtual ~MccMount() - { - logDebug("Delete MccMount class instance: thread = {}", getThreadId()); - } + virtual ~MccMount() { logDebug("Delete MccMount class instance: thread = {}", getThreadId()); } /* Public methods */ - template - void setMountState(StateT state) - { - _exitCurrentState(); // exit from current state - _exitCurrentState = [&state]() { state.exit(); }; - - _currentStateName = [&state]() { return state.name(); }; - - state.enter(); - } - - std::string_view currenStateName() const - { - return _currentStateName(); - } - - MccMountPosition getMountData() const noexcept - { - return _currentMountOrient.load(); - } + MccMountPosition getMountData() const noexcept { return _currentMountOrient.load(); } // geo location setters/getters - void setGeoLocation(const MccMountSiteInfo& geoloc) - { - _geoLocation.store(geoloc); - } + void setGeoLocation(const MccMountSiteInfo& geoloc) { _geoLocation.store(geoloc); } void setSiteLatitude(const MccAngle& lat) { @@ -424,23 +282,14 @@ public: } - MccMountSiteInfo getGeoLocation() const - { - return _geoLocation.load(); - } + MccMountSiteInfo getGeoLocation() const { return _geoLocation.load(); } // current meteo setters/getters - void setMeteo(const MccMountMeteo& meteo) - { - _currentMeteo.store(meteo); - } + void setMeteo(const MccMountMeteo& meteo) { _currentMeteo.store(meteo); } - MccMountMeteo getMeteo() const - { - return _currentMeteo.load(); - } + MccMountMeteo getMeteo() const { return _currentMeteo.load(); } /* prohibited zone related methods */ @@ -561,10 +410,6 @@ public: protected: mount_config_t _mountCurrentConfig; - // std::shared_ptr _mountLogger; - std::function _exitCurrentState; - std::function _currentStateName; - // time scales related databases astrom::MccLeapSeconds _leapSecondsDB; astrom::MccIersBulletinA _earthOrientDB;