Browse Source

[heatpump] added first routes

Maxime Alves LIRMM@home 3 years ago
parent
commit
285ded56bf
4 changed files with 83 additions and 16 deletions
  1. 0
    0
      __init__.py
  2. 10
    0
      app.py
  3. 23
    0
      heatpump.py
  4. 50
    16
      modbus_get.py

+ 0
- 0
__init__.py View File


+ 10
- 0
app.py View File

@@ -0,0 +1,10 @@
1
+#!/usr/bin/env python3
2
+
3
+from starlette.routing import Router
4
+from starlette.mounting import Mount
5
+
6
+app = Router(routes=[
7
+    Route('/', index),
8
+    Mount('/heatpump', heatpump),
9
+    Mount('/config', config),
10
+])

+ 23
- 0
heatpump.py View File

@@ -0,0 +1,23 @@
1
+#!/usr/bin/env python3
2
+from modbus.client.serial import rtu
3
+from starlette.routing import Router
4
+from starlette.responses import JSONResponse
5
+from conf import config
6
+from db.db import sql
7
+
8
+def get_variables(request):
9
+    pass
10
+
11
+
12
+def get_variables_by_type(request):
13
+    pass
14
+
15
+
16
+def get_var_type_addresses(request):
17
+    request.path_params['type'] 
18
+
19
+heatpump = [
20
+    Route('/', get_variables, methods=['GET']),
21
+    Route('/{type:str}', get_variables_by_type, methods=['GET'])
22
+]
23
+

+ 50
- 16
modbus_get.py View File

@@ -9,6 +9,8 @@ from serial.serialutil import SerialException
9 9
 from umodbus.client.serial import rtu
10 10
 import json
11 11
 
12
+from db.db import sql
13
+
12 14
 s = Serial('/dev/ttyUSB0',19200)
13 15
 try:
14 16
     s.open()
@@ -18,8 +20,17 @@ except SerialException:
18 20
 BOOLEAN=1000
19 21
 FLOAT=500
20 22
 INT=1250
21
-try:
22
-    res = {'A':{},'D':{},'I':{}}
23
+
24
+def read_digital():
25
+    global req_adu
26
+    global sql
27
+    global s
28
+
29
+    start_address, end_address = sql(
30
+        f"SELECT start_address, end_address FROM var_type
31
+        WHERE slabel LIKE 'D';")
32
+
33
+    res = []
23 34
 
24 35
     # digital - boolean
25 36
     req_adu = rtu.read_coils(
@@ -32,48 +43,71 @@ try:
32 43
     address = -1
33 44
     try:
34 45
         for address in range(0, BOOLEAN, 1):
35
-            res['D'][address+1] = (resp[address] > 0)
46
+            res[address+1] = (resp[address] > 0)
36 47
     except KeyError as e:
37 48
         print(e)
38
-        res['D'][address] = False
49
+        res[address] = False
39 50
     except IndexError:
40 51
         print(address)
41 52
 
53
+    return res
54
+
55
+def read_analog():
56
+    global req_adu
57
+    global sql
58
+    global s
59
+
60
+    start_address, end_address = sql(
61
+        f"SELECT start_address, end_address FROM var_type
62
+        WHERE slabel LIKE 'A';")
63
+
42 64
     # analog - float
43
-    for address in range(0, FLOAT, 125):
65
+    for address in range(1, end_address - start_address, 125):
44 66
         req_adu = rtu.read_holding_registers(
45 67
             slave_id=1,
46
-            starting_address=1+address,
68
+            starting_address=start_address + address,
47 69
             quantity=125)
48 70
 
49 71
         resp = rtu.send_message(req_adu, s)
50 72
 
51 73
         for n in range(125):
52 74
             try:
53
-                res['A'][address + n] =  int(resp[n]) / 10
75
+                res[address + n] =  int(resp[n]) / 10
54 76
             except Exception as e:
55 77
                 print(e)
56
-                res['A'][address + n] = 0.
57
-            #res['A'][ str(address + var) ] = int(resp[var]) / 10.
78
+                res[address + n] = 0.
79
+
80
+    return res
81
+
82
+def read_int():
83
+    global req_adu
84
+    global sql
85
+    global s
86
+
87
+    res = []
88
+
89
+    start_address, end_address = sql(
90
+        f"SELECT start_address, end_address FROM var_type
91
+        WHERE slabel LIKE 'I';")
58 92
 
59 93
     # integer - int
60
-    for address in range(1, INT, 125):
94
+    for address in range(1, end_address - start_address, 125):
61 95
         req_adu = rtu.read_coils(
62 96
             slave_id=1,
63
-            starting_address=address+5002,
97
+            starting_address=start_address + address,
64 98
             quantity=125)
65 99
 
66 100
         resp = rtu.send_message(req_adu, s)
67 101
 
68 102
         for n in range(125):
69 103
             try:
70
-                res['I'][address + n] = int(resp[n])
104
+                res[address + n] = int(resp[n])
71 105
             except KeyError as e:
72 106
                 print(e)
73
-                res['I'][address + n] = 0
74
-except KeyboardInterrupt:
75
-    print("Quitting")
76
-    pass
107
+                res[address + n] = 0
108
+
109
+    return res
110
+
77 111
 
78 112
 s.close()
79 113
 

Loading…
Cancel
Save