高级配置和集成 > 将资产连接到 Edge MicroServer (EMS)
将资产连接到 Edge MicroServer (EMS)
Edge MicroServer (EMS) 是安装在远程设备上的独立应用程序。它可在设备和 ThingWorx 主机之间建立实时在线的双向通信。
本部分介绍如何使用 Edge MicroServer (EMS) 和 EMS 所绑定的 Lua 脚本资源将 ThingWorx 中的资产属性连接到设备上。
有关使用 Edge SDK 连接资产属性的详细信息,请参阅 ThingWorx Edge Java SDK 帮助中心
先决条件
请确保具备以下条件:
Edge MicroServer (EMS) 和绑定的 Lua 脚本资源 (LSR)。
ThingWorx Composer 8.2.0 或更高版本且已安装 ThingWorx Apps
配置 ThingWorx
1. ThingWorx Composer 中,创建要在 EMS 中使用的应用程序密钥。
a. “安全” > “应用程序密钥”中,单击“新建”
b. 请输入相应值:
名称 - 在配置 EMS 时,需要此值。
用户名引用 - 选择将使用 EMS 的用户。例如,Administrator
到期日期 - 根据公司策略为应用程序密钥设置相应的到期日期。如果保留空白,则默认为一天。
c. 单击“保存”
2. ThingWorx 中创建想要连接到远程设备的资产(事物)。使用以下任一基本事物模板或任何可实施以下事物模板的事物模板:
RemoteThing
RemoteThingWithFileTransfer
RemoteThingWithTunnels
RemoteThingWithTunnelsAndFileTransfer
PTC.ISA95.PhysicalAssetBaseThingTemplate
无论使用何种基本事物模板,资产都必须实施 RemoteAccessible 事物形态。
最佳做法是在事物模板上实施事物形态,而不是在单个资产上实施事物形态。
配置 EMS
2. 导航到以下文件:<EMS 安装位置>/microserver/etc/config.json
3. 编辑 config.json 文件,使得 hostportappKey 均指向您的 ThingWorx。您可以使用以下示例,或 config.json.minimal 文件。
{
"ws_servers": [{
"host": "localhost",
"port": 8080
}],
"appKey": "84fbd93b-1ca5-4057-be39-5a2b3bd1cf9e",
"ws_connection":{
"encryption":"none"
"http_server" : {
"host": "10.232.247.58",
"port": 8000,
"ssl" : false,
"certificate": "<证书文件的绝对路径>",
"private_key": "<私钥的绝对路径>",
"passphrase": "password"
"authenticate": false,
"user": "johnsmith",
"password": "DP:AQABAKs1GFoUEW1XfDlwab4J4huKthm9GP3seM3PEjOMVoyFD3es2ZDtN2ngA99oLkcODQ",
"content_read_timeout": 20000,
"ports_to_try": 10,
"max_clients": 15,
"enable_csrf_tokens": true,
"csrf_token_rotation_period": 10
},

"logger":{
"level":"WARN",
"publish_directory":"<\MicroServer\edge\logs 的绝对路径>",
"publish_level":"INFO",
"max_file_storage":2000000,
"auto_flush":true
},

"certificates":{
"validate":false,
"allow_self_signed":true
},
"file":{
"buffer_size":32400,
"virtual_dirs":[{
"in":"<\MicroServer\edge\in 的绝对路径>"
}, {
"out":"<\MicroServer\edge\out 的绝对路径>"
},{
"staging":"<\MicroServer\edge\staging 的绝对路径>"
}],
"staging_dir":"staging"
}
}
4. 保存修改后的 config.json 文件。
5. <EMS 安装位置>/microserver/wsems.exe 文件作为选项运行,或者作为服务安装。
配置 Lua 脚本资源 (LSR)
1. <EMS 安装位置>/microserver/etc 目录中查找 config.lua.example 文件。
2. 在文件中找到 Connection parameters 部分,然后根据 EMS 设置对其进行修改。例如:
--
-- Connection parameters
--
scripts.rap_host = "EMS IP Address"
scripts.rap_port = "EMS Port"
scripts.rap_ssl = true
scripts.rap_userid = "EMS HTTP Server User ID"
scripts.rap_password = "password"
scripts.rap_server_authenticate = true
scripts.rap_cert_file = "Path to CA cert file."
scripts.rap_deny_selfsigned = true
scripts.rap_validate = true
3. 找到 HTTP Server Configuration 部分,对其进行配置以用于 Lua 脚本资源。例如:
--
-- HTTP Server Configuration
--
scripts.script_resource_host = "localhost"
scripts.script_resource_port = "8001"
scripts.script_resource_ssl = "false"
scripts.script_resource_certificate_chain = "/path/to/lsr_http_server_certificate_chain/file"
scripts.script_resource_private_key = "/path/to/private/key"
scripts.script_resource_passphrase = "password"
scripts.script_resource_authenticate = "false"
scripts.script_resource_userid = "johnsmith"
scripts.script_resource_password = "AES:EncryptedPassword"
scripts.script_resource_enable_csrf_tokens = true
scripts.script_resource_csrf_token_rotation_period = 10
scripts.script_resource_enable_rest_services = false
4. 确定要与这些属性进行绑定的资产。在 config.lua.example 文件中找到以下部分:
scripts.EdgeThing = {
file = "thing.lua",
template = "example",
}
EdgeThing 替换为 ThingWorx 中的实际资产“名称”值。
或者,您也可以在此处指定 identifier 属性。稍后在此过程中运行 Lua 脚本之后,系统会在 ThingWorx 中创建标识符,随后您可以“常规信息”页面上的“标识符”中对 ThingWorx Composer 中的资产选择该标识符。
* 
远程事物可以通过名称或标识符绑定,但无法同时通过二者绑定。如果通过名称绑定的远程事物也具有标识符,请对未正常工作的事物执行远程服务或提取远程属性。
要通过名称绑定远程事物,请勿指定标识符。如果已指定标识符,请将其移除。
template 属性代表的脚本用于标识您想要从设备发送到资产的信息,例如远程属性或远程服务。在上述代码示例中,示例文件可在以下位置找到:<EMS 安装位置>/microserver/etc/custom/templates/example.luaexample.lua 文件中包含了用于创建自己的远程属性和远程服务的示例。
运行 EMS 和 Lua 脚本资源 (LSR)
完成配置后,将 EMS 作为服务运行或从命令行运行,然后将 LSR 作为服务运行或从命令行运行。以下各图显示了从命令行运行上述两者的示例:
EMS
Lua 脚本:
如果配置中存在错误,命令行界面将报告错误。
绑定远程属性
一旦 LSR 成功运行,您在脚本中定义为 template 的远程属性即可与 ThingWorx 中的资产绑定。
1. ThingWorx Composer 中,打开您的资产。
2. “属性和警报”页面上,单击“管理绑定”
3. “管理绑定”窗口中,单击“远程”。将想要与资产绑定的远程属性从左窗格拖放到右窗格。
4. 单击“完成”
5. 单击“保存”以保存资产。