Hi Community
I’ve written typed classes for OPC variables because I want to be able to spot errors at compile time.
To do this, I’ve written a factory to handle the different variants (OPC-Bool/-Int/etc. & Session-Bool/-Real/etc.).
The class must be async because a data type match is checked at startup (runtime).
If ((typeof await myObject.getValue()) !== usedType) → Exception
Now, a very strange behavior:
- The ValueChanged event integrated into the classes for the factory fires when I add an additional “opcua(myOpcPath).valueChanged(e => { DoStuff })”
- If I comment out the additional “<opcUa_EventAction>”, no event fires anymore.
Interpretation:
The B&R JavaScript logic doesn’t recognize when an OPC variable needs to be “activated” at a (relatively low?) nesting depth. The OPC subscriptions are deactivated similarly to when leaving a page.
Example code:
(async () => {
const opcPath = "::LineCtrl:stGui.BirdViewTransfStr";
const logger = new Logger('debug');
opcua(opcPath).valueChanged(e => { console.log('Something happens: ' + e.detail.newValue) } );
const birdviewTransform = await ValueSourceFactory.fromOpcString(opcPath, logger);
if (birdviewTransform) {
logger.info('birdviewTransform was created and will be started.');
birdviewTransform
.subscribe(e => console.log('New value:', e))
.start();
} else {
logger.error('ValueSource could not be created (type fault or no OPC variable)');
}
})();
If the line
opcua(opcPath).valueChanged(e => { console.log('Shit happens: ' + e.detail.newValue) } );
is commented out, then
.subscribe(e => console.log('New value:', e))
will no longer work.