Browse Source

[clean] cleaned logs and old code

Maxime Alves LIRMM@home 3 years ago
parent
commit
bbf2baae2d

+ 0
- 12
pyheatpump/cli.py View File

@@ -10,7 +10,6 @@ import sys
10 10
 import time
11 11
 import urllib
12 12
 import uvicorn
13
-from pprint import pprint
14 13
 import requests
15 14
 
16 15
 from pyheatpump.logger import logger_init
@@ -189,17 +188,6 @@ def supervise(since):
189 188
 
190 189
     build_url = lambda d: '{scheme}://{hostname}:{port}{path}'.format(**d)
191 190
 
192
-    """
193
-    @TODO : Use a proper certificate 
194
-
195
-    if base_url['scheme'] == 'https':
196
-        certificate = config.get('supervisor', 'certificate')
197
-        if not os.path.isfile(certificate):
198
-            raise Exception(f'Certificate not found :{certificate}') 
199
-        print(certificate)
200
-    else:
201
-        certificate = None
202
-    """
203 191
 
204 192
     post_url = {
205 193
         **base_url,

+ 5
- 6
pyheatpump/config.py View File

@@ -8,7 +8,6 @@ from starlette.requests import Request
8 8
 from starlette.routing import Route, Router
9 9
 from starlette.responses import PlainTextResponse, JSONResponse
10 10
 from starlette.exceptions import HTTPException
11
-from pprint import pprint
12 11
 import uvicorn
13 12
 import json
14 13
 
@@ -67,11 +66,11 @@ def mac_address_init():
67 66
 
68 67
     interface = gateways()['default'][AF_INET][1]
69 68
     if len(ifaddresses(interface)) == 0:
70
-        logger.error("Can't find interface")
69
+        logger.error("Can't find interface %s", interface)
71 70
         sys.exit(1)
72 71
 
73 72
     if len(ifaddresses(interface)[AF_LINK]) == 0:
74
-        logger.error("Can't find interface")
73
+        logger.error("Can't find interface %s", interface)
75 74
         sys.exit(1)
76 75
 
77 76
     addr = ifaddresses(interface)[AF_LINK][0]['addr']
@@ -140,7 +139,7 @@ async def mac_route(request, *args, **kwargs):
140 139
             config.set('heatpump','mac_address',mac_address)
141 140
             save_config()
142 141
         else:
143
-            print(f'Wrong mac format : {mac_address}')
142
+            logger.error(f'Wrong mac format : {mac_address}')
144 143
 
145 144
         return PlainTextResponse(' '.join((
146 145
             'Current ->',
@@ -172,10 +171,10 @@ async def last_update_route(request, *args, **kwargs):
172 171
         body = await request.body()
173 172
         last_update = int(body.decode() or 0)
174 173
         if last_update == 0:
175
-            print(f'Reset last update to current time')
176 174
             last_update = int(datetime.now().strftime('%s'))
175
+            logger.info('Reset last update to current time (%s)', last_update)
177 176
         else:
178
-            print(f'{"Forward" if last_update < 0 else "Rewind"} of {last_update} seconds')
177
+            logger.debug('%s of %s seconds', ("Forward" if last_update < 0 else "Rewind"), last_update)
179 178
             last_update = int(datetime.now().strftime('%s')) - last_update
180 179
 
181 180
 

+ 4
- 5
pyheatpump/db.py View File

@@ -3,7 +3,6 @@ import sqlite3
3 3
 from subprocess import Popen
4 4
 from .config import config
5 5
 import sys
6
-from pprint import pprint
7 6
 
8 7
 from pyheatpump.logger import logger_init
9 8
 logger = logger_init()
@@ -22,7 +21,7 @@ class DB():
22 21
             except sqlite3.ProgrammingError:
23 22
                 pass
24 23
             except Exception as e:
25
-                print(e)
24
+                logger.error('DB.connect: %s', e)
26 25
                 sys.exit(1)
27 26
 
28 27
         logger.info('Will connect to database {}'.format(
@@ -71,11 +70,11 @@ class DB():
71 70
 
72 71
             try:
73 72
                 return DB.Conn.execute((' '.join(f.readlines())).replace('\n', ''))
74
-            except StopIteration:
75
-                print('failed')
73
+            except StopIteration as e:
74
+                logger.error('sqlf(%s): %s', filename, e)
76 75
                 return []
77 76
             except sqlite3.IntegrityError as e:
78
-                print(e)
77
+                logger.error('sqlf(%s): %s', filename, e)
79 78
                 return []
80 79
             except sqlite3.OperationalError as e:
81 80
                 raise Exception(f'Can\'t find DB file {filename}') from e

+ 7
- 40
pyheatpump/modbus.py View File

@@ -18,9 +18,9 @@ def connect():
18 18
         real_serial_port = os.path.realpath(
19 19
             config.get('heatpump', 'serial_port'))
20 20
 
21
-        print('Connecting to serial port *{}* ({})'.format(
21
+        logger.info('Connecting to serial port *%s* (%s)',
22 22
             config.get('heatpump', 'serial_port'),
23
-            real_serial_port))
23
+            real_serial_port)
24 24
 
25 25
         serial_conn = Serial(
26 26
             port=real_serial_port,
@@ -31,7 +31,7 @@ def connect():
31 31
             timeout=20)
32 32
 
33 33
     if serial_conn.open is False:
34
-        print('Opening serial port')
34
+        logger.debug('Opening serial port *%s*', config.get('heatpump', 'serial_port')
35 35
         serial_conn.open()
36 36
 
37 37
     return serial_conn
@@ -59,8 +59,8 @@ def read_coils(start, end):
59 59
             response = rtu.send_message(req_adu, serial_conn)
60 60
             res.extend(response)
61 61
     except umodbus.exceptions.IllegalDataAddressError as e:
62
-        print(e)
63
-        print(f'{address} {qty}')
62
+        logger.error(e)
63
+        logger.debug('address:%s qty:%s', address, qty)
64 64
 
65 65
     logger.debug('read_coils [%s, %s] result: %s', start, end, res)
66 66
     return res
@@ -93,8 +93,8 @@ def read_holding_registers(start, end):
93 93
             response = rtu.send_message(req_adu, serial_conn)
94 94
             res.extend(response)
95 95
     except umodbus.exceptions.IllegalDataAddressError as e:
96
-        print(e)
97
-        print(f'{address} {qty}')
96
+        logger.error('read_holding_registers(%s, %s): address:%s, qty:%s\n%s',
97
+            start, end, address, qty, e)
98 98
 
99 99
     logger.debug('read_holding_registers [%s, %s] result: %s', start, end, res)
100 100
     return res
@@ -145,36 +145,3 @@ def write_holding_register(var_value):
145 145
         return True
146 146
     except Exception as e:
147 147
         raise e
148
-
149
-if __name__ == '__main__':
150
-    response = read_holding_registers(1, 10)
151
-    print(len(response))
152
-    response = read_coils(90, 100)
153
-    print(len(response))
154
-    
155
-    """
156
-    Example codeo
157
-    write_coil(VariableValue(**{
158
-        'type':'D',
159
-        'address':91,
160
-        'value':1}))
161
-
162
-    resp = read_coils(91, 93)
163
-    print(f'91 : {response[0]} - 92 : {response[1]}')
164
-
165
-    write_coil(VariableValue(**{
166
-        'type':'D',
167
-        'address':91,
168
-        'value':0}))
169
-
170
-    response = read_coils(91, 93)
171
-    print(f'91 : {response[0]} - 92 : {response[1]}')
172
-
173
-    response = read_holding_registers(240, 242) 
174
-    print(response)
175
-
176
-    write_holding_register(VariableValue(**{
177
-        'type':'D',
178
-        'address':91,
179
-        'value':1})
180
-    """

+ 1
- 1
pyheatpump/models/variable.py View File

@@ -37,7 +37,7 @@ class Variable(RowClass):
37 37
             })
38 38
             return True
39 39
         except Exception as e:
40
-            print(e)
40
+            logger.error('Variable.insert: %s', e)
41 41
             return False
42 42
 
43 43
 

+ 3
- 1
pyheatpump/models/variable_type.py View File

@@ -1,5 +1,7 @@
1 1
 from pyheatpump.db import DB, RowClass
2 2
 
3
+from pyheatpump.logger import logger_init
4
+logger = logger_init()
3 5
 
4 6
 class VariableType(RowClass):
5 7
     slabel: str = '' 
@@ -39,7 +41,7 @@ class VariableType(RowClass):
39 41
         try:
40 42
             elt = next(super().select('slabel', 'var_type'))
41 43
         except StopIteration:
42
-            print('No element exists')
44
+            logger.error('VariableType.select(%s, %s): does not exist')
43 45
         return elt
44 46
 
45 47
 

+ 1
- 2
pyheatpump/models/variable_value.py View File

@@ -46,7 +46,7 @@ class VariableValue(RowClass):
46 46
             )
47 47
             return True
48 48
         except Exception as e:
49
-            print(e)
49
+            logger.error('VariableValue.insert: %s', e)
50 50
             return False
51 51
 
52 52
 
@@ -67,7 +67,6 @@ class VariableValue(RowClass):
67 67
     @staticmethod
68 68
     def get(var_type, address, time=datetime.now()):
69 69
         try:
70
-            print(int(time.strftime('%s'))+1)
71 70
             row = next(DB.sql(
72 71
             """SELECT * FROM var_value
73 72
             WHERE

+ 0
- 1
pyheatpump/variable_types.py View File

@@ -4,7 +4,6 @@ from datetime import datetime
4 4
 from configparser import ConfigParser
5 5
 from starlette.routing import Route, Router
6 6
 from starlette.responses import PlainTextResponse, JSONResponse
7
-from pprint import pprint
8 7
 import uvicorn
9 8
 import json
10 9
 

+ 0
- 2
pyheatpump/variable_values.py View File

@@ -5,7 +5,6 @@ from configparser import ConfigParser
5 5
 from starlette.routing import Route, Router
6 6
 from starlette.responses import PlainTextResponse, JSONResponse
7 7
 from starlette.exceptions import HTTPException
8
-from pprint import pprint
9 8
 import uvicorn
10 9
 import json
11 10
 
@@ -30,7 +29,6 @@ async def get_variable_value(var_type: str, address: int, time: str=None):
30 29
             'address': address,
31 30
             'time': time
32 31
         }
33
-        print(args)
34 32
         if time is None:
35 33
             args.pop('time')
36 34
         else:

+ 0
- 1
pyheatpump/variables.py View File

@@ -4,7 +4,6 @@ from datetime import datetime
4 4
 from configparser import ConfigParser
5 5
 from starlette.routing import Route, Router
6 6
 from starlette.responses import PlainTextResponse, JSONResponse
7
-from pprint import pprint
8 7
 import uvicorn
9 8
 import json
10 9
 

Loading…
Cancel
Save