// used to append interfaces to omnicore sdk so that created app and AppStudio can use
import API from './ecosystem-base.js';
import {Logger} from './../function/log-helper.js';
import {InfoType, WarningType} from '../information/informationCode.js';
import {ErrorCode} from '../exception/exceptionDesc.js';
const factoryApiVariableMonitor = function (es) {
const logModule = 'ecosystem-variable';
/**
* The API.VARIABLEMONITOR namespace provides a set of interfaces for easily and quickly adding RAPID variable monitoring and processing transactions
* @alias API.VARIABLEMONITOR
* @namespace
*/
es.VARIABLEMONITOR = new (function () {
const subscribeRes = async function (variable, func) {
if (es.isSubscriptionBlocked) {
Logger.w(
WarningType.RWSSubscriptionBlocked,
'API.VARIABLEMONITOR: Subscription disabled when trying to monitor variable',
);
return;
}
variable.addCallbackOnChanged(func);
await variable.subscribe();
};
/**
* Subscribes to a RAPID variable.
* @alias monitorVariable
* @memberof API.VARIABLEMONITOR
* @param {object} [variableObj] The variable attributes including the variable name and other necessary information.
* @param {function} [callback] The callback function that is called when the variable changes.
*
* @example
* API.VARIABLEMONITOR.monitorVariable(
* {type: 'tooldata', task: 'task1', module: 'module1', name: 'Wobj_1'},
* (v)=>{
* console.log(v);
* }
* )
*/
this.monitorVariable = async function (
variableObj = {type: 'tooldata', task: 'task1', module: 'module1', name: 'Wobj_1'},
callback = null,
) {
const resourceName = `${variableObj.type}-${variableObj.task}-${variableObj.module}-${variableObj.name}`;
if (es.isFetchControllerBlocked) {
Logger.w(
WarningType.RWSRequestBlocked,
`API.VARIABLEMONITOR: HttpRequest disabled when trying to monitor variable ${resourceName}`,
);
return;
}
//read the state the first time
try {
var variableData = await RWS.Rapid.getData(variableObj.task, variableObj.module, variableObj.name);
typeof callback === 'function' && callback(await variableData.getRawValue());
} catch (e) {
return API.rejectWithStatus(
`Failed to read variable ${variableObj.name}'s value before subscription.`,
e,
ErrorCode.FailedToGetVariableValue,
logModule,
);
}
if (es.isSubscriptionBlocked) {
Logger.w(
WarningType.RWSSubscriptionBlocked,
`API.VARIABLEMONITOR: Subscription disabled when trying to monitor variable ${resourceName}`,
);
return;
}
if (this[resourceName] === undefined) {
try {
//Remind:这一块是为了修复,初始化时候一个信号被订阅了多次的bug,因为没有赋值,会在初始化的时候重复订阅
this[resourceName] = 'initialize value';
const cbVariable = function (data) {
Logger.i(logModule, `Variable value: ${data}`);
this[resourceName] = data || '';
es._events.trigger(resourceName, this[resourceName]);
Logger.i(logModule, `Triggered variable value:${this[resourceName]}`);
};
Logger.i(logModule, `Subscribed variable: ${resourceName}`);
subscribeRes(variableData, cbVariable.bind(this));
} catch (e) {
return API.rejectWithStatus(
`Subscription to ${variableObj.name} failed.`,
e,
ErrorCode.FailedToSubscribeVariable,
logModule,
);
}
}
es._events.on(resourceName, callback);
};
})();
es.constructedVariableMonitor = true;
};
if (typeof API.constructedVariableMonitor === 'undefined') {
factoryApiVariableMonitor(API);
}
export default API;
export {factoryApiVariableMonitor};