ソースを参照

first testable db implementation for storing radiator gui values

tags/v1.0.0
Dirk Alders 1年前
コミット
a3e4b8839c
3個のファイルの変更85行の追加22行の削除
  1. 1
    0
      .gitignore
  2. 51
    0
      function/db.py
  3. 33
    22
      function/modules.py

+ 1
- 0
.gitignore ファイルの表示

@@ -1,4 +1,5 @@
1 1
 config.py
2
+database.db
2 3
 
3 4
 # ---> Linux
4 5
 *~

+ 51
- 0
function/db.py ファイルの表示

@@ -0,0 +1,51 @@
1
+import sqlite3
2
+
3
+
4
+def get_gui_radiator_data(topic):
5
+    return __storage__().get_gui_radiator_data(topic)
6
+
7
+
8
+def set_gui_radiator_data(topic, away_mode, summer_mode, user_temperatur_setpoint):
9
+    return __storage__().store_gui_radiator_data(topic, away_mode, summer_mode, user_temperatur_setpoint)
10
+
11
+
12
+class __storage__(object):
13
+    def __init__(self):
14
+        self.conn = sqlite3.connect("database.db")
15
+        self.c = self.conn.cursor()
16
+        with self.conn:
17
+            self.c.execute("""CREATE TABLE IF NOT EXISTS gui_radiator (
18
+                    topic text PRIMARY KEY,
19
+                    away_mode integer,
20
+                    summer_mode integer,
21
+                    user_temperatur_setpoint real
22
+                )""")
23
+
24
+    def store_gui_radiator_data(self, topic, away_mode, summer_mode, user_temperatur_setpoint):
25
+        data = [topic, away_mode, summer_mode, user_temperatur_setpoint]
26
+        try:
27
+            with self.conn:
28
+                self.c.execute(
29
+                    'INSERT INTO gui_radiator VALUES (?, ?, ?, ?)', data)
30
+        except sqlite3.IntegrityError:
31
+            data = [away_mode, summer_mode, user_temperatur_setpoint]
32
+            db_data = self.get_gui_radiator_data(topic)
33
+            if db_data != data:
34
+                with self.conn:
35
+                    self.c.execute(
36
+                        'UPDATE gui_radiator SET away_mode = ?, summer_mode = ?, user_temperatur_setpoint = ? WHERE topic = ?', data + [topic])
37
+
38
+    def get_gui_radiator_data(self, topic):
39
+        """ returns a list [away_mode, summer_mode, user_temperatur_setpoint] or [None, None, None]"""
40
+        self.c.execute("SELECT * FROM gui_radiator WHERE topic=?", (topic, ))
41
+        data = self.c.fetchone()
42
+        if data is not None:
43
+            data = list(data)
44
+            data[1] = data[1] == 1
45
+            data[2] = data[2] == 1
46
+            return data[1:]
47
+        else:
48
+            return [None, None, None]
49
+
50
+    def __del__(self):
51
+        self.conn.close()

+ 33
- 22
function/modules.py ファイルの表示

@@ -4,6 +4,7 @@
4 4
 
5 5
 import config
6 6
 import devices
7
+from function.db import get_gui_radiator_data, set_gui_radiator_data
7 8
 from function.rooms import room_shelly
8 9
 import logging
9 10
 import task
@@ -140,16 +141,22 @@ class radiator_function(object):
140 141
 
141 142
     def __init__(self, mqtt_client, topic_valve, topic_gui, default_temperature):
142 143
         self.default_temperature = default_temperature
143
-        self.regular_temp_setpoint = self.default_temperature
144
-        self.__away_mode__ = False
145
-        self.__summer_mode__ = False
146
-        #
147
-        self.ct = task.periodic(1, self.cyclic_task)
148
-        #
144
+        self.boost_timer = None
145
+        # device initialisation
149 146
         self.heating_valve = devices.brennenstuhl_heatingvalve(mqtt_client, topic_valve)
150 147
         self.gui_heating = devices.nodered_gui_radiator(mqtt_client, topic_gui)
151
-        #
152
-        self.heating_valve.set_heating_setpoint(self.default_temperature)
148
+        # db-stored data initialisation
149
+        db_data = get_gui_radiator_data(topic_gui)
150
+        self.__away_mode__ = db_data[0] or False
151
+        self.__summer_mode__ = db_data[1] or False
152
+        self.__user_temperature_setpoint__ = db_data[2] or default_temperature
153
+        if self.__away_mode__:
154
+            self.away_mode(None, None, True)
155
+        elif self.__summer_mode__:
156
+            self.summer_mode(None, None, True)
157
+        else:
158
+            self.set_heating_setpoint(None, None, self.__user_temperature_setpoint__)
159
+        # callback initialisation
153 160
         self.heating_valve.add_callback(devices.brennenstuhl_heatingvalve.KEY_TEMPERATURE, None, self.gui_heating.set_temperature_mcb)
154 161
         self.heating_valve.add_callback(devices.brennenstuhl_heatingvalve.KEY_HEATING_SETPOINT, None, self.get_radiator_setpoint)
155 162
         #
@@ -158,9 +165,8 @@ class radiator_function(object):
158 165
         self.gui_heating.add_callback(devices.nodered_gui_radiator.KEY_SETPOINT_TO_DEFAULT, None, self.setpoint_to_default)
159 166
         self.gui_heating.add_callback(devices.nodered_gui_radiator.KEY_AWAY, None, self.away_mode)
160 167
         self.gui_heating.add_callback(devices.nodered_gui_radiator.KEY_SUMMER, None, self.summer_mode)
161
-        #
162
-        self.boost_timer = None
163
-        #
168
+        # cyclic task initialisation
169
+        self.ct = task.periodic(1, self.cyclic_task)
164 170
         self.ct.run()
165 171
 
166 172
     def cyclic_task(self, rt):
@@ -170,19 +176,23 @@ class radiator_function(object):
170 176
             self.boost_timer -= self.ct.cycle_time
171 177
             if self.boost_timer <= 0:
172 178
                 self.cancel_boost()
173
-                self.heating_valve.set_heating_setpoint(self.regular_temp_setpoint)
179
+                self.heating_valve.set_heating_setpoint(self.__user_temperature_setpoint__)
180
+                self.heating_valve.logger.info('Timer expired. returning to regular temperature setpoint %.1f°C.', self.__user_temperature_setpoint__)
174 181
 
175 182
     def cancel_boost(self, device=None, key=None, data=None):
176 183
         if self.boost_timer is not None:
177
-            self.heating_valve.logger.info('Timer expired. returning to regular temperature setpoint %.1f°C.', self.regular_temp_setpoint)
178 184
             self.boost_timer = None
179 185
             self.gui_heating.set_timer('-')
180 186
 
181
-    def update_states(self, away_mode=None, summer_mode=None):
187
+    def update_states(self, away_mode=None, summer_mode=None, user_temperature_setpoint=None):
182 188
         if away_mode is not None:
183 189
             self.__away_mode__ = away_mode
184 190
         if summer_mode is not None:
185 191
             self.__summer_mode__ = summer_mode
192
+        if user_temperature_setpoint is not None:
193
+            self.__user_temperature_setpoint__ = user_temperature_setpoint
194
+        set_gui_radiator_data(self.gui_heating.topic, self.__away_mode__, self.__summer_mode__, self.__user_temperature_setpoint__)
195
+        #
186 196
         self.gui_heating.set_away(self.__away_mode__)
187 197
         self.gui_heating.set_summer(self.__summer_mode__)
188 198
         self.gui_heating.set_enable(not self.__away_mode__ and not self.__summer_mode__)
@@ -191,10 +201,10 @@ class radiator_function(object):
191 201
         if value is True:
192 202
             self.cancel_boost()
193 203
             self.update_states(away_mode=value, summer_mode=False)
194
-            self.heating_valve.set_heating_setpoint(self.default_temperature - self.AWAY_REDUCTION)
204
+            self.heating_valve.set_heating_setpoint(self.__user_temperature_setpoint__ - self.AWAY_REDUCTION)
195 205
         else:
196 206
             self.update_states(away_mode=value)
197
-            self.heating_valve.set_heating_setpoint(self.default_temperature)
207
+            self.heating_valve.set_heating_setpoint(self.__user_temperature_setpoint__)
198 208
 
199 209
     def summer_mode(self, device, key, value):
200 210
         if value is True:
@@ -203,7 +213,7 @@ class radiator_function(object):
203 213
             self.heating_valve.set_heating_setpoint(self.SUMMER_TEMPERATURE)
204 214
         else:
205 215
             self.update_states(summer_mode=value)
206
-            self.heating_valve.set_heating_setpoint(self.default_temperature)
216
+            self.heating_valve.set_heating_setpoint(self.__user_temperature_setpoint__)
207 217
 
208 218
     def boost(self, device, key, data):
209 219
         if self.boost_timer is None:
@@ -220,12 +230,13 @@ class radiator_function(object):
220 230
         self.set_heating_setpoint(device, key, self.default_temperature)
221 231
 
222 232
     def set_heating_setpoint(self, device, key, data):
223
-        self.heating_valve.set_heating_setpoint(data)
224 233
         self.cancel_boost()
225
-        self.update_states(away_mode=False, summer_mode=False)
234
+        self.update_states(away_mode=False, summer_mode=False, user_temperature_setpoint=data)
235
+        self.heating_valve.set_heating_setpoint(data)
226 236
 
227 237
     def get_radiator_setpoint(self, device, key, data):
228
-        self.gui_heating.set_setpoint_temperature(data)
229
-        self.update_states()
230 238
         if self.boost_timer is None and not self.__away_mode__ and not self.__summer_mode__:
231
-            self.regular_temp_setpoint = data
239
+            self.update_states(user_temperature_setpoint=data)
240
+        else:
241
+            self.update_states()
242
+        self.gui_heating.set_setpoint_temperature(data)

読み込み中…
キャンセル
保存