šŸ“œ ā¬†ļø ā¬‡ļø

IoT & GPS Service ViaLatM - scripting language (part 3)

This part of the article describes the use of a scripting language for working with object icons and new (added to the language) operators and functions.



Logical operators


Now you can use logical operators in expressions: & (AND), | (OR) and ^ (XOR).
Examples:
ALARM = SENSOR1 & SENSOR2;
GROUP_STATE = UNIT ("12345"). X1 | UNIT ("67890"). X2;
These operators work on variable values ​​bit by bit.
')

BOOL function


BOOL function - returns the value 1 if the argument passed is greater than 0, or 0 if it is less than or equal to 0. You can use the IFSET function, but in some cases using a BOOL, a shorter record is obtained.

HEX, BIN functions


For convenience of setting the value in hexadecimal, you can use the HEX and BIN functions:
HEX ("FE") - returns 254
BIN ("10110") - returns 22
Functions are useful when working with attributes of objects that are passed in the form of bit strings.

MAPVALUE function


Format: MAPVALUE (VAL0, RET0, VAL1, RET1, ..., VALN, RETN, PARAM);
The function checks the value of the PARAM parameter (the last parameter of the function) by the parameters VAL0, VAL1, ... VALN. Finds the one for which the condition PARAM> VALX is satisfied and returns the corresponding parameter RETX. Of all the parameters that satisfy the condition, the one with the highest VAL value is selected, that is, the one closest to PARAM. This allows you to pass arguments in an order that is not sorted by VAL, that is, variables, attributes of objects can be specified as VAL arguments — for example, the distance of your devices to a particular location, which are calculated at the time of the script. If there is no such parameter, the RET value is always returned, which corresponds to the minimum of all VAL parameters.
Example:
X1 = MAPVALUE (10, 100, 20, 200, 30,300,23);
the value of X1 will be set to 200.

MAPVALUESTR function


Format: MAPVALUESTR (VAL0, RET0, VAL1, RET1, ..., VALN, RETN, PARAM);
The function is similar to the function MAPVALUE, but string variables are set as RET variables and the function returns a string. An example of the application of this function will be given below, when describing the dynamic installation of the image and the color of the object icon.

Before describing the work with icons in the script, I will give additional. information on working with icons in the service:

Upload custom icons


In the main menu of the Profile service, by selecting the Custom Elements submenu, you can upload your images for objects.



Assign an icon to an object


For each object, selecting ā€œEditā€ in the context menu, you can assign an icon to the object. The icon is selected from predefined groups or from the ā€œCustom Iconsā€ group. You can change the color of the icon and its size. The following describes how this can be done dynamically using a scripting language.



Working with scripting language icons


In this case, an example is given of changing the color and image of an icon when messages arrive from an object. To do this, use the context menu of the object to add the following statements to the script:

UNIT.ICON.COLOR = MAPVALUESTR (0, "FF6A00", 10, "B6FF00", 20, "00FFFF", 40, "0094FF", 60, "0026FF", 80, "B200FF", 100, "FF006E", UNIT.V);
UNIT.ICON.IMAGE = MAPVALUESTR (0, "car5pack.png", 50, "car5.png", 100, "car5sign.png", UNIT.V);

With the help of the first operator, the object icon changes color depending on the current speed. With the help of the second operator, the image of the icon changes based on the current speed of the object. The following object attributes are used for working with the icon:
UNIT.ICON.COLOR
UNIT.ICON.IMAGE
UNIT.ICON.SIZE
The last attribute allows using the script to change the size of the object icon.

As part of the script for a single object, you can change the icons of other objects (if they are in the same folder, or belong to the same user who owns the object for which the script is being run).
The image must exist within the group in which the object icon is selected. That is, it is impossible within the framework of the script to assign an icon to an object from the ā€œBusā€ group if it was initially selected in the ā€œInternet of Thingsā€ group.

An example of how this script works can be seen in the example of the Barcelona object in ViaLatM Service in the demo access ..

Source: https://habr.com/ru/post/270021/


All Articles