diff --git a/Makefile b/Makefile index 6777fbe..58dcf68 100644 --- a/Makefile +++ b/Makefile @@ -1,74 +1,74 @@ -TARGET = Cidn-SH -OBJ_PATH = $(shell pwd)/debug -PREFIX_BIN = - -CC = ${CXX} -STRIP = aarch64-poky-linux-strip -INCLUDES = -I../Tools/GatewayThirdParty/boost/include \ - -I../Tools/GatewayThirdParty/fftw/include \ - -I../Tools/GatewayThirdParty/jsoncpp/include \ - -I../Tools/GatewayThirdParty/sqlite/include/ \ - -I../Tools/GatewayThirdParty/curl/include/ \ - -I../Tools/GatewayThirdParty/zlog/include \ - -I ./ \ - -LIBS = -L../Tools/renesas_thirdparty/lib \ - -L../Tools/renesas_thirdparty/lib \ - -L../Tools/renesas_thirdparty/lib \ - -L../Tools/renesas_thirdparty/lib \ - -L../Tools/renesas_thirdparty/lib \ - -L../Tools/renesas_thirdparty/lib \ - -lsqlite3 -lboost_system -lpthread -lboost_thread -lboost_date_time -lboost_filesystem -lfftw3 -ljsoncpp -lmosquitto -lcurl -lzlog - -CFLAGS = -O0 -g -std=c++14 -fpermissive -fstack-protector-all #-Wall -Werror -LINKFLAGS = - -AllDirs :=$(shell ls -R | grep '^\./.*:' | awk '{if( \ - substr($$1,3,5) != "debug") {gsub(":",""); print}}') . - -Sources := $(foreach n,$(AllDirs) , $(wildcard $(n)/*.cpp)) - -#C_SOURCES = $(wildcard *.c) -C_SRCDIR = $(AllDirs) -C_SOURCES = $(foreach d,$(C_SRCDIR),$(wildcard $(d)/*.c) ) -C_OBJS = $(patsubst %.c, $(OBJ_PATH)/%.o, $(C_SOURCES)) - -#CPP_SOURCES = $(wildcard *.cpp) -CPP_SRCDIR = $(AllDirs) -CPP_SOURCES = $(foreach d,$(CPP_SRCDIR),$(wildcard $(d)/*.cpp) ) -CPP_OBJS = $(patsubst %.cpp, $(OBJ_PATH)/%.o, $(CPP_SOURCES)) - -default:init compile strip - -$(C_OBJS):$(OBJ_PATH)/%.o:%.c - $(CC) -c $(CFLAGS) $(INCLUDES) $< -o $@ - -$(CPP_OBJS):$(OBJ_PATH)/%.o:%.cpp - $(CC) -c $(CFLAGS) $(INCLUDES) $< -o $@ - -init: - $(foreach d,$(AllDirs), mkdir -p $(OBJ_PATH)/$(d);) - -test: - @echo "C_SOURCES: $(C_SOURCES)" - @echo "C_OBJS: $(C_OBJS)" - @echo "CPP_SOURCES: $(CPP_SOURCES)" - @echo "CPP_OBJS: $(CPP_OBJS)" - -compile:$(C_OBJS) $(CPP_OBJS) - $(CC) -O2 -o $(TARGET) $^ $(LINKFLAGS) $(LIBS) - -clean: - rm -rf $(OBJ_PATH) - rm -f $(TARGET) - -install: $(TARGET) - cp $(TARGET) $(PREFIX_BIN) - -uninstall: - rm -f $(PREFIX_BIN)/$(TARGET) -strip: - $(STRIP) $(TARGET) - cp $(TARGET) ~/tftpboot - -rebuild: clean compile +TARGET = Cidn-SH +OBJ_PATH = $(shell pwd)/debug +PREFIX_BIN = + +CC = ${CXX} +STRIP = aarch64-poky-linux-strip +INCLUDES = -I../Tools/GatewayThirdParty/boost/include \ + -I../Tools/GatewayThirdParty/fftw/include \ + -I../Tools/GatewayThirdParty/jsoncpp/include \ + -I../Tools/GatewayThirdParty/sqlite/include/ \ + -I../Tools/GatewayThirdParty/curl/include/ \ + -I../Tools/GatewayThirdParty/zlog/include \ + -I ./ \ + +LIBS = -L../Tools/renesas_thirdparty/lib \ + -L../Tools/renesas_thirdparty/lib \ + -L../Tools/renesas_thirdparty/lib \ + -L../Tools/renesas_thirdparty/lib \ + -L../Tools/renesas_thirdparty/lib \ + -L../Tools/renesas_thirdparty/lib \ + -lsqlite3 -lboost_system -lpthread -lboost_thread -lboost_date_time -lboost_filesystem -lfftw3 -ljsoncpp -lmosquitto -lcurl -lzlog -lbluetooth + +CFLAGS = -O0 -g -std=c++14 -fpermissive -fstack-protector-all #-Wall -Werror +LINKFLAGS = + +AllDirs :=$(shell ls -R | grep '^\./.*:' | awk '{if( \ + substr($$1,3,5) != "debug") {gsub(":",""); print}}') . + +Sources := $(foreach n,$(AllDirs) , $(wildcard $(n)/*.cpp)) + +#C_SOURCES = $(wildcard *.c) +C_SRCDIR = $(AllDirs) +C_SOURCES = $(foreach d,$(C_SRCDIR),$(wildcard $(d)/*.c) ) +C_OBJS = $(patsubst %.c, $(OBJ_PATH)/%.o, $(C_SOURCES)) + +#CPP_SOURCES = $(wildcard *.cpp) +CPP_SRCDIR = $(AllDirs) +CPP_SOURCES = $(foreach d,$(CPP_SRCDIR),$(wildcard $(d)/*.cpp) ) +CPP_OBJS = $(patsubst %.cpp, $(OBJ_PATH)/%.o, $(CPP_SOURCES)) + +default:init compile strip + +$(C_OBJS):$(OBJ_PATH)/%.o:%.c + $(CC) -c $(CFLAGS) $(INCLUDES) $< -o $@ + +$(CPP_OBJS):$(OBJ_PATH)/%.o:%.cpp + $(CC) -c $(CFLAGS) $(INCLUDES) $< -o $@ + +init: + $(foreach d,$(AllDirs), mkdir -p $(OBJ_PATH)/$(d);) + +test: + @echo "C_SOURCES: $(C_SOURCES)" + @echo "C_OBJS: $(C_OBJS)" + @echo "CPP_SOURCES: $(CPP_SOURCES)" + @echo "CPP_OBJS: $(CPP_OBJS)" + +compile:$(C_OBJS) $(CPP_OBJS) + $(CC) -O2 -o $(TARGET) $^ $(LINKFLAGS) $(LIBS) + +clean: + rm -rf $(OBJ_PATH) + rm -f $(TARGET) + +install: $(TARGET) + cp $(TARGET) $(PREFIX_BIN) + +uninstall: + rm -f $(PREFIX_BIN)/$(TARGET) +strip: + $(STRIP) $(TARGET) + cp $(TARGET) ~/tftpboot + +rebuild: clean compile diff --git a/common/global.hpp b/common/global.hpp index 3f66941..3567c20 100644 --- a/common/global.hpp +++ b/common/global.hpp @@ -19,8 +19,9 @@ enum enumZigBeeTransmitStatus { //#define NR5G_MODULE //#define Q4G_MODULE -#define WIFI_MODULE +//#define WIFI_MODULE //#define NR5G_MEIGE +//#define BLUETEETH_MODULE class GlobalConfig diff --git a/dbaccess/sql_db.cpp b/dbaccess/sql_db.cpp index 1a39c5b..bac59c9 100644 --- a/dbaccess/sql_db.cpp +++ b/dbaccess/sql_db.cpp @@ -108,6 +108,10 @@ void SqliteDB::SqliteInit(const char *pDbName) { if(iRet == 0){ CreateTable("ALTER TABLE t_sensor_info ADD COLUMN 'upgradeStatus'"); } + iRet = GetTableRows(" sqlite_master "," name = 'receive_wave_status' and sql LIKE '%resend%' "); + if(iRet == 0){ + CreateTable("ALTER TABLE receive_wave_status ADD COLUMN 'resend'"); + } memset(sql_exec, 0, 2048); sprintf(sql_exec,"update t_sensor_info set MeasurementID = dataNodeNo where MeasurementID IS NULL ;"); @@ -1378,7 +1382,27 @@ int SqliteDB::QueryofflineData() { } return res; } - +int SqliteDB::ClearExpireData(){ + char whereCon[1024] = {0}; + char deleteSql[1024] = {0}; + char selectSql[1024] = {0}; + memset(whereCon, 0x00, sizeof(whereCon)); + memset(deleteSql, 0x00, sizeof(deleteSql)); + memset(selectSql, 0x00, sizeof(selectSql)); + sprintf(selectSql, " MeasurementID "); + array_t vecRes; + char localtimestamp[32] = {0}; + GetTimeNet(localtimestamp, 1); + vecRes = GetDataMultiLine(T_SENSOR_INFO(TNAME), selectSql, NULL); + for (size_t i = 0; i < vecRes.size(); i++) { + sprintf(whereCon, " timeStamp < %ld", atol(localtimestamp) - 2592000);//前一个月 + sprintf(deleteSql, "delete from t_dataStatic_%s where %s", vecRes[i][0].c_str(), whereCon); + ExeSql(deleteSql); + sprintf(deleteSql, "delete from t_data_%s where %s", vecRes[i][0].c_str(), whereCon); + ExeSql(deleteSql); + } + return 0; +} int SqliteDB::TransBegin() { return sqlite3_exec(mDBAcess, "begin;", 0, 0, 0); } int SqliteDB::TransRollback() { return sqlite3_exec(mDBAcess, "rollback;", 0, 0, 0); } diff --git a/dbaccess/sql_db.hpp b/dbaccess/sql_db.hpp index 4367e1a..e2dfdb2 100644 --- a/dbaccess/sql_db.hpp +++ b/dbaccess/sql_db.hpp @@ -68,6 +68,7 @@ public: int CalculateDip(); int InintGateway(); int QueryofflineData(); + int ClearExpireData(); std::string GetNodeConfigureInfor(const char *whereCon); int CloseDB(); diff --git a/jsonparse/web_cmd_parse3.cpp b/jsonparse/web_cmd_parse3.cpp index a60663d..fc58195 100644 --- a/jsonparse/web_cmd_parse3.cpp +++ b/jsonparse/web_cmd_parse3.cpp @@ -904,16 +904,16 @@ std::string JsonData::JsonCmd_Cgi_62(Param_62 ¶m){ if(param.straxis == ""){ if (param.mMode == 1){ - sprintf(whereCon, " channelID like '%s-X' and timeStamp < '%s' and timeStamp > '%s' ", param.MeasurementID.c_str(), param.timeEnd.c_str(), param.timeStart.c_str()); + sprintf(whereCon, " channelID like '%s-X' and timeStamp < '%s' and timeStamp > '%s' and (nodeResend = 0 or nodeResend is null)", param.MeasurementID.c_str(), param.timeEnd.c_str(), param.timeStart.c_str()); }else{ - sprintf(whereCon, " data_nodeno like '%%%s%%' and timeStamp < '%s' and timeStamp > '%s' ", param.MeasurementID.c_str(), param.timeEnd.c_str(), param.timeStart.c_str()); + sprintf(whereCon, " data_nodeno like '%%%s%%' and timeStamp < '%s' and timeStamp > '%s' and (resend = 0 or resend == '')", param.MeasurementID.c_str(), param.timeEnd.c_str(), param.timeStart.c_str()); } } else{ if (param.mMode == 1){ - sprintf(whereCon, " channelID = '%s-X' and timeStamp < '%s' and timeStamp > '%s' ", param.MeasurementID.c_str(), param.timeEnd.c_str(), param.timeStart.c_str()); + sprintf(whereCon, " channelID = '%s-X' and timeStamp < '%s' and timeStamp > '%s' and (nodeResend = 0 or nodeResend is null)", param.MeasurementID.c_str(), param.timeEnd.c_str(), param.timeStart.c_str()); }else{ - sprintf(whereCon, " data_nodeno = '%s-%s' and timeStamp < '%s' and timeStamp > '%s' ", param.MeasurementID.c_str(),param.straxis.c_str(), param.timeEnd.c_str(), param.timeStart.c_str()); + sprintf(whereCon, " data_nodeno = '%s-%s' and timeStamp < '%s' and timeStamp > '%s' and (resend = 0 or resend == '')", param.MeasurementID.c_str(),param.straxis.c_str(), param.timeEnd.c_str(), param.timeStart.c_str()); } } @@ -947,15 +947,15 @@ std::string JsonData::JsonCmd_Cgi_62(Param_62 ¶m){ if(param.straxis == "") { if (param.mMode == 1){ - sprintf(whereCon, " channelID like '%s-X' and timeStamp < '%s' and timeStamp > '%s' order by timeStamp asc LIMIT %d OFFSET %d", param.MeasurementID.c_str(), param.timeEnd.c_str(), param.timeStart.c_str(), 550, packgeNum); + sprintf(whereCon, " channelID like '%s-X' and timeStamp < '%s' and timeStamp > '%s' and (nodeResend = 0 or nodeResend is null) order by timeStamp asc LIMIT %d OFFSET %d", param.MeasurementID.c_str(), param.timeEnd.c_str(), param.timeStart.c_str(), 550, packgeNum); }else{ - sprintf(whereCon, " data_nodeno like '%%%s%%' and timeStamp < '%s' and timeStamp > '%s' order by timeStamp asc LIMIT %d OFFSET %d", param.MeasurementID.c_str(), param.timeEnd.c_str(), param.timeStart.c_str(), 550, packgeNum); + sprintf(whereCon, " data_nodeno like '%%%s%%' and timeStamp < '%s' and timeStamp > '%s' and (resend = 0 or resend == '') order by timeStamp asc LIMIT %d OFFSET %d", param.MeasurementID.c_str(), param.timeEnd.c_str(), param.timeStart.c_str(), 550, packgeNum); } }else{ if (param.mMode == 1){ - sprintf(whereCon, " channelID = '%s-X' and timeStamp < '%s' and timeStamp > '%s' order by timeStamp asc LIMIT %d OFFSET %d", param.MeasurementID.c_str(), param.timeEnd.c_str(), param.timeStart.c_str(), 550, packgeNum); + sprintf(whereCon, " channelID = '%s-X' and timeStamp < '%s' and timeStamp > '%s' and (nodeResend = 0 or nodeResend is null) order by timeStamp asc LIMIT %d OFFSET %d", param.MeasurementID.c_str(), param.timeEnd.c_str(), param.timeStart.c_str(), 550, packgeNum); }else{ - sprintf(whereCon, "data_nodeno = '%s-%s' and timeStamp < '%s' and timeStamp > '%s' order by timeStamp asc LIMIT %d OFFSET %d", param.MeasurementID.c_str(),param.straxis.c_str(), param.timeEnd.c_str(), param.timeStart.c_str(), 550, packgeNum); + sprintf(whereCon, "data_nodeno = '%s-%s' and timeStamp < '%s' and timeStamp > '%s' and (resend = 0 or resend == '') order by timeStamp asc LIMIT %d OFFSET %d", param.MeasurementID.c_str(),param.straxis.c_str(), param.timeEnd.c_str(), param.timeStart.c_str(), 550, packgeNum); } } array_t vecRes; @@ -1100,9 +1100,9 @@ std::string JsonData::JsonCmd_Cgi_65(Param_65 ¶m){ scheduler::instance().GetScheduleConfig(featureInterVal,waveInterVal,featureInterTime,waveInterTime,maxSensorNum); Json::Value jsBody; char count_sql[1024] = {0}; - int recive_feature = 0, send_feature = 0; - int recive_x = 0, recive_y = 0, recive_z = 0; - int send_x = 0, send_y = 0, send_z = 0; + int recive_feature = 0, send_feature = 0,resend_feature = 0; + int recive_x = 0, recive_y = 0, recive_z = 0,resend_z = 0; + //int send_x = 0, send_y = 0, send_z = 0; for (size_t i = 0; i < nSize; i++) { Json::Value iTem; @@ -1117,16 +1117,18 @@ std::string JsonData::JsonCmd_Cgi_65(Param_65 ¶m){ memset(count_sql,0,sizeof(count_sql)); snprintf(count_sql,sizeof(count_sql), "SELECT " - "COUNT(*) FILTER (WHERE channelID = '%s-X') AS recive_feature," - "COUNT(*) FILTER (WHERE channelID = '%s-X' and sendMsg = 1) AS send_feature " + "COUNT(*) FILTER (WHERE channelID = '%s-X' and nodeResend = 0 ) AS recive_feature," + "COUNT(*) FILTER (WHERE channelID = '%s-X' and sendMsg = 1 and nodeResend = 0 ) AS send_feature, " + "COUNT(*) FILTER (WHERE channelID = '%s-X' and nodeResend <> 0) AS resend_feature " "FROM ( " "SELECT * FROM %s " - "WHERE timeStamp BETWEEN '%s' AND '%s');",MeasurementID.c_str(),MeasurementID.c_str(),szTableName,param.timeStart.c_str(),param.timeEnd.c_str()); - vec_t vecResult1 = sqlite_db_ctrl::instance().GetDataSingleLine(count_sql); - if (vecResult1.size() > 0) + "WHERE timeStamp BETWEEN '%s' AND '%s');",MeasurementID.c_str(),MeasurementID.c_str(),MeasurementID.c_str(),szTableName,param.timeStart.c_str(),param.timeEnd.c_str()); + vec_t vecResult = sqlite_db_ctrl::instance().GetDataSingleLine(count_sql); + if (vecResult.size() > 0) { - recive_feature = atoi(vecResult1[0].c_str()); - send_feature = atoi(vecResult1[1].c_str()); + recive_feature = atoi(vecResult[0].c_str()); + send_feature = atoi(vecResult[1].c_str()); + resend_feature = atoi(vecResult[2].c_str()); } iTem.append(MeasurementID); @@ -1135,16 +1137,18 @@ std::string JsonData::JsonCmd_Cgi_65(Param_65 ¶m){ "SELECT " "COUNT(*) FILTER (WHERE data_nodeno = '%s-X') AS recive_x," "COUNT(*) FILTER (WHERE data_nodeno = '%s-Y') AS recive_y," - "COUNT(*) FILTER (WHERE data_nodeno = '%s-Z') AS recive_z " + "COUNT(*) FILTER (WHERE data_nodeno = '%s-Z') AS recive_z, " + "COUNT(*) FILTER (WHERE data_nodeno = '%s-Z' and resend is NOT NULL and resend <> '') AS resend_z " "FROM ( " "SELECT * FROM receive_wave_status " - "WHERE error_code = 1 and timeStamp BETWEEN '%s' AND '%s');",MeasurementID.c_str(),MeasurementID.c_str(),MeasurementID.c_str(),param.timeStart.c_str(),param.timeEnd.c_str()); + "WHERE error_code = 1 and timeStamp BETWEEN '%s' AND '%s');",MeasurementID.c_str(),MeasurementID.c_str(),MeasurementID.c_str(),MeasurementID.c_str(),param.timeStart.c_str(),param.timeEnd.c_str()); vec_t vecResult2 = sqlite_db_ctrl::instance().GetDataSingleLine(count_sql); if (vecResult2.size() > 0) { recive_x = atoi(vecResult2[0].c_str()); recive_y = atoi(vecResult2[1].c_str()); recive_z = atoi(vecResult2[2].c_str()); + resend_z = atoi(vecResult2[3].c_str()); iTem.append((float(recive_feature)/(interval/featureInterVal))); iTem.append(recive_feature); iTem.append((float( recive_x)/(interval/waveInterVal))); @@ -1153,39 +1157,48 @@ std::string JsonData::JsonCmd_Cgi_65(Param_65 ¶m){ iTem.append(recive_y); iTem.append((float(recive_z)/(interval/waveInterVal))); iTem.append(recive_z); + } - - memset(count_sql,0,sizeof(count_sql)); - snprintf(count_sql,sizeof(count_sql), - "SELECT " - "COUNT(*) FILTER (WHERE channelID = '%s-X') AS send_x," - "COUNT(*) FILTER (WHERE channelID = '%s-Y') AS send_y," - "COUNT(*) FILTER (WHERE channelID = '%s-Z') AS send_z " - "FROM ( " - "SELECT * FROM t_data_waveSend " - "WHERE sendMsg = 1 and timeStamp BETWEEN '%s' AND '%s');",MeasurementID.c_str(),MeasurementID.c_str(),MeasurementID.c_str(),param.timeStart.c_str(),param.timeEnd.c_str()); - vec_t vecResult3 = sqlite_db_ctrl::instance().GetDataSingleLine(count_sql); - if (vecResult3.size() > 0) - { - send_x = atoi(vecResult3[0].c_str()); - send_y = atoi(vecResult3[1].c_str()); - send_z = atoi(vecResult3[2].c_str()); - iTem.append((float(send_feature)/(interval/featureInterVal))); - iTem.append(send_feature); - iTem.append((float(send_x)/(interval/waveInterVal))); - iTem.append(send_x); - iTem.append((float(send_y)/(interval/waveInterVal))); - iTem.append(send_y); - iTem.append((float(send_z)/(interval/waveInterVal))); - iTem.append(send_z); - } + // memset(count_sql,0,sizeof(count_sql)); + // snprintf(count_sql,sizeof(count_sql), + // "SELECT " + // "COUNT(*) FILTER (WHERE channelID = '%s-X') AS send_x," + // "COUNT(*) FILTER (WHERE channelID = '%s-Y') AS send_y," + // "COUNT(*) FILTER (WHERE channelID = '%s-Z') AS send_z " + // "FROM ( " + // "SELECT * FROM t_data_waveSend " + // "WHERE sendMsg = 1 and timeStamp BETWEEN '%s' AND '%s');",MeasurementID.c_str(),MeasurementID.c_str(),MeasurementID.c_str(),param.timeStart.c_str(),param.timeEnd.c_str()); + // vec_t vecResult3 = sqlite_db_ctrl::instance().GetDataSingleLine(count_sql); + // if (vecResult3.size() > 0) + // { + // send_x = atoi(vecResult3[0].c_str()); + // send_y = atoi(vecResult3[1].c_str()); + // send_z = atoi(vecResult3[2].c_str()); + // iTem.append((float(send_feature)/(interval/featureInterVal))); + // iTem.append(send_feature); + // iTem.append((float(send_x)/(interval/waveInterVal))); + // iTem.append(send_x); + // iTem.append((float(send_y)/(interval/waveInterVal))); + // iTem.append(send_y); + // iTem.append((float(send_z)/(interval/waveInterVal))); + // iTem.append(send_z); + //} + iTem.append(0); + iTem.append(0); + iTem.append(0); + iTem.append(0); + iTem.append(0); + iTem.append(0); + iTem.append(0); + iTem.append(0); std::vector vRssi; boost::split(vRssi, vetRes[i][2], boost::is_any_of(","), boost::token_compress_on); if (vRssi.size() > 0 ){ iTem.append(atof(vRssi[0].c_str())/float(255)); iTem.append(atof(vRssi[1].c_str())/float(255)); } - + iTem.append(resend_feature); + iTem.append(resend_z); jsBody.append(iTem); } diff --git a/main.cpp b/main.cpp index 918d9cc..b2c51d1 100644 --- a/main.cpp +++ b/main.cpp @@ -130,7 +130,11 @@ int main(int argc, char *argv[]) { boost::thread DiskCheck(attrs, DiskSpaceCheck); DiskCheck.detach(); - +#ifdef BLUETEETH_MODULE + //启动蓝牙扫描 + boost::thread startScanBlueteeth(attrs, ScanBlueteethDevice); + startScanBlueteeth.detach(); +#endif // BLUETEETH_MODULE //启动CMT server boost::thread startTcpCmt(attrs, StartCMTServer); startTcpCmt.detach(); diff --git a/scheduler/schedule.cpp b/scheduler/schedule.cpp index 9faa70c..fe03a95 100644 --- a/scheduler/schedule.cpp +++ b/scheduler/schedule.cpp @@ -449,7 +449,7 @@ bool SensorScheduler::MissedWave(uint16_t short_addr) { return false; } -void SensorScheduler::WaveSuccess(uint16_t short_addr) { +int SensorScheduler::WaveSuccess(uint16_t short_addr) { zlog_warn(zct, "[WaveSuccess][%x]", short_addr); success_set_.insert(short_addr); patch_set_.erase(short_addr); @@ -457,12 +457,13 @@ void SensorScheduler::WaveSuccess(uint16_t short_addr) { if (iter != failure_map_.end()) { zlog_warn(zct, "[WaveSuccess][%x] try %d times success", short_addr, 4 - iter->second); failure_map_.erase(short_addr); - return; + return 4 - iter->second; } - return; + return 0; } void SensorScheduler::ClearFailureSuccessMap() { + zlog_warn(zct, "[ClearFailureSuccessMap]"); failure_map_.clear(); success_set_.clear(); patch_set_.clear(); diff --git a/scheduler/schedule.hpp b/scheduler/schedule.hpp index 7646317..4d88843 100644 --- a/scheduler/schedule.hpp +++ b/scheduler/schedule.hpp @@ -43,7 +43,7 @@ public: // int GetNextDuration(int short_addr); int WaveError(uint16_t short_addr); - void WaveSuccess(uint16_t short_addr); + int WaveSuccess(uint16_t short_addr); long GetBaseTimestamp(int id); long CalcNextTimestamp(int id, uint16_t short_addr, int& next_task_id); diff --git a/threadfunc/check_thread.cpp b/threadfunc/check_thread.cpp index 8e23c62..316a943 100644 --- a/threadfunc/check_thread.cpp +++ b/threadfunc/check_thread.cpp @@ -221,6 +221,7 @@ void CheckThread() { sqlite_db_ctrl::instance().DeleteTableData(" t_battery_history ", whereCon); sqlite_db_ctrl::instance().DeleteTableData(" receive_wave_status ", whereCon); + sqlite_db_ctrl::instance().ClearExpireData(); } #ifdef WIFI_MODULE if (wifi_reconnect_count == 600) { // 每 10分钟,重连WiFi网络 diff --git a/threadfunc/thread_func.cpp b/threadfunc/thread_func.cpp index 2b9e99b..b00d45b 100644 --- a/threadfunc/thread_func.cpp +++ b/threadfunc/thread_func.cpp @@ -21,6 +21,7 @@ #include "utility/search_dev.hpp" #include "localserver/local_server.hpp" #include "localserver/cmt_server.hpp" +#include "wifi_5g/scan_blueteeth.h" extern zlog_category_t *zct; extern zlog_category_t *zbt; @@ -235,6 +236,13 @@ void InitModule() { system("/usr/sbin/wpa_supplicant -Dnl80211 -iwlan0 -c/etc/wpa_supplicant.conf &"); system("udhcpc -b -i wlan0 &"); #endif // WIFI_MODULE +#ifdef BLUETEETH_MODULE + zlog_info(zbt, "Init Blueteeth!"); + InitGpio(GlobalConfig::GPIO_G.wifiReset, 1); // WiFi模组复位,1复位,0取消复位 + gpio_set(GlobalConfig::GPIO_G.wifiReset, 0); + InitGpio(GlobalConfig::GPIO_G.wifiPower, 1); // WiFi模组上电 + gpio_set(GlobalConfig::GPIO_G.wifiPower, 1); +#endif } void TestUart() { @@ -415,4 +423,9 @@ void DiskSpaceCheck() { } sleep(30); } +} +void ScanBlueteethDevice(){ + ScanBlueteeth scan_blueteeth; + scan_blueteeth.InitDevice(); + scan_blueteeth.StartScan(); } \ No newline at end of file diff --git a/threadfunc/thread_func.hpp b/threadfunc/thread_func.hpp index 0ed55ad..64475f5 100644 --- a/threadfunc/thread_func.hpp +++ b/threadfunc/thread_func.hpp @@ -29,3 +29,4 @@ void InitModule(); void RunLED(); void RecvUpdateFile(); void DiskSpaceCheck(); +void ScanBlueteethDevice(); diff --git a/uart/uart_feature_parse.cpp b/uart/uart_feature_parse.cpp index 52b033d..b847eff 100644 --- a/uart/uart_feature_parse.cpp +++ b/uart/uart_feature_parse.cpp @@ -48,8 +48,10 @@ int Uart::DealDataNodeFeature(const char *pData, int flag) { char whereCon[1024] = {0}; char updateSql[1024] = {0}; char buf[20] = {0x00}; - int nodeResend = 0, timing = 0; + int timing = 0; long staticIndex = 0; + uint8_t nodeResend = pData[6] & 0xFF; + zlog_info(zct, "nodeResend = %d", nodeResend); sprintf(buf, "%02x%02x", pRecvData->ShortAddr[0], pRecvData->ShortAddr[1]); if (flag == 1) { zlog_info(zct, "DealDataNodeFeature %02x%02x, %d", pRecvData->ShortAddr[0], pRecvData->ShortAddr[1], flag); @@ -157,7 +159,10 @@ int Uart::DealDataNodeFeature(const char *pData, int flag) { std::vector& data_vec = map_send_data[u_short_addr]; const uint8_t* send_data = data_vec.data(); WriteToUart((const char*)send_data, 100); - return -3; + mssleep(50000); + WriteToUart((const char*)send_data, 100); + mssleep(50000); + WriteToUart((const char*)send_data, 100); } memset(whereCon, 0x00, sizeof(whereCon)); sprintf(szTableNameData, "t_data_%s", strMeasurementID.c_str()); @@ -308,7 +313,7 @@ int Uart::DealDataNodeFeature(const char *pData, int flag) { Amp1='%f',Amp2='%f',Amp3='%f',Amp4='%f',Amp5='%f',Phase1='%f',Phase2='%f',Phase3='%f',Phase4='%f',timeStamp='%s',StaticIndex = %ld,nodeResend = %d ", dataDymX.DiagnosisPk, dataDymX.IntegratPk, dataDymX.IntegratRMS, dataDymX.RmsValues, dataDymX.EnvelopEnergy, dataDymX.Amp1, dataDymX.Amp2, dataDymX.Amp3, dataDymX.Amp4, dataDymX.Amp5, dataDymX.Phase1, dataDymX.Phase2, dataDymX.Phase3, dataDymX.Phase4, nowTimetamp.c_str(), staticIndex, nodeResend); - if ((Count * 3 < SAVE_COUNT && lTime < OneWeek) || strTime.size() == 0) { // 1 week + if ((Count * 3 < SAVE_COUNT && lTime < OneWeek) || strTime.size() == 0 || nodeResend != 0) { // 1 week char insertSql[1024] = {0}; memset(insertSql, 0x00, sizeof(insertSql)); sprintf(insertSql, "'%s','%s','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f',%ld,'%s','1',%d", strMeasurementID.c_str(), (strMeasurementID + "-X").c_str(), dataDymX.DiagnosisPk, dataDymX.IntegratPk, dataDymX.IntegratRMS, dataDymX.RmsValues, @@ -321,7 +326,7 @@ int Uart::DealDataNodeFeature(const char *pData, int flag) { sqlite_db_ctrl::instance().UpdateTableData(T_DATA_INFO(TNAME), updateSql, whereCon); } else { memset(whereCon, 0x00, sizeof(whereCon)); - sprintf(whereCon, "channelID='%s' and timeStamp = '%s'", (strMeasurementID + "-X").c_str(), strTime.c_str()); + sprintf(whereCon, "channelID='%s' and timeStamp = '%s' and nodeResend = 0", (strMeasurementID + "-X").c_str(), strTime.c_str()); sqlite_db_ctrl::instance().UpdateTableData(szTableName, updateSql, whereCon); memset(whereCon, 0x00, sizeof(whereCon)); @@ -401,7 +406,7 @@ int Uart::DealDataNodeFeature(const char *pData, int flag) { Amp1='%f',Amp2='%f',Amp3='%f',Amp4='%f',Amp5='%f',Phase1='%f',Phase2='%f',Phase3='%f',Phase4='%f',timeStamp='%s',StaticIndex = %ld,nodeResend = %d ", dataDymY.DiagnosisPk, dataDymY.IntegratPk, dataDymY.IntegratRMS, dataDymY.RmsValues, dataDymY.EnvelopEnergy, dataDymY.Amp1, dataDymY.Amp2, dataDymY.Amp3, dataDymY.Amp4, dataDymY.Amp5, dataDymY.Phase1, dataDymY.Phase2, dataDymY.Phase3, dataDymY.Phase4, nowTimetamp.c_str(), staticIndex, nodeResend); - if (/*0 == sqlite_db_ctrl::instance().GetTableRows(T_DATA_INFO(TNAME), whereCon)*/ (Count * 3 < SAVE_COUNT && lTime < OneWeek) || strTime.size() == 0) { + if (/*0 == sqlite_db_ctrl::instance().GetTableRows(T_DATA_INFO(TNAME), whereCon)*/ (Count * 3 < SAVE_COUNT && lTime < OneWeek) || strTime.size() == 0 ) { char insertSql[1024] = {0}; memset(insertSql, 0x00, sizeof(insertSql)); sprintf(insertSql, "'%s','%s','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f',%ld,'%s','1',%d", strMeasurementID.c_str(), (strMeasurementID + "-Y").c_str(), dataDymY.DiagnosisPk, dataDymY.IntegratPk, dataDymY.IntegratRMS, dataDymY.RmsValues, @@ -414,7 +419,7 @@ int Uart::DealDataNodeFeature(const char *pData, int flag) { sqlite_db_ctrl::instance().UpdateTableData(T_DATA_INFO(TNAME), updateSql, whereCon); } else { memset(whereCon, 0x00, sizeof(whereCon)); - sprintf(whereCon, "channelID='%s' and timeStamp = '%s'", (strMeasurementID + "-Y").c_str(), strTime.c_str()); + sprintf(whereCon, "channelID='%s' and timeStamp = '%s' and nodeResend = 0", (strMeasurementID + "-Y").c_str(), strTime.c_str()); sqlite_db_ctrl::instance().UpdateTableData(szTableName, updateSql, whereCon); memset(whereCon, 0x00, sizeof(whereCon)); sprintf(whereCon, "channelID='%s' ", (strMeasurementID + "-Y").c_str()); @@ -496,7 +501,7 @@ int Uart::DealDataNodeFeature(const char *pData, int flag) { Amp1='%f',Amp2='%f',Amp3='%f',Amp4='%f',Amp5='%f',Phase1='%f',Phase2='%f',Phase3='%f',Phase4='%f',timeStamp='%s',StaticIndex = %ld,nodeResend = %d ", dataDymZ.DiagnosisPk, dataDymZ.IntegratPk, dataDymZ.IntegratRMS, dataDymZ.RmsValues, dataDymZ.EnvelopEnergy, dataDymZ.Amp1, dataDymZ.Amp2, dataDymZ.Amp3, dataDymZ.Amp4, dataDymZ.Amp5, dataDymZ.Phase1, dataDymZ.Phase2, dataDymZ.Phase3, dataDymZ.Phase4, nowTimetamp.c_str(), staticIndex, nodeResend); - if (/*0 == sqlite_db_ctrl::instance().GetTableRows(T_DATA_INFO(TNAME), whereCon)*/ Count * 3 < SAVE_COUNT && (lTime < OneWeek || strTime.size() == 0)) { + if (/*0 == sqlite_db_ctrl::instance().GetTableRows(T_DATA_INFO(TNAME), whereCon)*/ (Count * 3 < SAVE_COUNT && lTime < OneWeek) || strTime.size() == 0 ) { char insertSql[1024] = {0}; memset(insertSql, 0x00, sizeof(insertSql)); sprintf(insertSql, "'%s','%s','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f','%f',%ld,'%s','1',%d", strMeasurementID.c_str(), (strMeasurementID + "-Z").c_str(), dataDymZ.DiagnosisPk, dataDymZ.IntegratPk, dataDymZ.IntegratRMS, dataDymZ.RmsValues, @@ -509,7 +514,7 @@ int Uart::DealDataNodeFeature(const char *pData, int flag) { sqlite_db_ctrl::instance().UpdateTableData(T_DATA_INFO(TNAME), updateSql, whereCon); } else { memset(whereCon, 0x00, sizeof(whereCon)); - sprintf(whereCon, "channelID='%s' and timeStamp = '%s'", (strMeasurementID + "-Z").c_str(), strTime.c_str()); + sprintf(whereCon, "channelID='%s' and timeStamp = '%s' and nodeResend = 0", (strMeasurementID + "-Z").c_str(), strTime.c_str()); sqlite_db_ctrl::instance().UpdateTableData(szTableName, updateSql, whereCon); memset(whereCon, 0x00, sizeof(whereCon)); sprintf(whereCon, "channelID='%s' ", (strMeasurementID + "-Z").c_str()); @@ -841,17 +846,17 @@ void Uart::DealWave() { memset(whereCon, 0x00, sizeof(whereCon)); if (vecDataX.size() > 0) { - sprintf(insertSql, "'%s-X','%02x%02x','%s',0,'1','%s' ", strMeasurementID.c_str(),(wave_shortAddr >> 8) & 0xFF,wave_shortAddr & 0xFF,localtimestamp,""); + sprintf(insertSql, "'%s-X','%02x%02x','%s',0,'1','%s','' ", strMeasurementID.c_str(),(wave_shortAddr >> 8) & 0xFF,wave_shortAddr & 0xFF,localtimestamp,""); sqlite_db_ctrl::instance().InsertData(" receive_wave_status ", insertSql); } if (vecDataY.size() > 0) { - sprintf(insertSql, "'%s-Y','%02x%02x','%s',0,'1','%s' ", strMeasurementID.c_str(),(wave_shortAddr >> 8) & 0xFF,wave_shortAddr & 0xFF,localtimestamp,""); + sprintf(insertSql, "'%s-Y','%02x%02x','%s',0,'1','%s','' ", strMeasurementID.c_str(),(wave_shortAddr >> 8) & 0xFF,wave_shortAddr & 0xFF,localtimestamp,""); sqlite_db_ctrl::instance().InsertData(" receive_wave_status ", insertSql); } if (vecDataZ.size() > 0) { - sprintf(insertSql, "'%s-Z','%02x%02x','%s',0,'1','%s' ", strMeasurementID.c_str(),(wave_shortAddr >> 8) & 0xFF,wave_shortAddr & 0xFF,localtimestamp,""); + sprintf(insertSql, "'%s-Z','%02x%02x','%s',0,'1','%s','' ", strMeasurementID.c_str(),(wave_shortAddr >> 8) & 0xFF,wave_shortAddr & 0xFF,localtimestamp,""); sqlite_db_ctrl::instance().InsertData(" receive_wave_status ", insertSql); } zlog_warn(zct,"waveCountX = %d, waveCountY = %d, waveCountZ = %d", vecDataX.size() , vecDataY.size(), vecDataZ.size()); @@ -865,7 +870,14 @@ void Uart::DealWave() { (vecDataY.size() > 0 && g_mapCompress[strShortAddr].CountY > 0) || (vecDataZ.size() > 0 && g_mapCompress[strShortAddr].CountZ > 0)) { - scheduler::instance().WaveSuccess(wave_shortAddr); + int iRet = scheduler::instance().WaveSuccess(wave_shortAddr); + if (iRet != 0) { + memset(whereCon, 0x00, sizeof(whereCon)); + char updateSql[1024] = { 0 }; + sprintf(updateSql, "resend = '%d'", iRet); + sprintf(whereCon, "short_addr='%02x%02x' and timestamp = '%s'", (wave_shortAddr >> 8) & 0xFF,wave_shortAddr & 0xFF,localtimestamp); + sqlite_db_ctrl::instance().UpdateTableData(" receive_wave_status ", updateSql, whereCon); + } } if (g_mapCompress[strShortAddr].CountX <= 0 && g_mapCompress[strShortAddr].CountY <= 0 && g_mapCompress[strShortAddr].CountZ <= 0) { diff --git a/uart/uart_parameter_config.cpp b/uart/uart_parameter_config.cpp index dfbd45e..545466e 100644 --- a/uart/uart_parameter_config.cpp +++ b/uart/uart_parameter_config.cpp @@ -458,9 +458,9 @@ int Uart::UpdateConfig(uint16_t ushortAdd) { WriteToUart((const char*)UpdateData, 100); int iRet = CheckZigbeeACK(); if (iRet == 0) { - zlog_info(zct, "updataconfig ACK send success,shortAddr = %02x%02x", UINT16_HIGH(ushortAdd),UINT16_LOW(ushortAdd)); + zlog_info(zct, "updateconfig ACK send success,shortAddr = %02x%02x", UINT16_HIGH(ushortAdd),UINT16_LOW(ushortAdd)); } else { - zlog_warn(zct, "updataconfig ACK send failed,shortAddr = %02x%02x", UINT16_HIGH(ushortAdd),UINT16_LOW(ushortAdd)); + zlog_warn(zct, "updateconfig ACK send failed,shortAddr = %02x%02x", UINT16_HIGH(ushortAdd),UINT16_LOW(ushortAdd)); } mssleep(50000); diff --git a/wifi_5g/scan_blueteeth.cpp b/wifi_5g/scan_blueteeth.cpp new file mode 100644 index 0000000..00262a9 --- /dev/null +++ b/wifi_5g/scan_blueteeth.cpp @@ -0,0 +1,188 @@ +#include "scan_blueteeth.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "common/common_func.hpp" +#include "dbaccess/sql_db.hpp" + +extern zlog_category_t *zct; + + + +ScanBlueteeth::ScanBlueteeth() : device_id(-1), sock(-1) { + retry_count = 0; + max_retries = 5; + // Constructor initializes device_id and sock to invalid values +} + +ScanBlueteeth::~ScanBlueteeth() { + // Destructor ensures resources are released + if (sock >= 0) { + hci_le_set_scan_enable(sock, 0x00, 0x00, 1000); // Disable scanning + close(sock); // Close the socket + } +} +// 初始化 socket 成为非阻塞模式 +int set_nonblocking(int fd) { + int flags = fcntl(fd, F_GETFL, 0); + if (flags == -1) return -1; + return fcntl(fd, F_SETFL, flags | O_NONBLOCK); +} +int ScanBlueteeth::InitDevice(){ + + zlog_info(zct,"Initializing HCI device..."); + system("hciconfig hci0 reset"); + device_id = hci_get_route(NULL); + sock = hci_open_dev(device_id); + + if (device_id < 0 || sock < 0) { + zlog_error(zct,"Failed to open HCI device."); + return -1; + } + + // 设置扫描参数 扫描间隔450ms + if (hci_le_set_scan_parameters(sock, 0x01, 0x02D0, 0x02D0, 0x00, 0x00, 1000) < 0) { + zlog_error(zct,"Set scan parameters failed"); + close(sock); + return -2; + } + + // 启动扫描 + if (hci_le_set_scan_enable(sock, 0x01, 0x00, 1000) < 0) { + zlog_error(zct,"Enable scan failed"); + close(sock); + return -3; + } + + // 设置事件过滤器 + struct hci_filter nf; + hci_filter_clear(&nf); + hci_filter_set_ptype(HCI_EVENT_PKT, &nf); + hci_filter_set_event(EVT_LE_META_EVENT, &nf); + setsockopt(sock, SOL_HCI, HCI_FILTER, &nf, sizeof(nf)); + retry_count = 0; + + if (sock >= 0) { + set_nonblocking(sock); // 设置非阻塞 + } + + return 0; +} + +void ScanBlueteeth::StartScan(){ + while (1) { + if (sock < 0 || retry_count > max_retries) { + zlog_error(zct,"Reinitializing HCI device..."); + + if (sock >= 0) close(sock); + if (InitDevice() != 0) { + zlog_error(zct,"Failed to initialize HCI device."); + sleep(3); + retry_count++; + continue; + } + retry_count = 0; + } + unsigned char buf[HCI_MAX_EVENT_SIZE]; + int len = read(sock, buf, sizeof(buf)); + if (len < 0) { + if (errno == EAGAIN || errno == EINTR) { + continue; // Ignore temporary errors + } else { + zlog_error(zct,"Read error: %s", strerror(errno)); + retry_count++; + sleep(1); + continue; + } + continue; + } + + evt_le_meta_event *meta = (evt_le_meta_event *)(buf + (1 + HCI_EVENT_HDR_SIZE)); + if (meta->subevent != EVT_LE_ADVERTISING_REPORT) continue; + + uint8_t *ptr = meta->data; + uint8_t reports = *ptr++; + for (int i = 0; i < reports; i++) { + le_advertising_info *info = (le_advertising_info *)ptr; + + char addr[18]; + ba2str(&info->bdaddr, addr); + + int info_len = info->length; + + int rssi_offset = (uint8_t *)info->data - buf + info_len; + int8_t rssi = (int8_t)buf[rssi_offset]; + ParseData(info->data, info->length,rssi); + ptr += sizeof(le_advertising_info) + info->length + 1; // 最后的 +1 是 RSSI 字节 + } + } + + hci_le_set_scan_enable(sock, 0x00, 0x00, 1000); + close(sock); + +} + +void ScanBlueteeth::ParseData(unsigned char *data, int len,int8_t rssi){ + if (len < 25) return; + + // 找到 Manufacturer Specific 区块开头 + for (int i = 0; i < len - 4; i++) { + if (data[i] == 0x06 && data[i+1] == 0x09 && data[i+2] == 0x43 && data[i+3] == 0x68 && data[i+4] == 0x61) { + + char device_id[15]={0},mac[15] = {0},temp_bl[5]={0}; + int count = 0; + sprintf(device_id,"%02x%02x%02x%02x%02x%02x",data[2],data[3],data[4],data[5],data[6],data[7]); + sprintf(mac,"%02x%02x%02x%02x%02x%02x",data[13],data[14],data[15],data[16],data[17],data[18]); + sprintf(temp_bl,"%02x%02x",data[i + 24],data[i + 25]); + + int temp_ch = (data[i + 26] << 8) | data[i + 27]; + int volt = data[i + 23]; + short temp1001 = ((data[i + 28] << 8) | data[i + 29]) >> 4; + count = (data[i + 19] << 24) | (data[i + 20] << 16) | (data[i + 21] << 8) | data[i + 22]; + long num = strtol(temp_bl, NULL, 16); + short signed_num = (short)num; + float blueteeth_temp = (float)signed_num*0.01; + float chip_temp = 0.0; + if (temp_ch == 0) + { + chip_temp = 1000; + }else{ + chip_temp = ((float)temp_ch*0.0625) - 50.0625; + } + float env_temp = 0.0; + if (temp1001 < 0x800) + { + env_temp = (float)temp1001 * 0.0625; + }else{ + env_temp = float((~temp1001) & 0xfff) * (-1) * 0.0625; + } + float volt_ = (double)volt * 0.03125; + // 打印原始广播数据十六进制 + printf(" Raw Adv Data: "); + for (int k = 0; k < len; k++) { + printf("%02x ", data[k]); + } + printf("\n"); + + //printf("time %s Bluetooth temp :%f ℃ , NST 1001 temp: %f ℃\n",GetCurrentTime().c_str(),(float)temp*0.01, ((float)temp1*0.0625) - 50.0625); + //printf(" volt : %f v\n", (double)volt * 0.03125 ); + char insertSql[512] = {0}; + char localtimestamp[32] = {0}; + GetTimeNet(localtimestamp, 1); + sprintf(insertSql,"'%s','%s','%d',%d,'%.1f','%.1f','%.1f','%.1f','%s'",device_id,mac,rssi,count,volt_,blueteeth_temp,chip_temp,env_temp,localtimestamp); + sqlite_db_ctrl::instance().InsertData(" blueteeth_info ", insertSql); + return; + } + } + +} \ No newline at end of file diff --git a/wifi_5g/scan_blueteeth.h b/wifi_5g/scan_blueteeth.h new file mode 100644 index 0000000..1aabea3 --- /dev/null +++ b/wifi_5g/scan_blueteeth.h @@ -0,0 +1,20 @@ +#ifndef SCAN_BLUETEETH_H_ +#define SCAN_BLUETEETH_H_ + +typedef signed char int8_t; + +class ScanBlueteeth { +public: + ScanBlueteeth(); + virtual ~ScanBlueteeth(); + + int InitDevice(); + void StartScan(); + int device_id; + int sock; + int retry_count ; + int max_retries ; + + void ParseData(unsigned char *data, int len,int8_t rssi); +}; +#endif \ No newline at end of file