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

Example of Log Journal

iRidium server allows to store data about device statuses in the database. Users often want to see in their control interface the journal of events that happened in the house this day (week, month). This example shows how to create a log journal in a panel project.
To create a log jounal, you have to change a server project and a panel project,
Changes in the server project
1Set recording of tag values in the database

ServerProjectTags.png

2Add 2 virtual channels to the project: Day and Month
3Add a script to the project

var LIMIT_TO_RECORD = 300;  //The maximum number of records
/**
 * The funtion of getting values from the database
 * @param {string} Full tag name
 * @param {string} Tag value
 */
function makeDataFromDatabase(in_TagName, in_Value) {
   var Day = new iDate(); // Get iDate object from the current date and time
   var l_oSql = IR.GetDatabase();  
   var l_oDate;
   var l_sTagID;
   var l_sTagName;
   var l_nGetDate;
   var l_nValue;
   var l_sMakeDate;
 
 
 
   if (in_Value == "day") {
      // Create iDate object from the last date
      l_oDate = new iDate(Day.year, Day.month, (Day.date - 1), Day.hours, Day.minutes, Day.seconds, Day.ms);
   }
   else if (in_Value == "month") {
      // Create iDate object from the last month
      l_oDate = new iDate(Day.year, (Day.month - 1), Day.date, Day.hours, Day.minutes, Day.seconds, Day.ms); 
   }
   // Request the database, select all events with the indicated date
   var l_oDataByDate = l_oSql.Request('SELECT TAG_ID, DATETIME, VALUE FROM INTEGER_TAG_HISTORY WHERE DATETIME > '+l_oDate.value+' ORDER BY DATETIME DESC'); 
   if (l_oDataByDate.Rows) {
      for (var j = 0; j < LIMIT_TO_RECORD; j++) {
         l_sTagID = l_oDataByDate.GetRowValue(0,j); //Get tag ID
         l_nGetDate = l_oDataByDate.GetRowValue(1,j); //Get the time when the tag value was changed
         l_nValue = l_oDataByDate.GetRowValue(2,j);  //Get tag value
 
         // Create iDate object from the time when the tag value was changed.
         var date = new iDate(l_nGetDate);
         //Create a string with the date and time
         l_sMakeDate = '';
         if (date.date < 10)
            l_sMakeDate += "0" + date.date + ".";
         else l_sMakeDate += date.date + ".";
         if (date.month < 10)
            l_sMakeDate += "0" + date.month + "." + date.year + " [";
         else l_sMakeDate += date.month + "." + date.year + " [";  
         if (date.hours < 10)
            l_sMakeDate += "0" + date.hours + ":";   
         else l_sMakeDate += date.hours + ":";   
         if (date.minutes < 10)
            l_sMakeDate += "0" + date.minutes + ":"; 
         else l_sMakeDate += date.minutes + ":";   
         if (date.seconds < 10)
            l_sMakeDate += "0" + date.seconds + "] ";
         else l_sMakeDate += date.seconds + "] ";
 
         // Ask the tag name from the database by its ID
         var l_oDataByTagID = l_oSql.Request('SELECT NAME, TAG_ID FROM TAGS_PASSPORT WHERE TAG_ID = '+l_sTagID); 
         var l_oDataByTagIDRows = l_oDataByTagID.Rows;
         for (var k = 0; k < l_oDataByTagID.Rows; k++) {  //Add the tag name and its value to the string
            l_sTagName = l_oDataByTagID.GetRowValue(0,k);
            l_sMakeDate += l_sTagName + " is " + l_nValue;  
            IR.SetVariable("Server.Tags.Data", l_sMakeDate);  //Write value to the server tag
         }
      }
      l_oSql.Close()  
   }     
}
IR.SetGlobalListener(IR.EVENT_GLOBAL_TAG_CHANGE, function(in_sName, in_sValue)
{
   if (in_sValue == "day" || in_sValue == "month")  
      //Call a function to generate data from the database
      makeDataFromDatabase(in_sName, in_sValue);
});
 
// Subscribe to tags
IR.SubscribeTagChange("Server.Channels.Month");
IR.SubscribeTagChange("Server.Channels.Day");

4Then open a panel project, add to it a List item where a log journal will be displayed. Create buttons to diplay events for a day and for a month

PanelProjectJournal.png

4Add a script

IR.AddListener(IR.EVENT_START,0,function()
{  
   var l_sAllData = '';
   var l_bTest = false;
   var l_oInterval = null;
   var l_aNewData;
   var INTERVAL_TO_REFRESH = 60000;  //value of the data update interval
 
   //Interval of automatic data update
   IR.SetInterval(INTERVAL_TO_REFRESH, function(){
      IR.GetDevice("iRidium Server").Set("Day", "day");   
   });
   IR.SetTimeout(500, function () {
      IR.GetDevice("iRidium Server").Set("Day", "day");     
   });
   //Listener to change modes day\month
   IR.AddListener(IR.EVENT_CHANNEL_SET, IR.GetDevice("iRidium Server"), function (in_sName, in_sValue) {
      if (in_sName == "Day" || in_sName == "Month") {
         l_bTest = true;  
      } 
   });
 
   IR.AddListener(IR.EVENT_TAG_CHANGE, IR.GetDevice("iRidium Server"), function (in_sName, in_sValue) {      
      if (in_sName == "Data" && l_bTest) {
         IR.ClearInterval(l_oInterval);
         l_sAllData += (in_sValue + "\n");  
         IR.GetPage("Page 1").GetItem("Item 1").Clear();
         l_oInterval = IR.SetTimeout(1000, function () {  
            l_aNewData = l_sAllData.split("\n");  //Parse the string by "\n" separator
            for (var i = 0; i < l_aNewData.length - 1; i++) {
               IR.GetPage("Page 1").GetItem("Item 1").CreateItem(i, 1, {Text: l_aNewData[i]});   //fill in the list
            }
            l_bTest = false; 
            l_sAllData = '';
            l_aNewData = [];            
         })             
      }   
   });    
});


Download an example of a server project
Download an example of a panel project