進階組態設定與整合 > 連接資產至 Edge MicroServer (EMS)
連接資產至 Edge MicroServer (EMS)
Edge MicroServer (EMS) 為安裝於遠端裝置上的獨立應用程式。會在裝置與 ThingWorx 主機之間建立不間斷的雙向通訊。
本章節討論如何把 ThingWorx 中的資產屬性,連上使用 Edge MicroServer (EMS) 的裝置,並說明與 EMS 繫結的 Lua Script Resource。
如需有關使用 Edge SDK來連接資產屬性的資訊,請參閱 ThingWorx Edge Java SDK 說明中心
先決條件
請確認您具有下列:
Edge MicroServer (EMS) 與繫結的 Lua Script Resource (LSR)。
ThingWorx Composer 8.2.0 或更高版本,並已安裝 ThingWorx Apps 完畢。
ThingWorx WebSocket 式 Edge MicroServer (WS EMS) 與 Lua 指令集資源 (LSR) 說明中心。相關說明均假設您已熟悉該說明中心所列的內容。
配置 ThingWorx
1. ThingWorx Composer 中建立應用程式金鑰,以用於您的 EMS 中。
a. 找到安全性 > 應用程式金鑰,按一下新建
b. 輸入適當值:
名稱 - 設定 EMS 時需要該值。
使用者名稱參考 - 選取將會使用 EMS 的使用者。例如「管理員」
到期日 - 根據您的公司政策,為應用程式金鑰設定適當的到期日。若保留為空白,則預設為一天。
c. 按一下「儲存」
2. ThingWorx 中建立您要連接遠端裝置的資產 (項目)。使用下列基礎項目範本之一,或是用以實行下列項目範本之一的項目範本:
RemoteThing
RemoteThingWithFileTransfer
RemoteThingWithTunnels
RemoteThingWithTunnelsAndFileTransfer
PTC.ISA95.PhysicalAssetBaseThingTemplate
無論使用何種基礎項目範本,資產必須實行 RemoteAccessible 項目形式。
最佳工作模式是,在項目範本上實行項目形式,而非在單獨的資產上實行。
配置 EMS
2. 導覽至下列檔案:<EMS installation location>/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": "<absolute path to certificate file>",
"private_key": "<absolute path to 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":"<absolute path to \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":"<absolute path to \MicroServer\edge\in>"
}, {
"out":"<absolute path to \MicroServer\edge\out>"
},{
"staging":"<absolute path to \MicroServer\edge\staging>"
}],
"staging_dir":"staging"
}
}
4. 儲存已修改的 config.json 檔案。
5. 您可以執行 <EMS installation location>/microserver/wsems.exe 檔案,或可安裝以作為服務。
設定 Lua Script Resource (LSR)
1. 找出 <EMS installation location>/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 Script Resource 配置之。例如:
--
-- 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",
}
ThingWorx 的實際資產名稱值,取代 EdgeThing
或者,您也可以在此指定 identifier 屬性。在稍後於此流程中執行 Lua 指令集之後,會在 ThingWorx 中建立識別元,讓您可以針對 ThingWorx Composer 中的資產,在「一般資訊」頁面上的「識別元」中對其進行選取。
* 
可依據名稱或識別元繫結遠端項目,但無法同時依據兩者實現這一點。若按名稱繫結的遠端項目還具有識別元,則為項目執行遠端服務或擷取遠端屬性時,會出現異常情況。
欲依據名稱繫結遠端項目,請勿指定識別元。如果已指定識別元,請將其移除。
template 屬性,代表指令集所識別您要從裝置傳送到資產的資訊,例如遠端屬性或遠端服務。在上述程式碼範例中所示的範例檔案,可到此處找到:<EMS installation location>/microserver/etc/custom/templates/example.luaexample.lua 檔案包含範例,可建立您自己的遠端屬性和遠端服務。
執行 EMS 與 Lua Script Resource (LSR)
完成組態之後,執行 EMS 或透過指令行作為服務,然後再執行 LSR 或透過指令行作為服務。下圖範例即是同時透過指令行執行。
EMS
Lua 指令集:
如果您的組態中有錯誤,指令行介面會回報錯誤。
繫結遠端屬性
LSR 成功執行後,您指定為 template 的指令集中定義的遠端屬性即可用以繫結至 ThingWorx 中的資產。
1. ThingWorx Composer 中開啟您的資產。
2. 「屬性與警示」頁面上,按一下「管理繫結」
3. 「管理繫結」視窗中,按一下「遠端」。針對您要繫結至資產的遠端屬性,將之從左窗格拖曳到右窗格中。
4. 按一下「完成」
5. 按一下「儲存」以儲存資產。