Материал из for iRidium developers
Перейти к: навигация, поиск
Эта страница является переводом страницы JS Samples. Перевод выполнен на 100%.

Other languages:
English • ‎русский
The section is under development.

Examples of using iRidium JavaScript API with descriptions

Updated: 06.3.2017
Source language: JavaScript
Language version: 1.5
Specification: ECMAScript Edition 3
Report an error


Drivers (DDK, Drivers API)

Examples of creating and editing driver settings with the help of iRidium API


Custom TCP Server in iRidium Client

Custom Server on the basis of the AV & Custom Systems driver can take client connections on the basis of AV & Custom Systems and third-party applications. It can also process data sent by the client and send commands to it.

Remember, the server launched in the i3 pro application works only until the application is launched and the server cannot store the database as the iRidium Server specialized solution.

Use of "the server on a client" is very limited. It can be used only for synchronization of some data between control panels, when installation of iRidium Server is not possible for some reason.

// TCP server on port 5678
function start_tcp_server(server_name)
{
   // name, port, max connections
   var name = server_name;
   var port = 5678;
   var max_clients = 10;
   var tcp_device = IR.CreateDevice(IR.DEVICE_CUSTOM_SERVER_TCP, name, "", port, max_clients);
 
   // start server
   tcp_device.Connect();    
 
   // inform when the server online
   IR.AddListener(IR.EVENT_ONLINE, tcp_device, function(text)
   {
      IR.Log("Server is ONLINE");
   });
 
   // register connected client
   IR.AddListener(IR.EVENT_ACCEPT, tcp_device, function(id)
   {  
      IR.Log(tcp_device + " ACCEPT CLIENT "+id);
   });
 
   // process received data
   IR.AddListener(IR.EVENT_RECEIVE_TEXT, tcp_device, function(text)
   {
      //IR.Log("INPUT DATA: "+text);
      var strings = text.split('\r\n');
      var string;
      while(string = strings.shift())
      {
         var parts = string.split(":");  // parse the string with ":" devider
         switch(parts[0])
         {
            case "LOG":    // if the first substring of received data = "LOG"
            IR.GetItem("Page 1").GetItem("Item 1").GetState(0).Text = IR.GetVariable("System.Time.24")+" "+parts[1];
            IR.Log(IR.GetVariable("System.Time.24")+" "+parts[1]);
            break;
            case "ERROR":  // if the first substring = "ERROR"
            IR.GetItem("Page 1").GetItem("Item 2").GetState(0).Text = IR.GetVariable("System.Time.24")+" "+parts[1];
            IR.Log(IR.GetVariable("System.Time.24")+" "+parts[1]);
            break;
            case "DATABASE":  // if the first substring = "DATABASE"
            IR.Log(parts[1]);
            break;
         }
      }
   });
}
 
// start the server when project starts
start_tcp_server("Custom Server TCP");


Interface (GUI API)

Examples of controlling properties of pages, popups and graphic items with the help of iRidium API


Animation of Items with the Help of IR.Tween

The tweener specifies the variable value in the indicated segment calculated by some formula. It allows you to create animation with object movements on a particular segment of the indicated curve.

The tweener can be used for the gradual appearance of a popup from the screen side. Use the linear tweener for the popup to move straightforward.

function ShowTop() 
{
   TopTween("Popup 1", 820, 34, 250);  // popup, X, Y, moving range from Top
}
 
////////////////////////////////////////////////////////////////////////////////
// Creating Effects
////////////////////////////////////////////////////////////////////////////////
var timer = 0;
var speed = 20;    // showing speed
 
function TopTween(popup, X, Y, range)
{
   IR.ShowPopup(popup);
   IR.GetItem(popup).X = X;
   var Tm = IR.SetInterval(1, function()
   {          
      timer++;
      var Tween = IR.Tween("TWEEN_LINEAR", timer, 0, range, speed); 
      IR.GetItem(popup).Y = Y+Tween; 
      if (timer > speed) IR.ClearInterval(Tm); timer = 0;
   });
}

download:

sample of using


Font Control

You can set the font or change the font size for the item via the .FontId state property.

FontId - the font identifier. It stores the pointer to the font and the font size. You cannot change the font size separately.

Create a new font and assign it to the item state via FontId. For creation of new fonts it is important to use the identifiers which have not been used in the project (the editor creates its own system fonts using identifiers beginning with 1). Start the numeration of user fonts with FontId = 1000.

Indicate which font file must be used as a basis, what size must be set up. Assign a font identifier not used in the project before:

// item state
var itemState = IR.GetItem("Page 1").GetItem("Item 1").GetState(0); 
// new font IDs have to start from 1000 to make sure that u not rewrite a system fonts (starts from 1)
var fontCount = 1000;  
// create the font with new ID to make sure that you not rewrite some user font
function prepareNewFont(name, size)
{
     ++fontCount;
     IR.CreateFont(fontCount, name, size);
     IR.Log("The font has been created:" + name + " " + size + " " + fontCount);                 
     return fontCount;
}
 
//set the created fornt on item  (you can also set the font by ID, like itemState.FontId = 1001;)
itemState.FontId = prepareNewFont("segoeuil.ttf", 48);	
 
// this font: "segoeuil.ttf" has to be added in you GUI before you use it in script
// see the name of Font file in GUI Editor > OBJECT PROPERTIES > States > State 1(2) > Font: [...]
// In the Font editor you can see the Font name and the name of font File (use the File name in script)


Notifications (Push API)

Sending of Push notifications to control panels. The notifications are sent by the server (there are no any other possibility to do it in v1.0.10).


1 "'Create groups of control panels"'

By default, none of control panels will receive Push notifications until you place it in the Group of panels that can get Push notifications.

The groups are set up on the web site, see the instructions.

Each group has an ID and name. That is the group ID you will use when indicating which panels have to get notifications.


2 "'Set up the script for sending notifications from the server"'

In the server project create a new script file. Add the condition for notification sending and the command for sending Push notifications in it. The condition for sending Push notifications can be change of any server variable - associated with the driver or a virtual one.

Example: send a notification when channel "Address 1" of the "KNX" driver takes value 1. The notification will be received by the group panels with ID = 1.

function sendPush (text, group) 
{
   IR.SendPush(text, "", group);  
   // text - message that will be displayed in notification
   // group - group of Panels seted up in Cloud Object properties on iRidium Mobile web site
}
 
IR.AddListener(IR.EVENT_TAG_CHANGE, IR.GetDevice("KNX"), function(name, value)
{
   if (name == "Address 1" && value == 1) 
   {
      sendPush("Leakage in the kitchen! The water supply is turned off", 1); 
   } 
});

No additional settings and scripts are required on the control panel with i3 pro. The panel will get notifications if it is in the specified group.