diff --git a/CMakeLists.txt b/CMakeLists.txt index 70ce644..123aae6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,7 +7,7 @@ add_subdirectory(submodule/mepicsca) add_compile_options( -Wall -Wformat=2 - -O3 + -g -Wno-format-nonliteral -Wno-strict-aliasing -Wuninitialized @@ -49,19 +49,20 @@ find_package(Midas REQUIRED) ################################################################################ add_library( - ePowerSwitchEquipment - src/device/ePowerSwitchEquipment.cpp + powerSwitch + src/device/powerSwitch.cpp + src/utils/outlet.cpp ) set_property( TARGET - ePowerSwitchEquipment + powerSwitch PROPERTY CXX_STANDARD 17 ) target_link_libraries( - ePowerSwitchEquipment + powerSwitch PRIVATE midas::mfe m_epics_ca @@ -69,7 +70,7 @@ target_link_libraries( ) target_include_directories( - ePowerSwitchEquipment + powerSwitch PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} PRIVATE @@ -113,7 +114,7 @@ target_include_directories( target_link_libraries( ePowerSwitchFrontend PRIVATE - ePowerSwitchEquipment + powerSwitch midas::mfe m_epics_ca ${LIBS} diff --git a/include/ePowerSwitchFront.h b/include/ePowerSwitchFront.h deleted file mode 100644 index f4298e6..0000000 --- a/include/ePowerSwitchFront.h +++ /dev/null @@ -1,60 +0,0 @@ -#ifndef EPOWERSWITCH_FRONTEND_H -#define EPOWERSWITCH_FRONTEND_H - -#include "m_epics_ca.h" -#include "tmfe.h" -#include - -class ePowerSwitchEquipment : public TMFeEquipment { - - public: - /** - * @brief constructor - * - * @param equipmentName name show on Midas for the equipment - * @param equipmentFileName file name for automatically tagged message - */ - ePowerSwitchEquipment(std::string equipmentName, - const char *equipmentFileName); - - /** - * @brief called periodically by Midas - */ - void HandlePeriodic(); - - private: - std::vector *> outletSetRecords; - std::vector *> outletGetRecords; - mEpicsCa outletNumberRecord; - int numberOfOutlet; - - /** - * @brief refresh information about the given socket ID - * - * @param socketId which socket to refresh - */ - void refreshSocket(int socketId); - - /** - * @brief refresh information about all the available sockets - */ - void refreshAllSockets(); - - /** - * @brief detect and modify the current socket pool in case of change - */ - void updateSocketNumber(); -}; - -class ePowerSwitchFrontend : public TMFrontend { - public: - /** - * @brief constructor - * - * @param frontendName name shown on Midas for the frontend - * @param equipmentName name shown on Midas for the equipment - */ - ePowerSwitchFrontend(std::string frontendName, std::string equipmentName); -}; - -#endif \ No newline at end of file diff --git a/src/device/ePowerSwitchEquipment.cpp b/src/device/ePowerSwitchEquipment.cpp deleted file mode 100644 index 5699373..0000000 --- a/src/device/ePowerSwitchEquipment.cpp +++ /dev/null @@ -1,217 +0,0 @@ -#include "ePowerSwitchEquipment.h" -#include "ePowerSwitchEquipmentConfig.h" -#include "tmfe.h" -#include - -/** - * @brief Insert a integer value between two string - * @param str1 first string - * @param int value to insert - * @param str2 second string - * - * @return a new string instance - */ -std::string insert(const std::string *str1, int value, - const std::string *str2) { - return *str1 + std::to_string(value) + *str2; -} - -ePowerSwitchEquipment::ePowerSwitchEquipment(std::string equipmentName, - const char *equipmentFileName) - : TMFeEquipment(equipmentName.c_str(), equipmentFileName), - outletNumberRecord(mEpicsCa( - std::string_view(EPOWERSWITCH_OUTLETNUMBER_INFO_PREFIX))) { - - fEqConfPeriodMilliSec = 1000; // refresh rate of midas frontend - fEqConfLogHistory = - 60; // enable history system, generate one event per minutes - fEqConfReadOnlyWhenRunning = false; // allow to write values when running - fEqConfWriteEventsToOdb = true; // allow to write event to the odb - - this->numberOfOutlet = 0; -} - -void ePowerSwitchEquipment::HandlePeriodic() { - updateOutletNumber(); - refreshAllOutlets(); -} - -void ePowerSwitchEquipment::refreshOutlet(int outletId) { - - std::string usernameVarname; - { - static const std::string str1 = "Outlet "; - static const std::string str2 = " name"; - usernameVarname = insert(&str1, outletId + 1, &str2); - } - std::string requestedVarname; - { - static const std::string str1 = "|-> "; - static const std::string str2 = " requested"; - requestedVarname = insert(&str1, outletId + 1, &str2); - } - std::string currentVarname; - { - static const std::string str1 = "\\-> "; - static const std::string str2 = " current"; - currentVarname = insert(&str1, outletId + 1, &str2); - } - std::string midasUsername; - { - static const std::string str1 = "Outlet "; - static const std::string str2 = ""; - midasUsername = insert(&str1, outletId, &str2); - } - std::string midasRequestedOutletState = std::string("Off"); - MVOdbError ovbError; - fOdbEqVariables->RS(usernameVarname.c_str(), &midasUsername, true, - midasUsername.length() + 1, &ovbError); - if (ovbError.fError) { - fMfe->Msg(MERROR, __FUNCTION__, ovbError.fErrorString); - exit(EXIT_FAILURE); - } - - fOdbEqVariables->RS(requestedVarname.c_str(), &midasRequestedOutletState, - true, midasRequestedOutletState.length() + 1, - &ovbError); - - if (ovbError.fError) { - fMfe->Msg(MERROR, __FUNCTION__, ovbError.fErrorString); - exit(EXIT_FAILURE); - } - - std::string defaultCurrentState = "unknow"; - fOdbEqVariables->RS(currentVarname.c_str(), &defaultCurrentState, true, - defaultCurrentState.length() + 1, &ovbError); - - if (ovbError.fError) { - fMfe->Msg(MERROR, __FUNCTION__, ovbError.fErrorString); - exit(EXIT_FAILURE); - } - - std::string epicsCurrentOutletState; - int mepicscaReturnCode = - this->outletGetRecords.at(outletId)->get(&epicsCurrentOutletState); - - if (mepicscaReturnCode != CM_SUCCESS) - return; - - if (epicsCurrentOutletState != midasRequestedOutletState) { - mepicscaReturnCode = this->outletSetRecords.at(outletId)->put( - &midasRequestedOutletState); - - if (mepicscaReturnCode != CM_SUCCESS) - return; - } - - fOdbEqVariables->WS(currentVarname.c_str(), epicsCurrentOutletState.c_str(), - epicsCurrentOutletState.length() + 1, &ovbError); - - if (ovbError.fError) { - fMfe->Msg(MERROR, __FUNCTION__, ovbError.fErrorString); - exit(EXIT_FAILURE); - } -} - -void ePowerSwitchEquipment::refreshAllOutlets() { - for (int i = 0; i < this->numberOfOutlet; i++) { - refreshOutlet(i); - } -} - -void ePowerSwitchEquipment::updateOutletNumber() { - int epicsOutletNumber = 0; - - int returnCode = outletNumberRecord.get(&epicsOutletNumber); - - if (returnCode != CM_SUCCESS) - return; - - if (this->numberOfOutlet < epicsOutletNumber) - fMfe->Msg(MDEBUG, __FUNCTION__, - "Outlet number increasing, creating %d new outlet(s)", - epicsOutletNumber - this->numberOfOutlet); - - /* - This for loop will only iterate if the condition on top of this - comment is true - */ - for (int i = this->numberOfOutlet; i < epicsOutletNumber; i++) { - std::string epicsSetRecordName; - { - static const std::string *str1 = &EPOWERSWITCH_OUTLET_SET_PREFIX; - static const std::string str2 = ""; - epicsSetRecordName = insert(str1, i, &str2); - } - - this->outletSetRecords.push_back( - std::make_unique>( - epicsSetRecordName.c_str())); - - std::string epicsGetRecordName; - { - static const std::string *str1 = &EPOWERSWITCH_OUTLET_GET_PREFIX; - static const std::string str2 = ""; - epicsGetRecordName = insert(str1, i, &str2); - } - - this->outletGetRecords.push_back( - std::make_unique>( - epicsGetRecordName.c_str())); - } - - if (this->numberOfOutlet > epicsOutletNumber) - fMfe->Msg(MDEBUG, __FUNCTION__, - "Outlet number decreasing, destroying %d outlet(s)", - this->numberOfOutlet - epicsOutletNumber); - /* - This for loop will only iterate if the condition on top of this - comment is true - */ - for (int i = epicsOutletNumber; i < this->numberOfOutlet; i++) { - - std::string usernameVarname; - { - static const std::string str1 = "Outlet "; - static const std::string str2 = " name"; - usernameVarname = insert(&str1, i + 1, &str2); - } - std::string requestedVarname; - { - static const std::string str1 = "|-> "; - static const std::string str2 = " requested"; - requestedVarname = insert(&str1, i + 1, &str2); - } - std::string currentVarname; - { - static const std::string str1 = "\\-> "; - static const std::string str2 = " current"; - currentVarname = insert(&str1, i + 1, &str2); - } - - MVOdbError ovbError; - - fOdbEqVariables->Delete(usernameVarname.c_str(), &ovbError); - if (ovbError.fError) { - fMfe->Msg(MERROR, __FUNCTION__, ovbError.fErrorString); - exit(EXIT_FAILURE); - } - - fOdbEqVariables->Delete(requestedVarname.c_str(), &ovbError); - if (ovbError.fError) { - fMfe->Msg(MERROR, __FUNCTION__, ovbError.fErrorString); - exit(EXIT_FAILURE); - } - - fOdbEqVariables->Delete(currentVarname.c_str(), &ovbError); - if (ovbError.fError) { - fMfe->Msg(MERROR, __FUNCTION__, ovbError.fErrorString); - exit(EXIT_FAILURE); - } - - this->outletSetRecords.pop_back(); - this->outletGetRecords.pop_back(); - } - - this->numberOfOutlet = epicsOutletNumber; -} diff --git a/src/device/ePowerSwitchEquipment.h b/src/device/ePowerSwitchEquipment.h deleted file mode 100644 index e514de2..0000000 --- a/src/device/ePowerSwitchEquipment.h +++ /dev/null @@ -1,50 +0,0 @@ -#ifndef EPOWERSWITCH_EQUIPMENT_H -#define EPOWERSWITCH_EQUIPMENT_H - -#include "m_epics_ca.h" -#include "tmfe.h" -#include - -class ePowerSwitchEquipment : public TMFeEquipment { - - public: - /** - * @brief constructor - * - * @param equipmentName name show on Midas for the equipment - * @param equipmentFileName file name for automatically tagged message - */ - ePowerSwitchEquipment(std::string equipmentName, - const char *equipmentFileName); - - /** - * @brief called periodically by Midas - */ - void HandlePeriodic(); - - private: - std::vector>> outletSetRecords; - std::vector>> outletGetRecords; - std::vector outletNames; - mEpicsCa outletNumberRecord; - int numberOfOutlet; - - /** - * @brief refresh information about the given outlet ID - * - * @param outletId which outlet to refresh - */ - void refreshOutlet(int outletId); - - /** - * @brief refresh information about all the available outlets - */ - void refreshAllOutlets(); - - /** - * @brief detect and modify the current outlet pool in case of change - */ - void updateOutletNumber(); -}; - -#endif \ No newline at end of file diff --git a/src/device/ePowerSwitchEquipmentConfig.h b/src/device/ePowerSwitchEquipmentConfig.h deleted file mode 100644 index e4e83f1..0000000 --- a/src/device/ePowerSwitchEquipmentConfig.h +++ /dev/null @@ -1,13 +0,0 @@ -#ifndef EPOWERSWITCH_CONFIG_H -#define EPOWERSWITCH_CONFIG_H -#include - -const std::string EPOWERSWITCH_OUTLET_SET_PREFIX = "ePowerSwitch_set_outlet_"; -const std::string EPOWERSWITCH_OUTLET_GET_PREFIX = "ePowerSwitch_get_outlet_"; -const std::string EPOWERSWITCH_OUTLETNUMBER_INFO_PREFIX = - "ePowerSwitch_config_maxOutlet"; - -const std::string EPOWERSWITCH_DEFAULT_OUTLET_NAME = "Outlet"; -const std::string EPOWERSWITCH_DEFAULT_OUTLET_STATUS = "Off"; - -#endif diff --git a/src/frontend/ePowerSwitchFrontend.cpp b/src/frontend/ePowerSwitchFrontend.cpp index 29b06a9..117d47d 100644 --- a/src/frontend/ePowerSwitchFrontend.cpp +++ b/src/frontend/ePowerSwitchFrontend.cpp @@ -1,5 +1,5 @@ #include "ePowerSwitchFrontend.h" -#include "../device/ePowerSwitchEquipment.h" +#include "../device/powerSwitch.h" #include "ePowerSwitchFrontendConfig.h" #include "tmfe.h" @@ -45,5 +45,5 @@ int main(int argc, char *argv[]) { ePowerSwitchFrontend::ePowerSwitchFrontend(std::string frontendName, std::string equipmentName) { FeSetName(frontendName.c_str()); - FeAddEquipment(new ePowerSwitchEquipment(equipmentName.c_str(), __FILE__)); + FeAddEquipment(new powerSwitch(equipmentName.c_str(), __FILE__)); } \ No newline at end of file diff --git a/submodule/mepicsca b/submodule/mepicsca index 419df88..b6aca41 160000 --- a/submodule/mepicsca +++ b/submodule/mepicsca @@ -1 +1 @@ -Subproject commit 419df88dde85d8dc3150c7632c4214950b92c881 +Subproject commit b6aca4174e2789cb1bf4a3f56b8a26446994d961