finished, ready for code review
This commit is contained in:
+16
-16
@@ -49,20 +49,20 @@ find_package(Midas REQUIRED)
|
||||
################################################################################
|
||||
|
||||
add_library(
|
||||
powerSwitch
|
||||
src/device/powerSwitch.cpp
|
||||
power_switch
|
||||
src/device/power_switch.cpp
|
||||
src/utils/outlet.cpp
|
||||
)
|
||||
|
||||
set_property(
|
||||
TARGET
|
||||
powerSwitch
|
||||
power_switch
|
||||
PROPERTY
|
||||
CXX_STANDARD 17
|
||||
)
|
||||
|
||||
target_link_libraries(
|
||||
powerSwitch
|
||||
power_switch
|
||||
PRIVATE
|
||||
midas::mfe
|
||||
m_epics_ca
|
||||
@@ -70,7 +70,7 @@ target_link_libraries(
|
||||
)
|
||||
|
||||
target_include_directories(
|
||||
powerSwitch
|
||||
power_switch
|
||||
PUBLIC
|
||||
${CMAKE_CURRENT_SOURCE_DIR}
|
||||
PRIVATE
|
||||
@@ -87,20 +87,20 @@ target_include_directories(
|
||||
## Frontend
|
||||
################################################################################
|
||||
|
||||
add_executable(ePowerSwitchFrontend
|
||||
src/frontend/ePowerSwitchFrontend.cpp
|
||||
add_executable(power_switch_scfe
|
||||
src/frontend/power_switch_scfe.cpp
|
||||
)
|
||||
|
||||
set_property(
|
||||
TARGET
|
||||
ePowerSwitchFrontend
|
||||
power_switch_scfe
|
||||
PROPERTY
|
||||
CXX_STANDARD 17
|
||||
)
|
||||
|
||||
|
||||
target_include_directories(
|
||||
ePowerSwitchFrontend
|
||||
power_switch_scfe
|
||||
PUBLIC
|
||||
${CMAKE_CURRENT_SOURCE_DIR}
|
||||
PRIVATE
|
||||
@@ -112,15 +112,15 @@ target_include_directories(
|
||||
)
|
||||
|
||||
target_link_libraries(
|
||||
ePowerSwitchFrontend
|
||||
power_switch_scfe
|
||||
PRIVATE
|
||||
powerSwitch
|
||||
power_switch
|
||||
midas::mfe
|
||||
m_epics_ca
|
||||
${LIBS}
|
||||
)
|
||||
# install(
|
||||
# TARGETS
|
||||
# ePowerSwitchFrontend
|
||||
# RUNTIME DESTINATION bin
|
||||
# )
|
||||
install(
|
||||
TARGETS
|
||||
power_switch_scfe
|
||||
RUNTIME DESTINATION bin
|
||||
)
|
||||
|
||||
@@ -1,105 +0,0 @@
|
||||
#include "powerSwitch.h"
|
||||
#include "odbxx.h"
|
||||
#include "powerSwitchConfig.h"
|
||||
#include "tmfe.h"
|
||||
#include <string>
|
||||
|
||||
#include <sstream>
|
||||
|
||||
/**
|
||||
* @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;
|
||||
}
|
||||
|
||||
powerSwitch::powerSwitch(std::string equipmentName,
|
||||
const char *equipmentFilename)
|
||||
: TMFeEquipment(equipmentName.c_str(), equipmentFilename),
|
||||
outletNumberRecord(mEpicsCa<int>(EPOWERSWITCH_OUTLETNUMBER_INFO_PREFIX)) {
|
||||
fEqConfReadOnlyWhenRunning = false;
|
||||
|
||||
this->equipmentPath = std::string("/Equipment/") + equipmentName;
|
||||
}
|
||||
|
||||
void powerSwitch::HandlePeriodic() {
|
||||
|
||||
updateOutletNumber();
|
||||
midas::odb o(this->equipmentPath);
|
||||
|
||||
for (int i = 0; i < this->numberOfOutlet; i++) {
|
||||
Outlet *outlet = &this->outlets.at(i);
|
||||
|
||||
if (outlet->commandHasChange()) {
|
||||
o[COMMAND_PATH.c_str()][i] = outlet->getCommand();
|
||||
}
|
||||
o[READBACK_PATH.c_str()][i] = outlet->getState();
|
||||
}
|
||||
}
|
||||
TMFeResult powerSwitch::HandleInit(const std::vector<std::string> &args) {
|
||||
|
||||
midas::odb o = {
|
||||
{SETTING_DIR.c_str(), {{EDITABLE_VARNAME.c_str(), COMMAND_VARNAME}}}};
|
||||
|
||||
o.connect(this->equipmentPath.c_str());
|
||||
|
||||
midas::odb to_watch(this->equipmentPath + SLASH + VARIABLES_DIR + SLASH +
|
||||
COMMAND_VARNAME);
|
||||
|
||||
to_watch.watch(
|
||||
[&](midas::odb &arg) { this->sendCommand(arg.get_last_index()); });
|
||||
return TMFeOk();
|
||||
}
|
||||
void powerSwitch::updateOutletNumber() {
|
||||
int newNumberOfOutlet = 0;
|
||||
|
||||
int returnCode = outletNumberRecord.get(&newNumberOfOutlet);
|
||||
if (returnCode != CM_SUCCESS) {
|
||||
fMfe->Msg(MERROR, __FUNCTION__, "Unable to read outlet record number");
|
||||
}
|
||||
|
||||
if (this->numberOfOutlet < newNumberOfOutlet)
|
||||
fMfe->Msg(MDEBUG, __FUNCTION__,
|
||||
"Outlet number increasing, creating %d new outlet(s)",
|
||||
newNumberOfOutlet - this->numberOfOutlet);
|
||||
|
||||
for (int i = this->numberOfOutlet; i < newNumberOfOutlet; i++) {
|
||||
std::string epicsSetRecordName;
|
||||
{
|
||||
static const std::string *str1 = &EPOWERSWITCH_OUTLET_SET_PREFIX;
|
||||
static const std::string str2 = "";
|
||||
epicsSetRecordName = insert(str1, i + 1, &str2);
|
||||
}
|
||||
std::string epicsGetRecordName;
|
||||
{
|
||||
static const std::string *str1 = &EPOWERSWITCH_OUTLET_GET_PREFIX;
|
||||
static const std::string str2 = "";
|
||||
epicsGetRecordName = insert(str1, i + 1, &str2);
|
||||
}
|
||||
this->outlets.emplace_back(i, epicsGetRecordName, epicsSetRecordName);
|
||||
}
|
||||
|
||||
if (this->numberOfOutlet > newNumberOfOutlet)
|
||||
fMfe->Msg(MDEBUG, __FUNCTION__,
|
||||
"Outlet number decreasing, destroying %d outlet(s)",
|
||||
this->numberOfOutlet - newNumberOfOutlet);
|
||||
|
||||
for (int i = newNumberOfOutlet; i < this->numberOfOutlet; i++) {
|
||||
this->outlets.pop_back();
|
||||
}
|
||||
|
||||
this->numberOfOutlet = newNumberOfOutlet;
|
||||
}
|
||||
|
||||
void powerSwitch::sendCommand(int index) {
|
||||
midas::odb o(this->equipmentPath);
|
||||
std::string command = o[COMMAND_PATH.c_str()][index];
|
||||
Outlet *outlet = &outlets.at(index);
|
||||
outlet->setState(command);
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
#ifndef POWER_SWITCH_H
|
||||
#define POWER_SWITCH_H
|
||||
|
||||
#include "../utils/outlet.h"
|
||||
#include "tmfe.h"
|
||||
#include <vector>
|
||||
|
||||
class powerSwitch : public TMFeEquipment {
|
||||
public:
|
||||
powerSwitch(std::string equipmentName, const char *equipmentFilename);
|
||||
void HandlePeriodic();
|
||||
TMFeResult HandleInit(const std::vector<std::string> &args);
|
||||
|
||||
private:
|
||||
std::vector<Outlet> outlets;
|
||||
mEpicsCa<int> outletNumberRecord;
|
||||
int numberOfOutlet;
|
||||
std::string equipmentPath;
|
||||
|
||||
void updateOutletNumber();
|
||||
|
||||
void sendCommand(int index);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,28 +0,0 @@
|
||||
#ifndef POWER_SWITCH_CONFIG_H
|
||||
#define POWER_SWITCH_CONFIG_H
|
||||
|
||||
#include <string>
|
||||
|
||||
const std::string EPOWERSWITCH_OUTLETNUMBER_INFO_PREFIX =
|
||||
"ePowerSwitch_config_maxOutlet";
|
||||
|
||||
const std::string VARIABLES_DIR = "Variables";
|
||||
const std::string SETTING_DIR = "Settings";
|
||||
const std::string COMMAND_VARNAME = "Command";
|
||||
const std::string READBACK_VARNAME = "Readback";
|
||||
|
||||
const std::string SLASH = "/";
|
||||
|
||||
const std::string EPOWERSWITCH_OUTLET_SET_PREFIX = "ePowerSwitch_set_outlet_";
|
||||
const std::string EPOWERSWITCH_OUTLET_GET_PREFIX = "ePowerSwitch_get_outlet_";
|
||||
|
||||
const std::string EDITABLE_VARNAME = std::string("Editable");
|
||||
|
||||
// ##################################################
|
||||
// Paths names : do not modify
|
||||
// ##################################################
|
||||
|
||||
const std::string COMMAND_PATH = VARIABLES_DIR + SLASH + COMMAND_VARNAME;
|
||||
const std::string READBACK_PATH = VARIABLES_DIR + SLASH + READBACK_VARNAME;
|
||||
|
||||
#endif
|
||||
@@ -1,49 +0,0 @@
|
||||
#include "ePowerSwitchFrontend.h"
|
||||
#include "../device/powerSwitch.h"
|
||||
#include "ePowerSwitchFrontendConfig.h"
|
||||
#include "tmfe.h"
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
|
||||
if (argv[0][0] == '.') {
|
||||
TMFE::Instance()->Msg(MERROR, __FUNCTION__,
|
||||
"Relative paths are strongly discouraged; "
|
||||
"please use an absolute path.");
|
||||
}
|
||||
|
||||
std::string frontendName;
|
||||
std::string equipmentName;
|
||||
|
||||
if (argc == 1) {
|
||||
frontendName = DEFAULT_FRONTEND_NAME;
|
||||
TMFE::Instance()->Msg(MINFO, __FUNCTION__,
|
||||
"No frontend name provided; %s will be used",
|
||||
DEFAULT_FRONTEND_NAME);
|
||||
equipmentName = DEFAULT_EQUIPMENT_NAME;
|
||||
TMFE::Instance()->Msg(MINFO, __FUNCTION__,
|
||||
"No equipment name provided; %s will be used",
|
||||
DEFAULT_EQUIPMENT_NAME);
|
||||
}
|
||||
if (argc == 2) {
|
||||
frontendName = std::string(argv[1]);
|
||||
argv++;
|
||||
argc--;
|
||||
TMFE::Instance()->Msg(MINFO, __FUNCTION__,
|
||||
"No equipment name provided; %s will be use",
|
||||
DEFAULT_EQUIPMENT_NAME);
|
||||
} else if (argc == 3) {
|
||||
frontendName = std::string(argv[1]);
|
||||
equipmentName = std::string(argv[2]);
|
||||
argv += 2;
|
||||
argc -= 2;
|
||||
}
|
||||
|
||||
auto front = ePowerSwitchFrontend(frontendName, equipmentName);
|
||||
front.FeMain(argc, argv);
|
||||
}
|
||||
|
||||
ePowerSwitchFrontend::ePowerSwitchFrontend(std::string frontendName,
|
||||
std::string equipmentName) {
|
||||
FeSetName(frontendName.c_str());
|
||||
FeAddEquipment(new powerSwitch(equipmentName.c_str(), __FILE__));
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
#ifndef EPOWERSWITCH_FRONTEND_H
|
||||
#define EPOWERSWITCH_FRONTEND_H
|
||||
|
||||
#include "tmfe.h"
|
||||
|
||||
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
|
||||
@@ -1,7 +0,0 @@
|
||||
#ifndef EPOWERSWITCH_CONFIG_H
|
||||
#define EPOWERSWITCH_CONFIG_H
|
||||
|
||||
#define DEFAULT_FRONTEND_NAME "ePowerSwitch"
|
||||
#define DEFAULT_EQUIPMENT_NAME "powerswitch"
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user