hwmon.cpp 5.0 KB

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