更新时间:2025-06-12 GMT+08:00
Demo1
该Demo主要展示edge.h和edge_daemon.h里的回调函数和接口函数使用。
#include "edge.h"
#include "edge_daemon.h"
#include "edge_dc_driver.h"
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
/*
* 描述:针对模块的命令的回调函数
* 参数:
* command_name:命令名称
* device_id:设备Id
* service_id:服务Id
* request_id:请求Id(响应命令)
* body:命令体
* body_len:命令体长度
* */
EDGE_RETCODE moduleCmdCb(const char* command_name, const char* device_id,
const char* service_id, const char* request_id, const char* body, unsigned int body_len)
{
printf("command_name:%s, device_id:%s, service_id:%s, request_id:%s, body:%s, body_len:%u",
command_name, device_id, service_id, request_id, body, body_len);
char* rsp = "{\"error_desc\":\"ok\"}";
//命令响应
edge_send_command_rsp(NULL, request_id, 0, rsp, strlen(rsp));
return EDGE_SUCCESS;
}
/*
* 描述:针对子设备的命令的回调函数
* 参数:
* command_name:命令名称
* device_id:子设备Id
* service_id:服务Id
* request_id:请求Id(响应命令)
* body:命令体
* body_len:命令体长度
* */
EDGE_RETCODE deviceCmdCb(const char* command_name, const char* device_id,
const char* service_id, const char* request_id, const char* body, unsigned int body_len)
{
printf("command_name:%s, device_id:%s, service_id:%s, request_id:%s, body:%s, body_len:%u",
command_name, device_id, service_id, request_id, body, body_len);
char* rsp = "{\"error_desc\":\"ok\"}";
edge_send_command_rsp(NULL, request_id, 0, rsp, strlen(rsp));
return EDGE_SUCCESS;
}
/*
* 描述:影子回调
* 参数:
* shadow:影子数据
* body_len:长度
* */
EDGE_RETCODE shadowCb(const char* shadow, unsigned int shadow_len)
{
printf("shadowCb:%s\n", shadow);
return EDGE_SUCCESS;
}
/*
* 描述:自定义topic消息
* 参数:
* shadow:影子数据
* body_len:长度
* */
EDGE_RETCODE customizedMessageCb(const char* topic, const char* payload, unsigned int len)
{
printf("topic:%s\n", topic);
printf("payload:%s\n", payload);
return EDGE_SUCCESS;
}
EDGE_RETCODE moduleRequestCb(const char* request_id, const char* payload, unsigned int len)
{
printf("request_id:%s\n", request_id);
printf("payload:%s\n", payload);
char* rsp = "{\"result\":\"0\"}";
edge_send_module_request_rsp(request_id, rsp, strlen(rsp));
return EDGE_SUCCESS;
}
void doModuleSetProperties()
{
char* req = "{\n"
" \"device_id\": \"2B\",\n"
" \"service_id\": \"dataService\",\n"
" \"properties\": {\n"
" \"Switch\": 1\n"
" },\n"
" \"metadata\": {}\n"
"}";
char* resp = NULL;
unsigned int len;
edge_send_module_set_properties(req, strlen(req), &resp, &len, 89);
printf("resp:%s\n", resp);
printf("len:%u\n", len);
if (resp != NULL) {
edge_release_data(resp);
}
}
void doModuleGetProperties()
{
char* req = "{\n"
" \"device_id\": \"2B\",\n"
" \"service_id\": \"dataService\",\n"
" \"properties\": [\"Switch\"]\n"
"}";
char* resp = NULL;
unsigned int len;
edge_send_module_get_properties(req, strlen(req), &resp, &len, 89);
printf("resp:%s\n", resp);
printf("len:%u\n", len);
if (resp != NULL) {
edge_release_data(resp);
}
}
/*
* 描述:收到设备上报数据的回调处理
* 参数:
* body: 上报的数据
* body_len: 上报数据的大小
* */
EDGE_RETCODE messageReceivedCb(const char* body, unsigned int body_len)
{
printf("body is: %s\nbody len is: %u\n", body, body_len);
return EDGE_SUCCESS;
}
EDGE_RETCODE pointsSetReceiveCb(const char* request_id, const char* points_info, unsigned int len)
{
printf("requestId is: %s\n points size is: %u, points_info:%s\n", request_id, len, points_info);
ST_IOT_EDGE_RESULT result;
result.result_desc = "ok";
result.result_code = 0;
edge_dc_driver_points_set_rsp(request_id, &result);
return EDGE_SUCCESS;
}
EDGE_RETCODE pointsGetReceiveCb(const char* request_id, ST_POINTS_GET* st_points_get)
{
printf("requestId is: %s\n points size is: %u\n", request_id, st_points_get->size);
ST_POINTS_INFO st_points_info = {0};
st_points_info.track_links = st_points_get->track_links;
st_points_info.link_size = st_points_get->link_size;
st_points_info.points_info ="{\n"
" \"point_id_1\": \"value_1\",\n"
" \"point_id_2\": \"value_2\"\n"
" }";
st_points_info.points_info_len = strlen(st_points_info.points_info);
char *info = "connect failed";
ST_METADATA_MAP metadatas[2];
ST_METADATA_MAP metadata_1 = {0};
metadata_1.points_id = "point_id_1";
metadata_1.metadata_info.quality = "quality";
metadata_1.metadata_info.reason = "reason";
metadatas[0] = metadata_1;
ST_METADATA_MAP metadata_2 = {0};
metadata_2.points_id = "point_id_2";
metadata_2.metadata_info.quality = "quality";
metadata_2.metadata_info.reason = "reason";
metadatas[1] = metadata_2;
st_points_info.metadata = metadatas;
st_points_info.metadata_size = 2;
edge_dc_driver_points_get_rsp(request_id, &st_points_info, info, strlen(info));
return EDGE_SUCCESS;
}
void sendPointReport()
{
char* point_info = "{\n"
" \"point_id_1\": \"value_1\",\n"
" \"point_id_2\": \"value_2\"\n"
" }";
char* metadata = "{\"point_id_1\": {\"quality\": \"quality\",\"reason\": \"reason\"}}";
ST_POINTS_REPORT_INFO points_report_info;
points_report_info.points_info = point_info;
points_report_info.points_info_len = strlen(point_info);
points_report_info.metadata = metadata;
points_report_info.metadata_len = strlen(metadata);
edge_dc_driver_points_report(&points_report_info);
}
void sendDsConnectionState()
{
ST_DS_CONNECTION_STATE st_ds_connection_state = {0};
st_ds_connection_state.connection_status = "200";
st_ds_connection_state.info = "datasource connect success.";
printf("report ds connectionState");
edge_ds_connection_state_report(&st_ds_connection_state);
}
/*
* 描述:子设备添加的回调
* 参数:
* add_device_info:添加的子设备信息
* len:长度
* */
EDGE_RETCODE sub_device_add_cb(const char* add_device_info, unsigned int len)
{
printf("sub_device_add_cb:%s\n", add_device_info);
return EDGE_SUCCESS;
}
/*
* 描述:子设备删除的回调
* 参数:
* delete_device_info:删除的子设备信息
* len:长度
* */
EDGE_RETCODE sub_device_delete_cb(const char* delete_device_info, unsigned int len)
{
printf("sub_device_delete_cb:%s\n", delete_device_info);
return EDGE_SUCCESS;
}
/*
* 描述:子设备请求响应的回调
* 参数:
* getProductsRspEvent:获取到的子设备请求响应信息
* */
EDGE_RETCODE get_products_response_cb(ST_GET_PRODUCTS_RSP_EVENT* getProductsRspEvent)
{
int i, j, k, l, m;
for (i = 0; i < getProductsRspEvent->product_len; i++)
{
printf("%d-th product's info shows below:\n", i+1);
printf("product_id:%s\n", getProductsRspEvent->products[i].product_id);
printf("name:%s\n", getProductsRspEvent->products[i].name);
printf("device_type:%s\n", getProductsRspEvent->products[i].device_type);
printf("protocol_type:%s\n", getProductsRspEvent->products[i].protocol_type);
printf("data_format:%s\n", getProductsRspEvent->products[i].data_format);
printf("industry:%s\n", getProductsRspEvent->products[i].industry);
printf("name:%s\n", getProductsRspEvent->products[i].name);
printf("service_capabilities:\n");
for (j = 0; j < getProductsRspEvent->products[i].service_capability_len; j++)
{
printf(" service_id:%s, ", getProductsRspEvent->products[i].serviceCapabilities[j].service_id);
printf("service_type:%s, ", getProductsRspEvent->products[i].serviceCapabilities[j].service_type);
printf("description:%s, ", getProductsRspEvent->products[i].serviceCapabilities[j].description);
printf("option:%s\n", getProductsRspEvent->products[i].serviceCapabilities[j].option);
printf(" properties:\n");
for(k = 0; k < getProductsRspEvent->products[i].serviceCapabilities[j].property_len; k++)
{
printf(" property_name:%s, ", getProductsRspEvent->products[i].serviceCapabilities[j].properties[k].property_name);
printf("required:%s, ", getProductsRspEvent->products[i].serviceCapabilities[j].properties[k].required ? "true" : "false");
printf("data_type:%s, ", getProductsRspEvent->products[i].serviceCapabilities[j].properties[k].data_type);
printf("enum_list:%s, ", getProductsRspEvent->products[i].serviceCapabilities[j].properties[k].enum_list);
printf("min:%s, ", getProductsRspEvent->products[i].serviceCapabilities[j].properties[k].min);
printf("max:%s, ", getProductsRspEvent->products[i].serviceCapabilities[j].properties[k].max);
printf("max_length:%d, ", getProductsRspEvent->products[i].serviceCapabilities[j].properties[k].max_length);
printf("step:%lf, ", getProductsRspEvent->products[i].serviceCapabilities[j].properties[k].step);
printf("unit:%s, ", getProductsRspEvent->products[i].serviceCapabilities[j].properties[k].unit);
printf("method:%s, ", getProductsRspEvent->products[i].serviceCapabilities[j].properties[k].method);
printf("description:%s, ", getProductsRspEvent->products[i].serviceCapabilities[j].properties[k].description);
printf("default_value:%s\n", getProductsRspEvent->products[i].serviceCapabilities[j].properties[k].default_value);
}
printf(" commands:\n");
for(k = 0; k < getProductsRspEvent->products[i].serviceCapabilities[j].command_len; k++)
{
printf(" command_name:%s\n", getProductsRspEvent->products[i].serviceCapabilities[j].commands[k].command_name);
printf(" paras:\n");
for(l = 0; l < getProductsRspEvent->products[i].serviceCapabilities[j].commands[k].para_len; l++)
{
printf(" para_name:%s, ", getProductsRspEvent->products[i].serviceCapabilities[j].commands[k].paras[l].para_name);
printf("required:%s, ", getProductsRspEvent->products[i].serviceCapabilities[j].commands[k].paras[l].required ? "true" : "false");
printf("data_type:%s, ", getProductsRspEvent->products[i].serviceCapabilities[j].commands[k].paras[l].data_type);
printf("enum_list:%s, ", getProductsRspEvent->products[i].serviceCapabilities[j].commands[k].paras[l].enum_list);
printf("min:%s, ", getProductsRspEvent->products[i].serviceCapabilities[j].commands[k].paras[l].min);
printf("max:%s, ", getProductsRspEvent->products[i].serviceCapabilities[j].commands[k].paras[l].max);
printf("max_length:%d, ", getProductsRspEvent->products[i].serviceCapabilities[j].commands[k].paras[l].max_length);
printf("step:%lf, ", getProductsRspEvent->products[i].serviceCapabilities[j].commands[k].paras[l].step);
printf("unit:%s, ", getProductsRspEvent->products[i].serviceCapabilities[j].commands[k].paras[l].unit);
printf("description:%s\n", getProductsRspEvent->products[i].serviceCapabilities[j].commands[k].paras[l].description);
}
printf(" responses:\n");
for(l = 0; l < getProductsRspEvent->products[i].serviceCapabilities[j].commands[k].response_len; l++)
{
printf(" response_name:%s\n", getProductsRspEvent->products[i].serviceCapabilities[j].commands[k].responses[l].response_name);
printf(" paras:\n");
for(m = 0; m < getProductsRspEvent->products[i].serviceCapabilities[j].commands[k].responses[l].service_command_para_len; m++)
{
printf(" para_name:%s, ", getProductsRspEvent->products[i].serviceCapabilities[j].commands[k].responses[l].paras[m].para_name);
printf("required:%s, ", getProductsRspEvent->products[i].serviceCapabilities[j].commands[k].responses[l].paras[m].required ? "true" : "false");
printf("data_type:%s, ", getProductsRspEvent->products[i].serviceCapabilities[j].commands[k].responses[l].paras[m].data_type);
printf("enum_list:%s, ", getProductsRspEvent->products[i].serviceCapabilities[j].commands[k].responses[l].paras[m].enum_list);
printf("min:%s, ", getProductsRspEvent->products[i].serviceCapabilities[j].commands[k].responses[l].paras[m].min);
printf("max:%s, ", getProductsRspEvent->products[i].serviceCapabilities[j].commands[k].responses[l].paras[m].max);
printf("max_length:%d, ", getProductsRspEvent->products[i].serviceCapabilities[j].commands[k].responses[l].paras[m].max_length);
printf("step:%lf, ", getProductsRspEvent->products[i].serviceCapabilities[j].commands[k].responses[l].paras[m].step);
printf("unit:%s, ", getProductsRspEvent->products[i].serviceCapabilities[j].commands[k].responses[l].paras[m].unit);
printf("description:%s\n", getProductsRspEvent->products[i].serviceCapabilities[j].commands[k].responses[l].paras[m].description);
}
}
}
}
printf("\n");
}
return EDGE_SUCCESS;
}
/*
* 描述:子设备事件的回调
* 参数:
* device_id:子设备Id
* body:事件体
* len:长度
* */
EDGE_RETCODE sub_device_event_cb(const char* device_id, const char* body, unsigned int body_len)
{
printf("%s\n", device_id);
printf("%s", body);
return EDGE_SUCCESS;
}
/*
* 描述:子设备start_scan事件的回调
* 参数:
* protocol:协议
* channel:通道
* parentDeviceId:父设备Id
* scan_setting:扫描设置
* body_len:长度
* */
EDGE_RETCODE sub_device_start_scan_cb(const char* protocol, const char* channel, const char* parentDeviceId,const char* scan_setting, unsigned int body_len)
{
printf("protocol = %s, channel = %s, parentDeviceId = %s, scan_setting = %s", protocol, channel, parentDeviceId, scan_setting);
return EDGE_SUCCESS;
}
/*
* 描述:子设备消息的回调
* 参数:
* message_id:消息Id
* channel:通道
* device_id:设备Id
* body:消息体
* body_len:长度
* */
EDGE_RETCODE sub_device_messages_down_cb(const char* message_id, const char* message_name, const char* device_id, const char* body, unsigned int body_len)
{
printf("message_id = %s, message_name = %s, device_id = %s, body = %s", message_id, message_name, device_id, body);
return EDGE_SUCCESS;
}
/*
* 描述:子设备收到属性设置的回调
* 参数:
* sub_device_property_set:属性设置的具体数据
* */
EDGE_RETCODE sub_device_properties_set_cb(ST_PROPERTY_SET* sub_device_property_set)
{
printf("object_device_id = %s, request_id = %s\n", sub_device_property_set->object_device_id, sub_device_property_set->request_id);
printf("services_size = %u\n", sub_device_property_set->services_size);
int i;
for(i = 0; i< sub_device_property_set->services_size; i++)
{
printf("service_id = %s, properties = %s\n", sub_device_property_set->services[i].service_id, sub_device_property_set->services[i].properties);
}
ST_IOT_RESULT result;
result.result_desc = "ok";
result.result_code = 0;
edge_send_sub_device_property_set_rsp(sub_device_property_set->request_id, &result);
return EDGE_SUCCESS;
}
EDGE_RETCODE sub_device_properties_get_cb(ST_PROPERTY_GET* sub_device_property_get)
{
printf("object_device_id = %s, request_id = %s, service_id = %s", sub_device_property_get->object_device_id, sub_device_property_get->request_id, sub_device_property_get->service_id);
char* data_body1 = "{\n"
"\t\"PhV_phsA\":1,\n"
"\t\"PhV_phsB\":2\n"
"}";
ST_DEVICE_PROPERTY_GET_RSP device_property_get_rsp;
ST_SERVICE_DATA service_data_1 = {0};
service_data_1.service_id = "service_id_1";
service_data_1.properties = data_body1;
service_data_1.event_time = "20200520T115630Z";
device_property_get_rsp.services = &service_data_1;
device_property_get_rsp.services_size = 1;
edge_send_sub_device_property_get_rsp(sub_device_property_get->request_id, &device_property_get_rsp);
return EDGE_SUCCESS;
}
EDGE_RETCODE sub_device_shadow_cb(ST_DEVICE_SHADOW* sub_device_shadow)
{
printf("object_device_id = %s, request_id = %s\n", sub_device_shadow->object_device_id, sub_device_shadow->request_id);
printf("services_size = %d\n", sub_device_shadow->shadow_size);
int i;
for(i = 0; i< sub_device_shadow->shadow_size; i++) {
printf("service_id = %s, properties = %s\n", sub_device_shadow->shadow[i].service_id, sub_device_shadow->shadow[i].desired_properties);
}
return EDGE_SUCCESS;
}
/*
* 描述:添加子设备调用方式示例
* */
void send_add_sub_device(){
//构造设备数据
ST_DEVICE_INFO device_info[2];
ST_DEVICE_INFO device_info_1 = {0};
device_info_1.device_id = "device_id-1";
device_info_1.parent_device_id = "parent_device_id_1";
device_info_1.node_id = "node_id_1";
device_info_1.name = "name_1";
device_info_1.product_id = "product_id_1";
device_info_1.status = "RUNNING";
ST_DEVICE_INFO device_info_2 = {0};
device_info_2.device_id = "device_id-2";
device_info_2.parent_device_id = "parent_device_id_2";
device_info_2.node_id = "node_id_2";
device_info_2.name = "name_2";
device_info_2.product_id = "product_id_2";
device_info_2.status = "RUNNING";
device_info[0] = device_info_1;
device_info[1] = device_info_2;
edge_send_add_sub_device(device_info, 2);
}
/*
* 描述:添加子设备调用方式示例
* */
void send_delete_sub_device(){
//构造设备数据
ST_DEVICE_INFO device_info[2];
ST_DEVICE_INFO device_info_1 = {0};
device_info_1.device_id = "device_id-1";
device_info_1.parent_device_id = "parent_device_id_1";
device_info_1.node_id = "node_id_1";
device_info_1.name = "name_1";
device_info_1.product_id = "product_id_1";
device_info_1.status = "RUNNING";
ST_DEVICE_INFO device_info_2 = {0};
device_info_2.device_id = "device_id-2";
device_info_2.parent_device_id = "parent_device_id_2";
device_info_2.node_id = "node_id_2";
device_info_2.name = "name_2";
device_info_2.product_id = "product_id_2";
device_info_2.status = "RUNNING";
device_info[0] = device_info_1;
device_info[1] = device_info_2;
edge_send_delete_sub_device(device_info, 2);
}
/*
* 描述:同步子设备调用方式示例
* 参数;
* version:版本(添加子设备成功后回调接口有此字段)
* */
void send_sync_sub_device(long long version){
edge_send_sync_sub_device(version);
}
/*
* 描述:发送子设备状态方式示例
* */
void send_sub_device_status(){
/*
* 构造 {"device_statuses":[{
* "device_id":"deviceId",
* "status":"status"
* }]}
* */
ST_DEVICE_STATUS device_status[2];
ST_DEVICE_STATUS device_status_1 = {0};
device_status_1.device_id = "device_id_1";
device_status_1.status = "RUNNING";
device_status[0] = device_status_1;
ST_DEVICE_STATUS device_status_2 = {0};
device_status_2.device_id = "device_id_2";
device_status_2.status = "RUNNING";
device_status[1] = device_status_2;
edge_send_sub_device_status(device_status, 2);
}
/*
* 发送子设备数据
* */
void send_batch_device_data(){
/*
* 具体上报的子设备数据和设备的产品模型有关系,和属性对应
* */
char* data_body1 = "{\n"
"\t\"PhV_phsA\":1,\n"
"\t\"PhV_phsB\":2\n"
"}";
char* data_body2 = "{\"PhV_phsA\":\"1\",\"PhV_phsB\":\"2\"}";
printf("data_body1:%s\n", data_body1);
printf("data_body2:%s\n", data_body2);
ST_DEVICE_SERVICE device_data[2];
ST_DEVICE_SERVICE device_data_1 = {0};
ST_DEVICE_SERVICE device_data_2 = {0};
ST_SERVICE_DATA service_data[2];
ST_SERVICE_DATA service_data_1 = {0};
service_data_1.service_id = "service_id_1";
service_data_1.properties = data_body1;
service_data_1.event_time = "20200520T115630Z";
ST_SERVICE_DATA service_data_2 = {0};
service_data_2.service_id = "service_id_2";
service_data_2.properties = data_body2;
service_data_2.event_time = "20200520T115630Z";
service_data[0] = service_data_1;
service_data[1] = service_data_2;
device_data_1.services = service_data;
device_data_1.device_id = "device_id_1";
device_data_1.size = 2;
device_data_2.services = service_data;
device_data_2.device_id = "device_id_2";
device_data_2.size = 2;
device_data[0] = device_data_1;
device_data[1] = device_data_2;
edge_send_batch_device_data(device_data, 2);
}
/*
* 发送发现子设备数据
* */
void send_batch_device_discovery_data(){
/*
* 具体上报的子设备数据和设备的产品模型有关系,和属性对应
* */
char* data_body1 = "{\n"
"\t\"PhV_phsA\":1,\n"
"\t\"PhV_phsB\":2\n"
"}";
char* data_body2 = "{\"PhV_phsA\":\"1\",\"PhV_phsB\":\"2\"}";
printf("data_body1:%s\n", data_body1);
printf("data_body2:%s\n", data_body2);
ST_DEVICE_DISCOVERY_SERVICE device_data[2];
ST_DEVICE_DISCOVERY_SERVICE device_data_1 = {0};
ST_DEVICE_DISCOVERY_SERVICE device_data_2 = {0};
ST_SERVICE_DATA service_data[2];
ST_SERVICE_DATA service_data_1 = {0};
service_data_1.service_id = "service_id_1";
service_data_1.properties = data_body1;
service_data_1.event_time = "20200520T115630Z";
ST_SERVICE_DATA service_data_2 = {0};
service_data_2.service_id = "service_id_2";
service_data_2.properties = data_body2;
service_data_2.event_time = "20200520T115630Z";
service_data[0] = service_data_1;
service_data[1] = service_data_2;
device_data_1.services = service_data;
device_data_1.device_id = "device_id_1";
device_data_1.product_id = "product_id_1";
device_data_1.device_name = "device_name_1";
device_data_1.size = 2;
device_data_2.services = service_data;
device_data_2.device_id = "device_id_2";
device_data_2.product_id = "product_id_2";
device_data_2.device_name = "device_name_2";
device_data_2.size = 2;
device_data[0] = device_data_1;
device_data[1] = device_data_2;
edge_send_batch_device_discovery_data(device_data, 2);
}
/*
* 描述:连接到hub的回调
* */
void connected()
{
edge_get_shadow();
ST_CLOUD_TOKEN token = {0};
edge_get_cloud_token(&token);
printf("ak:%s sk:%s region:%s expires_at:%s\n", token.ak, token.sk, token.region, token.expire_time);
char* body = "{\n"
"\t\"module_id\": \"module_id\",\n"
"\t\"old_status\": \"STOPPED|RUNNING|UNHEALTHY\",\n"
"\t\"new_status\": \"STOPPED|RUNNING|UNHEALTHY\"\n"
"}";
edge_send_service_event("module_management", "module_status_change", body, strlen(body));
sendDsConnectionState();
sleep(10);
send_add_sub_device();
sleep(10);
send_delete_sub_device();
sleep(10);
send_sync_sub_device(1234);
sleep(10);
send_sub_device_status();
sleep(10);
}
/*
* 描述:与hub断链的回调
* */
void disconnected()
{
printf("disconnected.");
}
/*
* 描述:获取模块的token
* */
void send_get_daemon_token()
{
ST_DAEMON_TOKEN daemon_token;
int ret = edge_daemon_get_token(&daemon_token);
if (ret != EDGE_SUCCESS) {
printf("get daemon token fail.");
return;
}
printf("get daemon token success.");
printf("daemon token:%s\n", daemon_token.token);
printf("daemon token expires_at:%lld\n", daemon_token.expires_at);
}
/*
* 描述:获取云端授权token信息,仅系统模块可用
* */
void send_get_daemon_cloud_token()
{
ST_DAEMON_CLOUD_TOKEN daemon_token;
int ret = edge_daemon_cloud_token(&daemon_token);
if (ret != EDGE_SUCCESS) {
printf("get daemon cloud token fail.");
return;
}
printf("get daemon cloud token success.");
printf("daemon cloud token:%s\n", daemon_token.token);
printf("daemon cloud token expires_at:%s\n", daemon_token.access_key);
}
/*
* 描述:A模块校验B的访问模块Token
* */
void send_check_daemon_token()
{
ST_VERIFY_RESPONSE token;
int ret = edge_daemon_verify("Algorithm=HMAC_SHA_256;NodeId=076aeddff400d2472f60c012d120bc7f;ModuleId=user_edge_test;SignedTime=1617759175394;Signature=6af3c8306e4f370a2939803d06719737da1b03cf0d7a04ccdadcc8830460af6c", &token);
if (ret != EDGE_SUCCESS) {
printf("check daemon token fail.");
return;
}
printf("check daemon token success.");
printf("verify moduleId:%s\n", token.module_id);
printf("verify token expires_at:%lld\n", token.expires_at);
}
/*
* 描述:获取节点证书,系统模块可用
* */
void send_get_node_certs()
{
ST_NODE_CERT node_cert;
int ret = edge_daemon_node_certs(&node_cert);
if (ret != EDGE_SUCCESS) {
printf("get node cert fail.");
return;
}
printf("get node cert success.");
printf("node cert certificate:%s\n", node_cert.certificate);
printf("node cert expires_at:%lld\n", node_cert.expires_at);
}
/*
* 描述:获取模块信任的证书
* */
void send_get_module_trust_certs()
{
ST_MODULE_TRUST_CERTS module_trust_certs;
int ret = edge_daemon_trust_certs(&module_trust_certs);
if (ret != EDGE_SUCCESS) {
printf("get module trust cert fail.");
return;
}
printf("get module trust cert success.");
printf("module trust cert certificate:%s\n", module_trust_certs.certificate);
printf("module trust cert expires_at:%lld\n", module_trust_certs.expires_at);
}
void send_send_get_sub_device_shadow()
{
ST_DEVICE_SHADOW_GET device_shadow_get = {0};
device_shadow_get.service_id = "service1";
device_shadow_get.object_device_id = "deviceId1";
char* request_id = "123";
edge_send_get_sub_device_shadow(request_id, &device_shadow_get);
}
void send_sub_device_message()
{
ST_DEVICE_MESSAGE device_message = {0};
device_message.object_device_id = "deviceId1";
device_message.name = "message_name";
device_message.id = "message_id";
device_message.content = "{\n"
"\t\"module_id\": \"module_id\",\n"
"\t\"old_status\": \"STOPPED|RUNNING|UNHEALTHY\",\n"
"\t\"new_status\": \"STOPPED|RUNNING|UNHEALTHY\"\n"
"}";
device_message.content_len = (int)strlen(device_message.content);
edge_send_sub_device_message(&device_message);
}
void send_sub_device_event()
{
ST_DEVICE_EVENT device_event = {0};
device_event.object_device_id = "deviceId1";
ST_SERVICE_EVENT service_event = {0};
service_event.service_id = "message_name";
service_event.event_id = "message_id";
service_event.event_type = "message_name";
service_event.paras = "{\n"
"\t\"module_id\": \"module_id\",\n"
"\t\"old_status\": \"STOPPED|RUNNING|UNHEALTHY\",\n"
"\t\"new_status\": \"STOPPED|RUNNING|UNHEALTHY\"\n"
"}";
service_event.paras_len = (int)strlen(service_event.paras);
device_event.services = &service_event;
device_event.services_size = 1;
edge_send_sub_device_event(&device_event);
}
void send_get_sub_device_product()
{
char* product_ids[10] = {0};
product_ids[0] = "1234";
product_ids[1] = "1234";
edge_send_get_product(product_ids, 2);
}
void send_customized_message()
{
char* topic = "hello";
char* body = "{\n"
"\t\"PhV_phsA\":1,\n"
"\t\"PhV_phsB\":2\n"
"}";;
edge_send_customized_message(topic, body, strlen(body));
}
void send_customized_message2()
{
char* topic = "/hello/test2";
char* body = "{\n"
"\t\"PhV_phsA\":1,\n"
"\t\"PhV_phsB\":2\n"
"}";;
edge_send_customized_message(topic, body, strlen(body));
}
void test_get_devices_statuses()
{
const char* device_ids[2] = {0};
device_ids[0] = "dev1";
device_ids[1] = "dev2";
ST_DEVICES_STATUSES devices_statuses = {0};
edge_init_devices_statuses(&devices_statuses, 2);
if (0 == edge_get_devices_statuses(&devices_statuses, device_ids, 2, 1000))
{
int count = 0;
for (; count < 2; count++)
{
printf("%s: %s\n", devices_statuses.devices[count].device_id, devices_statuses.devices[count].status);
}
}
edge_release_devices_statuses(&devices_statuses);
}
void test_set_send_mode()
{
edge_set_send_mode(SEND_SYNC);
sleep(1);
sendPointReport();
sleep(1);
send_batch_device_data();
edge_set_send_mode(SEND_ASYNC);
sleep(1);
sendPointReport();
sleep(1);
send_batch_device_data();
}
void test_broker_overloaded()
{
int test_count = 10;
edge_forbid_send_when_broker_overloaded(_TRUE);
while (test_count-- >= 0) {
sendPointReport();
sleep(5);
}
edge_forbid_send_when_broker_overloaded(_FALSE);
while (test_count++ <= 10) {
sendPointReport();
sleep(5);
}
}
下面是主函数demo,里面包括调用上面所有的demo函数,用不到的地方注释掉即可。
int main()
{
// 禁用缓冲区
setvbuf(stdout, NULL, _IONBF, 0);
//初始化sdk,工作路径设置(工作路径下需要含有 /conf 目录(该目录下包含证书等信息))
edge_init("../code/api_test/workdir");
printf("demo start.\n");
ST_MODULE_CBS cbs = {0};
cbs.pfn_command_cb = moduleCmdCb;
cbs.pfn_shadow_cb = shadowCb;
cbs.pfn_customized_message_cb = customizedMessageCb;
cbs.pfn_connected = connected;
cbs.pfn_disconnected = disconnected;
cbs.pfn_module_request_cb = moduleRequestCb;
ST_DC_CLIENT_CBS dc_client_cbs = {0};
dc_client_cbs.pfn_module_points_set_cb = pointsSetReceiveCb;
dc_client_cbs.pfn_module_points_get_cb = pointsGetReceiveCb;
ST_DEVICE_CBS device_cbs = {0};
device_cbs.pfn_sub_device_add_cb = sub_device_add_cb;
device_cbs.pfn_device_command_cb = deviceCmdCb;
device_cbs.pfn_sub_device_deleted_cb = sub_device_delete_cb;
device_cbs.pfn_device_event_cb = sub_device_event_cb;
device_cbs.pfn_on_start_scan_cb = sub_device_start_scan_cb;
device_cbs.pfn_device_message_cb = sub_device_messages_down_cb;
device_cbs.pfn_on_get_products_rsp_cb = get_products_response_cb;
device_cbs.pfn_device_properties_set_cb = sub_device_properties_set_cb;
device_cbs.pfn_device_properties_get_cb = sub_device_properties_get_cb;
device_cbs.pfn_device_shadow_cb = sub_device_shadow_cb;
//设置回调函数(无需全部设置,按需设置需要接受的回调)
edge_set_callbacks(&cbs, &device_cbs);
edge_dc_driver_set_callbacks(&dc_client_cbs);
//连接hub
edge_login();
sleep(10);
test_get_devices_statuses();
send_send_get_sub_device_shadow();
sleep(1);
send_sub_device_message();
sleep(1);
send_sub_device_event();
sleep(1);
send_get_sub_device_product();
sleep(1);
send_customized_message();
sleep(1);
send_customized_message2();
test_set_send_mode();
sleep(1);
send_batch_device_discovery_data();
test_broker_overloaded();
while(1) {
sleep(1000);
}
//登出
edge_logout();
sleep(10000);
//清理
edge_destroy();
return 0;
}
父主题: ModuleSDK-C Demo展示