当前位置:网站首页>14. Tencent cloud IOT device side learning - data template application development

14. Tencent cloud IOT device side learning - data template application development

2022-06-24 02:54:00 fancyxu

The main purpose of this series is to record the learning notes of Tencent cloud Internet of things device , And on the device side SDK Make a supplementary explanation .

brief introduction

Data template related concepts , Please see the Data template

Data template function implementation , Please see the Data template function and implementation

The following describes how to develop applications based on data templates , It mainly includes Data template coding Downlink data processing Uplink data reporting .

Please refer to the source code :

https://github.com/xyfancy/iot-hub-device-c-sdk/tree/master/app/data_template

Data template coding

The core idea : Convert the data template definition to the corresponding C Code data type , such as Integer type ->int, Provide general interface according to Indexes visit ( The implementation is in beta edition , Subsequently, the script is used for automatic generation ).

Source code :data_template_config.c and data_template_config.h

Take the lamp switch attribute as an example :

1. initialization

Properties include :

  • type: Corresponding data template type
  • key: For the keyword corresponding to the attribute
  • value: Value storage space
  • need_report: Whether it is necessary to report , When the value changes , The escalation platform is required for synchronization ( Specially : If it is only a downstream attribute , And the platform is not used to save the status , There is no need to report )
struct DataTemplateProperty {
    DataTemplatePropertyType  type;
    const char*               key;
    DataTemplatePropertyValue value;
    int                       need_report;
};

According to the definition of data template on the console , Initialize switch properties :

  • type : Boolean type
  • keyword :power_switch
  • value : You can use C In language int Storage , Where the value uses Joint type , Users need to select members according to the data template type .
  • Report or not : Because the switch can be modified manually when it is offline , Therefore, it is necessary to report during initialization )
sg_usr_data_template_property[USR_PROPERTY_INDEX_POWER_SWITCH].value.value_bool = 0;
sg_usr_data_template_property[USR_PROPERTY_INDEX_POWER_SWITCH].key              = "power_switch";
sg_usr_data_template_property[USR_PROPERTY_INDEX_POWER_SWITCH].type             = DATA_TEMPLATE_TYPE_BOOL;
sg_usr_data_template_property[USR_PROPERTY_INDEX_POWER_SWITCH].need_report      = 1;

2. Interface call

By indexing data templates , Access the corresponding data template through the index

typedef enum {
    USR_PROPERTY_INDEX_POWER_SWITCH = 0, //  Switch index 
    USR_PROPERTY_INDEX_COLOR,
    USR_PROPERTY_INDEX_BRIGHTNESS,
    USR_PROPERTY_INDEX_NAME,
    USR_PROPERTY_INDEX_POSITION,
    USR_PROPERTY_INDEX_POWER,
} UsrPropertyIndex;
// initialization 
void usr_data_template_init(void);
// Get the data template value of non structure type 
DataTemplatePropertyValue usr_data_template_property_value_get(UsrPropertyIndex index);
// Set the data template value of non structure type 
void usr_data_template_property_value_set(UsrPropertyIndex index, DataTemplatePropertyValue value);
// Get the data template value of the structure type 
DataTemplatePropertyValue usr_data_template_property_struct_value_get(UsrPropertyIndex struct_index,
                                                                      int              property_index);
// Set the data template value of the structure type 
void usr_data_template_property_struct_value_set(UsrPropertyIndex struct_index, int property_index,
                                                 DataTemplatePropertyValue value);
// Get the data template value of the input parameter in the behavior                                                  
DataTemplatePropertyValue usr_data_template_action_input_value_get(UsrActionIndex index, int property_index);
//  Through analysis control perhaps get_status in control In the news params Set the corresponding properties 
void usr_data_template_property_parse(UtilsJsonValue params);
//  Report all the attributes that need to be reported ,need_report by 1
int usr_data_template_property_report(void* client, char* buf, int buf_len);
//  Report the incident , Here, the user can construct json, More flexibility 
void usr_data_template_event_post(void* client, char* buf, int buf_len, UsrEventIndex id, const char* params);
//  Through analysis action The input parameters in the message set the corresponding properties 
int usr_data_template_action_parse(UtilsJsonValue action_id, UtilsJsonValue params, UsrActionIndex* index);
//  Response to action 
int usr_data_template_action_reply(void* client, char* buf, int buf_len, UsrActionIndex index,
                                   UtilsJsonValue client_token, int code, const char* response);

Downlink data processing

Source code :data_template_app.c

Downlink data mainly includes :

  1. control news : Set the property value in the callback , Then read the attribute value for processing
    1. method_control_callback->usr_data_template_property_parse->usr_data_template_property_value_get
  2. action news : Set the input parameter attribute value in the callback , Then read the attribute value for processing
    1. method_action_callback->usr_data_template_action_parse->usr_data_template_action_input_value_get
  3. get_status news : Set... In callback control The attribute value of the message , Then read the attribute value for processing
    1. method_get_status_reply_callback->usr_data_template_property_parse->usr_data_template_property_value_get
  4. Reply to various messages :
    1. If you need to implement QOS1 quality , Please establish a queue implementation in the application layer ( It depends on the business , Different business requirements have different requirements for retransmission and timeout time , Typical examples are summarized based on business experience ).
    2. If it is QOS0 quality , All replies can be ignored directly ( Most data collection messages can be handled this way ).

Uplink data reporting

Source code :data_template_app.c

The online data mainly includes :

  1. report news : Setting property values (need_reoprt Set as 1), Then call the interface to report all need_report by 1 Properties of
    1. usr_data_template_property_value_set->usr_data_template_property_report
  2. control_reply news : Call the interface in the callback to reply directly , It's OK not to reply , It depends on the business
    1. method_control_callback->IOT_DataTemplate_PropertyControlReply
  3. action_reply news : Reply according to the result in the call , Because of the clouds API There is a timeout limit , So the reply must be immediate , If you can't complete the action , Please reply by attribute reporting .
    1. method_action_callback->usr_data_template_action_reply
  4. event Report : Call interface , To remodel oneself json Report ,sdk Examples are provided in . This is more flexible and simple
    1. usr_data_template_event_post

matters needing attention

The method provided in this paper is designed to summarize the previous business , There are the following restrictions , Please modify according to the business :

  1. By default, all attributes are passed usr_data_template_property_value_set It will be set after setting need_report by 1, And in the call usr_data_template_property_report I'll report it later . If you do not need to report the attribute , You can filter by attribute index .
  2. usr_data_template_property_parse Can parse json And set all the properties , If multiple attributes are required to take effect together , Please integrate these attributes into the structure or string type , Or modify the implementation of the interface , Judge against the index .
  3. All replies will not be processed , For some special scenes ( Weak net ), The application layer implements QOS1, At present, there are few project demands .
原网站

版权声明
本文为[fancyxu]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/10/20211024230811453a.html