Custom Model interface and JavaScript
This example exposes a model's temperature as a Property, reports a threshold crossing through an Event, and packages a change to an internal Widget in a Method.
Create the model
- Open the Custom Model panel and add a model.
- Add custom Properties
temperatureandalarmLimitwith data typeLREAL, plusoverLimitwith data typeBOOL. Use these same values for Name for JS Access. - Add a custom Event
ThresholdExceededwith oneLREALparameter. Set its Name for JS Access toThresholdExceeded. - Add an internal Numeric Widget and bind its Value Property to the model's
temperatureProperty. Add an internal Picture Widget and bind its Visible Property tooverLimit. The internal Widgets stay behind the model's public interface. - Add a custom Method
ResetAlarm. Define a Set value Action that sets the model'soverLimitProperty tofalse; the Picture's binding updates its visibility. Set the Method's Name for JS Access toResetAlarm.
The Property binding and Method Actions package interaction with the Numeric Widget inside the model. A caller of the Custom Widget need not know the internal Widget's name or layout.
Add the model script
this.property.overLimit = this.property.temperature > this.property.alarmLimit;
const subscriptionId = this.property.$subscribe('temperature', (newValue, oldValue) => {
const overLimit = newValue > this.property.alarmLimit;
this.property.overLimit = overLimit;
if (overLimit && oldValue <= this.property.alarmLimit) {
this.event.ThresholdExceeded.emit(newValue);
}
});
// The model can invoke its own custom Method when its logic calls for it:
// this.method.ResetAlarm();
// Cancel the subscription if the script no longer needs it:
// this.property.$unsubscribe(subscriptionId);Deploy the model as a Custom Widget, bind each instance's custom Properties as needed, and handle ThresholdExceeded on the Custom Widget. The subscription callback receives (newValue, oldValue) and $subscribe returns the ID used by $unsubscribe.
Check the exposed Methods and Events
The members are exposed only when Name for JS Access is nonempty. Use in for a membership check and Object.keys to inspect the interface:
console.log('ResetAlarm' in this.method); // true
console.log('ThresholdExceeded' in this.event); // true
console.log(Object.keys(this.method)); // ['ResetAlarm']
console.log(Object.keys(this.event)); // ['ThresholdExceeded']
console.log(this.method.ResetAlarm.name); // 'ResetAlarm'
console.log(this.event.ThresholdExceeded.emit.name); // 'emit'
console.log(`${this.method} / ${this.event}`); // [object Method] / [object Event]Unknown or empty names return undefined. The namespaces are read-only; define the interface in the model editor. If selecting an Event dynamically, use this.event[eventName].emit(value) after checking eventName in this.event.
To exchange data with a Device, bind a custom Property to an EBX Variable or Property; the script works through that Property.