Malte Veerman hace 10 años
padre
commit
263e7c20bd

+ 4 - 2
helper/src/helper.cpp

@@ -65,7 +65,8 @@ ActionReply Helper::read(const QVariantMap &args)
     QString filename = args["filename"].toString();
     QFile file(filename);
 
-    if (!file.open(QIODevice::ReadOnly)) {
+    if (!file.open(QIODevice::ReadOnly))
+    {
        reply = ActionReply::HelperErrorType;
        reply.setErrorCode(ActionReply::AuthorizationDeniedError);
 
@@ -88,7 +89,8 @@ ActionReply Helper::write(const QVariantMap &args)
     QString filename = args["filename"].toString();
     QFile file(filename);
 
-    if (!file.open(QIODevice::WriteOnly)) {
+    if (!file.open(QIODevice::WriteOnly))
+    {
        reply = ActionReply::HelperErrorType;
        reply.addData("errorDescription", file.errorString());
 

+ 10 - 0
share/qml/PwmFan.qml

@@ -404,5 +404,15 @@ Rectangle {
                 onTextChanged: fan.minStart = text
             }
         }
+
+        Button {
+            text: "Auto"
+            height: 15
+            enabled: !fan.testing
+            onClicked: {
+                systemdCom.dbusAction("StopUnit", [systemdCom.serviceName + ".service", "replace"]);
+                fan.test();
+            }
+        }
     }
 }

+ 8 - 0
share/src/hwmon.cpp

@@ -99,3 +99,11 @@ QList<QObject *> Hwmon::temps() const
     }
     return list;
 }
+
+void Hwmon::testFans()
+{
+    for (int i=0; i<m_pwmFans.size(); i++)
+    {
+        m_pwmFans.at(i)->test();
+    }
+}

+ 1 - 0
share/src/hwmon.h

@@ -55,6 +55,7 @@ public:
     Fan * fan(int i) const { return m_fans.value(i, nullptr); }
     PwmFan * pwmFan(int i) const { return m_pwmFans.value(i, nullptr); }
     Temp * temp(int i) const { return m_temps.value(i, nullptr); }
+    Q_INVOKABLE void testFans();
 
 
 public slots:

+ 9 - 1
share/src/loader.cpp

@@ -315,7 +315,7 @@ void Loader::createConfigFile()
                     usedHwmons << pwmFan->temp()->parent();
         }
     }
-    
+
     m_configFile = "INTERVAL=" + QString::number(m_interval) + "\n";
 
     m_configFile += "DEVPATH=";
@@ -468,6 +468,14 @@ void Loader::createConfigFile()
     emit configFileChanged();
 }
 
+void Loader::testFans()
+{
+    for (int i=0; i<m_hwmons.size(); i++)
+    {
+        m_hwmons.at(i)->testFans();
+    }
+}
+
 QList<QObject *> Loader::hwmons() const
 {
     QList<QObject *> list;

+ 1 - 0
share/src/loader.h

@@ -47,6 +47,7 @@ public:
     Q_INVOKABLE void parseHwmons();
     Q_INVOKABLE void open(const QUrl & = QUrl());
     Q_INVOKABLE void save(const QUrl & = QUrl());
+    Q_INVOKABLE void testFans();
     QUrl configUrl() const { return m_configUrl; }
     void setConfigUrl(const QUrl &url) { open(url); if (m_configUrl != url) { m_configUrl = url; emit configUrlChanged(); } }
     QString configFile() const { return m_configFile; }

+ 93 - 5
share/src/sensors.cpp

@@ -21,6 +21,7 @@
 #include <QFile>
 #include <QDir>
 #include <KConfigGroup>
+#include <KF5/KAuth/KAuthExecuteJob>
 
 Sensor::Sensor(Hwmon *parent, uint index) : QObject(parent)
 {
@@ -89,6 +90,11 @@ PwmFan::PwmFan(Hwmon *parent, uint index) : Fan(parent, index)
     m_minStart = 255;
     m_minStop = 255;
 
+    m_testTimer.setSingleShot(true);
+
+    m_testStatus = notTesting;
+    m_testing = false;
+
     connect(this, SIGNAL(tempChanged()), parent, SLOT(updateConfig()));
     connect(this, SIGNAL(hasTempChanged()), parent, SLOT(updateConfig()));
     connect(this, SIGNAL(minTempChanged()), parent, SLOT(updateConfig()));
@@ -97,12 +103,13 @@ PwmFan::PwmFan(Hwmon *parent, uint index) : Fan(parent, index)
     connect(this, SIGNAL(maxPwmChanged()), parent, SLOT(updateConfig()));
     connect(this, SIGNAL(minStartChanged()), parent, SLOT(updateConfig()));
     connect(this, SIGNAL(minStopChanged()), parent, SLOT(updateConfig()));
+    connect(&m_testTimer, SIGNAL(timeout()), this, SLOT(continueTesting()));
 
     if (QDir(parent->path()).isReadable())
     {
         QFile *pwmFile = new QFile(parent->path() + "/pwm" + QString::number(index), this);
 
-        if (pwmFile->open(QFile::ReadOnly)) //TODO ReadWrite with su rights
+        if (pwmFile->open(QFile::ReadOnly))
         {
             m_pwmStream.setDevice(pwmFile);
             m_pwmStream >> m_pwm;
@@ -122,10 +129,91 @@ void PwmFan::update()
     emit pwmChanged();
 }
 
-//void PwmFan::writePwm()
-//{
-//    m_pwmStream << m_pwm;
-//}
+bool PwmFan::writePwm(int pwm)
+{
+    if (m_pwmStream.device()->isWritable())
+        m_pwmStream << m_pwm;
+    else
+    {
+        KAuth::Action action("fancontrol.gui.helper.write");
+        action.setHelperId("fancontrol.gui.helper");
+        QVariantMap map;
+        map["filename"] = qobject_cast<QFile *>(m_pwmStream.device())->fileName();
+        map["content"] = pwm;
+        action.setArguments(map);
+        KAuth::ExecuteJob *reply = action.execute();
+        return reply->exec();
+    }
+    return true;
+}
+
+void PwmFan::test()
+{
+    m_testing = true;
+    emit testingChanged();
+
+    m_testStatus = findingStop1;
+    setPwm(255);
+    m_testTimer.setInterval(2000);
+    m_testTimer.start();
+    qDebug() << "Start testing...";
+}
+
+void PwmFan::continueTesting()
+{
+    update();
+    switch (m_testStatus)
+    {
+    case findingStop1:
+        if (rpm() > 0)
+            setPwm(qMin(pwm() * 0.9, pwm() - 5.0));
+        else
+        {
+            m_testStatus = findingStart;
+            m_testTimer.setInterval(500);
+            qDebug() << "Start finding start value...";
+        }
+        m_testTimer.start();
+        break;
+
+    case findingStart:
+        if (rpm() == 0)
+            setPwm(pwm() + 2);
+        else
+        {
+            m_testStatus = findingStop2;
+            m_testTimer.setInterval(1000);
+            setMinStart(pwm());
+            qDebug() << "Start finding stop value...";
+        }
+        m_testTimer.start();
+        break;
+
+    case findingStop2:
+        if (rpm() > 0)
+        {
+            setPwm(pwm() - 1);
+            m_testTimer.start();
+        }
+        else
+        {
+            m_testStatus = notTesting;
+            m_testing = false;
+            emit testingChanged();
+            setMinStop(pwm() + 5);
+            qDebug() << "Finished testing!";
+        }
+        break;
+
+    case notTesting:
+        m_testing = false;
+        emit testingChanged();
+        break;
+
+    default:
+        break;
+    }
+}
 
 void PwmFan::reset()
 {

+ 19 - 3
share/src/sensors.h

@@ -22,6 +22,7 @@
 
 #include <QObject>
 #include <QTextStream>
+#include <QTimer>
 #include <QDebug>
 #include <KSharedConfig>
 
@@ -137,7 +138,7 @@ protected:
 class FANCONTROL_GUI_EXPORT PwmFan : public Fan
 {
     Q_OBJECT
-    Q_PROPERTY(int pwm READ pwm /*WRITE setPwm*/ NOTIFY pwmChanged)
+    Q_PROPERTY(int pwm READ pwm WRITE setPwm NOTIFY pwmChanged)
     Q_PROPERTY(Temp * temp READ temp WRITE setTemp NOTIFY tempChanged)
     Q_PROPERTY(bool hasTemp READ hasTemp WRITE setHasTemp NOTIFY hasTempChanged)
     Q_PROPERTY(int minTemp READ minTemp WRITE setMinTemp NOTIFY minTempChanged)
@@ -147,13 +148,14 @@ class FANCONTROL_GUI_EXPORT PwmFan : public Fan
     Q_PROPERTY(int minStart READ minStart WRITE setMinStart NOTIFY minStartChanged)
     Q_PROPERTY(int minStop READ minStop WRITE setMinStop NOTIFY minStopChanged)
     Q_PROPERTY(bool active READ active WRITE setActive NOTIFY activeChanged)
+    Q_PROPERTY(bool testing READ testing NOTIFY testingChanged)
 
 public:
 
     explicit PwmFan(Hwmon *parent, uint index);
 
     int pwm() const { return m_pwm; }
-//    void setPwm(int pwm) { if (pwm != m_pwm) { m_pwm = pwm; emit pwmChanged(); writePwm(); } }
+    void setPwm(int pwm) { if (pwm != m_pwm) { if (writePwm(pwm)) { m_pwm = pwm; emit pwmChanged(); } } }
     Temp * temp() const { return m_temp; }
     bool hasTemp() const { return m_hasTemp; }
     int minTemp() const { return m_minTemp; }
@@ -163,6 +165,7 @@ public:
     int minStart() const { return m_minStart; }
     int minStop() const { return m_minStop; }
     bool active() const;
+    bool testing() const { return m_testing; }
     void setTemp(Temp *temp) { setHasTemp(temp != nullptr); if (temp != m_temp) { m_temp = temp; emit tempChanged(); } }
     void setHasTemp(bool hasTemp) { if (hasTemp != m_hasTemp) { m_hasTemp = hasTemp; emit hasTempChanged(); } }
     void setMinTemp(int minTemp) { if (minTemp != m_minTemp) { m_minTemp = minTemp; emit minTempChanged(); } }
@@ -173,6 +176,7 @@ public:
     void setMinStop(int minStop) { if (minStop != m_minStop) { m_minStop = minStop; emit minStopChanged(); } }
     void setActive(bool active);
     void reset();
+    Q_INVOKABLE void test();
 
 
 signals:
@@ -187,26 +191,38 @@ signals:
     void minStartChanged();
     void minStopChanged();
     void activeChanged();
+    void testingChanged();
 
 
 protected slots:
 
     void update();
-//    void writePwm();
+    bool writePwm(int);
+    void continueTesting();
 
 
 protected:
 
     int m_pwm;
     QTextStream m_pwmStream;
+    QTimer m_testTimer;
     Temp *m_temp;
     bool m_hasTemp;
+    bool m_testing;
     int m_minTemp;
     int m_maxTemp;
     int m_minPwm;
     int m_maxPwm;
     int m_minStart;
     int m_minStop;
+
+    enum
+    {
+        findingStop1,
+        findingStop2,
+        findingStart,
+        notTesting
+    } m_testStatus;
 };
 
 #endif // SENSORS_H