python iot服务器_设备开发人员指南 (C) IoT 即插即用 | Microsoft Docs

模型 ID 公告Model ID announcement

若要公布模型 ID,设备必须将其包含在连接信息中:To announce the model ID, the device must include it in the connection information:

static const char g_ThermostatModelId[] = "dtmi:com:example:Thermostat;1";

IOTHUB_DEVICE_CLIENT_LL_HANDLE deviceHandle = NULL;

deviceHandle = CreateDeviceClientLLHandle();

iothubResult = IoTHubDeviceClient_LL_SetOption(

deviceHandle, OPTION_MODEL_ID, g_ThermostatModelId);

提示

对于模块和 IoT Edge,使用 IoTHubModuleClient_LL 代替 IoTHubDeviceClient_LL 。For modules and IoT Edge, use IoTHubModuleClient_LL in place of IoTHubDeviceClient_LL.

DPS 有效负载DPS payload

使用 设备预配服务 (DPS) 的设备可以包括 modelId 使用以下 JSON 有效负载的预配过程中要使用的。Devices using the Device Provisioning Service (DPS) can include the modelId to be used during the provisioning process using the following JSON payload.

{

"modelId" : "dtmi:com:example:Thermostat;1"

}

实现遥测、属性和命令Implement telemetry, properties, and commands

如 了解 IoT 即插即用模型中的组件中所述,设备构建者必须决定是否要使用组件来描述其设备。As described in Understand components in IoT Plug and Play models, device builders must decide if they want to use components to describe their devices. 使用组件时,设备必须遵循本部分中所述的规则。When using components, devices must follow the rules described in this section.

遥测Telemetry

默认组件不需要任何特殊属性。A default component doesn't require any special property.

使用嵌套组件时,设备必须使用组件名称设置消息属性:When using nested components, devices must set a message property with the component name:

void PnP_ThermostatComponent_SendTelemetry(

PNP_THERMOSTAT_COMPONENT_HANDLE pnpThermostatComponentHandle,

IOTHUB_DEVICE_CLIENT_LL_HANDLE deviceClientLL)

{

PNP_THERMOSTAT_COMPONENT* pnpThermostatComponent = (PNP_THERMOSTAT_COMPONENT*)pnpThermostatComponentHandle;

IOTHUB_MESSAGE_HANDLE messageHandle = NULL;

IOTHUB_CLIENT_RESULT iothubResult;

char temperatureStringBuffer[32];

if (snprintf(

temperatureStringBuffer,

sizeof(temperatureStringBuffer),

g_temperatureTelemetryBodyFormat,

pnpThermostatComponent->currentTemperature) < 0)

{

LogError("snprintf of current temperature telemetry failed");

}

else if ((messageHandle = PnP_CreateTelemetryMessageHandle(

pnpThermostatComponent->componentName, temperatureStringBuffer)) == NULL)

{

LogError("Unable to create telemetry message");

}

else if ((iothubResult = IoTHubDeviceClient_LL_SendEventAsync(

deviceClientLL, messageHandle, NULL, NULL)) != IOTHUB_CLIENT_OK)

{

LogError("Unable to send telemetry message, error=%d", iothubResult);

}

IoTHubMessage_Destroy(messageHandle);

}

// ...

PnP_ThermostatComponent_SendTelemetry(g_thermostatHandle1, deviceClient);

只读属性Read-only properties

从默认组件报告属性不需要任何特殊构造:Reporting a property from the default component doesn't require any special construct:

static const char g_maxTemperatureSinceRebootFormat[] = "{\"maxTempSinceLastReboot\":%.2f}";

char maxTemperatureSinceRebootProperty[256];

snprintf(

maxTemperatureSinceRebootProperty,

sizeof(maxTemperatureSinceRebootProperty),

g_maxTemperatureSinceRebootFormat,

38.7);

IOTHUB_CLIENT_RESULT iothubClientResult = IoTHubDeviceClient_LL_SendReportedState(

deviceClientLL,

(const unsigned char*)maxTemperatureSinceRebootProperty,

strlen(maxTemperatureSinceRebootProperty), NULL, NULL));

用下一个报告的属性更新设备克隆:The device twin is updated with the next reported property:

{

"reported": {

"maxTempSinceLastReboot" : 38.7

}

}

使用嵌套组件时,必须在组件名称中创建属性:When using nested components, properties must be created within the component name:

STRING_HANDLE PnP_CreateReportedProperty(

const char* componentName,

const char* propertyName,

const char* propertyValue

)

{

STRING_HANDLE jsonToSend;

if (componentName == NULL)

{

jsonToSend = STRING_construct_sprintf(

"{\"%s\":%s}",

propertyName, propertyValue);

}

else

{

jsonToSend = STRING_construct_sprintf(

"{\"""%s\":{\"__t\":\"c\",\"%s\":%s}}",

componentName, propertyName, propertyValue);

}

if (jsonToSend == NULL)

{

LogError("Unable to allocate JSON buffer");

}

return jsonToSend;

}

<
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值