Modifying Variables from JS


Modification is calling a function to process a variable before sending to equipment or before displaying on control panel. Modification-function is to be indicated in variable properties in Scriptline. It allows to modify values, received from a control panle of from equipment and only then write it in Server variable. The peculiarity of modification-function is that a channel or a tag that calls a functions gives arguments to it:



  • ID - tag or channel ID, corresponding to its ordinal number starting with zero
  • value - value that comes to a channle or is written in tag (can be modified)
Any server variable can be changed. The function returns a modified value with the help of return command.




Example of usage of modification-functions for auto-modification od values:



/**   change channels/tags   
   on input    :   in_Type     - Type 0 - channels, 1 - tags
                    in_Name     - Name channel/tags
                    in_Value    - Value
    on outputs   :  result value channels/tags
*/
function test(in_Type, in_Name, in_Value)
{
    IR.Log("Type: " + in_Type);
    IR.Log("Name: " + in_Name);
    IR.Log("Value: " + in_Value);
}
ModifyJSinWindow.png
ModifyJS.png

Driver Tokens



Driver Tokens are driver variables. Status of connection to equipment is displayed. Driver variables are available only for reading, but they can be modified with the help of JavaScript before sending it to a control panel. Let's create a modification-function in a server project to modify a variable.



Example of modification-function for Driver Tokens:



/*
    Online token of any driver can return only value 1 = Online and 0 = Offline.
    Substitute 1 with the current time and "Connection established" phrase with; 0 for time and " Connection closed"
    Return the modification result to the one that called the function
*/
function onlineModify(in_Type, in_Name, in_Value){
 if(in_Value == 1 )
    {
      in_Value = IR.GetVariable("System.Time.24")+" Connection established";
      return in_Value;
    }else{
      in_Value = IR.GetVariable("System.Time.24")+" Connection closed";
      return in_Value;
   }
}
Modification-function is called automatically when a variable connected with it changes, it returns a modified value. Modification-function has two arguments at the input :



  • ID - channel ID, corresponding to its ordinal number beginning with zero
  • value - value that came to the channel (it can be modified)

To choose a variable that is processed by a modification-function, give the function name in the variable settings:


Return Mod Value FromJStoPToken.png
Only modification result comes to the server variable and no the incoming variable value.






Channels



Channels are commands to be sent to the driver. The allow to write valur to the variable of automation equipment or send a command, that is earlier formed in the driver. Driver commands can be done from a control panel or from script. To do a command it has to be sent s value (sometimes a value is nor required, in this case an empty line is sent). This value can be modified with the help of JavaScript before sending it to equipment. Let's create a modification-function in a server project to modify sent value.



Example of modification-function for Channels:



/*
    Channel is activated when sending a value in the following range 0-100
    Let's modify the value to 0-255 range, approximate it and send to equipment
*/
function rangeModify (in_Type, in_Name, in_Value){
      in_Value = 255/100;
      return in_Value.toFixed(0);
}
Modification-function is called automatically when a variable connected with it changes, it returns a modified value. Modification-function has two arguments at the input :



  • ID - channel ID, corresponding to its ordinal number beginning with zero
  • value - value that came to the channel (it can be modified)
To choose a variable that is processed by a modification-function, give the function name in the variable settings:



Return Mod Value FromJStoCmd.png
The command sends only modification result but not the initial value.



A name function without an argument can be done the same way.



Tags



Tags - feedback channels (Feedbacks). They allow to get state of equipment variable or the result of parsing incoming data. Driver tags can be displayed on a control panle or used in the script. Tag value can be modified with the help of JavaScript before next use. To modify a value create a modification-function in a server project.



Example of modification-function for Tags:



/*    Channel receives value in 0-255 range
    Change value to 0-100 range, approximate an send back to the feedback channel
*/
function rangeModifyBack (ID, value) {
      value *= 100/255;
      return value.toFixed(0);
}<span class="br0"></span>
Modification-function is called automatically when a value comes to the feedback channel. The function returns a modified value that is available when reading the channel state. Modification-function has two arguments at the input :




  • ID - channel ID, corresponding to its ordinal number beginning with zero
  • value - value that the channel received from automation equipment of from the script (it can be modified)

To select which tag is processed by a modification-function, enter name in the variable settings:



Return Mod Value FromJStoFb.png
Tag gives onle modification result but not the initial value.







Virtual Channels



Virtual Channels - virtual driver variables. They look like commands on the side of a control panel. Value can be sent to the value to be processed by a modification fucntion, A command can be formed on the basis of value and sent tp automation equipment.



Example of modification-function for Virtual Channels:



/*
    TCP command to DVD player looks this way: "VOLUME XX", ХХ - is sound value from 00 to 100
    Sound value can consisy of 2 or 3 symbols i.e. 00, 01, 02, ... 100 (ASCII)
    We get 0-100 value from a control panel, form and send a command
*/
var driver = IR.GetDevice("AV (TCP)"; 
function volume (in_Type, in_Name, in_Value)
{
     if(in_Value <10)
    {
        in_Value ="0"+in_Value;
            driver.Send(["VOLUME "+in_Value+"\r\n"]);
    }else{
            driver.Send(["VOLUME "+in_Value+"\r\n]);
     }
}
Modification-function ia called automatically when a command connected with it is activated (a command can be activated by iRidium control panel or by script). Modification-function has two arguments at the input:



  • ID - variable ID corresponding to its ordinal number beginning with zero
  • value - value, that was written to the variable from a control panel of from script (it can be modified and used)

To select a variable to process, input function name in the variable settings:



Return Mod Value VirtModFromJS.png

A named function without argument can be done the same way.






Virtual Tags



Virtual Tags are server virtual variables. They look like feedback channels on the side of a control panel. Value can be written to the variable from script and then processed by a modification-function. The final value is available for reading from a control panel.



Example of modification-function for Virtual Tags:



/*
    Virtual tag does NOT get values from equipment
    Write value to tag from script, when required.
*/
IR.SetVariable("Server.Tags.Virtual 1",100)
/*
    Value can be additionalle processed with the help of a modification-function.
*/
function value (in_Type, in_Name, in_Value){
    in_Value="brightness: "+in_Value+"%";
    return in_Value;// brightness: 100%
}
Modification-function is called automatically when a tag connected with it is changed (script writes data to tag) ункция-модификатор будет вызвана автоматически когда изменяется связанный с ней тег (скрипт записывает в тег данные). The function has two arguments at the input:



  • ID - variable ID corresponding to its ordinal number beginning with zero
  • value - value that was written to the variable from script (it can be modified and used)

To select what variable to modify, input function name in the variable settings:



Return Mod Value VirtTagFromJS.png

A named function without argument can be done the same way.

Is this article helpful for you?