I cannot sem to find the documentation that actually list the methods inside the itemManager API, I have run a report out of the API but I am still not able to complete the task i am after.
I need to read the "Value" of data source assigned to "item2" in my Custom Widget. Since "setValue" allows me to write a value (1 or 0) to a connection through "Item" I assumed "getValue" value would read teh connection on "Item2" but I countine to get errors in the debug that it is not a function. I have tried the following and recieved either the same error or i just get nothing.
.getValue
.data.
.getItemvalue
.Values
.valueOf
My project in it's current state is attached if that helps, maybe i am using the wrong syntax? I am trying to assign a tag in WebIQ interface and then use the value of that tag (1 or 0) to set the "state" of my button with differnt svg. files. I know I will need to subscribe to the data in a finale iteration but I just wanted to get it functional first.
Best reply by webiq-sk
"The issue is that internal to the js code, there is no way to access the .value of an item and read that as an integer." Sure there is - there are two ways available: 1. When you only need a one-shot read: readDirect 2. When you want to react to continuous item changes: subscribeItem
Here is the corresponding code from the scripting demo ("demo-item-subscribe") that illustrates this:
// Define our item names to use - it's best to put them here and not somewhere in the code below
// for easier changeability and understanding
const observedItemName = 'ls-observed-item';
// See below
let subscriptionToken = null;
// Get a reference to the ItemManager
const im = shmi.requires("visuals.session.ItemManager");
// Create a new instance of an item handler to use and define a function that will be called
// asynchronously by the framework whenever the item value changes (or is received initially)
const myItemHandler = im.getItemHandler();
myItemHandler.setValue = function (value) {
console.log("[LS demo-item-subscribe] The value of the item has been set to " + value);
};
// Obtain a subscription token. You ALWAYS have to store this token and call the appropriate unlisten function
// when you no longer need it, in this case when the LocalScript will be disabled (see below)
// If you do not do that this will likely cause memory leaks (https://blog.logrocket.com/escape-memory-leaks-javascript/)
// leading to more and more RAM used in your HMI until the amount of free RAM will be exhausted and the browser will crash
// The WebIQ Visuals framework always calls the onDisable function automatically when your LocalScript should not be running anymore,
// e.g. when it has been included in a screen view and the user switches to another screen.
subscriptionToken = im.subscribeItem(observedItemName, myItemHandler);
/* called when this local-script is disabled */
self.onDisable = function () {
if (subscriptionToken) {
subscriptionToken.unlisten(); // Remove the listener and tell WebIQ Server to stop sending us item updates
}
self.run = false; /* from original .onDisable function of LocalScript control */
};
And if your item does not have the type integer in your PLC you can of course cast it with JavaScripts parseInt() method into an integer value.