This commit is contained in:
Your Name
2024-02-16 19:31:03 -06:00
parent 19673dc652
commit 5c2c44b780
3 changed files with 11 additions and 10 deletions

View File

@@ -5,6 +5,7 @@ import json
watcher_sock = None watcher_sock = None
class Watcher: class Watcher:
@staticmethod
def ensure_socket_connected(): def ensure_socket_connected():
global watcher_sock global watcher_sock
if watcher_sock is None or watcher_sock.fileno() == -1: # Checks if socket is not initialized or closed if watcher_sock is None or watcher_sock.fileno() == -1: # Checks if socket is not initialized or closed
@@ -16,19 +17,17 @@ class Watcher:
# If connection fails, set sock to None and do nothing # If connection fails, set sock to None and do nothing
watcher_sock = None watcher_sock = None
@staticmethod
def log_watch(var_name, message): def log_watch(var_name, message):
global watcher_sock global watcher_sock
self.ensure_socket_connected() # Ensure the socket is connected before attempting to log Watcher.ensure_socket_connected() # Ensure the socket is connected before attempting to log
if watcher_sock: # Proceed only if sock is not None (i.e., is connected) if watcher_sock: # Proceed only if sock is not None (i.e., is connected)
message_json = json.dumps(message) message_json = json.dumps({var_name: message})
try: try:
watcher_sock.sendall(message_json.encode('utf-8') + b'\n') watcher_sock.sendall(message_json.encode('utf-8') + b'\n')
except socket.error: except socket.error:
nothing=False
# Handle potential error in sending (e.g., if connection was lost) # Handle potential error in sending (e.g., if connection was lost)
watcher_sock.close() # Close the current socket to clean up resources watcher_sock = None # Ensure that the next attempt will try to reconnect
watcher_sock = None # Reset sock to ensure reconnection attempt on next call finally:
watcher_sock.close() # Close the current socket to clean up resources
#message = {"variable": "car_stats", "value": {"car_lights_on": [3, 5, 7], "car_state": "on"}} watcher_sock = None # Reset sock to ensure reconnection attempt on next call
#log_watch(sock, message)

View File

@@ -44,6 +44,7 @@ unix_socket_server.listen(socketPath, () => {
}); });
function processMessage(message) { function processMessage(message) {
console.log(message);
try { try {
const { variable, value } = JSON.parse(message); const { variable, value } = JSON.parse(message);
const diff = calculateDiff(progvars[variable], value); const diff = calculateDiff(progvars[variable], value);

View File

@@ -16,7 +16,6 @@ from openpilot.common.watcher import Watcher
REQUEST_HEADERS = {'User-Agent': "openpilot-" + get_version()} REQUEST_HEADERS = {'User-Agent': "openpilot-" + get_version()}
def set_timezone(valid_timezones, timezone): def set_timezone(valid_timezones, timezone):
if timezone not in valid_timezones: if timezone not in valid_timezones:
cloudlog.error(f"Timezone not supported {timezone}") cloudlog.error(f"Timezone not supported {timezone}")
@@ -48,6 +47,8 @@ def main() -> NoReturn:
set_timezone(valid_timezones, timezone) set_timezone(valid_timezones, timezone)
while True: while True:
Watcher.log_watch("location", "starting")
time.sleep(60) time.sleep(60)
location = params.get("LastGPSPosition", encoding='utf8') location = params.get("LastGPSPosition", encoding='utf8')