first ePowerSwitch verion
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
#include "ePowerSwitchFront.h"
|
||||
#include "ePowerSwitchConfig.h"
|
||||
#include "m_epics_ca.h"
|
||||
#include "tmfe.h"
|
||||
#include <format>
|
||||
#include <sstream>
|
||||
|
||||
/**
|
||||
* @brief local function, append an integer at the end of a string
|
||||
* @param str string prfix
|
||||
* @param value integer to append
|
||||
* @return "str" + integer
|
||||
*/
|
||||
std::string format(std::string str, int value) {
|
||||
std::stringstream ss;
|
||||
ss << str;
|
||||
ss << value;
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
std::string frontendName = "ePowerSwitch";
|
||||
std::string equipmentName = "powerswitch";
|
||||
|
||||
if (argc == 1) {
|
||||
frontendName = std::string(argv[1]);
|
||||
argv++;
|
||||
argc--;
|
||||
} else if (argc == 2) {
|
||||
frontendName = std::string(argv[1]);
|
||||
equipmentName = std::string(argv[2]);
|
||||
argv += 2;
|
||||
argc -= 2;
|
||||
}
|
||||
|
||||
auto front = new ePowerSwitchFrontend(frontendName.c_str());
|
||||
front->FeMain(argc, argv);
|
||||
}
|
||||
|
||||
ePowerSwitchEquipment::ePowerSwitchEquipment(std::string equipmentName,
|
||||
const char *equipmentFileName)
|
||||
: TMFeEquipment(equipmentName, equipmentFileName),
|
||||
outletNumberRecord(
|
||||
mEpicsCa<int>(EPOWERSWITCH_SOCKETNUMBER_INFO_PREFIX, true)) {
|
||||
fEqConfReadConfigFromOdb = false;
|
||||
fEqConfPeriodMilliSec = 1000;
|
||||
fEqConfLogHistory = 1;
|
||||
fEqConfReadOnlyWhenRunning = false;
|
||||
fEqConfWriteEventsToOdb = true;
|
||||
|
||||
this->numberOfOutlet = 0;
|
||||
}
|
||||
|
||||
void ePowerSwitchEquipment::HandlePeriodic() {
|
||||
updateSocketNumber();
|
||||
refreshAllSockets();
|
||||
}
|
||||
|
||||
void ePowerSwitchEquipment::refreshSocket(int socketId) {
|
||||
std::string varname = format("Socket ", socketId);
|
||||
|
||||
int midasSocketState = 0; // default => turn off
|
||||
fOdbEqVariables->RI(varname.c_str(), &midasSocketState, true);
|
||||
|
||||
if (midasSocketState != this->outletLastState.at(socketId)) {
|
||||
this->outletCommandRecords.at(socketId)->put(&midasSocketState);
|
||||
printf("send command : %d\n", midasSocketState);
|
||||
}
|
||||
|
||||
this->outletLastState.at(socketId) = midasSocketState;
|
||||
}
|
||||
|
||||
void ePowerSwitchEquipment::refreshAllSockets() {
|
||||
for (int i = 0; i < this->numberOfOutlet; i++) {
|
||||
refreshSocket(i);
|
||||
}
|
||||
}
|
||||
|
||||
void ePowerSwitchEquipment::updateSocketNumber() {
|
||||
int epicsOutletNumber = 0;
|
||||
|
||||
if (!outletNumberRecord.connected())
|
||||
return;
|
||||
int returnCode = outletNumberRecord.get(&epicsOutletNumber);
|
||||
|
||||
if (returnCode < 0) {
|
||||
printf("error\n");
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = this->numberOfOutlet; i < epicsOutletNumber; i++) {
|
||||
std::string epicsRecordName =
|
||||
format(EPOWERSWITCH_SOCKET_COMMAND_PREFIX, i);
|
||||
|
||||
this->outletCommandRecords.push_back(
|
||||
new mEpicsCa<int>(epicsRecordName.c_str(), true));
|
||||
this->outletLastState.push_back(0);
|
||||
|
||||
printf("new socket created\n");
|
||||
}
|
||||
|
||||
for (int i = epicsOutletNumber; i < this->numberOfOutlet; i++) {
|
||||
mEpicsCa<int> *ptr = this->outletCommandRecords.back();
|
||||
delete ptr;
|
||||
|
||||
this->outletCommandRecords.pop_back();
|
||||
this->outletLastState.pop_back();
|
||||
printf("old socket destroyed\n");
|
||||
}
|
||||
|
||||
this->numberOfOutlet = epicsOutletNumber;
|
||||
}
|
||||
|
||||
ePowerSwitchFrontend::ePowerSwitchFrontend(std::string frontendName,
|
||||
std::string equipmentName) {
|
||||
FeSetName(frontname.c_str());
|
||||
FeAddEquipment(new ePowerSwitchEquipment(equipmentName.c_str(), __FILE__));
|
||||
}
|
||||
Reference in New Issue
Block a user