物模型通信
1. 属性上报
属性上报函数:ThingPropertyPost()
,传入参数propData
为要上报的属性内容
属性上报结果类:ThingPropertyPostResult
,包含属性:Success
,MsgId
:
属性名称 | 属性类型 | 是否必填 | 描述 |
---|---|---|---|
Success | bool |
是 | 上报结果,true 为成功,false 为失败 |
MsgId | string |
是 | 上报消息的msgId ,用于标识上报消息,用于与回调函数传入的上报结果进行匹配,MsgId 由SDK生成 |
属性上报回调函数:OnThingPropertyPost()
,传入参数msgId
,code
,data
,message
:
参数名称 | 参数类型 | 是否必填 | 描述 |
---|---|---|---|
msgId | string |
是 | 服务端响应消息的msgId ,用于与SDK上报消息进行匹配 |
code | int |
是 | 服务端响应消息的code ,用于标识上报结果,200 为成功,其他为失败 |
data | object |
否 | 服务端响应的数据 |
message | string |
是 | 服务端响应消息的message ,用于标识上报结果,success 为成功,其他为失败 |
示例代码:
linkKit.OnThingPropertyPost = (msgId, code, data, message) =>
{
Console.WriteLine($"property msgId: {msgId}, code: {code}, data: {data}, message: {message}");
};
var thingPropertyPostResult = linkKit.ThingPropertyPost(new Dictionary<string, object>
{
{ "LightSwitch", 1 },
{ "ColorTemperature", 1000 },
{ "Brightness", 100 },
});
2. 事件上报
事件上报函数:ThingEventPost()
,传入参数identifier
为要上报的事件标识符,outputData
为要上报的事件内容
事件上报结果类:ThingEventPostResult
,包含属性:Success
,MsgId
:
属性名称 | 属性类型 | 是否必填 | 描述 |
---|---|---|---|
Success | bool |
是 | 上报结果,true 为成功,false 为失败 |
MsgId | string |
是 | 上报消息的msgId ,用于标识上报消息,用于与上报结果进行匹配,msgId 由SDK生成 |
事件上报回调函数:OnThingEventPost()
,传入参数msgId
,code
,data
,message
:
参数名称 | 参数类型 | 是否必填 | 描述 |
---|---|---|---|
msgId | string |
是 | 服务端响应消息的msgId ,用于与SDK上报消息进行匹配 |
code | int |
是 | 服务端响应消息的code ,用于标识上报结果,200 为成功,其他为失败 |
data | object |
否 | 服务器响应的数据 |
message | string |
是 | 服务端响应消息的msg ,用于标识上报结果,success 为成功,其他为失败 |
示例代码:
linkKit.OnThingEventPost = (msgId, code, data, message) =>
{
Console.WriteLine($"event msgId: {msgId}, code: {code}, data: {data}, message: {message}");
};
var thingEventPostResult = linkKit.ThingEventPost("powerEvent", new Dictionary<string, object>
{
{ "LightSwitch", 1 },
{ "ColorTemperature", 1000 },
{ "Brightness", 100 },
});
3. 监听云端属性设置
属性设置监听回调函数:OnThingPropertySet
,在监听到属性下发时,会触发该回调函数,传入参数msgId
为本次下发的消息ID,propData
为本次下发的属性内容
属性设置监听回调函数结果类:ThingPropertySetResult
,包含属性:Success
,Code
,Message
:
属性名称 | 属性类型 | 是否必填 | 描述 |
---|---|---|---|
Success | bool |
是 | 返回给服务端的属性设置结果,true 为成功,false 为失败 |
Code | int |
是 | 响应给服务端的code ,用于标识上报结果,200 为成功,其他为失败 |
Message | string |
是 | 响应给服务端的msg ,用于标识上报结果,success 为成功,其他为失败 |
示例代码:
linkKit.OnThingPropertySet = (msgId, propData) =>
{
linkKit.ThingPropertyPost(propData);
return new ThingPropertySetResult
{
Success = true
};
};
4. 监听云端服务调用
服务调用监听回调函数:OnThingServiceCall
,在监听到服务调用时,会触发该回调函数,传入参数msgId
为本次服务调用的消息ID,identifier
为本次服务调用的标识符,inputData
为本次服务调用的输入参数
服务调用监听回调函数结果类:ThingServiceCallResult
,包含属性:Success
,Code
,Message
,OutputData
:
属性名称 | 属性类型 | 是否必填 | 描述 |
---|---|---|---|
Success | bool |
是 | 响应给服务端的结果,true 为成功,false 为失败 |
Code | int |
是 | 响应给服务端的code ,用于标识上报结果,200 为成功,其他为失败 |
Message | string |
是 | 响应给服务端的msg ,用于标识上报结果,success 为成功,其他为失败 |
OutputData | object |
否 | 响应给服务端的数据 |
示例代码:
linkKit.OnThingServiceCall = (msgId, identifier, inputData) =>
{
return new ThingServiceCallResult
{
Success = true,
OutputData = new Dictionary<string, object>
{
{ "LightSwitch", 1 },
{ "ColorTemperature", 1000 },
{ "Brightness", 100 },
}
};
};
最后修改时间: 1 年前