fireworkModules.py
The fireworkModules.py
file defines a system for managing a complex pyrotechnic network through object-oriented design principles. It encapsulates related data and behaviors into three distinct classes: Ignition
, Host
, and Module
.
- The
Ignition
class models the functionality of an individual firing mechanism. - The
Host
class manages a collection of ignition points, organizing them and providing network-related information. - The
Module
class groups multiple hosts, thereby facilitating control of firework displays across multiple host machines.
These classes interconnect to form a hierarchical system, enabling efficient and clear control of a large-scale firework display. By providing intuitive interfaces and encapsulating complexity, they make the management of large pyrotechnic displays more straightforward and less error-prone.
Ignition
class Ignition:
def __init__(self, endpoint, hasBeenFired=False):
self.endpoint = endpoint
self.hasBeenFired = hasBeenFired
async def fire(self):
headers = {'x-api-key': '4ae1a6f9-a088-4371-bfd6-b2ccf641fc87'}
async with aiohttp.ClientSession() as session:
try:
await session.post(self.endpoint, headers=headers)
self.hasBeenFired = True
except aiohttp.ClientError:
pass
def to_dict(self):
return {
'endpoint': self.endpoint,
'hasBeenFired': self.hasBeenFired
}
@classmethod
def from_dict(cls, data):
return cls(data['endpoint'], data['hasBeenFired'])
Parameter | Type | Description |
---|---|---|
endpoint | String | The API endpoint to which the request is sent. |
hasBeenFired | Boolean | Whether the ignition has been fired or not. |
The Ignition
class is used to represent a single ignition point for a firework. Each instance of the Ignition
class has an endpoint
parameter which represents the API endpoint that is called when the ignition is fired, and a hasBeenFired
parameter which is a boolean indicating whether or not the ignition has been fired. The fire
method is used to fire the ignition and the to_dict
and from_dict
methods are used to convert the Ignition
instance to a dictionary and vice versa for easier handling.
Host
class Host:
def __init__(self, ipAddress, modules, moduleId, moduleName, ignitions):
self.ipAddress = ipAddress
self.modules = modules
self.moduleId = moduleId
self.moduleName = moduleName
self.ignitions = [Ignition.from_dict(ignition) if isinstance(ignition, dict) else ignition for ignition in ignitions]
def fire_ignition(self, ignition_index):
self.ignitions[ignition_index].fire()
def display(self):
ignitions_str = " ".join([f"[{chr(0x25CF) if ignition.hasBeenFired else ignition.endpoint.split('/')[-1]}]" for ignition in self.ignitions])
return f"{self.ipAddress} - {self.moduleName} {self.moduleId}/{self.modules} - Ignitions: {ignitions_str}"
def to_dict(self):
return {
'modules': self.modules,
'moduleId': self.moduleId,
'moduleName': self.moduleName,
'ignitions': [ignition.to_dict() for ignition in self.ignitions]
}
@classmethod
def from_dict(cls, data):
return cls(data['modules'], data['moduleId'], data['moduleName'], data['ignitions'])
Parameter | Type | Description |
---|---|---|
ipAddress | String | The IP address of the host. |
modules | Integer | The total number of modules present on the host. |
moduleId | Integer | The unique identifier for the module. |
moduleName | String | The name of the module. |
ignitions | List of Ignition instances | The list of ignitions that are available to be fired. |
The Host
class represents a single host machine that contains multiple firework modules. It has the following parameters: ipAddress
which represents the IP address of the host machine, modules
which is the total number of modules present on the host, moduleId
which is a unique identifier for each module, moduleName
which is the name of the module, and ignitions
which is a list of Ignition
instances representing the ignitions available to be fired on that host. The fire_ignition
method is used to fire a specific ignition, the display
method is used to display the current state of the host, and the to_dict
and from_dict
methods are used to convert the Host
instance to a dictionary and vice versa for easier handling.
Module
class Module:
def __init__(self, moduleName, hosts=None):
self.moduleName = moduleName
self.hosts = hosts or []
def add_host(self, host):
self.hosts.append(Host.from_dict(host) if isinstance(host, dict) else host)
def display(self):
return "\n".join([str(host.display()) for host in self.hosts])
def to_dict(self):
return {
'hosts': [host.to_dict() for host in self.hosts]
}
@classmethod
def from_dict(cls, data):
return cls([Host.from_dict(host) for host in data['hosts']])
Parameter | Type | Description |
---|---|---|
moduleName | String | The name of the module. |
hosts | List of Hosts | A list of host instances associated with the module. |
The Module
class represents a firework module which can be hosted on multiple machines. Each Module
instance has a moduleName
parameter which represents the name of the module, and a hosts
parameter which is a list of Host
instances representing the hosts on which the module is present. The add_host
method is used to add a new host to the module, the display
method is used to display the current state of the module, and the to_dict
and from_dict
methods are used to convert the Module
instance to a dictionary and vice versa for easier handling.