ValueWrapper Template Class
ValueWrapper
Section titled “ValueWrapper”Path: src/modbus/ValueWrapper.h
Revision History: Initial documentation
A template class that wraps values of any type with threshold-based update notifications through Modbus. This class monitors changes in a value and sends notifications to the owner component when changes exceed specified thresholds, using different comparison modes. It helps reduce network traffic by only sending updates when significant changes occur.
REQUIREMENTS
Section titled “REQUIREMENTS”- An owning Component instance that receives notifications
- A Modbus address space for notifications
- Function code configuration for the Modbus messages
PROVIDES
Section titled “PROVIDES”-
ValueWrapper<T>
- Template class for wrapping values of any type -
ThresholdMode
enumeration:DIFFERENCE
- Triggers when absolute difference between old and new values exceeds thresholdINTERVAL_STEP
- Triggers when values cross step boundaries defined by the threshold
-
Macros:
DEFAULT_VW_POST_NOTIFY_LAMBDA
- Helper for creating standard post-notification logging lambdasINIT_COMPONENT_VALUE_WRAPPER
- Simplifies ValueWrapper initialization
FEATURES
Section titled “FEATURES”- Wraps any data type, including enums, with transparent type conversion
- Provides automatic threshold-based notifications through Modbus
- Supports different threshold modes for different types of data
- Handles both arithmetic types and enumerations correctly
- Provides callbacks when notifications are sent
- Automatic typecasting for Modbus message values
DEPENDENCIES
Section titled “DEPENDENCIES”BEHAVIOUR
Section titled “BEHAVIOUR”The ValueWrapper monitors changes to a value and sends notifications when thresholds are exceeded.
PERFORMANCE
Section titled “PERFORMANCE”- Consider providing specialized implementations for common types to avoid template instantiation overhead
- Investigate using std::variant for better type safety and potentially reduced memory footprint
- Add an optional hysteresis parameter to prevent rapid oscillations around threshold values
SECURITY
Section titled “SECURITY”- Add bounds checking for Modbus addresses to prevent accidental overwrites
- Consider adding validation callbacks to ensure values remain within acceptable ranges
COMPLIANCE
Section titled “COMPLIANCE”- Ensure all Modbus messages conform to the Modbus specification requirements
- Consider adding support for additional Modbus function codes as needed
RECOMMENDATIONS
Section titled “RECOMMENDATIONS”- Use the
INIT_COMPONENT_VALUE_WRAPPER
macro to simplify instance creation - Choose appropriate threshold values to balance responsiveness and network traffic
- For enum types, use threshold value of 1 to notify on any state change
- For float values, consider the INTERVAL_STEP mode to create logical boundaries for notifications