Browse Source

[heatpump] fix conversion adresses

Maxime Alves LIRMM@home 3 years ago
parent
commit
6c7e585b49

+ 13
- 15
pyheatpump/cli.py View File

1
 #!/usr/bin/env python3
1
 #!/usr/bin/env python3
2
 # builtins
2
 # builtins
3
 import click
3
 import click
4
-import uvicorn
4
+from datetime import datetime
5
+import importlib
6
+import json
5
 import os
7
 import os
6
-import sys
7
 import re
8
 import re
8
-import importlib
9
-from pprint import pprint
9
+import sys
10
+import time
10
 import urllib
11
 import urllib
12
+import uvicorn
13
+from pprint import pprint
11
 import requests
14
 import requests
12
-import json
13
-from datetime import datetime
14
 
15
 
15
 from pyheatpump.logger import logger_init
16
 from pyheatpump.logger import logger_init
16
 from pyheatpump.models import *
17
 from pyheatpump.models import *
205
 
206
 
206
     logger.info(build_url(post_url))
207
     logger.info(build_url(post_url))
207
 
208
 
208
-    for packet in post_packets:
209
-        try:
210
-            logger.debug(json.dumps(shift_response(packet)))
211
-        except Exception as e:
212
-            print(e)
213
-            sys.exit(1)
214
-
215
-
216
     for packet in post_packets:
209
     for packet in post_packets:
217
         logger.debug('Will send %s', shift_response(packet))
210
         logger.debug('Will send %s', shift_response(packet))
218
         post_resp = requests.post(
211
         post_resp = requests.post(
219
             url=build_url(post_url),
212
             url=build_url(post_url),
220
             json=shift_response(packet),
213
             json=shift_response(packet),
221
-            verify=False
214
+            verify=False,
215
+            timeout=20
222
         )
216
         )
217
+        time.sleep(5)
223
         if post_resp.status_code == 200:
218
         if post_resp.status_code == 200:
224
             logger.info('POST to supervisor succeeded')
219
             logger.info('POST to supervisor succeeded')
220
+            continue
221
+
222
+        logger.info('POST to supervisor failed')
225
 
223
 
226
     set_last_update(int(datetime.now().strftime('%s')))
224
     set_last_update(int(datetime.now().strftime('%s')))
227
 
225
 

+ 1
- 1
pyheatpump/lib.py View File

10
         d_res = {
10
         d_res = {
11
             str(k - offset): d_[k]
11
             str(k - offset): d_[k]
12
             for k in d_k
12
             for k in d_k
13
-            if (k in d_ and isinstance(d_[k], int))
13
+            if (k in d_ and (isinstance(d_[k], int) or isinstance(d_[k], float)))
14
         }
14
         }
15
 
15
 
16
         return d_res
16
         return d_res

+ 4
- 3
pyheatpump/models/heatpump.py View File

31
 
31
 
32
     def __dict__(self):
32
     def __dict__(self):
33
         r = {'macAddress': self.macformat}
33
         r = {'macAddress': self.macformat}
34
-        if self.last_update is None:
34
+        if not self.last_update:
35
+            # Since beginning
35
             return { **r, **self.get_var_values() }
36
             return { **r, **self.get_var_values() }
36
         return { **r, **self.get_var_values_since() }
37
         return { **r, **self.get_var_values_since() }
37
 
38
 
52
         return r
53
         return r
53
 
54
 
54
     def control(self, data):
55
     def control(self, data):
55
-        logger.warn('Received orders data :')
56
-        logger.warn(data)
56
+        logger.warning('Received orders data :')
57
+        logger.warning(data)
57
         if 'macAddress' not in data.keys():
58
         if 'macAddress' not in data.keys():
58
             logger.info(f'No orders')
59
             logger.info(f'No orders')
59
             return True
60
             return True

+ 3
- 0
pyheatpump/models/variable.py View File

6
 from .variable_type import VariableType
6
 from .variable_type import VariableType
7
 from .variable_value import VariableValue
7
 from .variable_value import VariableValue
8
 
8
 
9
+from pyheatpump.logger import logger_init
10
+logger = logger_init()
11
+
9
 class Variable(RowClass):
12
 class Variable(RowClass):
10
     address: int = None
13
     address: int = None
11
     unit: str = None
14
     unit: str = None

+ 37
- 4
tests/cli/test_supervise.py View File

33
     except ValueError:
33
     except ValueError:
34
         pass
34
         pass
35
 
35
 
36
-@patch('pyheatpump.cli.requests')
37
-def test_supervise(requestsMock, fetch, runner):
36
+@pytest.fixture
37
+def default_config():
38
     config['supervisor']['scheme'] = 'http'
38
     config['supervisor']['scheme'] = 'http'
39
     config['supervisor']['host'] = '127.0.0.1'
39
     config['supervisor']['host'] = '127.0.0.1'
40
     config['supervisor']['port'] = '8080'
40
     config['supervisor']['port'] = '8080'
42
     config['supervisor']['get_path'] = '/get'
42
     config['supervisor']['get_path'] = '/get'
43
     config['heatpump']['mac_address'] = '00:11:22:33:44:55'
43
     config['heatpump']['mac_address'] = '00:11:22:33:44:55'
44
 
44
 
45
+@pytest.mark.skip
46
+@patch('pyheatpump.cli.requests')
47
+def test_supervise(requestsMock, fetch, runner, default_config):
48
+    data = {'macAddress':'001122334455'}
49
+    data_analog = { **data, **{'Analog':{} } }
50
+    data_integer = { **data, **{'Integer':{} } }
51
+    data_digital = { **data, **{'Digital':{} } }
52
+
53
+    try:
54
+        r = runner.invoke(cli, 'supervise')
55
+    except ValueError:
56
+        pass
57
+
58
+    requestsMock.post.assert_called()
59
+    requestsMock.post.assert_called_with(
60
+        url='http://127.0.0.1:8080/post',
61
+        json=data_integer,
62
+        verify=False
63
+    )
64
+
65
+    requestsMock.get.assert_called()
66
+    requestsMock.get.assert_called_with(
67
+        url=''.join('http://127.0.0.1:8080/get/001122334455'),
68
+        verify=False)
69
+
70
+
71
+@patch('pyheatpump.cli.requests')
72
+def test_supervise_values(requestsMock, fetch, runner, default_config, set_db_values):
45
     data = {'macAddress':'001122334455'}
73
     data = {'macAddress':'001122334455'}
74
+    data_analog = { **data, **{'Analog':{} } }
75
+    data_integer = { **data, **{'Integer':{} } }
76
+    data_digital = { **data, **{'Digital':{} } }
46
 
77
 
47
     try:
78
     try:
48
         r = runner.invoke(cli, 'supervise')
79
         r = runner.invoke(cli, 'supervise')
52
     requestsMock.post.assert_called()
83
     requestsMock.post.assert_called()
53
     requestsMock.post.assert_called_with(
84
     requestsMock.post.assert_called_with(
54
         url='http://127.0.0.1:8080/post',
85
         url='http://127.0.0.1:8080/post',
55
-        json=data
86
+        json=data_integer,
87
+        verify=False
56
     )
88
     )
57
 
89
 
58
     requestsMock.get.assert_called()
90
     requestsMock.get.assert_called()
59
     requestsMock.get.assert_called_with(
91
     requestsMock.get.assert_called_with(
60
-        url=''.join('http://127.0.0.1:8080/get/001122334455'))
92
+        url=''.join('http://127.0.0.1:8080/get/001122334455'),
93
+        verify=False)

+ 14
- 3
tests/test_lib.py View File

6
 def test_shift_response():
6
 def test_shift_response():
7
     heatpump_dict = {}
7
     heatpump_dict = {}
8
     heatpump_dict['Analog'] = {
8
     heatpump_dict['Analog'] = {
9
-        key: 0
9
+        key: (0 if key % 2 == 0 else 1)
10
         for key in range(1, 1251)
10
         for key in range(1, 1251)
11
     }
11
     }
12
     heatpump_dict['Integer'] = {
12
     heatpump_dict['Integer'] = {
13
-        key: 0
13
+        key: (0 if key % 2 == 0 else 1)
14
         for key in range(5002, 6252 )
14
         for key in range(5002, 6252 )
15
     }
15
     }
16
     heatpump_dict['Digital'] = {
16
     heatpump_dict['Digital'] = {
17
-        key: 0
17
+            key: (0 if key % 2 == 0 else 1 )
18
         for key in range(1, 1001)
18
         for key in range(1, 1001)
19
     }
19
     }
20
 
20
 
25
 
25
 
26
     assert 'Analog' in shifted.keys()
26
     assert 'Analog' in shifted.keys()
27
     assert isinstance(shifted['Analog'], dict)
27
     assert isinstance(shifted['Analog'], dict)
28
+    analog_keys = list(map(int, shifted['Analog'].keys()))
29
+    assert min(analog_keys) == 1
30
+    assert shifted['Analog']["1"] == 1
31
+    assert shifted['Analog']["2"] == 0
28
 
32
 
29
     assert 'Digital' in shifted.keys()
33
     assert 'Digital' in shifted.keys()
30
     assert isinstance(shifted['Digital'], dict)
34
     assert isinstance(shifted['Digital'], dict)
31
 
35
 
36
+    digital_keys = list(map(int, shifted['Digital'].keys()))
37
+    assert min(digital_keys) == 1
38
+    assert shifted['Digital']["1"] == 1
39
+    assert shifted['Digital']["2"] == 0
40
+
32
     assert 'Integer' in shifted.keys()
41
     assert 'Integer' in shifted.keys()
33
     assert isinstance(shifted['Integer'], dict)
42
     assert isinstance(shifted['Integer'], dict)
34
     for k in shifted['Integer'].keys():
43
     for k in shifted['Integer'].keys():
37
     int_keys = list(map(int, shifted['Integer'].keys()))
46
     int_keys = list(map(int, shifted['Integer'].keys()))
38
     assert min(int_keys) == 1
47
     assert min(int_keys) == 1
39
     assert max(int_keys) == 1250
48
     assert max(int_keys) == 1250
49
+    assert shifted['Integer']["1"] == 0
50
+    assert shifted['Integer']["2"] == 1

Loading…
Cancel
Save