forked from EstevanTH/CAN-Monitoring_ELM327
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CANCaptureFrameHandler.py
60 lines (53 loc) · 1.85 KB
/
CANCaptureFrameHandler.py
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
import threading
from traceback import format_exc
from utility import printT
from CANCaptureHTTPServer import WebSocket_frames
class CANCaptureFrameHandler( threading.Thread ):
"""
Thread that delegates the work of CAN frames handling for improved timings
Particular attention is needed for thread safety.
"""
daemon = True
asyncException = None # exception caught in the process, re-raised on frame income
def __init__( self, canSource ):
threading.Thread.__init__( self )
self.canSource = canSource
self.pendingFrames = None
self.pendingFramesLock = threading.Lock()
self.continueProcessLock = threading.Lock()
self.continueProcessLock.acquire()
def handleNewFrame( self, frame ):
if self.asyncException is not None:
asyncException = self.asyncException
self.asyncException = None
raise asyncException
else:
self.pendingFramesLock.acquire()
self.pendingFrames.append( frame )
self.pendingFramesLock.release()
try:
self.continueProcessLock.release()
except RuntimeError:
pass
def run( self ):
canSource = self.canSource
self.pendingFrames = []
while True:
self.continueProcessLock.acquire()
presentPendingFrames = True
while presentPendingFrames:
self.pendingFramesLock.acquire()
pendingFrames = self.pendingFrames
self.pendingFrames = [] # cleanup
self.pendingFramesLock.release()
presentPendingFrames = len( pendingFrames )!=0
for frame in pendingFrames:
try:
if canSource.passesFilters( frame ): # thread-safety required
WebSocket_frames.broadcastFrame( frame )
if canSource.canExporter is not None:
canSource.canExporter.logFrame( frame )
except Exception as asyncException:
# This exception will be raised on next frame income, with an incorrect stack trace.
self.asyncException = asyncException
printT( format_exc() )