Dear Community User! We are updating our platform to a new system.
Read more: Important information on the platform change.

cancel
Showing results for 
Search instead for 
Did you mean: 
SOLVED

map plc array var by local script into a virtual item depending on array index set by button-rocker

map plc array var by local script into a virtual item depending on array index set by button-rocker

Zimmbo
Established Member

Hy all,

i like to set a virtual item (EventID) by local script depending by a item manipulated by and iq-button-rocker( ArrayPos). Like this small logic. The iq-button rocker is not able to use ui-actions? How can i map in every situation the PLCEventID from a Array to the virtual Item EventID depending the arrayPos? How can i run the local script every time?

If ArrayPos == 0

EventID = PLCEventID_0

If ArrayPos == 1

EventID = PLCEventID_1

 

10 REPLIES 10

webiq-sk
Frequent Contributor

I don't quite understand your question but I'll try to answer what I understood:

1. The IQ Rocker Button widget does not support UI Actions
That's true, because it has been created to change a specific item value - as a click on one of the buttons already increases or decreases an associated item automatically.

2. PLC Array
WebIQ does not support OPC-UA arrays at the moment for reading or writing. If you are referring to an array structure item.

3. EventID = PLCEventID_0
Something like this?

if (virtual Item attached to Rocker Button == 0) {
    setItemValue($eventId, $plcEventId0);
}

where $eventId is an item name and $plcEventId0 is the value of an item

?

Zimmbo
Established Member

Hi please help, i am note fine with the syntax from javascript. Can someone check what is wrong with my code. I like to copy the value from MsgArray0.EventID or MsgArray1.EventID to Dialog.EventID depending the rocket button value Dialog.ArrayIndex.

(function () {
    var MODULE_NAME = "SelectDialogArray",
        ENABLE_LOGGING = true,
        RECORD_LOG = false,
        logger = shmi.requires("visuals.tools.logging").createLogger(MODULE_NAME, ENABLE_LOGGING, RECORD_LOG),
        fLog = logger.fLog,
        log = logger.log,
        module = shmi.pkg( MODULE_NAME );

    // MODULE CODE - START

    /* private variables */

    /* private functions */

    /**
     * Implements local-script run function.
     *
     * This function will be called each time a local-script will be enabled.
     *
     * @param {LocalScript} self instance reference of local-script control
     */
        module.run = function (self) {

        //Place your Code here
        if (Dialog.ArrayIndex == 0) {
            setitemValue(Dialog.EventID,MSGArray0.EventID);
        }
        if (Dialog.ArrayIndex == 1) {
            setitemValue(Dialog.EventID,MSGArray1.EventID);
        }
        /* called when this local-script is disabled */
        self.onDisable = function () {
            self.run = false; /* from original .onDisable function of LocalScript control */
        };
    };


    // MODULE CODE - END

    fLog("module loaded");
})();

webiq-sk
Frequent Contributor

My code was written in pseudocode for trying to understand what you are trying to achieve. There is no method "setItemValue" in WebIQ!

I can only help you if you answer my question in such a way that I understand exactly what you are trying to achieve, specifically:

  1. What is "Dialog" ?
  2. What is "ArrayIndex" ?
  3. What is "EventID" ?
  4. What is "MsgArray0" and "MsgArray1" ?

Zimmbo
Established Member

Hi all,

the explanation

  1. What is "Dialog" ? -> Is only a name from value, like a prefix to get a better overview in Process Data Manager
  2. What is "Dialog.ArrayIndex" ? -> Value from rocket button set by button using internal item to save value in HMI.
  3. What is "Dialog.EventID" ? -> internal item should set by plc var depending the value ArrayIndex
  4. What is "MsgArray0.EventID" and "MsgArray1.EventID" ? -> two plc vars from ctrlX Core sent by opc ua. Integer e.g. 12345

a IQ-Label should show with the Dialog.EventID a text localizated in english or german. Dialog.EventID is only a number to get the right text in different languages.

webiq-sk
Frequent Contributor
Thanks for the clarification. This code is untested (because I don't have your items), but it should work:

(function () {
    /**
     * replace module name with a custom name for the local-script.
     *
     * All local-script should be attached to the "custom.ls" package.
     * If more than one script is required for an application, a common root package
     * should be created (e.g. "custom.ls.customerName.*").
     */
    var MODULE_NAME = "SelectDialogArray",
        ENABLE_LOGGING = true,
        RECORD_LOG = false,
        logger = shmi.requires("visuals.tools.logging").createLogger(MODULE_NAME, ENABLE_LOGGING, RECORD_LOG),
        fLog = logger.fLog,
        log = logger.log,
        module = shmi.pkg( MODULE_NAME );

    // MODULE CODE - START
    /* private variables */

    /* private functions */

    // Called when the actions are completed, regardless of an error just for demonstrating how to
    // perform other actions asynchronously
    function showMessageOnCompleted(successful)
    {
        if (successful) {
            alert("Done!");
        } else {
            alert("There was an error!");
        }
    }

    /**
     * Implements local-script run function.
     *
     * This function will be called each time a local-script will be enabled.
     *
     * @param {LocalScript} self instance reference of local-script control
     */
        module.run = function (self) {
            // Place your Code here
            const im = shmi.visuals.session.ItemManager;

            // Everything in WebIQ happens asynchronously so you always have to use callbacks
            // Please keep in mind that your code can only retrieve item values by either subscribing to them or reading them directly
            // Please note: if any of the items used are virtual items the corresponding code would have to be changed
            im.readDirect(["Dialog.ArrayIndex", "Dialog.ArrayIndex", "MSGArray0.EventID", "MSGArray1.EventID"], function(err, result) {
                if (!err) {
                    if (result["Dialog.ArrayIndex"] == 0) {
                        im.writeDirect({"Dialog.EventID": result["MSGArray0.EventID"]}, function(err, result) {
                            if (err) {
                                console.error("Error occurred writing item: "+err);
                                showMessageOnCompleted(false);
                            } else showMessageOnCompleted(true);
                        });
                    } else if (result["Dialog.ArrayIndex"] == 1) {
                        im.writeDirect({"Dialog.EventID": result["MSGArray1.EventID"]}, function(err, result) {
                            if (err) {
                                console.error("Error occurred writing item: "+err);
                                showMessageOnCompleted(false);
                            } else showMessageOnCompleted();
                        });
                    }
                } else {
                    console.log("Could not read items: ", err, result);
                    showMessageOnCompleted(false);
                }
            });           

            /* called when this local-script is disabled */
            self.onDisable = function () {
                self.run = false; /* from original .onDisable function of LocalScript control */
            };
        };

    // MODULE CODE - END
    fLog("module loaded");
})();

webiq-sk
Frequent Contributor

Regarding the localization: WebIQ already includes a complete localization solution where you define a text and can translate it. You then use the single placeholder everywhere in the HMI and depending on the localization in the HMI set it would show the correct text.

You should not use items to change any localizations form one language to the other as that's not required.

Zimmbo
Established Member

Hi,

thank you the code is nearly running i like.

What is the best way to change the dialog.EvendID value by click the button. So ich have to create a UI-Actions for the button-rocker? Our can i handle by click event the iq-button-rocker, so the script run if the value change?

webiq-sk
Frequent Contributor

Well, I would just utilize the function of the Rocker Button widget and assign the item "Dialog.EventID" to the widget.

Zimmbo
Established Member

Hi all,

i have some problem with datatyp now. If i set EvendID like the example before with Integers is running. Now i would to set MSGName like in the following code i get an empty string. Is the function writedirect not running with strings? The source of the data (MSGArray0.MSGName) is a string from PLC and the target also.

(function () {
    /**
     * replace module name with a custom name for the local-script.
     *
     * All local-script should be attached to the "custom.ls" package.
     * If more than one script is required for an application, a common root package
     * should be created (e.g. "custom.ls.customerName.*").
     */
    var MODULE_NAME = "SelectMessageName",
        ENABLE_LOGGING = true,
        RECORD_LOG = false,
        logger = shmi.requires("visuals.tools.logging").createLogger(MODULE_NAME, ENABLE_LOGGING, RECORD_LOG),
        fLog = logger.fLog,
        log = logger.log,
        module = shmi.pkg( MODULE_NAME );

    // MODULE CODE - START
    /* private variables */

    /* private functions */

    // Called when the actions are completed, regardless of an error just for demonstrating how to
    // perform other actions asynchronously
   
    function showMessageOnCompleted(successful)
    {
        if (successful) {
            alert("Done!");
        } else {
            alert("There was an error!");
        }
    }

    /**
     * Implements local-script run function.
     *
     * This function will be called each time a local-script will be enabled.
     *
     * @param {LocalScript} self instance reference of local-script control
     */
        module.run = function (self) {
            // Place your Code here
            const im = shmi.visuals.session.ItemManager;

            // Everything in WebIQ happens asynchronously so you always have to use callbacks
            // Please keep in mind that your code can only retrieve item values by either subscribing to them or reading them directly
            // Please note: if any of the items used are virtual items the corresponding code would have to be changed
            im.readDirect(["Dialog.ArrayIndex", "MSGArray0.MSGName", "MSGArray1.MSGName", "MSGArray2.MSGName", "MSGArray3.MSGName", "MSGArray4.MSGName"], function(err, result) {
                if (!err) {
                    if (result["Dialog.ArrayIndex"] == 0) {
                        im.writeDirect({"Dialog.MSGName": result["MSGArray0.MSGName"]}, function(err, result) {
                            if (err) {
                                console.error("Error occurred writing item: "+err);
                                showMessageOnCompleted(false);
                            } else showMessageOnCompleted(true);
                        });
                    }
                    else if (result["Dialog.ArrayIndex"] == 1) {
                        im.writeDirect({"Dialog.MSGName": result["MSGArray1.MSGName"]}, function(err, result) {
                            if (err) {
                                console.error("Error occurred writing item: "+err);
                                showMessageOnCompleted(false);
                            } else showMessageOnCompleted(true);
                        });
                    } else if (result["Dialog.ArrayIndex"] == 2) {
                        im.writeDirect({"Dialog.MSGName": result["MSGArray2.MSGName"]}, function(err, result) {
                            if (err) {
                                console.error("Error occurred writing item: "+err);
                                showMessageOnCompleted(false);
                            } else showMessageOnCompleted(true);
                        });
                    } else if (result["Dialog.ArrayIndex"] == 3) {
                        im.writeDirect({"Dialog.MSGName": result["MSGArray3.MSGName"]}, function(err, result) {
                            if (err) {
                                console.error("Error occurred writing item: "+err);
                                showMessageOnCompleted(false);
                            } else showMessageOnCompleted(true);
                        });
                    } else if (result["Dialog.ArrayIndex"] == 4) {
                        im.writeDirect({"Dialog.MSGName": result["MSGArray4.MSGName"]}, function(err, result) {
                            if (err) {
                                console.error("Error occurred writing item: "+err);
                                showMessageOnCompleted(false);
                            } else showMessageOnCompleted(true);
                        });
                    }
                } else {
                    console.log("Could not read items: ", err, result);
                    showMessageOnCompleted(false);
                }
            });          

            /* called when this local-script is disabled */
            self.onDisable = function () {
                self.run = false; /* from original .onDisable function of LocalScript control */
            };
        };

    // MODULE CODE - END
    fLog("module loaded");
})();

webiq-sk
Frequent Contributor

The easiest way to find out the reason is to add two IQ Input widgets to your HMI and setting the items to Dialog.ArrayIndex and MSGArray0.MSGName (or any other one used).

Then you can see if your input and output items have the correct values. Also have a look at the browser console (F12) if any corresponding error is shown.

Your main function code can by the way be simplified into this:

        module.run = function (self) {
            const im = shmi.visuals.session.ItemManager;

            // Everything in WebIQ happens asynchronously so you always have to use callbacks
            // Please keep in mind that your code can only retrieve item values by either subscribing to them or reading them directly
            // Please note: if any of the items used are virtual items the corresponding code would have to be changed
            im.readDirect(["Dialog.ArrayIndex", "MSGArray0.MSGName", "MSGArray1.MSGName", "MSGArray2.MSGName", "MSGArray3.MSGName", "MSGArray4.MSGName"], function(err, result) {
                if (!err) {
                    const idx = result["Dialog.ArrayIndex"];
                    if (idx >= 0 && idx <= 4) {
                        im.writeDirect({"Dialog.MSGName": result["MSGArray" + idx + ".MSGName"]}, function(err, result) {
                            if (err) {
                                console.error("Error occurred writing item: "+err);
                                showMessageOnCompleted(false);
                            } else showMessageOnCompleted(true);
                        });
                    }
                } else {
                    console.log("Could not read items: ", err, result);
                    showMessageOnCompleted(false);
                }
            });          

            /* called when this local-script is disabled */
            self.onDisable = function () {
                self.run = false; /* from original .onDisable function of LocalScript control */
            };
        };

   
// MODULE CODE - END
    fLog("module loaded");
})();
Icon--AD-black-48x48Icon--address-consumer-data-black-48x48Icon--appointment-black-48x48Icon--back-left-black-48x48Icon--calendar-black-48x48Icon--center-alignedIcon--Checkbox-checkIcon--clock-black-48x48Icon--close-black-48x48Icon--compare-black-48x48Icon--confirmation-black-48x48Icon--dealer-details-black-48x48Icon--delete-black-48x48Icon--delivery-black-48x48Icon--down-black-48x48Icon--download-black-48x48Ic-OverlayAlertIcon--externallink-black-48x48Icon-Filledforward-right_adjustedIcon--grid-view-black-48x48IC_gd_Check-Circle170821_Icons_Community170823_Bosch_Icons170823_Bosch_Icons170821_Icons_CommunityIC-logout170821_Icons_Community170825_Bosch_Icons170821_Icons_CommunityIC-shopping-cart2170821_Icons_CommunityIC-upIC_UserIcon--imageIcon--info-i-black-48x48Icon--left-alignedIcon--Less-minimize-black-48x48Icon-FilledIcon--List-Check-grennIcon--List-Check-blackIcon--List-Cross-blackIcon--list-view-mobile-black-48x48Icon--list-view-black-48x48Icon--More-Maximize-black-48x48Icon--my-product-black-48x48Icon--newsletter-black-48x48Icon--payment-black-48x48Icon--print-black-48x48Icon--promotion-black-48x48Icon--registration-black-48x48Icon--Reset-black-48x48Icon--right-alignedshare-circle1Icon--share-black-48x48Icon--shopping-bag-black-48x48Icon-shopping-cartIcon--start-play-black-48x48Icon--store-locator-black-48x48Ic-OverlayAlertIcon--summary-black-48x48tumblrIcon-FilledvineIc-OverlayAlertwhishlist