Skip to content

Commit

Permalink
Added argument to change the UPS (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
miervasi-edalab authored Jan 30, 2023
1 parent e8caa31 commit 6c59c9a
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 26 deletions.
7 changes: 5 additions & 2 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,15 @@ int main(int argc, char *argv[])

QCoreApplication coreApplication(argc, argv);

QString configPath;
QString configPath, upsDeviceName;
if(argc > 0) {
configPath = argv[1];
}
if(argc > 1) {
upsDeviceName = argv[2];
}

UpsService upsService(configPath);
UpsService upsService(configPath, upsDeviceName);

QObject::connect(&coreApplication, SIGNAL(aboutToQuit()), &upsService, SLOT(handleQuitSignal()));

Expand Down
23 changes: 5 additions & 18 deletions upscontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
#include <QDateTime>
#include "upscontroller.h"

const std::string upsDeviceName = "salicru";

UpsController::UpsController(QObject *parent) :
QObject(parent)
UpsController::UpsController(QObject *parent, const QString &upsDeviceName) :
QObject(parent),
upsDeviceName(upsDeviceName)
{

#ifdef UPS_ENABLE
Expand Down Expand Up @@ -44,29 +43,17 @@ void UpsController::sendUpsCommand()
QString upsState = "";
#ifdef UPS_ENABLE
try {
upsState = QString::fromStdString(m_nutClient->getDeviceVariableValue(upsDeviceName, "ups.status")[0]);
upsState = QString::fromStdString(m_nutClient->getDeviceVariableValue(upsDeviceName.toStdString(), "ups.status")[0]);
} catch (nut::NutException e) {
qWarning() << "Nut driver error while requesting ups data. Details: " << QString::fromStdString(e.str());
QProcess::execute("upsdrvctl start");
try {
m_nutClient = new nut::TcpClient("localhost", 3493);
} catch (nut::NutException e) {
qWarning() << "Nut driver error while new class. Details: " << QString::fromStdString(e.str());
}
try {
m_nutClient->authenticate("admin", "admin");
} catch (nut::NutException e) {
qWarning() << "Nut driver error while authenticate. Details: " << QString::fromStdString(e.str());
m_nutClient->logout();
}
return;
}

qDebug() << upsState;

if (upsState.contains("BYPASS") && !waitingForUpsOnline) {
try {
m_nutClient->executeDeviceCommand(upsDeviceName, "load.on"); // Be sure that the UPS is online mode!
m_nutClient->executeDeviceCommand(upsDeviceName.toStdString(), "load.on"); // Be sure that the UPS is online mode!
waitingForUpsOnline = true;
} catch (nut::NutException e) {
qWarning() << "Nut driver error while setting UPS online mode. Details: " << QString::fromStdString(e.str());
Expand Down
4 changes: 2 additions & 2 deletions upscontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class UpsController : public QObject
{
Q_OBJECT
public:
explicit UpsController(QObject *parent = nullptr);
UpsController(QObject *parent = nullptr, const QString &upsDeviceName = "salicru");

signals:
void newUpsState(const QString& upsState);
Expand All @@ -28,11 +28,11 @@ public slots:
protected slots:

protected:
void tryConnection();

private:
nut::Client* m_nutClient;
QTimer m_pollSaiStatusTimer;
QString upsDeviceName;
};

#endif // UPSCONTROLLER_H
11 changes: 9 additions & 2 deletions upsservice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include "upsservice.h"

UpsService::UpsService(const QString& configPath, QObject *parent) :
UpsService::UpsService(const QString& configPath, const QString &upsDeviceName, QObject *parent) :
SigmaService("UpsService", configPath, new UpsServiceSettings, parent)
{
#ifdef __arm__
Expand All @@ -16,7 +16,14 @@ UpsService::UpsService(const QString& configPath, QObject *parent) :

this->m_upsServiceSettings = dynamic_cast<UpsServiceSettings*>(this->serviceSettings());

this->m_upsController = new UpsController(this);
if(upsDeviceName != nullptr)
{
this->m_upsController = new UpsController(this, upsDeviceName);
}
else
{
this->m_upsController = new UpsController(this);
}

// Forward driver signals

Expand Down
3 changes: 1 addition & 2 deletions upsservice.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class UpsService : public SigmaService
{
Q_OBJECT
public:
explicit UpsService(const QString& configPath, QObject *parent = nullptr);
explicit UpsService(const QString& configPath, const QString &upsDeviceName, QObject *parent = nullptr);

signals:
void newUpsState(const QString upsState);
Expand All @@ -23,6 +23,5 @@ public slots:
protected:
UpsServiceSettings* m_upsServiceSettings;
UpsController* m_upsController;

};
#endif // UPSSERVICE_H

0 comments on commit 6c59c9a

Please sign in to comment.