Relies more on context class that stores the state
Less bloated functions and instead relying on smaller decorator functions that take in the context class as a parameter
As a result of fewer special function variables for each module, less need to set __all__ variables to protect against misusage
New on_exit decorator function for context manager library classes like SMBus2 and other I2C related components
Latch
Although this is also implemented simultaneously in C++, as a proof of concept in 100% Python has been implemented
Proof that other context can affect other context stores
A proper separation of responsibility(excludes features for motors)
Also demonstrates easier implementation of threading with async functions
Motor
Similarly to latch, the Motor class has a sibling implementation in C++ and has been made as a proof of concept in 100% Python
Proof that virtualOutputDevices can be used instead of outputDevice allowing for more flexible configuration.
CLI
Although the usage of CLI is minuscule the biggest benefit we can get out of the CLI is its ability to reliably execute scripts and commands every time on any device.
Samples commands are a good use of this benefit as I have realized the command for running Python script was a little complicated due to having to point at a Python path in the same command.
catbot sample <sample script file name> will execute PYTHONPATH=<path to raspi src file> python3 <path to samples directory>/<sample script file name>.py
Pythonpath environmental variable needs to be set because if not, other files outside the samples folder can not be found by the Python interpreter and produce import errors. If you run Python script files without pointing at the correct PYTHONPATH, I can not guarantee a functional script in the samples folder thus this CLI command was added. An alternative solution is to set the environment variable on the local computer. However, this could cause security issues with other projects having access to our project files and therefore is less recommended.
How to Test
For non Raspberry PI machine(fake device classes will be used):
npm install
catbot sample motor_left_right_loop
For Raspberry PI machine(check pinconfig.json for correct pin for the latch pin and address as well as motor pins)
npm install
catbot sample motor_left_right_loop
This will cause the motor to spin in both directions at a 3-second interval demonstrating the latch functionality and motor functionality with async functions.
Details of changes
New syntax for each component file
create_context: returns context class that is used for other state management functions
create_generic_context: same function as create_context (for backwards compatibility)
create_masked_context: returns a context class that is made from generic context.
device_parser: adds parser method to the context class used during config parsing
device_action: wraps action methods to swap identifiers to device instances into the parameter of the action methods
device_attr: wraps the class constructor to check the instance initiated in the subcomponent context or create one if the attribute of the subcomponent is provided.
subcomponent_action.ctx: a way to reference context made outside of the module
The benefits
Better docstring since there is no function as a variable
No need to destructure the result of a single function result and instead import all of the necessary functions from the state management module
More readable code as the code definition shortcut will take you to the implementation location of the state management function with one hit
Easier to cause an effect on other modules as we just access it with the .ctx from the module that we want to have an effect (see latch module for more complex implementation for this)
Easier to read since there is no function as a variable (function color is used instead of variable color on the text)
No more separate function for generic device and regular devices.
Limitations
This is a problem that has persisted from the last PR, but self-referencing with identifiers within a class is currently unavailable. This is nice to have and not necessary at the moment, but it could be a nice feature as this is one of the most impressive features for class relations. However, this will need a completely new syntax for declaring context therefore it will take a longer time to implement.
Redundancy in declaration of identifier attributes for the class.
Feature
catbot sample <sample script file name>
will executePYTHONPATH=<path to raspi src file> python3 <path to samples directory>/<sample script file name>.py
How to Test
npm install
catbot sample motor_left_right_loop
npm install
catbot sample motor_left_right_loop
This will cause the motor to spin in both directions at a 3-second interval demonstrating the latch functionality and motor functionality with async functions.
Details of changes
The benefits
.ctx
from the module that we want to have an effect (see latch module for more complex implementation for this)Limitations