-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutility.py
More file actions
247 lines (185 loc) · 7.38 KB
/
utility.py
File metadata and controls
247 lines (185 loc) · 7.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import logging
import traceback, sys
import serial, io, time
from math import trunc
from rich import pretty # una volta installato, definisce un output colorato di default
from rich import print
from datetime import datetime
LOG_NAME = "utility"
def getStrTimeElapsed( timeSec ):
if timeSec < 60:
return f"{ round ( timeSec ) }s"
elif timeSec < 60*60:
min = trunc ( timeSec/60 )
sec = timeSec - min * 60
return f"{ min }m { trunc ( sec ) }s"
else:
return f"{ round ( timeSec / 60 ) }m"
return "--"
def logException( msg = None ):
log = logging.getLogger ( LOG_NAME )
#
if msg == None:
log.exception ( "general error" )
else:
log.exception ( msg )
print ( f"{traceback.format_exc()}\{sys.exc_info()[2]}" )
class timer():
def __init__( self ):
self.update ()
def update( self ):
self.start = datetime.now ().timestamp ()
def elapsedAtLeast( self, deltaSec ):
self.lastDeltaSecs = datetime.now ().timestamp () - self.start
#
if self.lastDeltaSecs > deltaSec:
return True
else:
return False
def getDeltaTime( self ):
self.lastDeltaSecs = datetime.now ().timestamp () - self.start
return self.lastDeltaSecs
def strStart( self ):
return datetime.fromtimestamp ( self.start ).strftime ( "%a %d/%m/%Y %H:%M:%S" )
def __str__( self ):
return f"{ round ( self.getDeltaTime () ) }s"
class progresso():
LOG_NAME = "progresso"
TP_CAP = 0
TP_TEST = 1
def __init__( self ):
self.reset ()
def reset( self ):
self.totCount = 0
#
self.cap = "--"
self.num = "--"
#
self.test = "--"
self.capCount = 0
#
self.fasi = []
self.tmpFasi = []
self.mainRes = True
def set( self, kind, num, msg ):
log = logging.getLogger ( self.LOG_NAME )
self.totCount += 1
self.capCount += 1
#
if self.mainRes:
match kind:
case self.TP_CAP:
self.num = num
self.cap = msg
self.capCount = 0
self.tmpFasi = []
self.test = "--"
case self.TP_TEST:
self.test = msg
def update( self, kind, res, val="" ):
log = logging.getLogger ( self.LOG_NAME )
#
if self.mainRes:
match kind:
case self.TP_CAP:
for k in self.tmpFasi:
#print( f"k {k}" )
self.fasi.append ( k )
if not k [ 'res' ]: self.mainRes = res
case self.TP_TEST:
self.tmpFasi.append ( { "num": f"{self.num} ({self.capCount})", "cap": self.cap, "test": self.test, "val": val, "res": res, "op": f"{self.totCount}" } )
def sCap( self, num, msg ):
self.set ( self.TP_CAP, num, msg )
log = logging.getLogger ( self.LOG_NAME )
log.info ( f"{num:<6} {msg}" )
def sTest( self, msg ):
self.set ( self.TP_TEST, "", msg )
log = logging.getLogger ( self.LOG_NAME )
log.info ( f"{self.capCount:>03}) {msg} ({self.totCount})" )
def uCap( self ):
self.update ( self.TP_CAP, True )
return True
def close( self, res ):
self.update ( self.TP_CAP, res )
return True
def uTest( self, res, val="" ):
self.update ( self.TP_TEST, res, val )
return res
def getFasi( self ):
self.uCap ()
return self.fasi.copy ()
def __str__( self ):
return f"{ self.num }.{ self.capCount } - { self.cap } - { self.test }"
class port():
def __init__( self, com=None, uid=None, baud=115200, timeout=1, progr=progresso(), dm=None ):
self.com = com
self.uid = uid
self.baud = baud
self.timeoutDef = timeout
self.timeout = timeout
self.progr = progr
self.dm = dm
self.s = None
self.sio = None
#
if com == None and uid == None:
raise Exception ( "E' necessario definire una com o un uid" )
def getSio( self ): return self.sio
def getSer( self ): return self.s
def conn( self, timeout=None ):
log = logging.getLogger ( LOG_NAME )
justClosed = False
#
if self.com == None:
if self.dm == None: raise
else: self.com = self.dm.getComNumber ( self.uid )
if timeout == None: timeout = self.timeoutDef
#
if self.timeout != timeout:
if self.s != None: justClosed = True
self.close ()
#if self.timeout != 0: log.warning ( f"{com=} Switch to new timeout {timeout} secs" )
#
if self.s == None:
tm = timer ()
uscita = False
saveErr = None
while not tm.elapsedAtLeast ( 5.0 ) and not uscita:
try:
self.s = serial.Serial ( self.com, self.baud, timeout=timeout, interCharTimeout=0.001 )#0.001 )
self.timeout = timeout
#log.warning ( f"ser opened with {timeout=}" )
if not justClosed: log.debug ( f"{self.com=} aperta dopo {tm.getDeltaTime():3.2f}s" )
uscita = True
except Exception as err:
saveErr = err#log.critical ( f"{self.com=} {tm.getDeltaTime():3.2f}s - board gia' occupata\n{err}\n{traceback.format_exc()}\n{sys.exc_info()[2]}" )
time.sleep ( 0.1 )
#raise
#
if not uscita:
log.critical ( f"{self.com=} indisponibile o occupata\n{saveErr}" )
return False # propagazione errore apertura seriale
self.sio = io.TextIOWrapper ( io.BufferedRWPair ( self.s, self.s ) )
return True
def close( self ):
if self.s != None:
log = logging.getLogger ( LOG_NAME )
log.debug ( f"Chiusura di {self.com=}" )
self.s.close ()
self.s = None
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser ()
parser.add_argument ( 'com', nargs=1, help = 'numero com' )
args = vars ( parser.parse_args () )
#pretty.install ()
#
test = port ( "COM" + args [ 'com' ][0] )
if test.conn ():
#test.sio.write ( "N\x0D" )
#test.sio.flush()
recv = test.sio.read ()
print ( f"{recv=}" )
else:
print ( "impossibile connettere la com" )