AS: creating final node
This commit is contained in:
53
test/osc_ws/address_scan_thread.py
Normal file
53
test/osc_ws/address_scan_thread.py
Normal file
@@ -0,0 +1,53 @@
|
||||
import threading
|
||||
import time
|
||||
from pythonosc.dispatcher import Dispatcher
|
||||
from pythonosc.osc_server import BlockingOSCUDPServer
|
||||
import sys
|
||||
|
||||
# Define the IP and Port
|
||||
IP_ADDRESS = "0.0.0.0" # Listen on all interfaces
|
||||
PORT = 8000 # Choose an available UDP port
|
||||
TIMEOUT = 0.7 # Auto-terminate after x seconds of inactivity
|
||||
|
||||
# Set to track seen addresses
|
||||
seen = set()
|
||||
last_received_time = time.time() # Tracks the last time a new address was received
|
||||
|
||||
disp = Dispatcher()
|
||||
|
||||
def default_handler(address: str, *args) -> None:
|
||||
"""Handles incoming OSC messages and tracks new addresses."""
|
||||
global last_received_time
|
||||
address = address.split('/')[1]
|
||||
if address not in seen:
|
||||
seen.add(address)
|
||||
print(f"/{address}")
|
||||
last_received_time = time.time() # Update last received time ONLY when a new address is found
|
||||
|
||||
def detailed_handler(address: str, *args) -> None:
|
||||
"""Handles incoming OSC messages and tracks new addresses."""
|
||||
global last_received_time
|
||||
if address not in seen:
|
||||
seen.add(address)
|
||||
print(f"{address}")
|
||||
last_received_time = time.time() # Update last received time ONLY when a new address is found
|
||||
|
||||
if len(sys.argv)>1: disp.set_default_handler(detailed_handler)
|
||||
else: disp.set_default_handler(default_handler)
|
||||
|
||||
server = BlockingOSCUDPServer((IP_ADDRESS, PORT), disp)
|
||||
|
||||
def stop_server_on_timeout():
|
||||
"""Stops the server if no new addresses are discovered within TIMEOUT seconds."""
|
||||
global last_received_time
|
||||
while True:
|
||||
if time.time() - last_received_time > TIMEOUT:
|
||||
server.shutdown() # Properly shutdown the server
|
||||
return # Exit the thread
|
||||
|
||||
# Run timeout check in a separate thread
|
||||
timeout_thread = threading.Thread(target=stop_server_on_timeout, daemon=True)
|
||||
timeout_thread.start()
|
||||
|
||||
# Start the server
|
||||
server.serve_forever() # Blocking call, runs until shutdown
|
||||
30
test/osc_ws/simple_client.py
Normal file
30
test/osc_ws/simple_client.py
Normal file
@@ -0,0 +1,30 @@
|
||||
"""Small example OSC client
|
||||
|
||||
This program sends 10 random values between 0.0 and 1.0 to the /filter address,
|
||||
waiting for 1 seconds between each value.
|
||||
"""
|
||||
import argparse
|
||||
import random
|
||||
|
||||
from pythonosc import udp_client
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--ip", default="127.0.0.1",
|
||||
help="The ip of the OSC server")
|
||||
parser.add_argument("--port", type=int, default=5005,
|
||||
help="The port the OSC server is listening on")
|
||||
args = parser.parse_args()
|
||||
|
||||
client = udp_client.SimpleUDPClient(args.ip, args.port)
|
||||
|
||||
|
||||
while True:
|
||||
client.send_message("/filter", random.random())
|
||||
client.send_message("/hello", random.random())
|
||||
client.send_message("/hi", random.random())
|
||||
client.send_message("/su", random.random())
|
||||
client.send_message("/vedf", random.random())
|
||||
client.send_message("/venv", random.random())
|
||||
client.send_message("/lel", random.random())
|
||||
27
test/osc_ws/simple_server.py
Normal file
27
test/osc_ws/simple_server.py
Normal file
@@ -0,0 +1,27 @@
|
||||
"""Small example OSC server
|
||||
|
||||
This program listens to several addresses, and prints some information about
|
||||
received packets.
|
||||
"""
|
||||
|
||||
from pythonosc.dispatcher import Dispatcher
|
||||
from pythonosc import osc_server
|
||||
|
||||
def handler(unused_addr, args, volume):
|
||||
|
||||
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--ip",
|
||||
default="127.0.0.1", help="The ip to listen on")
|
||||
parser.add_argument("--port",
|
||||
type=int, default=5005, help="The port to listen on")
|
||||
args = parser.parse_args()
|
||||
|
||||
dispatcher = Dispatcher()
|
||||
dispatcher.map("/joint_states", handler)
|
||||
|
||||
server = osc_server.ThreadingOSCUDPServer(("127.0.0.1", 8000), dispatcher)
|
||||
|
||||
print("Serving on {}".format(server.server_address))
|
||||
server.serve_forever()
|
||||
Reference in New Issue
Block a user