Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions remoteio/remoteio_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,142 @@

class RemoteServer:
def __init__(self, server_ip, server_port):
""" Initialize the client with the server IP and port.

Args:
server_ip (str): The IP address of the server.
server_port (int): The port number of the server.


Raises:
OSError: If the connection to the server fails.
"""

self.server_ip = server_ip
self.server_port = server_port
self.client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.client_socket.connect((self.server_ip, self.server_port))

def pin(self, pin_number, numbering='b'):
""" Return a RemotePin object for the specified pin number and numbering scheme.

This method returns a RemotePin object that represents the specified pin number and numbering scheme.

Args:
pin_number (int): The pin number to be accessed.
numbering (str): The numbering scheme for the pin. Default is 'b'.

Returns:
RemotePin: An object representing the specified pin number and numbering scheme.
"""

return RemotePin(self.client_socket, pin_number, numbering)

def close(self):
""" Closes the client socket connection.

This method closes the client socket connection.
"""

self.client_socket.close()

class RemotePin:

def __init__(self, client_socket, pin_number, numbering):
""" Initialize the object with client socket, pin number, and numbering.

Args:
client_socket (socket): The client socket object.
pin_number (int): The pin number for the object.
numbering (int): The numbering for the object.
"""

self.client_socket = client_socket
self.pin_number = pin_number
self.numbering = numbering
self.time_ms = 0

def __create_command(self, command:str, time_ms:int=0):
""" Create a command with an optional time delay.

This function creates a command string by combining the given command with an optional time delay in milliseconds.

Args:
command (str): The base command to be executed.
time_ms (int?): The time delay in milliseconds. Defaults to 0.

Returns:
str: The constructed command string.
"""

cmd = f"{command} {time_ms}"
return cmd

def __send_command(self, command):
""" Send a command to the client socket.

This method constructs a command using the instance's numbering and pin_number attributes,
and then sends the command to the client socket.

Args:
command (str): The command to be sent to the client socket.
"""

command = f"{self.numbering} {self.pin_number} {command}"
self.client_socket.sendall(command.encode())

def on(self, time_ms:int=0):
""" Turn on the device for a specified duration.

This method sends a command to turn on the device for a specified duration in milliseconds.

Args:
time_ms (int?): The duration in milliseconds for which the device should be turned on. Defaults to 0.

Returns:
self: The current instance of the device.
"""

cmd = self.__create_command("on", time_ms)
self.__send_command(cmd)
return self

def blink(self):
""" Execute the blink command.

This method creates a command to execute the blink action and sends the command to the device.

Returns:
self: The instance of the class.
"""

cmd = self.__create_command("blink")
self.__send_command(cmd)
return self

def pulse(self):
""" Send a pulse command to the device.

This method creates a pulse command using the private method __create_command,
sends the command using the private method __send_command, and returns the instance of the object.

Returns:
object: The instance of the object.
"""

cmd = self.__create_command("pulse")
self.__send_command(cmd)
return self

def off(self):
""" Turn off the device.

This method creates a command to turn off the device and sends the command to the device.

Returns:
self: The current instance of the device.
"""

cmd = self.__create_command("off")
self.__send_command(cmd)
return self
Expand Down
41 changes: 41 additions & 0 deletions remoteio/remoteio_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@
}

def handle_timer(led, time_ms):
""" Handle a timer for turning on and off an LED.

This function takes an LED object and a time in milliseconds, converts the time to seconds, turns on the LED, waits for the specified time, and then turns off the LED.

Args:
led: An object representing the LED.
time_ms (int): The time in milliseconds for which the LED should remain on.
"""

time_ms = float(time_ms) / 1000.0
led.on()
time.sleep(time_ms)
Expand All @@ -31,6 +40,19 @@ def handle_timer(led, time_ms):

# Create gpiozero LED objects based on numbering system
def create_led(numbering, pin_number):
""" Create gpiozero LED objects based on the numbering system.

Args:
numbering (str): The numbering system to be used. It can be 'b' for BOARD or 'g' for BCM.
pin_number (int): The pin number to be used.

Returns:
PWMLED: The gpiozero PWMLED object based on the specified numbering system and pin number.

Raises:
ValueError: If the input numbering system is not 'b' or 'g'.
"""

if numbering.lower() == 'b':
return PWMLED("BOARD" + str(pin_number))
elif numbering.lower() == 'g':
Expand All @@ -40,6 +62,20 @@ def create_led(numbering, pin_number):

# Handle client requests
def handle_client(conn):
""" Handle client requests.

This function handles client requests by receiving data from the connection, processing the data, and executing
corresponding actions based on the received commands. It also performs cleanup actions upon disconnection.

Args:
conn (socket): The connection object for communicating with the client.


Raises:
ValueError: If the data cannot be parsed into the required format.
Exception: If any other unexpected error occurs during data processing or action execution.
"""

try:
while True:
data = conn.recv(1024).decode().strip()
Expand Down Expand Up @@ -82,6 +118,11 @@ def handle_client(conn):


def run_server():
""" Run a server to handle incoming connections.

This function creates a server socket and listens for incoming connections. When a connection is established, it spawns a new thread to handle the client.
"""

server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(('0.0.0.0', PORT))
server_socket.listen(5)
Expand Down