升级EVB_AIoT快速入门指南中mqtt_config_gen.py工具脚本,使用预定义常量到mqtt连接代码中

This commit is contained in:
HonestQiao
2021-12-27 14:10:50 +08:00
parent ebf7db4948
commit 6a924fc902
2 changed files with 73 additions and 12 deletions

View File

@@ -37,7 +37,7 @@ username = username.encode("utf-8")
passwd = passwd.encode("utf-8")
sign = hmacsha1(username, passwd)
template = ('#ifndef TOS_MQTT_CONFIG_H\n'
template_header = ('#ifndef TOS_MQTT_CONFIG_H\n'
'#define TOS_MQTT_CONFIG_H\n'
'\n'
'#define MQTT_SERVER_IP "111.230.189.156"\n'
@@ -50,22 +50,52 @@ template = ('#ifndef TOS_MQTT_CONFIG_H\n'
'#define MQTT_SUBSCRIBE_TOPIC "$product/$dev/$sub"\n'
'#define MQTT_PUBLISH_TOPIC "$product/$dev/$pub"\n'
'\n'
'#define MQTT_SERVER_DOMAIN "$product.iotcloud.tencentdevices.com"\n'
'#define MQTT_TOPIC "$product/$dev"\n'
'#define MQTT_SUBSCRIBE_TOPIC_DOWN "$thing/down/property/$product/$dev"\n'
'#define MQTT_PUBLISH_TOPIC_UP "$thing/up/property/$product/$dev"\n'
'\n'
'#endif\n'
'\n')
src = Template(template)
template_c = ('#ifndef TOS_MQTT_CONFIG_H\n'
'tos_sal_module_parse_domain(MQTT_SERVER_DOMAIN,host_ip,sizeof(host_ip));\n'
'\n'
'mqtt_set_port(client, MQTT_SERVER_PORT);\n'
'mqtt_set_host(client, host_ip);\n'
'mqtt_set_client_id(client, MQTT_CLIENT_ID);\n'
'mqtt_set_user_name(client, MQTT_USR_NAME);\n'
'mqtt_set_password(client, MQTT_PASSWORD);\n'
'mqtt_set_clean_session(client, 1);\n'
'\n'
'error = mqtt_subscribe(client, MQTT_SUBSCRIBE_TOPIC_DOWN, QOS0, tos_topic_handler);\n'
'\n'
'error = mqtt_publish(client, MQTT_PUBLISH_TOPIC_UP, &msg);\n'
'\n'
'#endif\n'
'\n')
src_header = Template(template_header)
src_c = Template(template_c)
d = {
'product':product_id,
'dev':dev_name,
'sign':sign,
'sub':sub,
'pub':pub
'pub':pub,
'thing':'$thing'
}
#do the substitution
dst = src.substitute(d)
dst_header = src_header.substitute(d)
dst_c = src_c.substitute(d)
print("===============Generate mqtt_config.h==================")
print(dst)
print(dst_header)
with open('mqtt_config.h', 'w') as f:
f.write(dst)
f.write(dst_header)
print("===============Generate mqtt_connect_demo.c==================")
print(dst_c)
with open('mqtt_connect_demo.c', 'w') as f:
f.write(dst_c)