Browse Source

[cli][supervise] added the *supervise* functions to insert data to supervisor

Maxime Alves LIRMM@home 3 years ago
parent
commit
88d769c29b
1 changed files with 71 additions and 0 deletions
  1. 71
    0
      pyheatpump/cli.py

+ 71
- 0
pyheatpump/cli.py View File

@@ -7,6 +7,10 @@ import sys
7 7
 import re
8 8
 import importlib
9 9
 from pprint import pprint
10
+import urllib
11
+import requests
12
+import json
13
+from datetime import datetime
10 14
 
11 15
 from pyheatpump.logger import logger_init
12 16
 from pyheatpump.models import *
@@ -129,3 +133,70 @@ def fetch():
129 133
 
130 134
     commit()
131 135
     logger.info('Successfully read all variables')
136
+
137
+
138
+@click.option('--since', is_flag=True)
139
+@cli.command()
140
+def supervise(since):
141
+    logger = logger_init()
142
+
143
+    from .config import config
144
+    from .models.heatpump import Heatpump
145
+
146
+    last_update = None
147
+    if since:
148
+        last_update = int(datetime.now().strftime('%s')) - config.getint('supervisor', 'interval')
149
+
150
+    h = Heatpump(config.get('heatpump','mac_address'), last_update)
151
+
152
+    base_url = {
153
+        'scheme':config.get('supervisor', 'scheme'),
154
+        'hostname':config.get('supervisor', 'host'),
155
+        'port':config.getint('supervisor', 'port')
156
+    }
157
+
158
+    build_url = lambda d: '{scheme}://{hostname}:{port}{path}'.format(**d)
159
+
160
+    if base_url['scheme'] == 'https':
161
+        certificate = config.get('supervisor', 'certificate')
162
+        if not os.path.isfile(certificate):
163
+            raise Exception(f'Certificate not found :{certificate}') 
164
+        print(certificate)
165
+    else:
166
+        certificate = None
167
+
168
+    post_url = {
169
+        **base_url,
170
+        **{'path': config.get('supervisor', 'post_path')}
171
+    }
172
+
173
+    logger.info(build_url(post_url))
174
+    try:
175
+        json.dumps(h.__dict__())
176
+    except Exception as e:
177
+        print(e)
178
+        sys.exit(1)
179
+
180
+    logger.debug(h.__dict__())
181
+
182
+    post_req = requests.post(
183
+        url=build_url(post_url),
184
+        json=h.__dict__(),
185
+        verify=False
186
+    )
187
+    if post_req.status_code == 200:
188
+        logger.info('POST to supervisor succeeded')
189
+
190
+    get_path = '/'.join((
191
+        config.get('supervisor', 'get_path'),
192
+        h.macformat
193
+    ))
194
+    get_url = {
195
+        **base_url,
196
+        **{'path': get_path}
197
+    }
198
+
199
+    get_req = requests.get(
200
+        url=build_url(get_url),
201
+        verify=False
202
+    )

Loading…
Cancel
Save