AliOS-Things--linkkitapp (6)上報屬性
阿新 • • 發佈:2018-10-31
在上報屬性的時候遇到的很多坑,一直遇到:
[err] TSL Property Assemble Failed: RGBColor.Red
[err] TSL Property Assemble Failed: RGBColor.Green
[err] TSL Property Assemble Failed: RGBColor.Blue
最後,終於解決了這個問題了,於是記錄一下。
上報屬性
全部的屬性如下:總的來說分為兩種
- 第一種簡單的(鍵:值):
"WIFI_Tx_Rate":1
- 第二種複雜巢狀的(鍵:{鍵:值}):
"RGBColor":{ "Red":0, "Blue":0, "Green":0 }
{
{
"actionType":"upstream",
"messageMethod":"thing.event.property.post",
"messageID":"12174726",
"messageParams":{
"WIFI_Tx_Rate":1,
"WIFI_AP_BSSID":"4:�:r:@:�:�",
"WiFI_RSSI":-30,"Brightness":0,
"HSVColor":{
"Saturation":0,
"Value":0,"Hue":0
},
"WIFI_Band" :"2.4G",
"RGBColor":{
"Red":0,
"Blue":0,
"Green":0
},
"WIFI_Rx_Rate":1,
"HSLColor":{
"Lightness":0,
"Saturation":0,
"Hue":0
},
"LightSwitch":0,
"Propertypoint":0,
"NightLightSwitch":0,
"ColorTemperature":0,
"PropertyCharacter":"",
"WiFI_SNR":30,
"WIFI_Channel" :1,
"WorkMode":0
},
"topic":"/sysa1a6auLEDNxLTS20181008001thing/event/property/post",
"uniMsgId":"1049558894138900480"
},
"messageResult":"200",
"logTime":"1539069314"
}
1、簡單的屬性上報:LightSwitch
屬性Json表示式:
"LightSwitch":0,
int lightswitch = 1;
linkkit_set_value(linkkit_method_set_property_value, sample->thing,
"LightSwitch", &lightswitch, NULL);
linkkit_post_property(sample->thing, "LightSwitch",post_property_cb);
2、複雜的屬性上報:RGBColor
屬性Json表示式:
"RGBColor":{
"Red":0,
"Blue":0,
"Green":0
}
錯誤的寫法
int red, green, blue;
red = 255;
green = 255;
blue = 255;
// RGB
linkkit_set_value(linkkit_method_set_property_value, sample->thing,
"RGBColor.Red", &red, NULL);
linkkit_post_property(sample->thing, "RGBColor.Red",post_property_cb);
linkkit_set_value(linkkit_method_set_property_value, sample->thing,
"RGBColor.Green", &green, NULL);
linkkit_post_property(sample->thing, "RGBColor.Green",post_property_cb);
linkkit_set_value(linkkit_method_set_property_value, sample->thing,
"RGBColor.Blue", &blue, NULL);
linkkit_post_property(sample->thing, "RGBColor.Blue",post_property_cb);
正確的寫法
int red, green, blue;
red = 255;
green = 255;
blue = 255;
linkkit_set_value(linkkit_method_set_property_value, sample->thing,
"RGBColor.Red", &red, NULL);
linkkit_set_value(linkkit_method_set_property_value, sample->thing,
"RGBColor.Green", &green, NULL);
linkkit_set_value(linkkit_method_set_property_value, sample->thing,
"RGBColor.Blue", &blue, NULL);
linkkit_post_property(sample->thing,"RGBColor",post_property_cb);
而一般來說一個上報屬性的函式是這樣寫的:
static int post_property_color(sample_context_t *sample_ctx)
{
int ret = -1;
unsigned int red, green, blue;
if (is_active(sample_ctx)) {
red = 1;
green = 2;
blue = 3;
linkkit_set_value(linkkit_method_set_property_value, sample_ctx->thing,
"RGBColor.Red", &red, NULL);
linkkit_set_value(linkkit_method_set_property_value, sample_ctx->thing,
"RGBColor.Green", &green, NULL);
linkkit_set_value(linkkit_method_set_property_value, sample_ctx->thing,
"RGBColor.Blue", &blue, NULL);
linkkit_post_property(sample_ctx->thing,"RGBColor",post_property_cb);
ret = 0;
}
return ret;
}