Browse Source

[cli] fetch function

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

+ 81
- 0
pyheatpump/cli.py View File

@@ -0,0 +1,81 @@
1
+#!/usr/bin/env python3
2
+# builtins
3
+import click
4
+import uvicorn
5
+import os
6
+import sys
7
+import re
8
+import importlib
9
+from pprint import pprint
10
+
11
+CONTEXT_SETTINGS={
12
+    'default_map':{'run': {}} 
13
+}
14
+
15
+@click.group(invoke_without_command=True, context_settings=CONTEXT_SETTINGS)
16
+@click.option('--version', is_flag=True)
17
+@click.pass_context
18
+def cli(ctx, version):
19
+    if version:
20
+        from pyheatpump import version
21
+        return click.echo(pyheatpump.version())
22
+
23
+    if ctx.invoked_subcommand is None: 
24
+        return run()
25
+
26
+
27
+@click.option('--host', default=None)
28
+@click.option('--port', default=None)
29
+@cli.command()
30
+def run(host, port):
31
+    from pyheatpump.conf import (API_HOST, API_PORT)
32
+
33
+    if not host:
34
+        host = API_HOST
35
+
36
+    if not port:
37
+        port = API_PORT
38
+
39
+    log_level = 'info'
40
+
41
+    click.echo('Launching PyHeatpump application')
42
+
43
+    uvicorn.run('pyheatpump.app:application',
44
+        host=host,
45
+        port=int(port),
46
+        log_level=log_level,
47
+        reload=False)
48
+
49
+
50
+@cli.command()
51
+def fetch():
52
+    from pyheatpump.models import *
53
+    from pyheatpump import modbus
54
+
55
+
56
+    var_types = VariableType.getall()
57
+
58
+    # Analog - float
59
+    analog = var_types['Analog']
60
+    res = modbus.read_holding_registers(analog.start_address, analog.end_address)
61
+
62
+    for r in range(len(res)):
63
+        val = VariableValue(str(analog), r + analog.start_address, res[r])
64
+        val.insert()
65
+
66
+
67
+    # Integer - int
68
+    integer = var_types['Integer']
69
+    res = modbus.read_holding_registers(integer.start_address, integer.end_address)
70
+
71
+    for r in res:
72
+        val = VariableValue(str(integer), r + integer.start_address, res[r])
73
+        val.insert()
74
+
75
+    # Digital - bool
76
+    digital = var_types['Digital']
77
+    res = modbus.read_coils(digital.start_address, digital.end_address)
78
+
79
+    for r in res:
80
+        val = VariableValue(str(integer), r + integer.start_address, res[r])
81
+        val.insert()

Loading…
Cancel
Save