Thermostat pour piloter jusqu'à 4 radiateurs avec fil pilote
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
3.0 KiB

  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. /*
  3. * Qt mutizone MQTT thermostat
  4. *
  5. * Copyright (C) 2019 Richard Genoud
  6. *
  7. */
  8. #include <QtWidgets>
  9. #include <QPushButton>
  10. #include <QTime>
  11. #include <QLocale>
  12. #include <QVector>
  13. #include "mqttclient.h"
  14. #include "zoneitem.h"
  15. #include "settings.h"
  16. #include "mainwindow.h"
  17. MainWindow::MainWindow(QWidget *parent) :
  18. QMainWindow(parent)
  19. {
  20. ZoneItem *zone;
  21. Settings *s = Settings::getInstance();
  22. MQTTClient *mqttclient = MQTTClient::getInstance();
  23. for (int i = 0; i < s->nbZones(); i++) {
  24. zone = new ZoneItem(s->m_rooms.at(i).name, this);
  25. m_zones << zone;
  26. }
  27. connect(mqttclient, SIGNAL(new_temperature(int, double)),
  28. this, SLOT(temperature_slot(int, double)));
  29. connect(mqttclient, SIGNAL(new_hygro(int, double)),
  30. this, SLOT(hygro_slot(int, double)));
  31. connect(mqttclient, SIGNAL(new_availability(int, bool)),
  32. this, SLOT(availability_slot(int, bool)));
  33. /*
  34. * Sensors-related layout
  35. */
  36. QGridLayout *mainLayout = new QGridLayout;
  37. m_state_btn.setText(tr("Auto"));
  38. if (s->nbZones() < 4) {
  39. // TODO
  40. qDebug() << "bad configuration" ;
  41. } else {
  42. mainLayout->addWidget(m_zones.at(0), 0, 0);
  43. mainLayout->addWidget(m_zones.at(1), 0, 1);
  44. mainLayout->addWidget(new QPushButton(tr("Menu")), 0, 2);
  45. mainLayout->addWidget(m_zones.at(2), 1, 0);
  46. mainLayout->addWidget(m_zones.at(3), 1, 1);
  47. mainLayout->addWidget(&m_state_btn, 1, 2);
  48. }
  49. m_pwr_state = OFF;
  50. update_state_btn(m_pwr_state);
  51. QWidget *mainWidget = new QWidget;
  52. mainWidget->setLayout(mainLayout);
  53. /*
  54. * Top widget
  55. */
  56. setCentralWidget(mainWidget);
  57. setWindowTitle(tr("Sorico's thermostat"));
  58. m_backLoop.start();
  59. connect(&m_state_btn, SIGNAL(clicked()), this, SLOT(change_state()));
  60. }
  61. MainWindow::~MainWindow()
  62. {
  63. }
  64. void MainWindow::update_state_btn(enum power_states st)
  65. {
  66. switch (st) {
  67. case OFF:
  68. m_state_btn.setText(tr("Force off"));
  69. break;
  70. case ON:
  71. m_state_btn.setText(tr("Force on"));
  72. break;
  73. case AUTO:
  74. m_state_btn.setText(tr("Auto"));
  75. break;
  76. default:
  77. m_state_btn.setText(tr("Error"));
  78. break;
  79. }
  80. }
  81. void MainWindow::change_state(void)
  82. {
  83. switch (m_pwr_state) {
  84. case OFF:
  85. m_pwr_state = ON;
  86. break;
  87. case ON:
  88. m_pwr_state = AUTO;
  89. break;
  90. case AUTO:
  91. /* fall through */
  92. default:
  93. m_pwr_state = OFF;
  94. break;
  95. }
  96. update_state_btn(m_pwr_state);
  97. }
  98. void MainWindow::temperature_slot(int idx, double val)
  99. {
  100. Settings *s = Settings::getInstance();
  101. qDebug() << "temperature idx:" << idx << " val: " << val << endl;
  102. if (idx < s->nbZones()) {
  103. m_zones.at(idx)->set_temperature_value(val);
  104. }
  105. }
  106. void MainWindow::hygro_slot(int idx, double val)
  107. {
  108. Settings *s = Settings::getInstance();
  109. qDebug() << "hygro idx:" << idx << " val: " << val << endl;
  110. if (idx < s->nbZones()) {
  111. m_zones.at(idx)->set_hygro_value(val);
  112. }
  113. }
  114. void MainWindow::availability_slot(int idx, bool ok)
  115. {
  116. Settings *s = Settings::getInstance();
  117. if ((idx < s->nbZones()) && !ok) {
  118. m_zones.at(idx)->set_hygro_value(0);
  119. m_zones.at(idx)->set_temperature_value(0);
  120. }
  121. }
  122. /* vim: set tabstop=8 shiftwidth=8 softtabstop=0 noexpandtab: */