1212import psutil
1313import os
1414from collections import deque
15+ import redis
16+
17+ REDIS_URL = ""
18+ REDIS_CHANNEL_NAME = "can_messages"
19+
20+
21+ # Setting up Redis client
22+ try :
23+ redis_client = redis .Redis ()
24+ print ("redis client initialized" )
25+ except Exception as e :
26+ print ("redis database couldn't be reached" )
27+ print (e )
1528
1629# Optional cantools import
1730try :
3649# Configuration
3750UDP_PORT = 12345
3851TIME_SYNC_PORT = 12346
39- NAMED_PIPE_PATH = "/tmp/can_data_pipe"
52+ # NAMED_PIPE_PATH = "/tmp/can_data_pipe"
4053HTTP_FORWARD_URL = "http://127.0.0.1:8085/can"
4154
4255# Memory safeguards
5366udp_sock .setsockopt (socket .SOL_SOCKET , socket .SO_BROADCAST , 1 )
5467udp_sock .bind (('' , UDP_PORT ))
5568
56- def setup_named_pipe ():
57- """Create a named pipe for local communication."""
58- try :
59- if os .path .exists (NAMED_PIPE_PATH ):
60- os .unlink (NAMED_PIPE_PATH )
61- print (f"Removed existing named pipe: { NAMED_PIPE_PATH } " )
62- os .mkfifo (NAMED_PIPE_PATH )
63- print (f"Created named pipe: { NAMED_PIPE_PATH } " )
64- except FileExistsError :
65- print (f"Named pipe already exists: { NAMED_PIPE_PATH } " )
66- except Exception as e :
67- print (f"Error creating named pipe: { e } " )
69+ # def setup_named_pipe():
70+ # """Create a named pipe for local communication."""
71+ # try:
72+ # if os.path.exists(NAMED_PIPE_PATH):
73+ # os.unlink(NAMED_PIPE_PATH)
74+ # print(f"Removed existing named pipe: {NAMED_PIPE_PATH}")
75+ # os.mkfifo(NAMED_PIPE_PATH)
76+ # print(f"Created named pipe: {NAMED_PIPE_PATH}")
77+ # except FileExistsError:
78+ # print(f"Named pipe already exists: {NAMED_PIPE_PATH}")
79+ # except Exception as e:
80+ # print(f"Error creating named pipe: {e}")
6881
69- setup_named_pipe ()
82+ # setup_named_pipe()
7083print (f"Base station listening for ESP32 CAN JSON on UDP { UDP_PORT } " )
71- print (f"CAN data available via named pipe: { NAMED_PIPE_PATH } " )
84+ print (f"CAN data available via Redis pub/sub " )
7285
7386# Use deque with maxlen for automatic memory management
7487batched_frames = deque (maxlen = MAX_BATCH_SIZE )
7588batch_lock = threading .Lock ()
76- pipe_fd = None
77- pipe_file = None
89+ # pipe_fd = None
90+ # pipe_file = None
7891last_batch_time = time .time ()
7992
8093# Statistics
8194stats = {
8295 'udp_messages_received' : 0 ,
8396 'can_frames_processed' : 0 ,
84- 'pipe_writes_success ' : 0 ,
85- 'pipe_writes_failed ' : 0 ,
97+ 'messages_published_success ' : 0 ,
98+ 'messages_published_failed ' : 0 ,
8699 'http_forwards_success' : 0 ,
87100 'http_forwards_failed' : 0 ,
88- 'last_message_time' : 0
101+ 'last_message_time' : 0.0
89102}
90103
91104def print_stats ():
@@ -105,65 +118,65 @@ def print_stats():
105118 print (f"Batched frames: { batch_size } /{ MAX_BATCH_SIZE } " )
106119 print (f"UDP messages received: { stats ['udp_messages_received' ]} " )
107120 print (f"CAN frames processed: { stats ['can_frames_processed' ]} " )
108- print (f"Pipe writes: { stats ['pipe_writes_success ' ]} success, { stats ['pipe_writes_failed ' ]} failed" )
121+ print (f"Pipe writes: { stats ['messages_published_success ' ]} success, { stats ['messages_published_failed ' ]} failed" )
109122 print (f"HTTP forwards: { stats ['http_forwards_success' ]} success, { stats ['http_forwards_failed' ]} failed" )
110123 print (f"Time since last message: { time_since_last :.1f} s" )
111124 print (f"==================" )
112125
113- def open_pipe ():
114- """Open the named pipe for writing."""
115- global pipe_fd , pipe_file
116- try :
117- if pipe_fd is not None :
118- return True
119- pipe_fd = os .open (NAMED_PIPE_PATH , os .O_WRONLY | os .O_NONBLOCK )
120- pipe_file = os .fdopen (pipe_fd , 'w' )
121- print ("Opened named pipe for writing" )
122- return True
123- except Exception as e :
124- print (f"Error opening pipe: { e } " )
125- pipe_fd = None
126- pipe_file = None
127- return False
126+ # def open_pipe():
127+ # """Open the named pipe for writing."""
128+ # global pipe_fd, pipe_file
129+ # try:
130+ # if pipe_fd is not None:
131+ # return True
132+ # pipe_fd = os.open(NAMED_PIPE_PATH, os.O_WRONLY | os.O_NONBLOCK)
133+ # pipe_file = os.fdopen(pipe_fd, 'w')
134+ # print("Opened named pipe for writing")
135+ # return True
136+ # except Exception as e:
137+ # print(f"Error opening pipe: {e}")
138+ # pipe_fd = None
139+ # pipe_file = None
140+ # return False
128141
129- def close_pipe ():
130- """Close the named pipe."""
131- global pipe_fd , pipe_file
132- try :
133- if pipe_file :
134- pipe_file .close ()
135- pipe_fd = None
136- pipe_file = None
137- except Exception as e :
138- print (f"Error closing pipe: { e } " )
142+ # def close_pipe():
143+ # """Close the named pipe."""
144+ # global pipe_fd, pipe_file
145+ # try:
146+ # if pipe_file:
147+ # pipe_file.close()
148+ # pipe_fd = None
149+ # pipe_file = None
150+ # except Exception as e:
151+ # print(f"Error closing pipe: {e}")
139152
140153def canserver_broadcast (frames ):
141154 """Write CAN frames to named pipe with error handling."""
142- global pipe_file
143155 if not frames :
144156 return
145157
146158 try :
147- if not open_pipe () :
159+ if not redis_client :
148160 print ("Failed to open pipe for writing" )
149- stats ['pipe_writes_failed ' ] += 1
161+ stats ['messages_published_failed ' ] += 1
150162 return
151163
152164 for frame in frames :
153165 line = json .dumps (frame ) + "\n "
154- pipe_file .write (line )
155- pipe_file .flush ()
156- stats ['pipe_writes_success' ] += 1
166+ redis_client .publish (REDIS_CHANNEL_NAME , line )
167+ # pipe_file.write(line)
168+ # pipe_file.flush()
169+ stats ['messages_published_success' ] += 1
157170 print (f"Successfully wrote { len (frames )} frames to pipe" )
158171 except (OSError , IOError ) as e :
159- stats ['pipe_writes_failed ' ] += 1
172+ stats ['messages_published_failed ' ] += 1
160173 if e .errno != 32 : # Ignore "Broken pipe" when no reader
161174 print (f"Pipe write error: { e } " )
162- close_pipe ()
175+ # close_pipe()
163176 except Exception as e :
164- stats ['pipe_writes_failed ' ] += 1
177+ stats ['messages_published_failed ' ] += 1
165178 print (f"Unexpected pipe error: { e } " )
166- close_pipe ()
179+ # close_pipe()
167180
168181def send_can_messages_batch (messages_batch ):
169182 """Send a batch of CAN messages to HTTP endpoint."""
@@ -292,9 +305,10 @@ def send_test_messages():
292305 print ("Exiting..." )
293306finally :
294307 udp_sock .close ()
295- close_pipe ()
296- try :
297- os .unlink (NAMED_PIPE_PATH )
298- print (f"Cleaned up named pipe: { NAMED_PIPE_PATH } " )
299- except FileNotFoundError :
300- pass
308+ redis_client .close ()
309+ # close_pipe()
310+ # try:
311+ # os.unlink(NAMED_PIPE_PATH)
312+ # # print(f"Cleaned up named pipe: {NAMED_PIPE_PATH}")
313+ # except FileNotFoundError:
314+ # pass
0 commit comments