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.

164 lines
4.3 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 <QTranslator>
  9. #include <QSettings>
  10. #include <QString>
  11. #include <QDebug>
  12. #include "settings.h"
  13. const char default_room_names[MAX_NB_ZONES][42] = {
  14. "Bedroom",
  15. "Living Room",
  16. "Workspace",
  17. "Bathroom"
  18. };
  19. const char default_sensors_topics[MAX_NB_ZONES][64] = {
  20. "sensors/bedroom/xiaomi",
  21. "sensors/living_room/xiaomi",
  22. "sensors/workspace/xiaomi",
  23. "sensors/bathroom/xiaomi"
  24. };
  25. const char default_availability_topics[MAX_NB_ZONES][64] = {
  26. "sensors/bedroom/availability",
  27. "sensors/living_room/availability",
  28. "sensors/workspace/availability",
  29. "sensors/bathroom/availability"
  30. };
  31. const char default_control_topics[MAX_NB_ZONES][64] = {
  32. "heaters/bedroom",
  33. "heaters/living_room",
  34. "heaters/workspace",
  35. "heaters/bathroom"
  36. };
  37. Settings *Settings::_singleton = NULL;
  38. Settings::Settings(QObject *parent) : QSettings(parent)
  39. {
  40. struct Room room;
  41. struct Heater heater;
  42. struct Program program;
  43. int nb_rooms;
  44. nb_rooms = this->beginReadArray("rooms");
  45. if (nb_rooms == 0) {
  46. this->endArray();
  47. set_rooms_default_config();
  48. nb_rooms = this->beginReadArray("rooms");
  49. }
  50. if (nb_rooms > MAX_NB_ZONES) {
  51. // TODO;
  52. nb_rooms = MAX_NB_ZONES;
  53. }
  54. // TODO: handle unset values in a better way
  55. for (int i = 0; i < nb_rooms; ++i) {
  56. this->setArrayIndex(i);
  57. room.name = this->value("name", default_room_names[i]).toString();
  58. room.sensor_topic = this->value("sensor_topic", default_sensors_topics[i]).toString();
  59. room.availability_topic = this->value("availability_topic",
  60. default_availability_topics[i]).toString();
  61. int size = this->beginReadArray("heaters");
  62. for (int j = 0; j < size; ++j) {
  63. this->setArrayIndex(j);
  64. heater.ctrl_topic = this->value("control_topic", "").toString();
  65. room.heaters << heater;
  66. }
  67. this->endArray();
  68. room.default_temperature = this->value("default_temperature", 20).toDouble();
  69. size = this->beginReadArray("temperature_schedule");
  70. for (int j = 0; j < size; ++j) {
  71. this->setArrayIndex(j);
  72. program.temperature = this->value("temperature", 20).toDouble();
  73. program.DoW = this->value("days_of_week", 127).toInt();
  74. program.start_time = QTime::fromString(this->value("start_time", "").toString(), "H:m");
  75. program.end_time = QTime::fromString(this->value("end_time", "").toString(), "H:m");
  76. if (program.start_time.isValid() && program.end_time.isValid() &&
  77. (program.DoW > 0) && (program.DoW < 128)) {
  78. room.progs << program;
  79. }
  80. }
  81. this->endArray();
  82. m_rooms << room;
  83. }
  84. this->endArray();
  85. m_broker.address = this->value("broker/address", "localhost").toString();
  86. m_broker.port = this->value("broker/port", 1883).toInt();
  87. }
  88. void Settings::set_rooms_default_config(void)
  89. {
  90. this->beginWriteArray("rooms");
  91. for (int i = 0; i < MAX_NB_ZONES; i++) {
  92. this->setArrayIndex(i);
  93. this->setValue("name", tr(default_room_names[i]));
  94. this->setValue("sensor_topic", default_sensors_topics[i]);
  95. this->setValue("availability_topic", default_availability_topics[i]);
  96. this->setValue("default_temperature", 20);
  97. this->beginWriteArray("heaters");
  98. this->setArrayIndex(0);
  99. this->setValue("control_topic", default_control_topics[i]);
  100. this->endArray();
  101. }
  102. this->setArrayIndex(0);
  103. this->beginWriteArray("temperature_schedule");
  104. this->setArrayIndex(0);
  105. this->setValue("temperature", 18);
  106. this->setValue("days_of_week", 127);
  107. this->setValue("start_time", QTime(22,0).toString("H:m"));
  108. this->setValue("end_time", QTime(7,0).toString("H:m"));
  109. this->endArray();
  110. this->setArrayIndex(3);
  111. this->beginWriteArray("temperature_schedule");
  112. this->setArrayIndex(0);
  113. this->setValue("temperature", 99); // Force On
  114. this->setValue("days_of_week", 31);
  115. this->setValue("start_time", QTime(6,30).toString("H:m"));
  116. this->setValue("end_time", QTime(7,0).toString("H:m"));
  117. this->setArrayIndex(1);
  118. this->setValue("temperature", 99); // Force On
  119. this->setValue("days_of_week", 96);
  120. this->setValue("start_time", QTime(7,30).toString("H:m"));
  121. this->setValue("end_time", QTime(8,0).toString("H:m"));
  122. this->endArray();
  123. this->endArray();
  124. this->sync();
  125. }
  126. int Settings::nbZones(void)
  127. {
  128. return m_rooms.size();
  129. }
  130. Settings::~Settings(void)
  131. {
  132. }
  133. Settings *Settings::getInstance(void)
  134. {
  135. if (!_singleton) {
  136. _singleton = new Settings();
  137. }
  138. return _singleton;
  139. }
  140. /* vim: set tabstop=8 shiftwidth=8 softtabstop=0 noexpandtab: */