Example 3
Control of the output, using of internal functions and user's defined functions
Previous  Top  Next


This example shows how to use JavaScript to control or configure the output of SNMP OPC Server, and how to use internal and user's defined functions. According to the input value, the output will show today's date, or current time.

First, new Script Item called DateTime is created:

clip0100  

Than add new JS variable input, and set the Item Path to the Data Item. The output is different, according to the input value:
   0 – View today's date
   1 – View current time
   2 – View both, date and time
   Other – Undefined command

And the script :

// according to input value, shows current date or time  
// the input is a data item  
function OnItemRead_DateTime(input)  
{  
  var RetVal;  
     
  switch( input )  
  {  
    // if 0 show date   
    case 0 : RetVal = GetCurrentDate();   
             break;  
 
    // if 1 show time  
    case 1 : RetVal = GetCurrentTime();    
             break;  
 
    // if 2 show both, date and time  
    case 2 : RetVal = GetCurrentDate();   
             RetVal += "  ";  
             RetVal += GetCurrentTime();  
             break;  
 
    // if 3 undefined commands  
    default : RetVal = "UNDEFINED COMMAND";  
              break;  
 
  };  
  return RetVal;  
}  
 
// get current date in format : "DD.MM.YYYY"  
function GetCurrentDate()  
{  
  var s = "";  
 
  dt = new Date();  
 
  s = dt.getDate();                 // days  
  s += "." + ( dt.getMonth() + 1 ); // months  
  s += "." + dt.getFullYear();      // years  
    
  return s;  
}  
 
// get current time in format "HH:MM:SS"  
function GetCurrentTime()  
{  
  var s ="";  
 
  dt = new Date();  
 
  s = dt.getHours();            // hours  
  s += ":" + dt.getMinutes();   // minutes  
  s += ":" + dt.getSeconds();   // seconds  
 
  return s;  
}  


 


Send feedback on this topic.
Copyright © 2004-2013, SAE - Automation, s.r.o. (Ltd.), All rights reserved.