Skip to content

Commit

Permalink
feat: add system functions $getAlarms and $ackAlarm to use in script #…
Browse files Browse the repository at this point in the history
  • Loading branch information
unocelli committed Dec 1, 2024
1 parent 96ade97 commit cff90fa
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 21 deletions.
22 changes: 20 additions & 2 deletions client/src/app/_models/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,16 @@ export class SystemFunctions {
mode: null,
text: 'script.sys-fnc-setview-text',
tooltip: 'script.sys-fnc-setview-tooltip',
params: [false]
params: [false],
paramsText: 'script.sys-fnc-setview-params'
},
{
name: '$openCard',
mode: null,
text: 'script.sys-fnc-opencard-text',
tooltip: 'script.sys-fnc-opencard-tooltip',
params: [false]
params: [false],
paramsText: 'script.sys-fnc-opencard-params'
},
{
name: '$enableDevice',
Expand Down Expand Up @@ -198,6 +200,22 @@ export class SystemFunctions {
tooltip: 'script.sys-fnc-sendMessage-tooltip',
params: [false, false, false],
paramsText: 'script.sys-fnc-sendMessage-params'
},
{
name: '$getAlarms',
mode: null,
text: 'script.sys-fnc-getAlarms-text',
tooltip: 'script.sys-fnc-getAlarms-tooltip',
params: [],
paramsText: 'script.sys-fnc-getAlarms-params'
},
{
name: '$ackAlarm',
mode: null,
text: 'script.sys-fnc-ackAlarms-text',
tooltip: 'script.sys-fnc-ackAlarms-tooltip',
params: [false, false],
paramsText: 'script.sys-fnc-ackAlarms-params'
}
];
}
Expand Down
11 changes: 11 additions & 0 deletions client/src/app/_services/script.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { HmiService, ScriptCommandEnum, ScriptCommandMessage } from './hmi.servi
import { Utils } from '../_helpers/utils';
import { DeviceType, TagDaq, TagDevice } from '../_models/device';
import { DaqQuery } from '../_models/hmi';
import { AlarmsType } from '../_models/alarm';

@Injectable({
providedIn: 'root'
Expand Down Expand Up @@ -97,6 +98,8 @@ export class ScriptService {
code = code.replace(/\$runServerScript\(/g, 'this.$runServerScript(');
code = code.replace(/\$getHistoricalTags\(/g, 'this.$getHistoricalTags(');
code = code.replace(/\$sendMessage\(/g, 'this.$sendMessage(');
code = code.replace(/\$getAlarms\(/g, 'await this.$getAlarms(');
code = code.replace(/\$ackAlarm\(/g, 'await this.$ackAlarm(');
return code;
}

Expand Down Expand Up @@ -175,4 +178,12 @@ export class ScriptService {
public async $sendMessage(to: string, subject: string, message: string) {
return await this.projectService.runSysFunctionSync('$sendMessage', [to, subject, message]);
}

public async $getAlarms() {
return await this.projectService.runSysFunctionSync('$getAlarms', null);
}

public async $ackAlarm(alarmName: string, types?: AlarmsType[]) {
return await this.projectService.runSysFunctionSync('$ackAlarm', [alarmName, types]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,11 @@ export class ScriptEditorComponent implements OnInit, OnDestroy {
}

private getFunctionText(sysfnc: SystemFunction): string {
let paramText = '\'MainView\'';
let paramText = '\'params\'';
const fx = this.systemFunctions.functions.find(sf => sf.name === sysfnc.name);
if (fx && fx.paramsText) {
if (!fx?.params?.length) {
paramText = '';
} else if (fx?.paramsText) {
paramText = this.translateService.instant(fx.paramsText) || paramText;
}
return `${sysfnc.name}(${paramText});`;
Expand Down
8 changes: 8 additions & 0 deletions client/src/assets/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1237,8 +1237,10 @@
"script.delay": "Delay (seconds)",
"script.sys-fnc-setview-text": "$setView (View name)",
"script.sys-fnc-setview-tooltip": "System function to set View on client: $setView (View name as string)",
"script.sys-fnc-setview-params": "'View name'",
"script.sys-fnc-opencard-text": "$openCard (View name)",
"script.sys-fnc-opencard-tooltip": "System function to open Card on client: $openCard (View name as string, Options as dict {left, top})",
"script.sys-fnc-opencard-params": "'View name'",
"script.sys-fnc-enableDevice-text": "$enableDevice (Device name, enable True/False)",
"script.sys-fnc-enableDevice-tooltip": "System function to enable Device connection: $enableDevice (Device name as string, enable as boolean)",
"script.sys-fnc-enableDevice-params": "'Device name', true",
Expand Down Expand Up @@ -1272,6 +1274,12 @@
"script.sys-fnc-sendMessage-text": "$sendMessage(address, subject, message)",
"script.sys-fnc-sendMessage-tooltip": "System function to send Message (Mail): $sendMessage(address as string, subject as string, message as string)",
"script.sys-fnc-sendMessage-params": "'address', 'subject', 'message'",
"script.sys-fnc-getAlarms-text": "$getAlarms()",
"script.sys-fnc-getAlarms-tooltip": "System function to get alarms list (): $getAlarms()",
"script.sys-fnc-getAlarms-params": "",
"script.sys-fnc-ackAlarms-text": "$ackAlarm(Alarm name, types)",
"script.sys-fnc-ackAlarms-tooltip": "System function to ACK alarm (): $ackAlarm(Alarm name as string, [types] 'highhigh|high|low')",
"script.sys-fnc-ackAlarms-params": "'Alarm name', 'types'",

"script.template-chart-data-text": "Customized chart data",
"script.template-chart-data-tooltip": "Code template of customized chart data to return",
Expand Down
10 changes: 6 additions & 4 deletions server/api/scripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module.exports = {

/**
* POST runscript
* Run script, can be call with script id or script content as test
* Run script, can be call with script id or script content as test
*/
scriptsApp.post("/api/runscript", secureFnc, function (req, res, next) {
var groups = checkGroupsFnc(req);
Expand All @@ -38,6 +38,7 @@ module.exports = {
res.status(401).json({ error: "unauthorized_error", message: "Unauthorized!" });
runtime.logger.error("api post runscript: Unauthorized");
} else {
//req.body.params.script.parameters.permission = groups;
runtime.scriptsMgr.runScript(req.body.params.script).then(function (result) {
res.json(result);
}).catch(function (err) {
Expand Down Expand Up @@ -65,7 +66,8 @@ module.exports = {
} else {
try {
if (runtime.scriptsMgr.sysFunctionExist(req.body.params.functionName)) {
const result = runtime.scriptsMgr.runSysFunction(req.body.params.functionName, req.body.params.parameters);
//req.body.params.parameters.permission = groups;
const result = await runtime.scriptsMgr.runSysFunction(req.body.params.functionName, req.body.params.parameters);
res.json(result);
} else {
res.status(400).json({ error: "not_found", message: 'script not found!'});
Expand All @@ -74,8 +76,8 @@ module.exports = {
} catch (error) {
res.status(400).json({ error: "error", message: error});
runtime.logger.error("api post runSysFunction: " + error);
}
}
}
}
});
return scriptsApp;
}
Expand Down
24 changes: 16 additions & 8 deletions server/runtime/alarms/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var utils = require('./../utils');

var ALARMS_CHECK_STATUS_INTERVAL = 1000;
var TimeMultiplier = 1000; //1000 = rates are in seconds - alpaslanske
const SEPARATOR = '^~^';

function AlarmsManager(_runtime) {
var runtime = _runtime;
Expand All @@ -25,6 +26,7 @@ function AlarmsManager(_runtime) {
var clearAlarms = false; // Flag to clear current alarms from DB
var actionsProperty = {}; // Actions property list, key = alarm name + ^~^ + type


/**
* Start TimerInterval to check Alarms
*/
Expand Down Expand Up @@ -242,6 +244,10 @@ function AlarmsManager(_runtime) {
});
}

this.getIdSeparator = () => {
return SEPARATOR;
}

/**
* Check the Alarms state machine
*/
Expand Down Expand Up @@ -505,14 +511,16 @@ function AlarmsManager(_runtime) {

var _filterAlarm = function (alarmType, alarmText, alarmGroup, alarmTagId, filter) {
var available = true;
if (filter.priority.length && filter.priority.indexOf(alarmType) === -1) {
available = false;
} else if (filter.text && (!alarmText || alarmText.toLowerCase().indexOf(filter.text.toLowerCase()) === -1)) {
available = false;
} else if (filter.group && (!alarmGroup || alarmGroup.toLowerCase().indexOf(filter.group.toLowerCase()) === -1)) {
available = false;
} else if (filter.tagIds.length && alarmTagId && filter.tagIds.indexOf(alarmTagId) === -1) {
available = false;
if (filter) {
if (filter.priority && filter.priority.length && filter.priority.indexOf(alarmType) === -1) {
available = false;
} else if (filter.text && (!alarmText || alarmText.toLowerCase().indexOf(filter.text.toLowerCase()) === -1)) {
available = false;
} else if (filter.group && (!alarmGroup || alarmGroup.toLowerCase().indexOf(filter.group.toLowerCase()) === -1)) {
available = false;
} else if (filter.tagIds && filter.tagIds.length && alarmTagId && filter.tagIds.indexOf(alarmTagId) === -1) {
available = false;
}
}
return available;
}
Expand Down
20 changes: 20 additions & 0 deletions server/runtime/scripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ function ScriptsManager(_runtime) {
sysFncs['$setDeviceProperty'] = runtime.devices.setDeviceProperty;
sysFncs['$getHistoricalTags'] = runtime.devices.getHistoricalTags;
sysFncs['$sendMessage'] = _sendMessage;
sysFncs['$getAlarms'] = _getAlarms;
sysFncs['$ackAlarm'] = _ackAlarm;

return sysFncs;
}
Expand All @@ -243,6 +245,24 @@ function ScriptsManager(_runtime) {
var temp = await runtime.notificatorMgr.sendMailMessage(null, address, subject, message, null, null);
return temp;
}

var _getAlarms = async function () {
return await runtime.alarmsMgr.getAlarmsValues(null, -1);
}

var _ackAlarm = async function (alarmName, types) {
const separator = runtime.alarmsMgr.getIdSeparator();
if (alarmName.indexOf(separator) === -1 && !utils.isNullOrUndefined(types)) {
var result = [];
for(var i = 0; i < types.length; i++) {
const alarmId = `${alarmName}${separator}${types[i]}`;
result.push(await runtime.alarmsMgr.setAlarmAck(alarmId, null, -1));
}
return result;
} else {
return await runtime.alarmsMgr.setAlarmAck(alarmName, null, -1);
}
}
}

module.exports = {
Expand Down
10 changes: 5 additions & 5 deletions server/runtime/scripts/msm.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,21 @@ function MyScriptsModule(_events, _logger) {

var result = _scriptsToModule(tempScripts, eventsIncludes);
if (result.module) {
var paramsValue = _script.parameters.map(p => utils.isNullOrUndefined(p.value) ? p : p.value);
var paramValues = _script.parameters.map(p => utils.isNullOrUndefined(p.value) ? p : p.value);
result.module[initEvents.name](events, _script.outputId);
return result.module[_script.name](...paramsValue);
return result.module[_script.name](...paramValues);
}
throw new Error(result);
}

this.runScript = function (_script) {
if (scriptsModule) {
var paramsValue = _script.parameters.map(p => utils.isNullOrUndefined(p.value) ? p : p.value);
var paramValues = _script.parameters.map(p => utils.isNullOrUndefined(p.value) ? p : p.value);
if (!_script.name) {
_script = Object.values(scriptsMap).find(s => s.id === _script.id);
}
try {
return scriptsModule[_script.name](...paramsValue);
return scriptsModule[_script.name](...paramValues);
} catch (err) {
console.error(err);
return err;
Expand All @@ -85,7 +85,7 @@ function MyScriptsModule(_events, _logger) {
}

this.runSysFunction = function (functionName, params) {
return global[functionName](...params);
return global[functionName](...params || []);
}

this.getScript = function (_script) {
Expand Down

0 comments on commit cff90fa

Please sign in to comment.