firmware-base/docs/Component.md

140 lines
4.7 KiB
Markdown

---
title: Component Class
description: Base class for all hardware and software components in the system
keywords: [component, base class, modular architecture, ESP32]
---
## Component
**Path**: [src/Component.h](../src/Component.h)
**Revision History**: Initial documentation
The Component class serves as the foundational base class for all hardware and software components in the system. It provides a standardized interface for component initialization, runtime behavior, and communication with other system components. The class implements a modular architecture that allows components to be easily added, removed, or modified without affecting the rest of the system.
## REQUIREMENTS
- Arduino framework
- ESP32 platform
- WString.h for string handling
- ArduinoLog.h for logging functionality
- Vector.h for dynamic arrays
## FEATURES
- Component identification with unique IDs and names
- Standardized initialization and setup processes
- Runtime flags for controlling component behavior
- Owner-child component relationship hierarchy
- Integration with Modbus communication
- Network capability flags
## DEPENDENCIES
- [WString.h](https://www.arduino.cc/reference/en/language/variables/data-types/stringobject/)
- [ArduinoLog.h](https://github.com/thijse/Arduino-Log)
- [Vector.h](https://github.com/janelia-arduino/Vector)
- [enums.h](../src/enums.h)
- [constants.h](../src/constants.h)
- [error_codes.h](../src/error_codes.h)
- [macros.h](../src/macros.h)
- [xtypes.h](../src/xtypes.h)
```mermaid
graph TD
Component --> WString
Component --> ArduinoLog
Component --> Vector
Component --> enums
Component --> constants
Component --> error_codes
Component --> macros
Component --> xtypes
Component --> Bridge[Bridge class]
Component --> ModbusTCP[ModbusTCP class]
Component --> ModbusBlockView[ModbusBlockView class]
Component --> MB_Registers[MB_Registers class]
Component --> RS485[RS485 class]
```
## BEHAVIOUR
The Component class follows a lifecycle pattern that includes initialization, setup, and runtime phases. Components can be dynamically enabled or disabled through runtime flags.
```mermaid
stateDiagram-v2
[*] --> Constructed
Constructed --> Setup: setup()
Setup --> Running: flags & E_OF_SETUP
Running --> LoopExecution: flags & D_OF_LOOP
LoopExecution --> Running
Running --> Disabled: disable()
Disabled --> Running: enable()
Running --> [*]: destroy()
```
## TODOS
### PERFORMANCE
- Optimize component instantiation for memory-constrained environments
- Consider implementing lazy initialization for components with heavy setup requirements
- Review the component hierarchy for potential performance bottlenecks in deeply nested structures
### SECURITY
- Implement access control mechanisms for components with critical functionality
- Add validation for component parameters passed through Modbus or other interfaces
- Consider encryption for sensitive component data communication
### COMPLIANCE
- Ensure component implementation adheres to industrial communication standards
- Verify compatibility with Modbus protocol specifications
- Document compliance with relevant industrial control system standards
### RECOMMENDATIONS
- Use meaningful component names and IDs to facilitate debugging and maintenance
- Consider implementing a component registry for easier system-wide management
- Implement proper error handling in component constructors and methods
- Use the owner-child relationship to implement clean component lifecycle management
## EXAMPLE
This section illustrates how to create and configure a Component instance. Since the complete implementation in Component.cpp is not accessible, this example provides a general pattern for component creation based on the header file.
```cpp
// Create a basic component with default settings
Component* basicComponent = new Component("BasicComponent", 100, Component::COMPONENT_DEFAULT);
// Add the component to the components vector
if (basicComponent) {
components.push_back(basicComponent);
Log.infoln(F("Basic component initialized. ID:%d"), 100);
} else {
Log.errorln(F("Basic component initialization failed."));
}
// Example with an owner component
Component* parentComponent = new Component("ParentComponent", 200, Component::COMPONENT_DEFAULT);
Component* childComponent = new Component(
"ChildComponent", // name
201, // id
Component::COMPONENT_DEFAULT, // flags
parentComponent // owner
);
if (childComponent) {
components.push_back(childComponent);
Log.infoln(F("Child component initialized. Name:%s, ID:%d"),
"ChildComponent", 201);
} else {
Log.errorln(F("Child component initialization failed."));
}
```
### References
- None available