https://github.com/project-chip/zap/blob/master/test/gen-template/matter/external-addon-helper.js
This guide explains how to add and manage externalized helper functions in the SDK, which interacts with ZAP (ZCL Advanced Platform).
This design enables SDK developers to specify helper functions via the gen-template.json
configuration file, which ZAP consumes during binary creation. The SDK can then call these helpers, passing in the necessary data, making them more flexible and reducing dependency on the internal ZAP codebase.
With external helpers, the SDK can call functions that are defined outside of the ZAP repository. Here's how it works:
Here’s the step-by-step process to add your own external helper file to the SDK:
External helper functions should be asynchronous and accept the necessary data passed from ZAP, typically via the api object. These helpers can perform any logic, such as querying ZAP's data or processing it.
Here’s an example of a helper that retrieves and counts available clusters:
/** * Helper function to get the total number of available events. * @param {object} api - The API object. * @returns {number} - The total number of events. */ async function get_total_events_helper(api) { let events = await api.availableEvents(this) let totalEvents = events.length return totalEvents }
api
object.api.availableClusters(this)
to fetch the list of available clusters.Each helper can interact with the ZAP API to fetch data and process it as needed, based on the context passed in.
The next step is to tell ZAP where to find your external helper functions. This is done by specifying the relative path to each helper file in the gen-template.json configuration file.
For example, your gen-template.json might look like this:
{ "helpers": [ "path/to/external_helpers/get_total_events_helper.js", "path/to/external_helpers/get_total_attributes_helper.js" ] }
The paths should be relative to the location of the gen-template.json file itself. This tells ZAP where to find your helper files when generating the binary.
After defining and specifying your helper files, the next step is to register them with ZAP so that they can be used during binary creation. You’ll need to call helperRegister.registerHelpers() for each helper function.
Here’s an example of how to register your helpers:
function initialize_helpers(helperRegister, context) { // Register the 'get_total_events_helper' function helperRegister.registerHelpers( 'get_total_events_helper', get_total_events_helper, context ) // Register other helpers similarly helperRegister.registerHelpers( 'get_total_attributes_helper', get_total_attributes_helper, context ) }
helperRegister.registerHelpers()
function registers each helper.Once your helpers are registered, they can be used in templates or other components of the SDK as needed.
For example, in your SDK code, you can call the helpers like this:
This design pattern allows SDK developers to create externalized helpers. By leveraging the gen-template.json configuration file and the API object, SDK developers can interact with ZAP without needing direct access to ZAP’s internal source code. The approach abstracts method and query name changes in ZAP, ensuring that SDK code remains resilient to changes in the ZAP API.