From ad93fb5bdb5ff80210d6cbf9cf50f714bc608ca2 Mon Sep 17 00:00:00 2001 From: James Date: Fri, 26 Apr 2024 11:53:15 +0100 Subject: [PATCH] cmd --- bin/2graphite | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/bin/2graphite b/bin/2graphite index ef924bb..132cc38 100644 --- a/bin/2graphite +++ b/bin/2graphite @@ -1,4 +1,3 @@ -#/bin/python import socket import sys import time @@ -37,14 +36,18 @@ def send_to_graphite(data, host='10.6.0.1', port=2003): while data: message = data.popleft() sock.sendall(message.encode('utf-8')) - #print(f"Sent to Graphite: {message.strip()}") except (socket.error, socket.timeout) as e: print(f"Failed to send data, will drop if buffer is full. Error: {e}", file=sys.stderr) def main(): - ser = serial.Serial('/dev/ttyUSB0', 115200, timeout=1) + if len(sys.argv) < 2: + print("Usage: python script.py ") + sys.exit(1) + + serial_device = sys.argv[1] + ser = serial.Serial(serial_device, 115200, timeout=1) data_buffer = deque(maxlen=100) # Buffer up to 100 messages - print("Reading data from /dev/ttyUSB0, press CTRL+C to quit:") + print(f"Reading data from {serial_device}, press CTRL+C to quit:") try: while True: @@ -56,13 +59,9 @@ def main(): for data in graphite_data: if len(data_buffer) < data_buffer.maxlen: data_buffer.append(data + '\n') - #print(f"Added to buffer: {data.strip()}") - else: - print("Buffer full, dropping data", file=sys.stderr) # Try to send data if the buffer is not empty if data_buffer: - #print("Attempting to send data to Graphite...") send_to_graphite(data_buffer) except KeyboardInterrupt: