Web Port exposes object and functions to access data in Web Port. This chapter describes how this data can be used in the script.
Tag data
In order to read and write tags from JavaScript, Web Port shares an object called tags. This object contains several functions and here are some examples of how these can be used.
Example
This example reads a tag value (AS01_GT31_PV) to an internal variable (copy) and then writes this value to two other tags
(AS02_GT31_PV and AS03_GT31_PV) in Web Port.
Script code:
tags["AS01_GT31_PV"].ReadValue(Moldeo.WebPortCommon.ReadTypes.PRIORITY);
var copy = tags["AS01_GT31_PV"].Value;
tags["AS02_GT31_PV"].WriteValue(copy, true);
tags["AS03_GT31_PV"].WriteValue(copy, true);
Trend data
Through the function getTrendData (string tag, DateTime from, DateTime to), Web Port returns a list of time stamps and trend values.
Example
This script retrieves trend data for heat consumption in Wh and calculates consumption in kWh per square meter. Consumption is calculated for the current and previous month and returned in csv format This example can be used to retrieve data from Web Port from an approved connection, for example, to be embedded in an external page. Data can also be used by other add-on modules in Web Port.
Script code:
// Key figures. heated m2
var nt_atemp = 44621;
// date current month, previous month
var = dt_im System.DateTime.Now;
var dt_fm = dt_im.AddMonths (-1);
// Temporary variables
var avg = 0;
var min = 0;
var max = 0;
// Calculate Heat current month
var heat_im = 0;
data = getTrendData('AS10_VMM_E', new System.DateTime
(dt_im.Year,dt_im.Month,1), dt_im);
if (data.Count > 0) {
for (i = 0; i < data.Count; i++) {
if (i == 0) {
min = data[i].Value;
max = data[i].Value;
}
if (min > data[i].Value)
min = data[i].Value;
if (max < data[i].Value)
max = data[i].Value;
}
heat_im = (max - min) * 1000 / nt_atemp;
}
// Calculate heat the previous month
var heat_fm = 0;
data = getTrendData('AS10_VMM_E', new System.DateTime(dt_fm.Year,dt_fm.Month,1),
new System.DateTime(dt_fm.Year,dt_fm.Month,
System.DateTime.DaysInMonth(dt_fm.Year,dt_fm.Month),23,59,59));
if (data.Count > 0) {
for (i = 0; i < data.Count; i++) {
if (i == 0) {
min = data[i].Value;
max = data[i].Value;
}
if (min > data[i].Value)
min = data[i].Value;
if (max < data[i].Value)
max = data[i].Value;
}
heat_fm = (max - min) * 1000 / nt_atemp;
}
// Return data according to CSV format
return 'State, Key figures, Previous month, Current month n' + 'Heat (kWh / m2),'
+ nt_heat.toFixed(2).toString ().
Replace (','. '.') + ',' + heat_fm.toFixed (2).toString ().
replace (',', '.') + ',' + heat_im.toFixed (2).toString ().
replace ('. ','. ') +' n ';
Alarm data
Through the function getActiveAlarmList (bool showBlocked), Web Port returns a list of all active alarms. If showBlocked is set to true, active alarms that are blocked are also returned.
Example
This example retrieves all active alarms and checks how many are active, how many are blocked and how many are unacknowledged. The result is then written to 3 tags in Web Port (ALARMSTATUS_PV1, ALARMSTATUS_PV2, ALARMSTATUS_PV3).
This can be used, for example, to tell a PLC how many active alarms are from different control systems and then indicate whether there are active alarms on a led the front of an appliance cabinet.
Script code:
var alarms = getActiveAlarmList(true);
var active = 0;
var blocked = 0;
var nack = 0;
for (i=0;i<alarms.Count;i++)
{
switch (alarms[i].State)
{
case Moldeo.WebPortCommon.AlarmStates.BLOCKED:
blocked++;
break;
case Moldeo.WebPortCommon.AlarmStates.ALARM_NACK:
active++;
nack++;
break;
case Moldeo.WebPortCommon.AlarmStates.ALARM:
active++;
break;
case Moldeo.WebPortCommon.AlarmStates.OK_NACK:
nack++;
break;
}
}
tags["ALARMSTATUS_PV1"].WriteValue(active, true);
tags["ALARMSTATUS_PV2"].WriteValue(nack, true);
tags["ALARMSTATUS_PV3"].WriteValue(blocked, true);
Schedule script
Schedule scripts can be used to read local schedule from control systems into Web Port Scheduling function. Contact Web Port support for examples.