Skip to content

Commit

Permalink
Merge pull request #20 from AlbertaSat/abhishek/feature/dict_of_execu…
Browse files Browse the repository at this point in the history
…table_methods

executable commands are now in a dictionary even for the IRIS subsystem
  • Loading branch information
abhisheknaik96 authored Aug 31, 2023
2 parents 74575ed + d01fe7b commit 4fccaa3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 44 deletions.
21 changes: 1 addition & 20 deletions EPS/eps_subsystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,33 +91,14 @@ def set_state_dict(self, new_state_dict):
" \n Reset State to default values.")


class EPSCommandFactory(CommandFactory): # pylint: disable=too-few-public-methods
"""Exteneds parent factory class with update, request, and execute commands."""

def command_execute(self, params=None):
"""Calls an associated function for the provided execute parameter if it exists.
Args:
params (list): A list of parameters to be used by the command
Returns:
str: A string containing the response to the command
"""
print("Execute command received: " + params[0])
if params and len(params) == 1 and params[0] in self.subsystem.executable_commands:
self.subsystem.executable_commands[params[0]]()
return f"Command {params[0]} executed \n"
return "ERROR: Invalid execute command \n"


if __name__ == "__main__":
# If there is no arg, port is default otherwise use the arg
PORT = sys.argv[1] if len(sys.argv) > 1 else DEFAULT_PORT

print(f"Starting EPS subsystem on port {PORT}")

# Concrete factory instance relies on subsystem class that contains associated state and fxns
command_factory = EPSCommandFactory(EPSSubsystem())
command_factory = CommandFactory(EPSSubsystem())

command_handler = CommandHandler(command_factory)

Expand Down
38 changes: 14 additions & 24 deletions IRIS/iris_subsystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"""

import sys
sys.path.append("../")
sys.path.append("../ex3_simulated_subsystems")
from socket_stuff import create_socket_and_listen # pylint: disable=C0413
from command_handler import CommandHandler # pylint: disable=C0413
from command_factory import CommandFactory # pylint: disable=C0413
Expand All @@ -35,11 +35,10 @@
DEFAULT_PORT = 1821


class IRISSubsystem: # pylint: disable=too-few-public-methods disable=too-many-instance-attributes
class IRISSubsystem: # pylint: disable=too-many-instance-attributes
"""Holds the state of the IRIS subsystem.
Tuples are provided that define the executable commands and updatable parameters.
"""
def __init__(self):
self.power_status = 1 # 1 means powered on, 0 means off
Expand All @@ -62,36 +61,27 @@ def __init__(self):
'SoftwareVersion': self.software_version,
}
self.updatable_parameters = ['PowerStatus', 'SensorStatus']
self.executable_commands = ['TakeImage', 'SetTime']


class IRISCommandFactory(CommandFactory): # pylint: disable=too-few-public-methods
"""Custom command factory for IRIS-specific commands."""

def command_execute(self, params=None):
"""Executes a command generated by the command factory and returns a response.
self.executable_commands = {
'TakeImage': self.take_image,
'SetTime': self.set_time
}

Args:
params (list): A list of parameters to be used by the command
def take_image(self):
"""Simulates taking a picture using the IRIS camera."""
self.state['NumImages'] += 1
print('Increased NumImages by 1')

Returns:
str: A string containing the response to the command
"""
print("Execute command received: " + params[0])
if params and len(params) == 1 and params[0] in self.subsystem.executable_commands:
if params[0] == 'TakeImage':
self.subsystem.state['NumImages'] += 1
print('Increased NumImages by 1')
return f"Command {params[0]} executed \n"
return "ERROR: Invalid execute command \n"
def set_time(self):
"""Simulates setting the time for the IRIS subsystem."""
print('Not implemented yet')


if __name__ == "__main__":
# If there is no arg, port is default otherwise use the arg
PORT = sys.argv[1] if len(sys.argv) > 1 else DEFAULT_PORT
print(f"Starting IRIS subsystem on port {PORT}")

command_factory = IRISCommandFactory(IRISSubsystem())
command_factory = CommandFactory(IRISSubsystem())
handler = CommandHandler(command_factory)

create_socket_and_listen(host=DEFAULT_HOST, port=PORT,
Expand Down
1 change: 1 addition & 0 deletions command_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def command_execute(self, params=None):
"""
print("Execute command received: " + params[0])
if params and len(params) == 1 and params[0] in self.subsystem.executable_commands:
self.subsystem.executable_commands[params[0]]()
return f"Command {params[0]} executed \n"
return "ERROR: Invalid execute command \n"

Expand Down

0 comments on commit 4fccaa3

Please sign in to comment.