更新时间:2024-10-21 GMT+08:00
Demo1
该Demo主要展示edge.h和edge_daemon.h里的回调函数和接口函数使用。
#include "edge.h" #include "edge_daemon.h" #include <stdio.h> #include <unistd.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:%d", 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:%d", 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; } /* * 描述:收到设备上报数据的回调处理 * 参数: * device_id:设备ID * product_id: 产品ID * body: 上报的数据 * body_len: 上报数据的大小 * */ EDGE_RETCODE messageReceivedCb(const char* device_id, const char* product_id, const char* body, unsigned int body_len) { printf("body is: %s\nbody len is: %d\n", body, body_len); printf("product_id is: %s, device_id: %s\n", product_id, device_id); return EDGE_SUCCESS; } /* * 描述:子设备添加的回调 * 参数: * 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 = %d\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 devcie_data_1 = {0}; ST_DEVICE_SERVICE devcie_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; devcie_data_1.services = service_data; devcie_data_1.device_id = "device_id_1"; devcie_data_1.size = 2; devcie_data_2.services = service_data; devcie_data_2.device_id = "device_id_2"; devcie_data_2.size = 2; device_data[0] = devcie_data_1; device_data[1] = devcie_data_2; edge_send_batch_device_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)); 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); send_batch_device_data(); 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)); }
下面是主函数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_on_message_received_cb = messageReceivedCb; 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); //连接hub edge_login(); sleep(10); 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(); while(1) { sleep(1000); } //登出 edge_logout(); sleep(10000); //清理 edge_destroy(); return 0; }
父主题: ModuleSDK-C Demo展示