提交 df503b11 编写于 作者: 麦壳饼's avatar 麦壳饼

Refactoring Code

上级 5aeb4c0d
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
"generator": "Unix Makefiles", "generator": "Unix Makefiles",
"configurationType": "Debug", "configurationType": "Debug",
"buildRoot": "${projectDir}\\build\\", "buildRoot": "${projectDir}\\build\\",
"installRoot": "${projectDir}\\out\\install\\${name}", "installRoot": "${projectDir}\\build\\",
"cmakeExecutable": "/usr/bin/cmake", "cmakeExecutable": "/usr/bin/cmake",
"cmakeCommandArgs": "", "cmakeCommandArgs": "",
"buildCommandArgs": "", "buildCommandArgs": "",
......
/*
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the GNU Affero General Public License, version 3
* or later ("AGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef TDENGINE_MQTT_INIT_H
#define TDENGINE_MQTT_INIT_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#include "MQTTAsync.h"
#include "os.h"
#include "taos.h"
#include "tglobal.h"
#include "tsocket.h"
#include "ttimer.h"
#include "tsclient.h"
char split(char str[], char delims[], char** p_p_cmd_part, int max);
void mqttConnnectLost(void* context, char* cause);
int mqttMessageArrived(void* context, char* topicName, int topicLen, MQTTAsync_message* message);
void mqtt_query_insert_callback(void* param, TAOS_RES* result, int32_t code);
void onDisconnectFailure(void* context, MQTTAsync_failureData* response);
void onDisconnect(void* context, MQTTAsync_successData* response);
void onSubscribe(void* context, MQTTAsync_successData* response);
void onSubscribeFailure(void* context, MQTTAsync_failureData* response);
void mqttInitConnCb(void* param, TAOS_RES* result, int32_t code);
#define CLIENTID "taos"
#define TOPIC "/taos/+/+/+/" // taos/<token>/<db name>/<table name>/
#define PAYLOAD "Hello World!"
#define QOS 1
#define TIMEOUT 10000L
#ifdef __cplusplus
}
#endif
#endif
/*
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the GNU Affero General Public License, version 3
* or later ("AGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef TDENGINE_MQTT_PLYLOAD_H
#define TDENGINE_MQTT_PLYLOAD_H
#ifdef __cplusplus
extern "C" {
#endif
char split(char str[], char delims[], char** p_p_cmd_part, int max);
char* converJsonToSql(char* json, char* _dbname, char* _tablename);
#ifdef __cplusplus
}
#endif
#endif
...@@ -18,28 +18,11 @@ ...@@ -18,28 +18,11 @@
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
#include <stdint.h> #include <stdint.h>
#include "MQTTAsync.h"
#include "os.h"
#include "taos.h"
#include "tglobal.h"
#include "tsocket.h"
#include "ttimer.h"
#include "tsclient.h"
int32_t mqttInitSystem(); int32_t mqttInitSystem();
int32_t mqttStartSystem(); int32_t mqttStartSystem();
void mqttStopSystem(); void mqttStopSystem();
void mqttCleanUpSystem(); void mqttCleanUpSystem();
char split(char str[], char delims[], char** p_p_cmd_part, int max);
void connlost(void* context, char* cause);
int msgarrvd(void* context, char* topicName, int topicLen, MQTTAsync_message* message);
void mqtt_query_insert_callback(void* param, TAOS_RES* result, int32_t code);
void onDisconnectFailure(void* context, MQTTAsync_failureData* response);
void onDisconnect(void* context, MQTTAsync_successData* response);
void onSubscribe(void* context, MQTTAsync_successData* response);
void onSubscribeFailure(void* context, MQTTAsync_failureData* response);
void mqttInitConnCb(void* param, TAOS_RES* result, int32_t code);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
......
/*
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the GNU Affero General Public License, version 3
* or later ("AGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define _DEFAULT_SOURCE
#include "mqttUitl.h"
#include "cJSON.h"
#include "string.h"
#include "taos.h"
#include "mqttLog.h"
#include "os.h"
char split(char str[], char delims[], char** p_p_cmd_part, int max) {
char* token = strtok(str, delims);
char part_index = 0;
char** tmp_part = p_p_cmd_part;
while (token) {
*tmp_part++ = token;
token = strtok(NULL, delims);
part_index++;
if (part_index >= max) break;
}
return part_index;
}
char* converJsonToSql(char* json, char* _dbname, char* _tablename) {
cJSON* jPlayload = cJSON_Parse(json);
char _names[102400] = {0};
char _values[102400] = {0};
int i = 0;
int count = cJSON_GetArraySize(jPlayload);
for (; i < count; i++) //jsonֵ
{
cJSON* item = cJSON_GetArrayItem(jPlayload, i);
if (cJSON_Object == item->type) {
mqttPrint("The item '%s' is not supported", item->string);
} else {
strcat(_names, item->string);
if (i < count - 1) {
strcat(_names, ",");
}
char* __value_json = cJSON_Print(item);
strcat(_values, __value_json);
free(__value_json);
if (i < count - 1) {
strcat(_values, ",");
}
}
}
cJSON_free(jPlayload);
char _sql[102400] = {0};
sprintf(_sql, "INSERT INTO %s.%s (%s) VALUES(%s);", _dbname, _tablename, _names, _values);
return _sql;
}
\ No newline at end of file
...@@ -26,12 +26,8 @@ ...@@ -26,12 +26,8 @@
#include "tsclient.h" #include "tsclient.h"
#include "tsocket.h" #include "tsocket.h"
#include "ttimer.h" #include "ttimer.h"
#include "mqttInit.h"
#define CLIENTID "taos" #include "mqttPlyload.h"
#define TOPIC "/taos/+/+/+/" // taos/<token>/<db name>/<table name>/
#define PAYLOAD "Hello World!"
#define QOS 1
#define TIMEOUT 10000L
MQTTAsync client; MQTTAsync client;
MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer; MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
...@@ -42,7 +38,7 @@ int subscribed = 0; ...@@ -42,7 +38,7 @@ int subscribed = 0;
int finished = 0; int finished = 0;
int can_exit = 0; int can_exit = 0;
void connlost(void* context, char* cause) { void mqttConnnectLost(void* context, char* cause) {
MQTTAsync client = (MQTTAsync)context; MQTTAsync client = (MQTTAsync)context;
MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer; MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
int rc; int rc;
...@@ -58,20 +54,9 @@ void connlost(void* context, char* cause) { ...@@ -58,20 +54,9 @@ void connlost(void* context, char* cause) {
finished = 1; finished = 1;
} }
} }
char split(char str[], char delims[], char** p_p_cmd_part, int max) {
char* token = strtok(str, delims);
char part_index = 0;
char** tmp_part = p_p_cmd_part;
while (token) {
*tmp_part++ = token;
token = strtok(NULL, delims);
part_index++;
if (part_index >= max) break;
}
return part_index;
}
int msgarrvd(void* context, char* topicName, int topicLen, MQTTAsync_message* message) {
int mqttMessageArrived(void* context, char* topicName, int topicLen, MQTTAsync_message* message) {
mqttTrace("Message arrived,topic is %s,message is %.*s", topicName, message->payloadlen, (char*)message->payload); mqttTrace("Message arrived,topic is %s,message is %.*s", topicName, message->payloadlen, (char*)message->payload);
char _token[128] = {0}; char _token[128] = {0};
char _dbname[128] = {0}; char _dbname[128] = {0};
...@@ -93,36 +78,10 @@ int msgarrvd(void* context, char* topicName, int topicLen, MQTTAsync_message* me ...@@ -93,36 +78,10 @@ int msgarrvd(void* context, char* topicName, int topicLen, MQTTAsync_message* me
strncpy(_tablename, p_p_cmd_part[3], 127); strncpy(_tablename, p_p_cmd_part[3], 127);
mqttPrint("part count=%d,access token:%s,database name:%s, table name:%s", part_index, _token, _dbname, mqttPrint("part count=%d,access token:%s,database name:%s, table name:%s", part_index, _token, _dbname,
_tablename); _tablename);
cJSON* jPlayload = cJSON_Parse((char*)message->payload); char* sql = converJsonToSql((char*)message->payload, _dbname, _tablename);
char _names[102400] = {0};
char _values[102400] = {0};
int i = 0;
int count = cJSON_GetArraySize(jPlayload);
for (; i < count; i++) //jsonֵ
{
cJSON* item = cJSON_GetArrayItem(jPlayload, i);
if (cJSON_Object == item->type) {
mqttPrint("The item '%s' is not supported", item->string);
} else {
strcat(_names, item->string);
if (i < count - 1) {
strcat(_names, ",");
}
char* __value_json = cJSON_Print(item);
strcat(_values, __value_json);
free(__value_json);
if (i < count - 1) {
strcat(_values, ",");
}
}
}
cJSON_free(jPlayload);
char _sql[102400] = {0};
sprintf(_sql, "INSERT INTO %s.%s (%s) VALUES(%s);", _dbname, _tablename, _names, _values);
if (mqtt_conn != NULL) { if (mqtt_conn != NULL) {
mqttPrint("query:%s", _sql); mqttPrint("query:%s", _sql);
taos_query_a(mqtt_conn, _sql, mqtt_query_insert_callback, &client); taos_query_a(mqtt_conn, _sql, mqttQueryInsertCallback, &client);
} }
} }
} }
...@@ -130,7 +89,7 @@ int msgarrvd(void* context, char* topicName, int topicLen, MQTTAsync_message* me ...@@ -130,7 +89,7 @@ int msgarrvd(void* context, char* topicName, int topicLen, MQTTAsync_message* me
MQTTAsync_free(topicName); MQTTAsync_free(topicName);
return 1; return 1;
} }
void mqtt_query_insert_callback(void* param, TAOS_RES* result, int32_t code) { void mqttQueryInsertCallback(void* param, TAOS_RES* result, int32_t code) {
if (code < 0) { if (code < 0) {
mqttError("mqtt:%d, save data failed, code:%s", code, tstrerror(code)); mqttError("mqtt:%d, save data failed, code:%s", code, tstrerror(code));
} else if (code == 0) { } else if (code == 0) {
...@@ -198,7 +157,7 @@ int32_t mqttInitSystem() { ...@@ -198,7 +157,7 @@ int32_t mqttInitSystem() {
rc = EXIT_FAILURE; rc = EXIT_FAILURE;
} else { } else {
if ((rc = MQTTAsync_setCallbacks(client, client, connlost, msgarrvd, NULL)) != MQTTASYNC_SUCCESS) { if ((rc = MQTTAsync_setCallbacks(client, client, mqttConnnectLost, mqttMessageArrived, NULL)) != MQTTASYNC_SUCCESS) {
mqttError("Failed to set callbacks, return code %d", rc); mqttError("Failed to set callbacks, return code %d", rc);
rc = EXIT_FAILURE; rc = EXIT_FAILURE;
} else { } else {
......
# Copyright (c) 2017 by TAOS Technologies, Inc. # CMAKE generated file: DO NOT EDIT!
# todo: library dependency, header file dependency # Generated by "Unix Makefiles" Generator, CMake Version 3.10
ROOT=./ # Default target executed when no arguments are given to make.
TARGET=exe default_target: all
LFLAGS = '-Wl,-rpath,/usr/local/taos/driver/' -ltaos -lpthread -lm -lrt
#LFLAGS = '-Wl,-rpath,/home/zbm/project/td/debug/build/lib/' -L/home/zbm/project/td/debug/build/lib -ltaos -lpthread -lm -lrt
CFLAGS = -O3 -g -Wall -Wno-deprecated -fPIC -Wno-unused-result -Wconversion -Wno-char-subscripts -D_REENTRANT -Wno-format -D_REENTRANT -DLINUX -msse4.2 -Wno-unused-function -D_M_X64 \
-I/usr/local/taos/include -std=gnu99
all: $(TARGET) .PHONY : default_target
exe: # Allow only one "make -f Makefile2" at a time, but pass parallelism.
gcc $(CFLAGS) ./asyncdemo.c -o $(ROOT)/asyncdemo $(LFLAGS) .NOTPARALLEL:
gcc $(CFLAGS) ./demo.c -o $(ROOT)/demo $(LFLAGS)
gcc $(CFLAGS) ./prepare.c -o $(ROOT)/prepare $(LFLAGS)
gcc $(CFLAGS) ./stream.c -o $(ROOT)/stream $(LFLAGS)
gcc $(CFLAGS) ./subscribe.c -o $(ROOT)subscribe $(LFLAGS)
#=============================================================================
# Special targets provided by cmake.
# Disable implicit rules so canonical targets will work.
.SUFFIXES:
# Remove some rules from gmake that .SUFFIXES does not remove.
SUFFIXES =
.SUFFIXES: .hpux_make_needs_suffix_list
# Produce verbose output by default.
VERBOSE = 1
# Suppress display of executed commands.
$(VERBOSE).SILENT:
# A target that is always out of date.
cmake_force:
.PHONY : cmake_force
#=============================================================================
# Set environment variables for the build.
# The shell in which to execute make rules.
SHELL = /bin/sh
# The CMake executable.
CMAKE_COMMAND = /usr/bin/cmake
# The command to remove a file.
RM = /usr/bin/cmake -E remove -f
# Escaping for special characters.
EQUALS = =
# The top-level source directory on which CMake was run.
CMAKE_SOURCE_DIR = /mnt/d/TDengine/TDengine
# The top-level build directory on which CMake was run.
CMAKE_BINARY_DIR = /mnt/d/TDengine/TDengine
#=============================================================================
# Targets provided globally by CMake.
# Special rule for the target install/strip
install/strip: preinstall
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Installing the project stripped..."
/usr/bin/cmake -DCMAKE_INSTALL_DO_STRIP=1 -P cmake_install.cmake
.PHONY : install/strip
# Special rule for the target install/strip
install/strip/fast: preinstall/fast
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Installing the project stripped..."
/usr/bin/cmake -DCMAKE_INSTALL_DO_STRIP=1 -P cmake_install.cmake
.PHONY : install/strip/fast
# Special rule for the target install/local
install/local: preinstall
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Installing only the local directory..."
/usr/bin/cmake -DCMAKE_INSTALL_LOCAL_ONLY=1 -P cmake_install.cmake
.PHONY : install/local
# Special rule for the target install/local
install/local/fast: preinstall/fast
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Installing only the local directory..."
/usr/bin/cmake -DCMAKE_INSTALL_LOCAL_ONLY=1 -P cmake_install.cmake
.PHONY : install/local/fast
# Special rule for the target install
install: preinstall
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Install the project..."
/usr/bin/cmake -P cmake_install.cmake
.PHONY : install
# Special rule for the target install
install/fast: preinstall/fast
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Install the project..."
/usr/bin/cmake -P cmake_install.cmake
.PHONY : install/fast
# Special rule for the target package
package: preinstall
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Run CPack packaging tool..."
cd /mnt/d/TDengine/TDengine && /usr/bin/cpack --config ./CPackConfig.cmake
.PHONY : package
# Special rule for the target package
package/fast: package
.PHONY : package/fast
# Special rule for the target package_source
package_source:
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Run CPack packaging tool for source..."
cd /mnt/d/TDengine/TDengine && /usr/bin/cpack --config ./CPackSourceConfig.cmake /mnt/d/TDengine/TDengine/CPackSourceConfig.cmake
.PHONY : package_source
# Special rule for the target package_source
package_source/fast: package_source
.PHONY : package_source/fast
# Special rule for the target edit_cache
edit_cache:
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "No interactive CMake dialog available..."
/usr/bin/cmake -E echo No\ interactive\ CMake\ dialog\ available.
.PHONY : edit_cache
# Special rule for the target edit_cache
edit_cache/fast: edit_cache
.PHONY : edit_cache/fast
# Special rule for the target rebuild_cache
rebuild_cache:
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running CMake to regenerate build system..."
/usr/bin/cmake -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)
.PHONY : rebuild_cache
# Special rule for the target rebuild_cache
rebuild_cache/fast: rebuild_cache
.PHONY : rebuild_cache/fast
# Special rule for the target list_install_components
list_install_components:
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Available install components are: \"Unspecified\""
.PHONY : list_install_components
# Special rule for the target list_install_components
list_install_components/fast: list_install_components
.PHONY : list_install_components/fast
# The main all target
all: cmake_check_build_system
cd /mnt/d/TDengine/TDengine && $(CMAKE_COMMAND) -E cmake_progress_start /mnt/d/TDengine/TDengine/CMakeFiles /mnt/d/TDengine/TDengine/tests/examples/c/CMakeFiles/progress.marks
cd /mnt/d/TDengine/TDengine && $(MAKE) -f CMakeFiles/Makefile2 tests/examples/c/all
$(CMAKE_COMMAND) -E cmake_progress_start /mnt/d/TDengine/TDengine/CMakeFiles 0
.PHONY : all
# The main clean target
clean: clean:
rm $(ROOT)/asyncdemo cd /mnt/d/TDengine/TDengine && $(MAKE) -f CMakeFiles/Makefile2 tests/examples/c/clean
rm $(ROOT)/demo .PHONY : clean
rm $(ROOT)/prepare
rm $(ROOT)/stream # The main clean target
rm $(ROOT)/subscribe clean/fast: clean
.PHONY : clean/fast
# Prepare targets for installation.
preinstall: all
cd /mnt/d/TDengine/TDengine && $(MAKE) -f CMakeFiles/Makefile2 tests/examples/c/preinstall
.PHONY : preinstall
# Prepare targets for installation.
preinstall/fast:
cd /mnt/d/TDengine/TDengine && $(MAKE) -f CMakeFiles/Makefile2 tests/examples/c/preinstall
.PHONY : preinstall/fast
# clear depends
depend:
cd /mnt/d/TDengine/TDengine && $(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 1
.PHONY : depend
# Convenience name for target.
tests/examples/c/CMakeFiles/demo.dir/rule:
cd /mnt/d/TDengine/TDengine && $(MAKE) -f CMakeFiles/Makefile2 tests/examples/c/CMakeFiles/demo.dir/rule
.PHONY : tests/examples/c/CMakeFiles/demo.dir/rule
# Convenience name for target.
demo: tests/examples/c/CMakeFiles/demo.dir/rule
.PHONY : demo
# fast build rule for target.
demo/fast:
cd /mnt/d/TDengine/TDengine && $(MAKE) -f tests/examples/c/CMakeFiles/demo.dir/build.make tests/examples/c/CMakeFiles/demo.dir/build
.PHONY : demo/fast
demo.o: demo.c.o
.PHONY : demo.o
# target to build an object file
demo.c.o:
cd /mnt/d/TDengine/TDengine && $(MAKE) -f tests/examples/c/CMakeFiles/demo.dir/build.make tests/examples/c/CMakeFiles/demo.dir/demo.c.o
.PHONY : demo.c.o
demo.i: demo.c.i
.PHONY : demo.i
# target to preprocess a source file
demo.c.i:
cd /mnt/d/TDengine/TDengine && $(MAKE) -f tests/examples/c/CMakeFiles/demo.dir/build.make tests/examples/c/CMakeFiles/demo.dir/demo.c.i
.PHONY : demo.c.i
demo.s: demo.c.s
.PHONY : demo.s
# target to generate assembly for a file
demo.c.s:
cd /mnt/d/TDengine/TDengine && $(MAKE) -f tests/examples/c/CMakeFiles/demo.dir/build.make tests/examples/c/CMakeFiles/demo.dir/demo.c.s
.PHONY : demo.c.s
# Help Target
help:
@echo "The following are some of the valid targets for this Makefile:"
@echo "... all (the default if no target is provided)"
@echo "... clean"
@echo "... depend"
@echo "... install/strip"
@echo "... install/local"
@echo "... install"
@echo "... package"
@echo "... demo"
@echo "... package_source"
@echo "... edit_cache"
@echo "... rebuild_cache"
@echo "... list_install_components"
@echo "... demo.o"
@echo "... demo.i"
@echo "... demo.s"
.PHONY : help
#=============================================================================
# Special targets to cleanup operation of make.
# Special rule to run CMake to check the build system integrity.
# No rule that depends on this can have commands that come from listfiles
# because they might be regenerated.
cmake_check_build_system:
cd /mnt/d/TDengine/TDengine && $(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 0
.PHONY : cmake_check_build_system
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册