From 5417ff98d641139ccf02875d7ec098fa1c636ea5 Mon Sep 17 00:00:00 2001 From: Rahul Date: Sat, 6 Apr 2024 13:23:45 -0700 Subject: [PATCH] Cherry picked some files to delete --- .../bridge => demos/async}/acync_demos.py | 0 .../sensor_array/client_bridge_lidar.py | 16 --- .../sensor_array/client_lidar.py | 93 ---------------- unified_frameworks/sensor_array/lidar.py | 3 - .../sensor_array/server_lidar.py | 102 ------------------ .../sensor_array/websocket_client.py | 14 --- .../sensor_array/websocket_server.py | 18 ---- 7 files changed, 246 deletions(-) rename {unified_frameworks/bridge => demos/async}/acync_demos.py (100%) delete mode 100644 unified_frameworks/sensor_array/client_bridge_lidar.py delete mode 100644 unified_frameworks/sensor_array/client_lidar.py delete mode 100755 unified_frameworks/sensor_array/server_lidar.py delete mode 100644 unified_frameworks/sensor_array/websocket_client.py delete mode 100644 unified_frameworks/sensor_array/websocket_server.py diff --git a/unified_frameworks/bridge/acync_demos.py b/demos/async/acync_demos.py similarity index 100% rename from unified_frameworks/bridge/acync_demos.py rename to demos/async/acync_demos.py diff --git a/unified_frameworks/sensor_array/client_bridge_lidar.py b/unified_frameworks/sensor_array/client_bridge_lidar.py deleted file mode 100644 index 1e2a6e4..0000000 --- a/unified_frameworks/sensor_array/client_bridge_lidar.py +++ /dev/null @@ -1,16 +0,0 @@ -import sys -import re -root = (next(re.finditer(".*unified_frameworks", __file__)).group()) -sys.path.append(root) if root not in sys.path else None -from sensor_array.LidarClass import Lidar -from bridge import client_side -# import asyncio -# import websockets -from threading import Thread -import json -import time - -class WirelessBridgeLidar(list): - def __init__(self): - self.path = "/lidar" - \ No newline at end of file diff --git a/unified_frameworks/sensor_array/client_lidar.py b/unified_frameworks/sensor_array/client_lidar.py deleted file mode 100644 index cc1dfcb..0000000 --- a/unified_frameworks/sensor_array/client_lidar.py +++ /dev/null @@ -1,93 +0,0 @@ -import sys -import re - -root = next(re.finditer(".*unified_frameworks", __file__)).group() -sys.path.append(root) if root not in sys.path else None -from sensor_array.LidarClass import Lidar -import asyncio -import websockets -from threading import Thread -import json -import time - -wifi_address = { - "robotics_wifi": "ws://192.168.1.130:8765", - "tp_link": "ws://192.168.0.105:8765", -} - - -class WirelessLidar(Lidar): - def __init__(self, uri=wifi_address["robotics_wifi"]): - self.uri = uri - self.data = [] - - def connect(self, max_attempts=3, wait_seconds=1, verbose_attempts=False) -> bool: - self.stay_connected = True - self.connected = False - self.verbose_attempts = verbose_attempts - self.thread = Thread(target=self.start_connection) - self.thread.start() - for i in range(max_attempts): - if self.connected: - return True - time.sleep(1) - self.disconnect() - return False - - async def receive_data(self): - while self.stay_connected: - try: - print(f"Trying to connect to websocket at uri `{self.uri}`") - async with websockets.connect(self.uri) as websocket: - self.connected = True - while self.stay_connected: - data = await asyncio.wait_for(websocket.recv(), timeout=1) - # print(data) - self.data = json.loads(data) - self.connected = False - except asyncio.TimeoutError: - self.connected = False - print("Lidar update timed out") - time.sleep(1) - print("Trying again") - except websockets.exceptions.ConnectionClosedOK: - self.connected = False - print("Connection closed") - time.sleep(1) - print("Trying again") - - def start_connection(self): - try: - loop = asyncio.get_running_loop() - except: - loop = asyncio.new_event_loop() - loop.run_until_complete(self.receive_data()) - - def get_measures(self): - return self.data - - def disconnect(self): - self.stay_connected = False - for _ in range(10): - if self.connected: - print("still connected waiting to disconnect") - time.sleep(1) - - # def iter_scans(self): - # return iter(self) - # def __iter__(self): - # return self - # def __next__(self): - # time.sleep(1/4) - # return self.data - # def stop(self): - # return super().stop() - # def stop_motor(self): - # return super().stop_motor() - # def disconnect(self): - # self.connected = False - # self.thread.join() - - -if __name__ == "__main__": - WirelessLidar().test_Lidar(3) diff --git a/unified_frameworks/sensor_array/lidar.py b/unified_frameworks/sensor_array/lidar.py index 83c1907..df76e09 100644 --- a/unified_frameworks/sensor_array/lidar.py +++ b/unified_frameworks/sensor_array/lidar.py @@ -3,7 +3,6 @@ root = (next(re.finditer(".*unified_frameworks", __file__)).group()) sys.path.append(root) if root not in sys.path else None from sensor_array.actual_lidar import ActualLidar -from sensor_array.client_lidar import WirelessLidar from sensor_array.bridge_lidar import BridgeLidar import importlib from sensor_array.fake_lidar import FakeLidar @@ -16,8 +15,6 @@ import serial.tools.list_ports import serial -import sensor_array.client_lidar -importlib.reload(sensor_array.client_lidar) config = { "lidar_preference": [ActualLidar, BridgeLidar, FakeLidar], diff --git a/unified_frameworks/sensor_array/server_lidar.py b/unified_frameworks/sensor_array/server_lidar.py deleted file mode 100755 index ff5d639..0000000 --- a/unified_frameworks/sensor_array/server_lidar.py +++ /dev/null @@ -1,102 +0,0 @@ -import websockets -import asyncio -import rplidar -from actual_lidar import * -from fake_lidar import * -import json -import sys -import serial.tools.list_ports -import serial - - -def getDevicePort(): - ports = serial.tools.list_ports.comports() - - for port in ports: - if "USB" in port.device: - return port.device - - -port = getDevicePort() - - -# @note Quick encoder from numpy's ndarray to json -# @note since json cannot serialize ndarrays -class NumpyEncoder(json.JSONEncoder): - - def default(self, obj): - if isinstance(obj, np.ndarray): - return obj.tolist() - - -# lidar = RPLidar(port) -lidar = None - -if port is None: - print("[SERVER] Device port not found!") - print("[SERVER] Using Fake Lidar") - lidar = FakeLidar() -else: - print(f"[SERVER] Device port {port} found!") - lidar = ActualLidar() - -clients = [] # @note List of clients connected in the server -buffer = None - - -# Creating WebSocket server -# @note Server listens for ConnectionClosedOK -# @note Sends data from the server to the client via websockets -# @note checks if the lidar we are sending data from is either FakeLidar or ActualLidar -# @note If current lidar is FakeLidar then we utilize NumpyEncoder. -async def sendServerDataToClient(websocket): - clients.append(websocket) - print("[SERVER] Client has connected to the server") - # await websocket.send(json.dumps("[SERVER] You have connected to the server!")) - # await asyncio.sleep(0.01) - isConnectedStill = True - - try: - while isConnectedStill: - buffer = None - - # @note Checking lidars instance to see which data the buffer should contain - # @note Buffer will send the raw data from the lidar to the client - if isinstance(lidar, FakeLidar): - buffer = json.dumps(lidar.get_measures(), cls=NumpyEncoder) - else: - buffer = json.dumps(lidar.get_measures()) - - if len(clients) > 0: - await websocket.send(buffer) - await asyncio.sleep(0.2) - - except websockets.exceptions.ConnectionClosedOK: - print("[SERVER] Client disconnected from server!") - # await websocket.send(json.dumps("[SERVER] You have disconnected from the server")) - # await asyncio.sleep(0.01) - clients.remove(websocket) - except websockets.ConnectionClosedError: - print("[SERVER] Internal Server Error.") - # await websocket.send(json.dumps("[SERVER] Internal Server error has occurred!")) - # await asyncio.sleep(0.01) - except rplidar.RPLidarException: - print("[SERVER] RPLidarException has been caught!") - # await websocket.send(json.dumps("[SERVER] RPLidarException has occurred!")) - # await asyncio.sleep(0.01) - lidar.disconnect() - - -async def startServer(): - async with websockets.serve(sendServerDataToClient, "0.0.0.0", 8765): - await asyncio.Future() # run forever - - -if __name__ == "__main__": - try: - print("[SERVER] Server ON") - lidar.connect() - asyncio.run(startServer()) - except KeyboardInterrupt: - print("[SERVER] Keyboard Interrupt occurred!") - lidar.disconnect() diff --git a/unified_frameworks/sensor_array/websocket_client.py b/unified_frameworks/sensor_array/websocket_client.py deleted file mode 100644 index 246f72a..0000000 --- a/unified_frameworks/sensor_array/websocket_client.py +++ /dev/null @@ -1,14 +0,0 @@ -import asyncio -import websockets - -async def receive_data(): - uri = "ws://localhost:8765" - async with websockets.connect(uri) as websocket: - count = 0 - while count < 10: - data = await websocket.recv() - print(f"Received: {data}") - count+=1 - -if __name__=='__main__': - asyncio.get_event_loop().run_until_complete(receive_data()) diff --git a/unified_frameworks/sensor_array/websocket_server.py b/unified_frameworks/sensor_array/websocket_server.py deleted file mode 100644 index 6e9f74d..0000000 --- a/unified_frameworks/sensor_array/websocket_server.py +++ /dev/null @@ -1,18 +0,0 @@ -import asyncio -import websockets - - -async def data_stream(websocket, path): - count = 0 - while True: - # Replace this with your actual data source or generation logic - count += 1 - data = str(count) - await websocket.send(data) - await asyncio.sleep(1) # Adjust the delay as needed - -if __name__=='__main__': - start_server = websockets.serve(data_stream, "localhost", 8765) - - asyncio.get_event_loop().run_until_complete(start_server) - asyncio.get_event_loop().run_forever() \ No newline at end of file