kbot experiments

This commit is contained in:
lovebird 2025-06-04 08:19:09 +02:00
parent 29d3848456
commit 533dbf3afa
12 changed files with 568 additions and 44252 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

409
docs/SerialMessage.md Normal file
View File

@ -0,0 +1,409 @@
---
title: "SerialMessage Component"
description: "Serial communication component for handling command message parsing and processing"
keywords: ["serial", "communication", "command", "message", "parsing", "ESP32", "Modbus", "industrial"]
---
## SerialMessage
**Path**: [`src/SerialMessage.cpp`](../src/SerialMessage.cpp)
**Revision History**: Initial documentation
A serial communication component that handles reading, parsing, and processing of command messages from a serial stream. This component provides buffered serial communication with configurable message parsing intervals for industrial applications.
## REQUIREMENTS
### Hardware
- Serial communication interface (UART)
- No specific pin requirements (uses provided Stream object)
### Software Dependencies
- ArduinoLog library for logging
- Vector library for data structures
- Arduino Stream interface
- Component base class
### Configuration
- `SERIAL_RX_BUFFER_SIZE`: Receive buffer size (default: 256 bytes)
- `SERIAL_COMMAND_PARSE_INTERVAL`: Message parsing interval (default: 50ms)
## FEATURES
- Buffered serial message reading
- Command message parsing and validation
- Configurable message delimiters
- Serial connection status tracking
- Debug message handling with hex output
- Non-blocking message processing
- Integration with Component architecture
- Owner notification system for parsed messages
## DEPENDENCIES
- [`Component`](./Component.md) - Base component class
- [`CommandMessage`](./CommandMessage.md) - Message structure and parsing
- **ArduinoLog** - Logging functionality
- **Vector** - Data structure library
- **Arduino Stream** - Serial communication interface
```mermaid
graph TD
SerialMessage --> Component
SerialMessage --> CommandMessage
SerialMessage --> ArduinoLog
SerialMessage --> Stream
Component --> Owner[Owner Component]
CommandMessage --> MessageParsing[Message Parsing Logic]
```
## BEHAVIOUR
```mermaid
stateDiagram-v2
[*] --> Setup
Setup --> Idle
Idle --> CheckInterval: loop()
CheckInterval --> ReadMessage: interval elapsed
CheckInterval --> Idle: interval not elapsed
ReadMessage --> ParseMessage: message available
ReadMessage --> Idle: no message
ParseMessage --> ValidateMessage: parsing successful
ParseMessage --> Idle: parsing failed
ValidateMessage --> NotifyOwner: message valid
ValidateMessage --> Idle: message invalid
NotifyOwner --> Idle: owner notified
Idle --> [*]: component destroyed
```
## TODOS
### PERFORMANCE
- Consider implementing circular buffer for improved memory efficiency
- Optimize string operations to reduce memory fragmentation
- Implement message queuing for high-frequency communication
- Add configurable buffer overflow handling
### SECURITY
- Implement message authentication for command validation
- Add input sanitization for malformed messages
- Consider implementing rate limiting for message processing
- Add checksum validation for message integrity
### COMPLIANCE
- Ensure compliance with Modbus serial communication standards
- Implement proper error handling according to industrial communication protocols
- Add support for standard industrial message formats
### RECOMMENDATIONS
- Use hardware serial interfaces for better reliability
- Configure appropriate baud rates for industrial environments
- Implement proper error recovery mechanisms
- Consider using DMA for high-speed serial communication
- Add message statistics and monitoring capabilities
## EXAMPLE
This section illustrates how the SerialMessage component is constructed and mounted:
```cpp
#ifdef ENABLE_SERIAL_COMMANDS
serialMessage = new SerialMessage(
Serial, // stream (Hardware Serial)
this // owner component
);
if (serialMessage)
{
components.push_back(serialMessage);
Log.infoln(F("SerialMessage initialized. Stream: Hardware Serial"));
}
else
{
Log.errorln(F("SerialMessage initialization failed."));
}
#endif
// Alternative with Serial1 for RS485 communication
#ifdef ENABLE_RS485_SERIAL
rs485SerialMessage = new SerialMessage(
Serial1, // stream (Hardware Serial1)
this // owner component
);
if (rs485SerialMessage)
{
components.push_back(rs485SerialMessage);
Log.infoln(F("RS485 SerialMessage initialized. Stream: Hardware Serial1"));
}
else
{
Log.errorln(F("RS485 SerialMessage initialization failed."));
}
#endif
```
### Message Handling Example
```cpp
// Owner component implements onMessage to handle parsed commands
void MyComponent::onMessage(uint16_t id, uint8_t verb, uint8_t flags,
const String& payload, Component* sender)
{
if (sender == serialMessage)
{
Log.infoln(F("Received serial command: ID=%d, Verb=%d, Payload=%s"),
id, verb, payload.c_str());
// Process the command based on ID and verb
switch (id)
{
case COMMAND_SET_CONFIG:
handleConfigCommand(verb, flags, payload);
break;
case COMMAND_GET_STATUS:
handleStatusRequest(verb, flags, payload);
break;
default:
Log.warningln(F("Unknown command ID: %d"), id);
break;
}
}
}
```
### References
${DOXYGEN_PLACEHOLDER}
${VENDOR_PLACEHOLDER}
</parameter>
</invoke>
<invoke name="str_replace_editor">
<parameter name="command">str_replace</parameter>
<parameter name="path">./docs/SerialMessage.md</parameter>
<parameter name="old_str">Now I'll create the documentation file for the SerialMessage component.
<invoke name="str_replace_editor">
<parameter name="command">create</parameter>
<parameter name="path">./docs/SerialMessage.md</parameter>
<parameter name="file_text">---
title: "SerialMessage Component"
description: "Serial communication component for handling command message parsing and processing"
keywords: ["serial", "communication", "command", "message", "parsing", "ESP32", "Modbus", "industrial"]
---
## SerialMessage
**Path**: [`src/SerialMessage.cpp`](../src/SerialMessage.cpp)
**Revision History**: Initial documentation
A serial communication component that handles reading, parsing, and processing of command messages from a serial stream. This component provides buffered serial communication with configurable message parsing intervals for industrial applications.
## REQUIREMENTS
### Hardware
- Serial communication interface (UART)
- No specific pin requirements (uses provided Stream object)
### Software Dependencies
- ArduinoLog library for logging
- Vector library for data structures
- Arduino Stream interface
- Component base class
### Configuration
- `SERIAL_RX_BUFFER_SIZE`: Receive buffer size (default: 256 bytes)
- `SERIAL_COMMAND_PARSE_INTERVAL`: Message parsing interval (default: 50ms)
## FEATURES
- Buffered serial message reading
- Command message parsing and validation
- Configurable message delimiters
- Serial connection status tracking
- Debug message handling with hex output
- Non-blocking message processing
- Integration with Component architecture
- Owner notification system for parsed messages
## DEPENDENCIES
- [`Component`](./Component.md) - Base component class
- [`CommandMessage`](./CommandMessage.md) - Message structure and parsing
- **ArduinoLog** - Logging functionality
- **Vector** - Data structure library
- **Arduino Stream** - Serial communication interface
```mermaid
graph TD
SerialMessage --> Component
SerialMessage --> CommandMessage
SerialMessage --> ArduinoLog
SerialMessage --> Stream
Component --> Owner[Owner Component]
CommandMessage --> MessageParsing[Message Parsing Logic]
```
## BEHAVIOUR
```mermaid
stateDiagram-v2
[*] --> Setup
Setup --> Idle
Idle --> CheckInterval: loop()
CheckInterval --> ReadMessage: interval elapsed
CheckInterval --> Idle: interval not elapsed
ReadMessage --> ParseMessage: message available
ReadMessage --> Idle: no message
ParseMessage --> ValidateMessage: parsing successful
ParseMessage --> Idle: parsing failed
ValidateMessage --> NotifyOwner: message valid
ValidateMessage --> Idle: message invalid
NotifyOwner --> Idle: owner notified
Idle --> [*]: component destroyed
```
## TODOS
### PERFORMANCE
- Consider implementing circular buffer for improved memory efficiency
- Optimize string operations to reduce memory fragmentation
- Implement message queuing for high-frequency communication
- Add configurable buffer overflow handling
### SECURITY
- Implement message authentication for command validation
- Add input sanitization for malformed messages
- Consider implementing rate limiting for message processing
- Add checksum validation for message integrity
### COMPLIANCE
- Ensure compliance with Modbus serial communication standards
- Implement proper error handling according to industrial communication protocols
- Add support for standard industrial message formats
### RECOMMENDATIONS
- Use hardware serial interfaces for better reliability
- Configure appropriate baud rates for industrial environments
- Implement proper error recovery mechanisms
- Consider using DMA for high-speed serial communication
- Add message statistics and monitoring capabilities
## EXAMPLE
This section illustrates how the SerialMessage component is constructed and mounted:
```cpp
#ifdef ENABLE_SERIAL_COMMANDS
serialMessage = new SerialMessage(
Serial, // stream (Hardware Serial)
this // owner component
);
if (serialMessage)
{
components.push_back(serialMessage);
Log.infoln(F("SerialMessage initialized. Stream: Hardware Serial"));
}
else
{
Log.errorln(F("SerialMessage initialization failed."));
}
#endif
// Alternative with Serial1 for RS485 communication
#ifdef ENABLE_RS485_SERIAL
rs485SerialMessage = new SerialMessage(
Serial1, // stream (Hardware Serial1)
this // owner component
);
if (rs485SerialMessage)
{
components.push_back(rs485SerialMessage);
Log.infoln(F("RS485 SerialMessage initialized. Stream: Hardware Serial1"));
}
else
{
Log.errorln(F("RS485 SerialMessage initialization failed."));
}
#endif
```
### Message Handling Example
```cpp
// Owner component implements onMessage to handle parsed commands
void MyComponent::onMessage(uint16_t id, uint8_t verb, uint8_t flags,
const String& payload, Component* sender)
{
if (sender == serialMessage)
{
Log.infoln(F("Received serial command: ID=%d, Verb=%d, Payload=%s"),
id, verb, payload.c_str());
// Process the command based on ID and verb
switch (id)
{
case COMMAND_SET_CONFIG:
handleConfigCommand(verb, flags, payload);
break;
case COMMAND_GET_STATUS:
handleStatusRequest(verb, flags, payload);
break;
default:
Log.warningln(F("Unknown command ID: %d"), id);
break;
}
}
}
```
### References
${DOXYGEN_PLACEHOLDER}
${VENDOR_PLACEHOLDER}
</parameter>
</invoke>
<invoke name="str_replace_editor">
<parameter name="command">str_replace</parameter>
<parameter name="path">./docs/SerialMessage.md</parameter>
<parameter name="old_str">Now I'll create the documentation file for the SerialMessage component.</parameter>
<parameter name="new_str"></parameter>
</invoke>
The documentation has been successfully created! Here's a summary of what was generated:
## SerialMessage Component Documentation
The documentation file has been created at `./docs/SerialMessage.md` with the following key sections:
### Key Features Documented:
- **Serial Communication**: Handles reading and parsing of command messages from serial streams
- **Buffered Processing**: Uses configurable buffer size and parsing intervals
- **Message Validation**: Parses and validates incoming command messages
- **Owner Notification**: Integrates with the Component architecture to notify owners of parsed messages
### Technical Details:
- **Requirements**: Hardware serial interface, various software dependencies
- **Configuration**: Configurable buffer size (256 bytes default) and parsing interval (50ms default)
- **Dependencies**: Component base class, CommandMessage, ArduinoLog, Vector library
- **Behavior**: State machine showing message processing flow
### Documentation Structure:
- Standard frontmatter for Astro static site generator
- Comprehensive feature list and requirements
- Mermaid diagrams for dependencies and behavior
- TODO sections covering performance, security, compliance, and recommendations
- Practical examples showing component construction and message handling
The documentation is ready for use in an Astro static site and follows all the specified formatting requirements including proper Markdown linting standards.

36
docs/ValueWrapper.md Normal file
View File

@ -0,0 +1,36 @@
---
title: "ValueWrapper - Threshold-based Value Change Notification"
description: "A template class that wraps values with threshold-based change notification logic, designed for efficient Modbus communication"
keywords: ["ESP32", "Modbus", "ValueWrapper", "Threshold", "Notification", "C++", "Template"]
---
## ValueWrapper
**Path**: [`src/ValueWrapper.h`](../../src/ValueWrapper.h)
**Revision History**: Initial documentation
ValueWrapper is a template class that wraps a value (of any type T) and provides threshold-based change notification. When the value changes beyond a specified threshold, it automatically generates Modbus notification messages, reducing network traffic by only sending significant changes.
## REQUIREMENTS
- C++17 compiler support
- No specific hardware pins required
## FEATURES
- Supports any data type, including primitives and enums
- Two threshold check modes:
- DIFFERENCE: Notifies when absolute difference exceeds threshold
- INTERVAL_STEP: Notifies when value crosses interval boundaries
- Automatic Modbus message generation when thresholds are crossed
- Callback support for additional logic on value changes
- Type-safe conversion operations
- Specialized handling for different data types (signed, unsigned, enum)
## DEPENDENCIES
- [Component](./Component.md): Base component functionality
- [ModbusTypes](./modbus/ModbusTypes.md): Modbus data structures
- [enums](./enums.md): System-wide enumeration definitions
- [Logger](./Logger.md): Logging facility

9
scripts/docs-c.sh Normal file
View File

@ -0,0 +1,9 @@
kbot-d --router2=openai --model=anthropic/claude-3.7-sonnet \
--prompt=./scripts/docs-s.md \
--each=./src/Value*.h \
--globExtension=match-cpp \
--wrap2=meta \
--mode=completion \
--preferences=none \
--filters=code \
--dst='./docs/${SRC_NAME}.md'

9
scripts/docs-d.sh Normal file
View File

@ -0,0 +1,9 @@
kbot-d --model2=anthropic/claude-3.7-sonnet \
--prompt=./scripts/docs.md \
--each=./src/Serial*.h \
--globExtension=match-cpp \
--wrap=meta \
--mode=completion \
--preferences=none \
--filters2=code \
--dst='./docs/${SRC_NAME}.md'

97
scripts/docs-s.md Normal file
View File

@ -0,0 +1,97 @@
# Context
- ESP-32, Platform.io, C17
- avoidance of std
- industrial application, using Modbus-485
# Instructions
- Generate documentation for a given component in a new Markdown file located at `./docs/<component-name>.md`.
- The documentation is intended for an Astro static site generator.
- Include frontmatter in the Markdown file with `title`, `description`, and `keywords` fields.
- You will be provided with the content of the component's header file (e.g., `src/<component-name>.h`).
- Ensure the generated Markdown adheres to standard linting rules.
- The generated Markdown document must follow the specific layout provided below.
- The component's C++ source file will contain an example of how it is constructed and mounted; use this as a basis for the "Example" section in the documentation.
## Layout
The Markdown document must adhere to the following structure:
----------------------------
## COMPONENT NAME
**Path**: `relative/path/to/component-name.cpp` (as a Markdown link)
**Revision History**: Add a file revision entry in the header to track modifications. For the initial documentation, use "Initial documentation". Skip this if a revision history already exists !
A short description of the component.
## REQUIREMENTS
Detail the hardware pins and software dependencies required by the component.
## FEATURES
List the key features and functionalities of the component.
## DEPENDENCIES
- Provide a list of dependencies as Markdown formatted links.
- Include a minimal Mermaid diagram illustrating the dependencies. The diagram node names should not contain braces or brackets.
## BEHAVIOUR
- Include a minimal Mermaid diagram illustrating the component's behaviour or state machine. The diagram node names should not contain braces or brackets.
## TODOS
### PERFORMANCE
Outline any performance considerations or areas for future optimization.
### SECURITY
Describe potential security vulnerabilities or hardening measures.
### COMPLIANCE
Note any compliance standards or requirements relevant to the component.
### RECOMMENDATIONS
Provide any recommendations for using or extending the component.
## EXAMPLE
This section should illustrate how the component is constructed and mounted.
Refer to the component's C++ source file for an example.
A general example structure is:
#ifdef PIN_LED_FEEDBACK_0
ledFeedback_0 = new LEDFeedback(
this, // owner
PIN_LED_FEEDBACK_0, // pin
LED_PIXEL_COUNT_0, // pixelCount
ID_LED_FEEDBACK_0, // id
LED_FEEDBACK_0_MB_ADDR // modbusAddress
);
if (ledFeedback_0)
{
components.push_back(ledFeedback_0);
Log.infoln(F("LEDFeedback_0 initialized. Pin:%d, Count:%d, ID:%d, MB:%d"),
PIN_LED_FEEDBACK_0, LED_PIXEL_COUNT_0,
ID_LED_FEEDBACK_0, LED_FEEDBACK_0_MB_ADDR);
}
else
{
Log.errorln(F("LEDFeedback_0 initialization failed."));
}
#endif
### References
${DOXYGEN_PLACEHOLDER}
${VENDOR_PLACEHOLDER}

View File

@ -1,9 +1,10 @@
kbot-d --router2=openai --model=anthropic/claude-3.7-sonnet:thinking \
--prompt=./scripts/docs.md \
--each=./src/components/Plunger.h \
--each=./src/Serial*.h \
--globExtension=match-cpp \
--include=../polymech-fw-apps/cassandra-rc2/src/config.h \
--include=./src/Component.h \
--wrap=meta \
--wrap2=meta \
--mode=tools \
--preferences=none \
--disableTools=read_files,list_files,file_exists,modify_project_files \