Эта страница является
переводом страницы
Demo schedule edit. Перевод выполнен на 100%.
Editing Schedule on a Control Panel
Schedules are stored in the server memory. If it is required to change schedule parameters on the screen of a smart phone or a tablet, iRidium platform has a solution.
Description of Demo-project
The followig is displayed on the screen of a panel:
- Event list - a list of events,
- Name - a field to display the name of an event,
- Enabled - a state indicator and a switch,
- Start and Finish - fields to display the time and date of event start and event finish,
- Enter new date-time - fields to eneter new date and time,
- Update - a button.
Event list contains all events from the server schedule. A user can choose any event from the list and data of this event will appear in the fields to the right of the list.
A user enters new data in the Enter new date-time fields and selects the event state (active/inactive) with the help of Enabled switch and then presses Update button. After it new data are sent to the server, the schedules are updates and if the update is successful, the information about the schedule returns to the panel.
The archive of the demo-project contains two ready projects:
- a project for the server (Schedule_edit.sirpz file)
- a project for a panel (Schedule_edit.irpz file).
Setting a Server Project
1 Add EventData_to_server virtual channel and EventData_from_server virtual tag.
2 Adding an event to a schedule:
3 Add the script:
var schedule = IR.GetScheduler("Schedule 1");
// creating a string with schedule data
var EventChanger = function() {
// writing schedule data in virtual tag
this.Write = function(in_events) {
var EventBuff = [];
for(var i in in_events) {
EventBuff.push({
"name": in_events[i].Name,
"end": in_events[i].End,
"start": in_events[i].Start,
"enable": in_events[i].enable
});
}
IR.SetVariable("Server.Tags.EventData_from_server", JSON.Stringify(EventBuff));
}
// reading schedule data in virtual tag
this.Read = function(in_schedule, in_NewData) {
var data = JSON.Parse(in_NewData);
var newData = JSON.Parse(data.join());
EventEdit = in_schedule.GetEvent(newData.name);
EventEdit.Start = newData.start;
EventEdit.End = newData.end;
EventEdit.Enabled = newData.enable;
EventEdit.Done(); // event update */
}
}
var EventsObj = new EventChanger();
// getting schedule data at server start
IR.AddListener(IR.EVENT_START,0,function() {
schedule = IR.GetScheduler("Schedule 1");
EventsObj.Write(schedule.GetEvents());
});
// getting new data from panel
IR.SetGlobalListener(IR.EVENT_GLOBAL_TAG_CHANGE, function(in_sName, in_sValue) {
var newData = in_sValue.split(',');
EventsObj.Read(schedule, newData);
EventsObj.Write(schedule.GetEvents());
});
// subscribing for tag changes
IR.SubscribeTagChange("Server.Channels.EventData_to_server");
You'll find a detailed description of JS methods to work with server routines and schedules here.
4 Save a project.
Setting a Panel Project
1 Add iRidium Server driver. Select the saved file of a server project in Source Project, and enter the server IP-address in the Host parameter.
2 Press Sync With Server Project button.
3 Add graphical items:
4 Add the script:
var Server = IR.GetDevice("iRidium Server");
var ListBox = IR.GetItem("Page 1").GetItem("ListBox");
var EventNameField = IR.GetItem("Page 1").GetItem("EventName");
var EventStartField = IR.GetItem("Page 1").GetItem("EventStart");
var EventFinishField = IR.GetItem("Page 1").GetItem("EventFinish");
var EventStartFieldEdit = IR.GetItem("Page 1").GetItem("EventStartEdit");
var EventFinishFieldEdit = IR.GetItem("Page 1").GetItem("EventFinishEdit");
var UpdateButton = IR.GetItem("Page 1").GetItem("UpdateBtn");
var Lamp = IR.GetItem("Page 1").GetItem("Lamp");
var Switch = IR.GetItem("Page 1").GetItem("Switch");
var EventsObj;
// processing incoming and outgoing values for the server
var EventChanger = function(in_list, in_server) {
this.list = in_list; // link to GUI list with items and data
// read event data from virtual tag in JSON format and fill the list with items
this.Read = function(in_ChannelName, in_Value) {
if (in_ChannelName == "EventData_from_server") {
this.events = JSON.Parse(in_Value);
this.list.Clear();
for (var event in this.events) {
this.list.AddItem(null, {
"Item 1":
{
Text: this.events[event].name
}
});
}
fill(0);
}
}
// write new date-times of event in virtual tag
this.Write = function(eventName, startNewTime, finishNewTime, enableEvent) {
var newEventsData = {
"name": eventName,
"start": startNewTime,
"end": finishNewTime,
"enable": enableEvent
};
Server.Set("EventData_to_server", JSON.Stringify(newEventsData));
}
// populating the first item in the list
IR.AddListener(IR.EVENT_ITEM_SELECT, ListBox, function(item, subitem) {
fill(item);
}, this);
IR.AddListener(IR.EVENT_TAG_CHANGE, in_server, this.Read, this);
}
IR.AddListener(IR.EVENT_START,0,function() {
EventsObj = new EventChanger(ListBox, Server);
});
// filling fields with data
function fill(index) {
var event = EventsObj.events[index];
var startTime = new Date(event.start * 1000);
var finishTime = new Date(event.end * 1000);
var enableState = event.enable;
EventNameField.Text = event.name;
EventStartField.Text = startTime;
EventFinishField.Text = finishTime;
EventStartFieldEdit.Text = startTime;
EventFinishFieldEdit.Text = finishTime;
Lamp.Value = enableState;
Switch.Value = enableState;
}
// sending new data to server
IR.AddListener(IR.EVENT_ITEM_PRESS, UpdateButton, function() {
var eventName = EventNameField.Text;
var startNewTime = Math.floor(new Date(EventStartFieldEdit.Text) / 1000);
var finishNewTime = Math.floor(new Date(EventFinishFieldEdit.Text) / 1000);
var enableEvent = Switch.Value;
if (startNewTime > 0 && finishNewTime > 0)
EventsObj.Write(eventName, startNewTime, finishNewTime, enableEvent);
else
IR.Log("invalid date")
});
Testing the System
You can start server and panel projects on a regular PC. The server project can be uploaded to iRidium server for Windows with the help of iRidium transfer. The panel project is launched in the Emulator.