/
test_tm_property.c
128 lines (105 loc) · 4.41 KB
/
test_tm_property.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* Copyright 2022-2024 Beijing Volcano Engine Technology Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//
// 属性上报
//
#define TAG "test_tm_property.c"
#include <unistd.h>
#include <core/iot_log.h>
#include <thing_model/iot_tm_api.h>
#include "core/iot_mqtt.h"
#include "thing_model/property.h"
#include "test_params.h"
char * SAMPLE_HTTP_HOST = "<PROVIDE_CORRECT_VALUE_HERE>";
char * SAMPLE_INSTANCE_ID = "<PROVIDE_CORRECT_VALUE_HERE>";
char * SAMPLE_MQTT_HOST = "<PROVIDE_CORRECT_VALUE_HERE>";
char * SAMPLE_DEVICE_NAME = "<PROVIDE_CORRECT_VALUE_HERE>";
char * SAMPLE_DEVICE_SECRET = "<PROVIDE_CORRECT_VALUE_HERE>";
char * SAMPLE_PRODUCT_KEY = "<PROVIDE_CORRECT_VALUE_HERE>";
char * SAMPLE_PRODUCT_SECRET = "<PROVIDE_CORRECT_VALUE_HERE>";
tm_handle_t *dm;
void s_test_iot_mqtt_topic_handler_fn(void *mqtt_ctx, iot_mqtt_pub_data_t *pub_data, void *userdata) {
LOGD(TAG, "s_test_iot_mqtt_topic_handler_fn topic =%s payload =%s ", pub_data->topic, pub_data->payload);
}
void s_test_iot_mqtt_event_callback_fn(void *mqtt_ctx, iot_mqtt_event_type_t event, iot_mqtt_event_data_t data, void *userdata) {
LOGD(TAG, "s_test_iot_mqtt_event_callback_fn event =%d data.error_code = %d data.return_code = %d", event, data.error_code, data.return_code);
switch (event) {
case IOT_MQTTEVT_CONNECT: {
}
break;
}
}
void test_aiot_dm_recv_handler_t(void *handler, const iot_tm_recv_t *recv, void *userdata) {
switch (recv->type) {
case IOT_TM_RECV_PROPERTY_SET_POST_REPLY: {
DEVICE_LOGD(TAG, "test_aiot_dm_recv_handler_t property_set id = %s, code = %d",
recv->data.property_set_post_reply.msg_id, recv->data.property_set_post_reply.code);
}
break;
}
}
int main(void) {
int ret = 0;
// 初始化
iot_core_init();
// 设置 log 保存地址
iot_log_init(g_log_dir);
// mqtt 初始化
iot_mqtt_ctx_t *mqtt_ctx = iot_mqtt_init();
// 设置 mqtt 连接配置
iot_mqtt_set_http_host(mqtt_ctx, SAMPLE_HTTP_HOST);
iot_mqtt_set_instance_id(mqtt_ctx, SAMPLE_INSTANCE_ID);
iot_mqtt_set_host(mqtt_ctx, SAMPLE_MQTT_HOST);
iot_mqtt_set_port(mqtt_ctx, 1883);
iot_mqtt_set_device_name(mqtt_ctx, SAMPLE_DEVICE_NAME);
iot_mqtt_set_device_secret(mqtt_ctx, SAMPLE_DEVICE_SECRET);
iot_mqtt_set_product_key(mqtt_ctx, SAMPLE_PRODUCT_KEY);
iot_mqtt_set_product_secret(mqtt_ctx, SAMPLE_PRODUCT_SECRET);
// 设置连接鉴权类型
iot_mqtt_set_auth_type(mqtt_ctx, IOT_AUTH_DEVICE_SECRET);
// 设置全局的 topic handler
iot_mqtt_add_global_receiver_topic_handler_fn(mqtt_ctx, s_test_iot_mqtt_topic_handler_fn, NULL);
// 设置mqtt 连接事件回调
iot_mqtt_set_event_handler_fn(mqtt_ctx, s_test_iot_mqtt_event_callback_fn, NULL);
// 连接 Mqtt 同步
ret = iot_connect(mqtt_ctx);
if (ret != 0) {
DEVICE_LOGD("test_mqtt", "iot_connect error ret =%d ", ret);
iot_mqtt_clean(mqtt_ctx);
return -1;
}
dm = iot_tm_init();
iot_tm_set_mqtt_handler(dm, mqtt_ctx);
iot_tm_set_tm_recv_handler_t(dm, test_aiot_dm_recv_handler_t, dm);
// 发送 属性
iot_tm_msg_t dm_msg2 = {0};
dm_msg2.type = IOT_TM_MSG_PROPERTY_POST;
iot_tm_msg_property_post_t *property_post;
iot_property_post_init(&property_post);
iot_property_post_add_param_num(property_post, "default:ErrorCurrentThreshold", 0.8);
iot_property_post_add_param_num(property_post, "default:OverVoltThreshold", 1);
iot_property_post_add_param_json_str(property_post, "default:GeoLocation", "{\"Altitude\":0,\"Latitude\":-90,\"Longitude\":-180}");
dm_msg2.data.property_post = property_post;
iot_tm_send(dm, &dm_msg2);
iot_property_post_free(property_post);
sleep(1000);
ret = iot_mqtt_disconnect(mqtt_ctx);
if (ret != 0) {
DEVICE_LOGD(TAG, "iot_disconnect error ret =%d ", ret);
iot_mqtt_clean(mqtt_ctx);
return -1;
}
}