2
0

hwmon.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright (C) 2015 Malte Veerman <maldela@halloarsch.de>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU Lesser General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. *
  18. */
  19. #include "hwmon.h"
  20. #include "loader.h"
  21. #include <QDir>
  22. #include <QTextStream>
  23. #include <QtQml>
  24. #include <QDebug>
  25. Hwmon::Hwmon(const QString &path, Loader *parent) : QObject(parent),
  26. m_parent(parent),
  27. m_path(path)
  28. {
  29. m_index = path.split('/').last().remove("hwmon").toInt();
  30. QFile nameFile(path + "/name");
  31. if (nameFile.open(QFile::ReadOnly))
  32. m_name = QTextStream(&nameFile).readLine();
  33. else
  34. m_name = path.split('/').last();
  35. connect(this, SIGNAL(configUpdateNeeded()), parent, SLOT(createConfigFile()));
  36. connect(this, SIGNAL(pwmFansChanged()), parent, SLOT(emitAllPwmFansChanged()));
  37. connect(this, SIGNAL(tempsChanged()), parent, SLOT(emitAllTempsChanged()));
  38. initialize();
  39. }
  40. void Hwmon::initialize()
  41. {
  42. QDir dir(m_path);
  43. QStringList entrys = dir.entryList(QDir::Files | QDir::NoDotAndDotDot);
  44. foreach (QString entry, entrys)
  45. {
  46. QString str = entry;
  47. uint index = str.remove(QRegExp("\\D+")).toUInt();
  48. if (entry.contains("fan") && entry.contains("input"))
  49. {
  50. if (QFile::exists(m_path + "/pwm" + QString::number(index)))
  51. {
  52. PwmFan *newPwmFan = Q_NULLPTR;
  53. foreach (PwmFan *pwmFan, m_pwmFans)
  54. {
  55. if (pwmFan->index() == index)
  56. {
  57. newPwmFan = pwmFan;
  58. newPwmFan->reset();
  59. break;
  60. }
  61. }
  62. if (!newPwmFan)
  63. {
  64. newPwmFan = new PwmFan(this, index);
  65. connect(this, SIGNAL(sensorsUpdateNeeded()), newPwmFan, SLOT(update()));
  66. m_pwmFans << newPwmFan;
  67. emit pwmFansChanged();
  68. }
  69. Fan *newFan = qobject_cast<Fan *>(newPwmFan);
  70. if (!m_fans.contains(newFan))
  71. {
  72. m_fans << newFan;
  73. emit fansChanged();
  74. }
  75. }
  76. else
  77. {
  78. Fan *newFan = Q_NULLPTR;
  79. foreach (Fan *fan, m_fans)
  80. {
  81. if (fan->index() == index)
  82. {
  83. newFan = fan;
  84. newFan->reset();
  85. break;
  86. }
  87. }
  88. if (!newFan)
  89. {
  90. newFan = new Fan(this, index);
  91. connect(this, SIGNAL(sensorsUpdateNeeded()), newFan, SLOT(update()));
  92. m_fans << newFan;
  93. emit fansChanged();
  94. }
  95. }
  96. }
  97. if (entry.contains("temp") && entry.contains("input"))
  98. {
  99. Temp *newTemp = Q_NULLPTR;
  100. foreach (Temp *temp, m_temps)
  101. {
  102. if (temp->index() == index)
  103. {
  104. newTemp = temp;
  105. newTemp->reset();
  106. break;
  107. }
  108. }
  109. if (!newTemp)
  110. {
  111. newTemp = new Temp(this, index);
  112. connect(this, SIGNAL(sensorsUpdateNeeded()), newTemp, SLOT(update()));
  113. m_temps << newTemp;
  114. emit tempsChanged();
  115. }
  116. }
  117. }
  118. }
  119. QList<QObject *> Hwmon::fans() const
  120. {
  121. QList<QObject *> list;
  122. foreach (Fan *fan, m_fans)
  123. {
  124. list << qobject_cast<QObject *>(fan);
  125. }
  126. return list;
  127. }
  128. QList<QObject *> Hwmon::pwmFans() const
  129. {
  130. QList<QObject *> list;
  131. foreach (PwmFan *pwmFan, m_pwmFans)
  132. {
  133. list << qobject_cast<QObject *>(pwmFan);
  134. }
  135. return list;
  136. }
  137. QList<QObject *> Hwmon::temps() const
  138. {
  139. QList<QObject *> list;
  140. foreach (Temp *temp, m_temps)
  141. {
  142. list << qobject_cast<QObject *>(temp);
  143. }
  144. return list;
  145. }
  146. void Hwmon::testFans()
  147. {
  148. for (int i=0; i<m_pwmFans.size(); i++)
  149. {
  150. m_pwmFans.at(i)->test();
  151. }
  152. }