Browse Source

[wip] [get][post] /config/last_update

Maxime Alves LIRMM@home 3 years ago
parent
commit
ca4849b2c7
3 changed files with 56 additions and 3 deletions
  1. 1
    1
      pyheatpump/cli.py
  2. 38
    1
      pyheatpump/config.py
  3. 17
    1
      tests/test_config.py

+ 1
- 1
pyheatpump/cli.py View File

@@ -150,7 +150,7 @@ def fetch(type):
150 150
 def supervise(since):
151 151
     logger = logger_init()
152 152
 
153
-    from .config import config, mac_address_init
153
+    from .config import config, mac_address_init, get_last_update, set_last_update
154 154
     mac_address = config.get('heatpump','mac_address')
155 155
     if mac_address == 'None':
156 156
         mac_address = mac_address_init()

+ 38
- 1
pyheatpump/config.py View File

@@ -31,7 +31,8 @@ default_config = {
31 31
         'interval': 10000,
32 32
         'auth': 'None',
33 33
         'initialization': str(datetime.now()),
34
-        'heatpump_id': 'None'
34
+        'heatpump_id': 'None',
35
+        'last_update': 0
35 36
     },
36 37
     'api': {
37 38
         'host': '127.0.0.1',
@@ -136,6 +137,42 @@ async def mac_route(request, *args, **kwargs):
136 137
             ', New -> ', mac_address)))
137 138
 
138 139
 
140
+def get_last_update():
141
+    return config.get('supervisor', 'last_update')
142
+
143
+
144
+def set_last_update(last_update: int=0):
145
+    global config
146
+    config.set('supervisor', 'last_update', last_update)
147
+    save_config()
148
+    return get_last_update()
149
+
150
+
151
+async def last_update_route(request, *args, **kwargs):
152
+    """
153
+    last_update :
154
+        == 0 : since current date - interval
155
+        < 0 : since current_date + last_update - interval
156
+        > 0 : since current_data - last_update - interval (same behaviour as < 0) 
157
+    """
158
+    global config
159
+    if request['method'] == 'GET':
160
+        return PlainTextResponse(get_last_update())
161
+
162
+    elif request['method'] == 'POST':
163
+        body = await request.body()
164
+        last_update = int(body.decode() or 0)
165
+        if int(body.decode() or 0):
166
+            last_update = int(datetime.now().strftime('%s'))
167
+        elif last_update <= 0:
168
+            last_update = int(datetime.now().strftime('%s')) + last_update
169
+        else:
170
+            last_update = int(datetime.now().strftime('%s')) - last_update
171
+
172
+
173
+        return PlainTextResponse(str(set_last_update(last_update)))
174
+
175
+
139 176
 ROUTES=[
140 177
     Route('/', config_route, methods=['GET', 'POST']),
141 178
     Route('/mac', mac_route, methods=['GET', 'POST'])

+ 17
- 1
tests/test_config.py View File

@@ -2,13 +2,17 @@
2 2
 import pytest
3 3
 from starlette.authentication import UnauthenticatedUser
4 4
 from starlette.testclient import TestClient
5
-from pyheatpump.config import app, config, default_config, CONFIG_FILES, get_config, set_config, config_route, ROUTES, mac_address_init, save_config
5
+from pyheatpump.config import app, config, default_config, \
6
+    CONFIG_FILES, get_config, set_config, config_route, ROUTES, \
7
+    mac_address_init, save_config, get_last_update, set_last_update, \
8
+    last_update_route
6 9
 from unittest.mock import patch, MagicMock
7 10
 from pprint import pprint
8 11
 import json
9 12
 from tempfile import mkstemp
10 13
 from configparser import ConfigParser
11 14
 import os
15
+from requests import Request
12 16
 
13 17
 @pytest.fixture
14 18
 def tmpconf(testdir):
@@ -75,3 +79,15 @@ def test_mac_address(testdir, tmpconf):
75 79
         assert config_mac_address == mac_address
76 80
     except Exception as e:
77 81
         raise e
82
+
83
+
84
+@pytest.mark.asyncio
85
+async def test_last_update(testdir, tmpconf):
86
+    req = Request()
87
+    req.method = 'POST'
88
+    res = await last_update_route(req)
89
+    last_update_0 = int(res.body)
90
+
91
+    res = await get_last_update(req)
92
+    last_update_1 = int(res.body)
93
+    assert last_update_0 == last_update_1

Loading…
Cancel
Save