於資產上顯示 Azure IoT Hub 資料為屬性值
可以使用 ThingWorx Composer 將 Azure IoT Hub 資料顯示為資產的屬性值。
先決條件
• 至少需 1.8 的 Java Runtime Environment (JRE)
• ThingWorx Composer 8.3.0 或更高版本,並已安裝 ThingWorx Apps。
• 已於 ThingWorx 上安裝 Azure IoT Connector 延伸功能。
• 已安裝 Azure IoT Hub。
• 已針對 ThingWorx Azure IoT Hub Connector 建立了 ThingWorx 應用程式金鑰,例如 azure_appkey。
若要顯示資產屬性的 Azure IoT Hub 資料:
|
欲在 ThingWorx Apps 中顯示 Azure IoT Hub 項目為資產,又不要讓標籤選取器通用元件能夠瀏覽 Azure IoT Hub 資料,您只要編輯項目以新增步驟 3 中所列的「已實行的形式」,並可略過下列程序的其他部分。
|
1. 在 ThingWorx Composer 中建立新的來源提供者項目範本,將 Azure IoT Hub 新增至標籤選取係通用元件中的設備類型下拉式清單:
◦ 名稱 - 項目範本的名稱,例如 AzureIot_ResourceProvider_ThingTemplate。
◦ 基礎項目範本 - GenericThing
◦ 已實行的形式 - PTC.SCA.SCO.RemoteConnectionResourceProviderThingShape
◦ 在服務之下,按一下 以下列程式碼取代 GetConnectedServers服務。(為展示之用,換行符號和空格均已添加到下方的程式碼。如果您要剪貼程式碼,則請在嘗試於您的應用程式中使用程式碼之前,先移除這些字元。)
var params = {
infoTableName : "InfoTable",
dataShapeName : "PTC.Factory.OPCDAServerNames"
};
var infoTableWithName = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape(params);
var params = {
columnName: "ResourceProviderName",
value: me.name
};
// result: INFOTABLE dataShape: PTC.SCA.SCO.RemoteConnectionSettingsTableDataShape
var result = Things["PTC.Factory.LaunchPointConfigurationThing"].
GetRemoteConnectionTableRowByStringProperty(params);
var thingTemplateName;
if (result) {
thingTemplateName = result.getRow(0).RemoteConnectionThingTemplateName;
}
var paramsForThingTemplate = {
maxItems : 1000,
nameMask : undefined,
query : undefined,
thingTemplate : thingTemplateName,
tags : undefined
};
var thingsWithTemplate = Resources["SearchFunctions"].SearchThingsByTemplate(paramsForThingTemplate);
for (var i = 0; i < thingsWithTemplate.rows.length; i++) {
var row = thingsWithTemplate.rows[i];
// infotable datashape iteration
var name = row.name;
var isThingEnabled = Things[name].IsEnabled();
if (name !== "" && name !== null && isThingEnabled) {
var serverName = row.ServerName;
var rowObject = new Object();
rowObject.Name = name;
if (serverName === null || serverName === undefined || serverName === "") {
serverName = name;
}
rowObject.ServerName = serverName;
infoTableWithName.AddRow(rowObject);
}
}
result = infoTableWithName;
2. 建立資源提供者項目,以實作步驟 1 中所建立的項目範本:
◦ 名稱 - 例如 AzureIot_ResourceProviderThing。
◦ 基礎項目範本 - 指定在步驟 1 中建立的新物範本。在此範例中則為 AzureIot_ResourceProvider_ThingTemplate。
3. 透過下列設定建立項目範本,用以建立 Azure IoT Hub 資產 (項目):
◦ 名稱 - 例如 AzureIot_RemoteThing_ThingTemplate。
◦ 敘述- 項目範本的描述。
◦ 基礎項目範本 - AzureIotThing (或從 AzureIotThing 延伸任何項目範本)
4. 遵循
建立自訂設備類型和結構 中的指示,定義
PTC.Factory.C_LaunchPointconfigurationThing_[ReleaseVersion] 上
EquipmentTypeSettings 組態表的 Azure IoT Hub 資產之設備類型。
◦ 對於 ThingTemplate 值,指定在步驟 3 中建立的項目範本。
◦ 對於 ResourceProvider 值,指定在步驟 2 中建立的資源供應者。
5. 新增 zure IoT Hub 連線類型至啟動點組態項目。
a. 開啟 PTC.Factory.C_LaunchPointConfigurationThing_[ReleaseVersion]。
b. 透過下列設定,將新的列新增至 RemoteConnectionSettings 表中:
▪ ConnectionType——輸入您要在 Controls Advisor 中的「建立連線」視窗所顯示的 Azure IoT Hub 連線名稱。例如 AzureIot。
▪ ResourceProviderName - 輸入在步驟 2 中建立的資源供應者項目。在此範例中則為 AzureIot_ResourceProviderThing。
▪ RemoteConnectionThingTemplateName - 選取 AzureIotThing 項目範本。
▪ EnabledInControlsAdvisor - 確保取消勾選此核取方塊。
▪ EnabledInTagPicker - 勾選此核取方塊,使能於標籤選取器中顯示連線類型。
| 如果您根據 AzureIotThing 項目範本建立資產,作為 ThingWorx Composer 中的項目,則必須針對該資產設定項目上的 displayName 屬性,以能於 ThingWorx Apps 中正確顯示。如果是在 ThingWorx Apps 中的 > 中建立資產,就會自動設定此屬性。 |