hwmon.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 <QtCore/QDir>
  21. #include <QtCore/QTextStream>
  22. namespace Fancontrol
  23. {
  24. Hwmon::Hwmon(const QString &path, QObject *parent) : QObject(parent),
  25. m_path(path),
  26. m_valid(true)
  27. {
  28. QDir dir(path);
  29. if (!dir.isReadable())
  30. {
  31. emit errorChanged(path + " is not readable!");
  32. m_valid = false;
  33. }
  34. bool success;
  35. m_index = path.split('/').last().remove("hwmon").toInt(&success);
  36. if (!success)
  37. {
  38. emit errorChanged(path + "is invalid!");
  39. m_valid = false;
  40. }
  41. QFile nameFile(path + "/name");
  42. if (nameFile.open(QFile::ReadOnly))
  43. m_name = QTextStream(&nameFile).readLine();
  44. else
  45. m_name = path.split('/').last();
  46. connect(this, SIGNAL(configUpdateNeeded()), parent, SLOT(createConfigFile()));
  47. connect(this, SIGNAL(pwmFansChanged()), parent, SLOT(emitAllPwmFansChanged()));
  48. connect(this, SIGNAL(tempsChanged()), parent, SLOT(emitAllTempsChanged()));
  49. connect(this, SIGNAL(errorChanged(QString)), parent, SLOT(setError(QString)));
  50. if (m_valid)
  51. initialize();
  52. }
  53. void Hwmon::initialize()
  54. {
  55. QDir dir(m_path);
  56. QStringList entrys = dir.entryList(QDir::Files | QDir::NoDotAndDotDot);
  57. foreach (QString entry, entrys)
  58. {
  59. QString str = entry;
  60. uint index = str.remove(QRegExp("\\D+")).toUInt();
  61. if (entry.contains("fan") && entry.contains("input"))
  62. {
  63. if (QFile::exists(m_path + "/pwm" + QString::number(index)))
  64. {
  65. PwmFan *newPwmFan = Q_NULLPTR;
  66. foreach (PwmFan *pwmFan, m_pwmFans)
  67. {
  68. if (pwmFan->index() == index)
  69. {
  70. newPwmFan = pwmFan;
  71. newPwmFan->reset();
  72. break;
  73. }
  74. }
  75. if (!newPwmFan)
  76. {
  77. newPwmFan = new PwmFan(this, index);
  78. connect(this, SIGNAL(sensorsUpdateNeeded()), newPwmFan, SLOT(update()));
  79. m_pwmFans << newPwmFan;
  80. emit pwmFansChanged();
  81. }
  82. Fan *newFan = qobject_cast<Fan *>(newPwmFan);
  83. if (!m_fans.contains(newFan))
  84. {
  85. m_fans << newFan;
  86. emit fansChanged();
  87. }
  88. }
  89. else
  90. {
  91. Fan *newFan = Q_NULLPTR;
  92. foreach (Fan *fan, m_fans)
  93. {
  94. if (fan->index() == index)
  95. {
  96. newFan = fan;
  97. newFan->reset();
  98. break;
  99. }
  100. }
  101. if (!newFan)
  102. {
  103. newFan = new Fan(this, index);
  104. connect(this, SIGNAL(sensorsUpdateNeeded()), newFan, SLOT(update()));
  105. m_fans << newFan;
  106. emit fansChanged();
  107. }
  108. }
  109. }
  110. if (entry.contains("temp") && entry.contains("input"))
  111. {
  112. Temp *newTemp = Q_NULLPTR;
  113. foreach (Temp *temp, m_temps)
  114. {
  115. if (temp->index() == index)
  116. {
  117. newTemp = temp;
  118. newTemp->reset();
  119. break;
  120. }
  121. }
  122. if (!newTemp)
  123. {
  124. newTemp = new Temp(this, index);
  125. connect(this, SIGNAL(sensorsUpdateNeeded()), newTemp, SLOT(update()));
  126. m_temps << newTemp;
  127. emit tempsChanged();
  128. }
  129. }
  130. }
  131. }
  132. QList<QObject *> Hwmon::fans() const
  133. {
  134. QList<QObject *> list;
  135. foreach (Fan *fan, m_fans)
  136. {
  137. list << qobject_cast<QObject *>(fan);
  138. }
  139. return list;
  140. }
  141. QList<QObject *> Hwmon::pwmFans() const
  142. {
  143. QList<QObject *> list;
  144. foreach (PwmFan *pwmFan, m_pwmFans)
  145. {
  146. list << qobject_cast<QObject *>(pwmFan);
  147. }
  148. return list;
  149. }
  150. QList<QObject *> Hwmon::temps() const
  151. {
  152. QList<QObject *> list;
  153. foreach (Temp *temp, m_temps)
  154. {
  155. list << qobject_cast<QObject *>(temp);
  156. }
  157. return list;
  158. }
  159. void Hwmon::testFans()
  160. {
  161. for (int i=0; i<m_pwmFans.size(); i++)
  162. {
  163. m_pwmFans.at(i)->test();
  164. }
  165. }
  166. Fan* Hwmon::fan(int i) const
  167. {
  168. return m_fans.value(i, Q_NULLPTR);
  169. }
  170. PwmFan* Hwmon::pwmFan(int i) const
  171. {
  172. return m_pwmFans.value(i, Q_NULLPTR);
  173. }
  174. Temp* Hwmon::temp(int i) const
  175. {
  176. return m_temps.value(i, Q_NULLPTR);
  177. }
  178. void Hwmon::setError(const QString &error)
  179. {
  180. emit errorChanged(error);
  181. }
  182. }