跳转至

自定义代码#

codeexecutionsvg

此任务允许在系统中执行自定义代码,类似于使用DEE操作创建的代码抽象层,该代码层可用于聚合多个命令,而通过常规工作流编辑器创建这些命令可能更加复杂。

customcode_01

输入#

名称 数据类型 是否是触发器 描述
<custom list> <custom> 具有操作输入的对象
激活 any 在MES中执行操作

输出#

名称 数据类型 描述
<custom list> <custom> 操作的结果
成功 Boolean 如果没有问题,则发出true
错误 Error 处理此任务期间发生的错误

设置#

在“常规”选项卡上,您有常用设置和以下设置:

名称 数据类型 默认 描述
到期时间 Integer 10000 代码执行的时间窗口(以毫秒为单位)

customcode_02

输入选项卡上,可以进行以下设置:

名称 数据类型 默认 描述
名称 String 输入名称
易记名称 String 要显示的名称
类型 Long
Decimal
DateTime
Boolean
String
Integer
ReferenceType
...
输入的类型
集合类型 None
Array
Map
默认值 - 默认值

customcode_03

输出选项卡上,可以进行以下设置:

名称 数据类型 默认 描述
名称 String 输出名称
易记名称 String 要显示的名称
类型 Long
Decimal
DateTime
Boolean
String
Integer
ReferenceType
...
输出的类型
集合类型 None
Array
Map
默认值 - 默认值

customcode_04

代码选项卡上,有一个具有语法高亮和智能代码完成功能的编辑器,用户可以在其中编辑应在此任务中运行的代码。这是该任务的主要焦点所在,应该谨慎使用,因为它允许通过API调用访问MES本身,并会将值存储在数据库表中并执行重要值的日志记录。

customcode_05

下面是一个直接MES交互的示例,其中该任务会调用一个业务方法,通过使用EmptyContainer方法从容器中删除所有已填满的位置:

import { Framework } from 'framework';

export default class {
    /** Allows accessing external functions */
    public framework: Framework;
    constructor(framework: Framework) {
        this.framework = framework;
    }
    /*
        * Entry point of the class (IMPORTANT: don't change the signature of this method)
        * Should return an object containing the values for each output to emit
        * If necessary, use the parameter "outputs" to emit data while running the code.
    */
    public async main(inputs: any, outputs: any): Promise<any> {
        // Add code here
        this.framework.logger.warning(inputs.xx);
        const input = new this.framework.LBOS.Cmf.Navigo.BusinessOrchestration.ContainerManagement.InputObjects.EmptyContainerInput();
        input.Container = new this.framework.LBOS.Cmf.Navigo.BusinessObjects.Container();
        input.Container.Name = "Container001";
        input.IgnoreLastServiceId = true;
        const output = await this.framework.system.call(input);
        this.framework.logger.error(JSON.stringify(output));
        // emit output during execution: outputs.output1.emit("something");
        // return example: return { output1: inputs.input1, output2: "Hello World" };
    }
}

行为#

此任务执行用户在任务设置的Code部分中创建的(TypeScript)代码。此代码使用接收的输入执行,并发出代码中定义的所有输出。虽然与在MES中执行DEE操作类似,但在这种特殊情况下,用户可以完全控制块的输入和输出,并可为标量和引用类型添加配置选项。

必须定义主类,并且名称必须如下:

export class CustomCode

在代码中,执行层必须定义以下属性:

public api: Api = null;
public customApi: CustomApi = null;

之后,这些属性可以在必须具有以下签名的方法中使用:

public async main(inputs: any, outputs: any): Promise<any>

在主方法中,用户可以创建代码来访问多个命名空间。下面是可用API命名空间以及可使用的方法和类的列Table:

数据存储区#

对象 类型 描述
retrieve 方法 从数据存储区检索值
store 方法 将值存储到数据存储区

驱动程序#

对象 类型 描述
automationControllerDriverDefinition 属性 访问当前自动化控制器的驱动程序定义
connect 方法 连接到驱动程序
disconnect 方法 断开与驱动程序的连接
executeCommand 方法 执行命令
getProperties 方法 从驱动程序获取属性值
notifyRaw 方法 向驱动程序发送通知
sendRaw 方法 向驱动程序发送消息
setProperties 方法 设置驱动程序中的属性值

日志记录器#

对象 类型 描述
debug 方法 记录调试消息
error 方法 记录错误消息
info 方法 记录信息消息
warning 方法 记录警告消息

消息总线#

对象 类型 描述
publish 方法 将消息发送到消息总线
sendRequest 方法 将消息发送到消息总线并等待回复

系统#

对象 类型 描述
call 方法 调用MES系统中的可用API

实用程序#

对象 类型 描述
convertValueToType 方法 对给定的输入运行类型转换
sleep 方法 将系统暂停一段预定义的时间

备注#

此任务应谨慎使用,因为用户可能会无意中在工作流中引入额外的复杂性,然而通过使用更简单、更直接的任务便可以实现更简单的方法来执行特定的任务。由于可能的破坏性更改(方法签名更改、参数重命名等),当没有其他形式的自动化工作流时,应谨慎使用该任务。

Info

作为加快和简化开发的简单技巧,在任务中编辑代码时按CtrlS组合键可保存现有缓冲区。