From a557cb4959ce6b6c17db5827bd762661ebeab6ca Mon Sep 17 00:00:00 2001 From: Evan Lombardo Date: Thu, 22 Jun 2023 01:10:45 -0400 Subject: [PATCH 1/6] Added and moved scripts --- client.py => scripts/client.py | 0 scripts/connect | 56 ++++++++++++++++++++++++++++++++++ scripts/disconnect | 21 +++++++++++++ server.py => scripts/server.py | 0 scripts/vars.py | 3 ++ 5 files changed, 80 insertions(+) rename client.py => scripts/client.py (100%) create mode 100755 scripts/connect create mode 100755 scripts/disconnect rename server.py => scripts/server.py (100%) create mode 100644 scripts/vars.py diff --git a/client.py b/scripts/client.py similarity index 100% rename from client.py rename to scripts/client.py diff --git a/scripts/connect b/scripts/connect new file mode 100755 index 00000000..f0fe49bf --- /dev/null +++ b/scripts/connect @@ -0,0 +1,56 @@ +#!/usr/bin/env python3 + +import os +from itertools import count +from vars import AP_NAME + +MAX_RETRIES = 100 + + +def main(): + print("Taking down other connections") + os.system("sudo ifdown -a --force -X \"{}\"".format(AP_NAME)) + + os.system("clear") + print("Connecting to AP...\n") + + for i in count(MAX_RETRIES): + + # Attempts to connect to AP + os.system("sudo iwconfig {} essid SilexAH".format(AP_NAME)) + + # 0 if no connection (grep result) 256 if found connection (no grep result) + result = os.system("iwconfig {} | grep \"Access Point: Not-Associated\"".format(AP_NAME)) + + if result == 256: + + print("Connected to AP.\n") + print("Attempting to get IP address...\n") + + # Runs dhclient to provision ip + os.system("sudo dhclient {}".format(AP_NAME)) + + # 0 if not connected (no result in terminal flipped) 256 if connected (result in terminal flipped) + result += abs(os.system("ip -4 address show dev {}".format(AP_NAME)) - 256) + + if result == 256 + 256: + break + else: + print("Failed, retrying.") + os.system("sudo dhclient -r") + os.system("sudo iwconfig {} essid off".format(AP_NAME)) + os.system("sudo ifdown {} --force".format(AP_NAME)) + os.system("sudo ifup {} --force".format(AP_NAME)) + os.system("clear") + print("Connecting to AP...\n") + + if i == (MAX_RETRIES - 1): + print("Failed to connect to AP and assign address.") + raise ConnectionError # throws error if exceeds retry limit + + print("IP address assigned.\n") + print("Success!") + + +if __name__ == "__main__": + main() diff --git a/scripts/disconnect b/scripts/disconnect new file mode 100755 index 00000000..752f22f7 --- /dev/null +++ b/scripts/disconnect @@ -0,0 +1,21 @@ +#!/usr/bin/env python3 + +import os +from vars import AP_NAME + + +def main(): + + print("Undoing mapped IP") + os.system("sudo dhclient -r") + + print("Removing connection to AP") + os.system("sudo iwconfig {} essid off".format(AP_NAME)) + os.system("sudo ifdown {} --force".format(AP_NAME)) + os.system("sudo ifconfig {} up".format(AP_NAME)) + + print("Complete!") + + +if __name__ == "__main__": + main() diff --git a/server.py b/scripts/server.py similarity index 100% rename from server.py rename to scripts/server.py diff --git a/scripts/vars.py b/scripts/vars.py new file mode 100644 index 00000000..4b705920 --- /dev/null +++ b/scripts/vars.py @@ -0,0 +1,3 @@ +#!/usr/bin/env python3 + +AP_NAME = "nrc1" From 2372c877b114ffe23187a7f07634273eece7cc11 Mon Sep 17 00:00:00 2001 From: Evan Lombardo Date: Thu, 22 Jun 2023 01:20:02 -0400 Subject: [PATCH 2/6] Removed unnecessary shebang --- scripts/vars.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/scripts/vars.py b/scripts/vars.py index 4b705920..679b5343 100644 --- a/scripts/vars.py +++ b/scripts/vars.py @@ -1,3 +1 @@ -#!/usr/bin/env python3 - AP_NAME = "nrc1" From a6c652dc72411331d7a3ef70d9fc4ac700c63952 Mon Sep 17 00:00:00 2001 From: Evan Lombardo Date: Thu, 22 Jun 2023 19:39:36 -0400 Subject: [PATCH 3/6] Renamed vars --- scripts/connect | 18 +++++++++--------- scripts/disconnect | 8 ++++---- scripts/vars.py | 2 +- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/scripts/connect b/scripts/connect index f0fe49bf..175e78d4 100755 --- a/scripts/connect +++ b/scripts/connect @@ -2,14 +2,14 @@ import os from itertools import count -from vars import AP_NAME +from vars import HALOW_INTERFACE MAX_RETRIES = 100 def main(): print("Taking down other connections") - os.system("sudo ifdown -a --force -X \"{}\"".format(AP_NAME)) + os.system("sudo ifdown -a --force -X \"{}\"".format(HALOW_INTERFACE)) os.system("clear") print("Connecting to AP...\n") @@ -17,10 +17,10 @@ def main(): for i in count(MAX_RETRIES): # Attempts to connect to AP - os.system("sudo iwconfig {} essid SilexAH".format(AP_NAME)) + os.system("sudo iwconfig {} essid SilexAH".format(HALOW_INTERFACE)) # 0 if no connection (grep result) 256 if found connection (no grep result) - result = os.system("iwconfig {} | grep \"Access Point: Not-Associated\"".format(AP_NAME)) + result = os.system("iwconfig {} | grep \"Access Point: Not-Associated\"".format(HALOW_INTERFACE)) if result == 256: @@ -28,19 +28,19 @@ def main(): print("Attempting to get IP address...\n") # Runs dhclient to provision ip - os.system("sudo dhclient {}".format(AP_NAME)) + os.system("sudo dhclient {}".format(HALOW_INTERFACE)) # 0 if not connected (no result in terminal flipped) 256 if connected (result in terminal flipped) - result += abs(os.system("ip -4 address show dev {}".format(AP_NAME)) - 256) + result += abs(os.system("ip -4 address show dev {}".format(HALOW_INTERFACE)) - 256) if result == 256 + 256: break else: print("Failed, retrying.") os.system("sudo dhclient -r") - os.system("sudo iwconfig {} essid off".format(AP_NAME)) - os.system("sudo ifdown {} --force".format(AP_NAME)) - os.system("sudo ifup {} --force".format(AP_NAME)) + os.system("sudo iwconfig {} essid off".format(HALOW_INTERFACE)) + os.system("sudo ifdown {} --force".format(HALOW_INTERFACE)) + os.system("sudo ifup {} --force".format(HALOW_INTERFACE)) os.system("clear") print("Connecting to AP...\n") diff --git a/scripts/disconnect b/scripts/disconnect index 752f22f7..50d0bd83 100755 --- a/scripts/disconnect +++ b/scripts/disconnect @@ -1,7 +1,7 @@ #!/usr/bin/env python3 import os -from vars import AP_NAME +from vars import HALOW_INTERFACE def main(): @@ -10,9 +10,9 @@ def main(): os.system("sudo dhclient -r") print("Removing connection to AP") - os.system("sudo iwconfig {} essid off".format(AP_NAME)) - os.system("sudo ifdown {} --force".format(AP_NAME)) - os.system("sudo ifconfig {} up".format(AP_NAME)) + os.system("sudo iwconfig {} essid off".format(HALOW_INTERFACE)) + os.system("sudo ifdown {} --force".format(HALOW_INTERFACE)) + os.system("sudo ifconfig {} up".format(HALOW_INTERFACE)) print("Complete!") diff --git a/scripts/vars.py b/scripts/vars.py index 679b5343..6f18f40b 100644 --- a/scripts/vars.py +++ b/scripts/vars.py @@ -1 +1 @@ -AP_NAME = "nrc1" +HALOW_INTERFACE = "nrc1" From 6a49ccd72d7a8a4bee13026ec63e1154316c072c Mon Sep 17 00:00:00 2001 From: Evan Lombardo Date: Thu, 22 Jun 2023 20:02:25 -0400 Subject: [PATCH 4/6] Fixed loop bug --- scripts/connect | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/connect b/scripts/connect index 175e78d4..202298e0 100755 --- a/scripts/connect +++ b/scripts/connect @@ -4,7 +4,7 @@ import os from itertools import count from vars import HALOW_INTERFACE -MAX_RETRIES = 100 +MAX_RETRIES = 150 def main(): @@ -14,7 +14,7 @@ def main(): os.system("clear") print("Connecting to AP...\n") - for i in count(MAX_RETRIES): + for i in count(): # Attempts to connect to AP os.system("sudo iwconfig {} essid SilexAH".format(HALOW_INTERFACE)) From 47aad12de04ca721725e1a3b9bb765deb6d15384 Mon Sep 17 00:00:00 2001 From: Evan Lombardo Date: Sat, 8 Jul 2023 00:38:26 -0400 Subject: [PATCH 5/6] Gotta repartition my drive --- scripts/client.py | 2 +- scripts/connect | 2 +- scripts/server.py | 25 ++++++++++++++----------- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/scripts/client.py b/scripts/client.py index bd2598cb..5d0bd8dd 100644 --- a/scripts/client.py +++ b/scripts/client.py @@ -2,7 +2,7 @@ import websockets from websockets.client import WebSocketClientProtocol -SERVER_IP = "192.168.0.147" +SERVER_IP = "192.168.0.4" SERVER_PORT = 8765 diff --git a/scripts/connect b/scripts/connect index 202298e0..cddcb469 100755 --- a/scripts/connect +++ b/scripts/connect @@ -31,7 +31,7 @@ def main(): os.system("sudo dhclient {}".format(HALOW_INTERFACE)) # 0 if not connected (no result in terminal flipped) 256 if connected (result in terminal flipped) - result += abs(os.system("ip -4 address show dev {}".format(HALOW_INTERFACE)) - 256) + result += abs(os.system("ip -4 address show dev {} | grep i".format(HALOW_INTERFACE)) - 256) if result == 256 + 256: break diff --git a/scripts/server.py b/scripts/server.py index 4f358c1d..fafb14a4 100644 --- a/scripts/server.py +++ b/scripts/server.py @@ -6,7 +6,7 @@ from typing import List -SERVER_IP = "192.168.0.147" +SERVER_IP = "192.168.0.4" SERVER_PORT = 8765 @@ -32,12 +32,12 @@ def update_message(self): self.data_count += 1 def get_string(self): - return f"T{self.time}{self.id:03x}{self.len}{self.get_data_string()}\r" + return "T{}{:03x}{}{}\r".format(self.time, self.id, self.len, self.get_data_string()) def get_data_string(self): out = "" for d in self.data: - out += f"{d:02x}" + out += "{:02x}".format(d) return out @@ -54,10 +54,11 @@ def __init__(self, socket: WebSocketServerProtocol): } async def run(self): + print(self.socket) async for message in self.socket: if message in self.callbacks: self.callbacks.get(message)() # call the function - + async def send(self, message): try: await self.socket.send(message) @@ -71,7 +72,7 @@ def stop_data(self): self.started = False -connections: List[Socket] = [] +connections = [] connection_lock = Lock() # to prevent concurrency issues between tasks m = TestMessage() @@ -100,13 +101,13 @@ async def monitor(): closed = filter(lambda con: con.socket.closed, connections) for c in closed: connections.remove(c) - print(f"Connections: {len(connections)}") + print("Connections: {}".format(len(connections))) for connection in connections: - print(f" {connection.socket.remote_address} - started: {connection.started}") + print(" {} - started: {}".format(connection.socket.remote_address, connection.started)) await asyncio.sleep(2) -async def handle(websocket: WebSocketServerProtocol): +async def handle(self, websocket: WebSocketServerProtocol): """ Handles incoming connections by adding them to the connection list """ @@ -118,10 +119,12 @@ async def handle(websocket: WebSocketServerProtocol): async def main(): async with websockets.serve(handle, SERVER_IP, SERVER_PORT): - asyncio.create_task(sender()) - asyncio.create_task(monitor()) + mloop = asyncio.get_event_loop() + mloop.create_task(sender()) + mloop.create_task(monitor()) await asyncio.Future() if __name__ == "__main__": - asyncio.run(main()) \ No newline at end of file + loop = asyncio.get_event_loop() + loop.run_until_complete(main()) \ No newline at end of file From 95e5c7795aac9ed55e5376b4e34b46767d8b7215 Mon Sep 17 00:00:00 2001 From: Evan Date: Wed, 13 Sep 2023 22:13:20 -0400 Subject: [PATCH 6/6] Removed Old Scripts, Just Using Siren --- scripts/client.py | 36 ------------- scripts/server.py | 130 ---------------------------------------------- 2 files changed, 166 deletions(-) delete mode 100644 scripts/client.py delete mode 100644 scripts/server.py diff --git a/scripts/client.py b/scripts/client.py deleted file mode 100644 index 5d0bd8dd..00000000 --- a/scripts/client.py +++ /dev/null @@ -1,36 +0,0 @@ -import asyncio -import websockets -from websockets.client import WebSocketClientProtocol - -SERVER_IP = "192.168.0.4" -SERVER_PORT = 8765 - - -async def receiver(websocket: WebSocketClientProtocol): - """ - Prints incoming data. - """ - while True: - data = await websocket.recv() - # Here is where the client can handle incoming data and send it somewhere - print(data) - - -async def controller(): - """ - Opens a websocket connection. - """ - async with websockets.connect(f"ws://{SERVER_IP}:{SERVER_PORT}", timeout=10, ping_interval=None) as websocket: - asyncio.create_task(receiver(websocket)) - while True: - # Here is where client code would exist. This would involve interacting with the - # control protocol on the server to perform certain actions. - # Currently just turning off and on data on a cycle - await websocket.send("start") - await asyncio.sleep(10) - await websocket.send("stop") - await asyncio.sleep(10) - - -if __name__ == "__main__": - asyncio.run(controller()) diff --git a/scripts/server.py b/scripts/server.py deleted file mode 100644 index fafb14a4..00000000 --- a/scripts/server.py +++ /dev/null @@ -1,130 +0,0 @@ -import asyncio -import websockets -from websockets.exceptions import ConnectionClosed -from websockets.server import WebSocketServerProtocol -from threading import Lock -from typing import List - - -SERVER_IP = "192.168.0.4" -SERVER_PORT = 8765 - - -class TestMessage(): - """ - Test message format used for testing the websocket connections with data similar to what is - coming from the car. - """ - START_TIME = 1654705626000 - - def __init__(self): - self.time = TestMessage.START_TIME - self.id = 514 - self.len = 8 - self.data = [0, 0, 24, 0, 0, 0, 0, 0] - self.data_count = 1 - self.time_count = 1 - - def update_message(self): - self.time += self.time_count - self.data[0] = self.data_count % 255 - self.time_count += 1 - self.data_count += 1 - - def get_string(self): - return "T{}{:03x}{}{}\r".format(self.time, self.id, self.len, self.get_data_string()) - - def get_data_string(self): - out = "" - for d in self.data: - out += "{:02x}".format(d) - return out - - -class Socket(): - """ - Represents an individual socket connection. - """ - def __init__(self, socket: WebSocketServerProtocol): - self.socket = socket - self.started = False # Whether data is being sent to this socket - self.callbacks = { - 'start': self.start_data, - 'stop': self.stop_data - } - - async def run(self): - print(self.socket) - async for message in self.socket: - if message in self.callbacks: - self.callbacks.get(message)() # call the function - - async def send(self, message): - try: - await self.socket.send(message) - except ConnectionClosed as cc: - pass - - def start_data(self): - self.started = True - - def stop_data(self): - self.started = False - - -connections = [] -connection_lock = Lock() # to prevent concurrency issues between tasks -m = TestMessage() - - -async def sender(): - """ - Asynchronous task to send data to connected clients every second. - """ - while True: - message = m.get_string() - with connection_lock: - for connection in connections: - if connection.started: - await connection.send(message) - m.update_message() - await asyncio.sleep(1) - - -async def monitor(): - """ - Asynchronous task to monitor the connections. Filters out closed connections every - few seconds, and prints status information. - """ - while True: - with connection_lock: - closed = filter(lambda con: con.socket.closed, connections) - for c in closed: - connections.remove(c) - print("Connections: {}".format(len(connections))) - for connection in connections: - print(" {} - started: {}".format(connection.socket.remote_address, connection.started)) - await asyncio.sleep(2) - - -async def handle(self, websocket: WebSocketServerProtocol): - """ - Handles incoming connections by adding them to the connection list - """ - socket = Socket(websocket) - with connection_lock: - connections.append(socket) - await socket.run() - - -async def main(): - async with websockets.serve(handle, SERVER_IP, SERVER_PORT): - mloop = asyncio.get_event_loop() - mloop.create_task(sender()) - mloop.create_task(monitor()) - await asyncio.Future() - - -if __name__ == "__main__": - loop = asyncio.get_event_loop() - loop.run_until_complete(main()) \ No newline at end of file