将 Azure IoT Hub 数据显示为资产属性值
Azure IoT Hub 数据可以在使用 ThingWorx Composer 的资产上显示为属性值。
先决条件
• 最低 Java 运行时环境 (JRE) 为 1.8
• 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。
◦ 基本事物模板 - PTC.SCA.SCO.EquipmentResourceProviderTemplate
◦ 实现的事物形态 - 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_[发布版本] 的
EquipmentTypeSettings 配置表中定义 Azure IoT Hub 资产的设备类型。
◦ 对于 ThingTemplate 值,指定在步骤 3 中创建的事物模板。
◦ 对于 ResourceProvider 值,指定在步骤 2 中创建的资源提供者。
5. 将 Azure IoT Hub 连接类型添加到启动点配置事物。
a. 打开 PTC.Factory.C_LaunchPointConfigurationThing_[发布版本]。
b. 在 RemoteConnectionSettings 表中添加一个包含以下设置的新行:
▪ ConnectionType - 输入 Azure IoT Hub 连接类型在 Controls Advisor的“创建连接”窗口中的显示名称。例如 AzureIot。
▪ ResourceProviderName - 输入在步骤 2 中创建的资源提供者事物。在本示例中为 AzureIot_ResourceProviderThing。
▪ RemoteConnectionThingTemplateName - 选择 AzureIotThing 事物模板。
▪ EnabledInControlsAdvisor - 确保此复选框已清空。
▪ EnabledInTagPicker - 选中此复选框可使该连接类型在标记选取器中可见。
| 如果您在 ThingWorx Composer 中基于 AzureIotThing 事物模板将资产创建为一个事物,则必须设置该事物的 displayName 属性,以使该资产在 ThingWorx Apps 中正确显示。如果从 ThingWorx Apps 的 > 中创建资产,则自动设置该属性。 |