From 2443e553c8d4517364bba12f6ef9d729b738d165 Mon Sep 17 00:00:00 2001 From: QuentinPerret Date: Fri, 2 Jun 2023 15:53:17 +0200 Subject: [PATCH] add database script in c for creation and init --- Code-C/CMakeLists.txt | 11 ++- Code-C/Include/database.h | 11 +++ Code-C/cmake/FindSQLite3.cmake | 37 ++++++++++ {Db-Script => Code-C}/database.c | 120 ++++++++++++++++--------------- Code-C/main.c | 8 +++ Code-C/simulateFlux.c | 10 +-- Db-Script/database.h | 14 ---- Db-Script/database.py | 4 +- Db-Script/db | Bin 17672 -> 17896 bytes Db-Script/robotgowest.db | Bin 12288 -> 28672 bytes 10 files changed, 134 insertions(+), 81 deletions(-) create mode 100644 Code-C/Include/database.h create mode 100644 Code-C/cmake/FindSQLite3.cmake rename {Db-Script => Code-C}/database.c (77%) delete mode 100644 Db-Script/database.h diff --git a/Code-C/CMakeLists.txt b/Code-C/CMakeLists.txt index 49a188a..8323475 100644 --- a/Code-C/CMakeLists.txt +++ b/Code-C/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 2.6) +cmake_minimum_required(VERSION 2.8) project(Traitement-signal-plantes C) include(CTest) @@ -10,16 +10,21 @@ file(MAKE_DIRECTORY Executable) set (CMAKE_RUNTIME_OUTPUT_DIRECTORY Executable) #Set Executable directory as default exectutable file location -add_executable(exect fileGestion.c getArray.c average.c growthRate.c power.c queue.c simulateFlux.c main.c) +add_executable(exect fileGestion.c getArray.c average.c growthRate.c power.c queue.c simulateFlux.c database.c main.c) # add_executable(exect main.c simulateFlux.c queue.c power.c growthRate.c average.c getArray.c fileGestion.c) +find_package (SQLite3) +include_directories(${SQLite3_INCLUDE_DIRS}) +target_link_libraries (exect ${SQLite3_LIBRARIES} m) + + find_package(Threads) target_link_libraries(exect ${CMAKE_THREAD_LIBS_INIT} m) set(CTEST_MEMORYCHECK_TYPE "AddressSanitizer") set(CTEST_MEMORYCHECK_SANITIZER_OPTIONS "verbosity=1:symbolize=1:abort_on_error=1:detect_leaks=1") -add_executable(ctest fileGestion.c getArray.c average.c growthRate.c queue.c simulateFlux.c ctest.c) +add_executable(ctest fileGestion.c getArray.c average.c growthRate.c queue.c simulateFlux.c database.c ctest.c) target_link_libraries(ctest ${CMAKE_THREAD_LIBS_INIT} m) add_test(test_queueCreateEmpty ./ctest queueCreateEmpty) diff --git a/Code-C/Include/database.h b/Code-C/Include/database.h new file mode 100644 index 0000000..8b2249c --- /dev/null +++ b/Code-C/Include/database.h @@ -0,0 +1,11 @@ +#include +#include +#include +#include +#include +#include +#include + +int createDb(); +int initiaizeNewTrial(char *position, double frequency, bool *captorOneHot); +int initiaizeNewTrial(char *position, double frequency, bool *captorOneHot); \ No newline at end of file diff --git a/Code-C/cmake/FindSQLite3.cmake b/Code-C/cmake/FindSQLite3.cmake new file mode 100644 index 0000000..c368920 --- /dev/null +++ b/Code-C/cmake/FindSQLite3.cmake @@ -0,0 +1,37 @@ +# Copyright (C) 2007-2009 LuaDist. +# Created by Peter Kapec +# Redistribution and use of this file is allowed according to the terms of the MIT license. +# For details see the COPYRIGHT file distributed with LuaDist. +# Note: +# Searching headers and libraries is very simple and is NOT as powerful as scripts +# distributed with CMake, because LuaDist defines directories to search for. +# Everyone is encouraged to contact the author with improvements. Maybe this file +# becomes part of CMake distribution sometimes. + +# - Find sqlite3 +# Find the native SQLITE3 headers and libraries. +# +# SQLITE3_INCLUDE_DIRS - where to find sqlite3.h, etc. +# SQLITE3_LIBRARIES - List of libraries when using sqlite. +# SQLITE3_FOUND - True if sqlite found. + +# Look for the header file. +FIND_PATH(SQLITE3_INCLUDE_DIR NAMES sqlite3.h) + +# Look for the library. +FIND_LIBRARY(SQLITE3_LIBRARY NAMES sqlite) + +# Handle the QUIETLY and REQUIRED arguments and set SQLITE3_FOUND to TRUE if all listed variables are TRUE. +INCLUDE(FindPackageHandleStandardArgs) +FIND_PACKAGE_HANDLE_STANDARD_ARGS(SQLITE3 DEFAULT_MSG SQLITE3_LIBRARY SQLITE3_INCLUDE_DIR) + +# Copy the results to the output variables. +IF(SQLITE3_FOUND) + SET(SQLITE3_LIBRARIES ${SQLITE3_LIBRARY}) + SET(SQLITE3_INCLUDE_DIRS ${SQLITE3_INCLUDE_DIR}) +ELSE(SQLITE3_FOUND) + SET(SQLITE3_LIBRARIES) + SET(SQLITE3_INCLUDE_DIRS) +ENDIF(SQLITE3_FOUND) + +MARK_AS_ADVANCED(SQLITE3_INCLUDE_DIRS SQLITE3_LIBRARIES) diff --git a/Db-Script/database.c b/Code-C/database.c similarity index 77% rename from Db-Script/database.c rename to Code-C/database.c index aececba..55214f5 100644 --- a/Db-Script/database.c +++ b/Code-C/database.c @@ -1,9 +1,9 @@ -#include "./database.h" +#include "./Include/simulateFlux.h" +#include "./Include/database.h" -int initializeTrailsTable() +int initializeTrialsTable() { sqlite3 *db; - char *err_msg; int rc; sqlite3_stmt *stmt; @@ -18,12 +18,12 @@ int initializeTrailsTable() rc = sqlite3_step(stmt); rc = sqlite3_finalize(stmt); sqlite3_close(db); + return 0; } int initializeCaptorMetadataTable() { sqlite3 *db; - char *err_msg; int rc; sqlite3_stmt *stmt; @@ -38,6 +38,7 @@ int initializeCaptorMetadataTable() rc = sqlite3_step(stmt); rc = sqlite3_finalize(stmt); sqlite3_close(db); + return 0; } char *generateDataTableName(int trialId, int captorNumber) @@ -56,7 +57,6 @@ char *generateDataTableName(int trialId, int captorNumber) int createCaptorDataTable(int trialId, int captorNumber) { sqlite3 *db; - char *err_msg; int rc; sqlite3_stmt *stmt; @@ -81,49 +81,13 @@ int createCaptorDataTable(int trialId, int captorNumber) return 0; } -/** - * @brief convert an interger N into a char* - * - * @param N - * @return char* - */ -char *convertIntegerToChar(int N) -{ - // Count digits in number N - int m = N; - int digit = 0; - while (m) - { - digit++; - m /= 10; - } - char *arr; - char arr1[digit]; - arr = (char *)malloc(digit * sizeof(char)); - int index = 0; - while (N) - { - arr1[++index] = N % 10 + '0'; - N /= 10; - } - int i; - for (i = 0; i < index; i++) - { - arr[i] = arr1[index - i]; - } - arr[i] = '\0'; - return (char *)arr; -} - int insertTrailElement(char *position, int start, double frequency) { sqlite3 *db; sqlite3_stmt *stmt; - char *err_msg; int rc; - int idx; rc = sqlite3_open("robotgowest.db", &db); @@ -140,6 +104,7 @@ int insertTrailElement(char *position, int start, double frequency) rc = sqlite3_step(stmt); rc = sqlite3_finalize(stmt); sqlite3_close(db); + return 0; } int updateTrailElement(int id, char *position, int start, double frequency) @@ -148,9 +113,7 @@ int updateTrailElement(int id, char *position, int start, double frequency) sqlite3 *db; sqlite3_stmt *stmt; - char *err_msg; int rc; - int idx; rc = sqlite3_open("robotgowest.db", &db); @@ -169,6 +132,7 @@ int updateTrailElement(int id, char *position, int start, double frequency) rc = sqlite3_step(stmt); rc = sqlite3_finalize(stmt); sqlite3_close(db); + return 0; } int insertCaptorMetadataElement(int captorNumber, int trialId) @@ -177,9 +141,7 @@ int insertCaptorMetadataElement(int captorNumber, int trialId) sqlite3 *db; sqlite3_stmt *stmt; - char *err_msg; int rc; - int idx; rc = sqlite3_open("robotgowest.db", &db); @@ -193,7 +155,9 @@ int insertCaptorMetadataElement(int captorNumber, int trialId) rc = sqlite3_step(stmt); rc = sqlite3_finalize(stmt); + sqlite3_close(db); + return 0; } int insertCaptorDataElement(int trialId, int captorNumber, double lux, double mean, double power) @@ -204,16 +168,13 @@ int insertCaptorDataElement(int trialId, int captorNumber, double lux, double me sqlite3 *db; sqlite3_stmt *stmt; - char *err_msg; int rc; - int idx; rc = sqlite3_open("robotgowest.db", &db); char sql[70] = "INSERT INTO "; strcat(sql, tableName); strcat(sql, "(Lux,Mean,Power) VALUES ( :lux , :mean , :power)"); - printf(sql); rc = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL); if (rc != SQLITE_OK) @@ -226,19 +187,62 @@ int insertCaptorDataElement(int trialId, int captorNumber, double lux, double me rc = sqlite3_step(stmt); rc = sqlite3_finalize(stmt); sqlite3_close(db); + return 0; } -int main(void) +int createDb() { - sqlite3_initialize(); + char *dbPosition = "./robotgowest.db"; + if (access(dbPosition, F_OK)) + { + initializeTrialsTable(); + initializeCaptorMetadataTable(); + return 0; + } + else + { + printf("Database already initialized"); + return 1; + } +} - initializeTrailsTable(); - // initializeCaptorMetadataTable(); - // createCaptorDataTable(12, 8); - // insertTrailElement("test", 1, 3.0); - // insertCaptorMetadataElement(1, 2); - // insertCaptorDataElement(12, 8, 10, 121, 1513); - updateTrailElement(1, "test2", 122, 22); +int getMaxTrialIdRow() +{ + int rc; + sqlite3 *db; + sqlite3_stmt *stmt; + int data = -1; - sqlite3_shutdown(); + rc = sqlite3_open("robotgowest.db", &db); + if (rc != SQLITE_OK) + { + return -1; + } + + rc = sqlite3_prepare_v2(db, "SELECT MAX(Id) FROM Trials", -1, &stmt, NULL); + + if (sqlite3_step(stmt) == SQLITE_ROW) + { + data = sqlite3_column_int(stmt, 0); + } + + rc = sqlite3_finalize(stmt); + sqlite3_close(db); + + return data; +} + +int initiaizeNewTrial(char *position, double frequency, bool *captorOneHot) +{ + insertTrailElement(position, -1, frequency); + int trialId = getMaxTrialIdRow(); + for (int i = 0; i < 8; i++) + { + if (captorOneHot[i]) + { + insertCaptorMetadataElement(i, trialId); + createCaptorDataTable(trialId, i + 1); + } + } + return 0; } \ No newline at end of file diff --git a/Code-C/main.c b/Code-C/main.c index 2bd72cd..d2ff25a 100644 --- a/Code-C/main.c +++ b/Code-C/main.c @@ -8,6 +8,7 @@ #include "./Include/average.h" #include "./Include/growthRate.h" #include "./Include/getArray.h" +#include "./Include/database.h" bool rawDataWriteFlag; int nRowRawData = 5000; @@ -160,6 +161,11 @@ int main(int argc, char **argv) invTimeBandWidth = 1 / (nRowRawData * period); firstRawDataQueue = queueCreateEmpty(); // change this for create empty + // launch DB related function + sqlite3_initialize(); + createDb(); // Created the Db if not exist + initiaizeNewTrial("test", freqEch, selectionCaptors); // Initialize Trials data and tables + pthread_t rawData; if (pthread_create(&rawData, NULL, threadSimulateFlux, "threadSimulflux") != 0) { @@ -182,4 +188,6 @@ int main(int argc, char **argv) // } pthread_exit(NULL); + + sqlite3_shutdown(); } \ No newline at end of file diff --git a/Code-C/simulateFlux.c b/Code-C/simulateFlux.c index d2b0e05..d2e0067 100644 --- a/Code-C/simulateFlux.c +++ b/Code-C/simulateFlux.c @@ -152,11 +152,11 @@ bool writeOneRawData(FILE *rawDataFile) { if (selectionCaptors[i - 1]) { - quartet value; - value.octet1 = buff[3 * i + 1]; - value.octet2 = buff[3 * i + 2]; - value.octet3 = buff[3 * i + 3]; - value.octet4 = 0; + // quartet value; + // value.octet1 = buff[3 * i + 1]; + // value.octet2 = buff[3 * i + 2]; + // value.octet3 = buff[3 * i + 3]; + // value.octet4 = 0; valbin[i] = buff[3 * i + 1] * 256 * 256 * 256 + buff[3 * i + 2] * 256 * 256 + buff[3 * i + 3] * 256; memcpy(&values[i], &valbin[i], sizeof(uint32_t)); diff --git a/Db-Script/database.h b/Db-Script/database.h deleted file mode 100644 index 1383f6a..0000000 --- a/Db-Script/database.h +++ /dev/null @@ -1,14 +0,0 @@ -#include -#include -#include -#include - -// Duplication from simulFlux.c - -/** - * @brief convert an interger N into a char* - * - * @param N - * @return char* - */ -char *convertIntegerToChar(int N); \ No newline at end of file diff --git a/Db-Script/database.py b/Db-Script/database.py index 49069d7..f7b5f91 100644 --- a/Db-Script/database.py +++ b/Db-Script/database.py @@ -1 +1,3 @@ -##This mist include all fetch function and also init and insert function for nn training annd testing result \ No newline at end of file +##This mist include all fetch function and also init and insert function for nn training annd testing result + +import numpy diff --git a/Db-Script/db b/Db-Script/db index 4c6ea1795e3755230eebe642a660152b435eb3d5..c4345b168399274fde20c9c3f9901ee0a0d78cc6 100755 GIT binary patch delta 3862 zcmZWs3s6+o8NO$Mg(V`p;Ig|S>>?V)$1b3VFJzHjT^hBhptkWbywtTsRFZ-|=++3CZH=HG zFuSfxBeUJKmF-N~FjVHAr*F%!@HVAs{xB%{pl@i?r{NcCPVBicd*k81u9;}_lBgMG zG(wHx(MsxDrMHn%!m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67;+ zjtsBNp~57E%Qb$h)-4(~f=LWmH?!{}92zc#1~Fjj<67(?F=QI%(lCsPp}98I+7;u3 zyNy$77y`kt6Mhx8nk>9;EHD-h&3*T3uX)-G=32iwligA{SSQv#jV%(Z4Osu@TNW;% zc66~y)v$2Hw5o<;vo_RWzM^WxeCtB6v(5tJbt~v$pphEWXrXGeKtm*C{$PbF9oNBu QNSV2}N)-)Qqm|kJ0p|EUA^-pY diff --git a/Db-Script/robotgowest.db b/Db-Script/robotgowest.db index 96dd36c5bad62397aa6f4723f1422deb84a23eb0..4dd5df7a31c3a85e21310d28d75ee092969125d2 100644 GIT binary patch literal 28672 zcmeI(O^ee&7{KwFrY{yX}FS2;?qSGV=TM+iJ9=804CX>lD?K8h8mj{xQ=bb?usyCDAD2Ua*xF@6(Pn8lv zl+9K$+p-nRL4G+fz5MI=Ux#I}d-P??%ogu_6ZR*2_wH=#$JWRUXb2#H00IagfB*sr zAb`OC5}3FI{AmESn%n0+>@YVC8?c71)QJ=OEt z-G+Cnj`XQ&^!?+uYp&^P*RQM2?7edP-2?5_RW}UA*+A7*Z}KjjW`UyETciy`R?>oFu}uRDgvR;l7R^7F$a7tP>pJehXGI5;;MSsmq9;#r-$ zVsg@p+}Y^NWWpq~uKejB7^a!6tJbln+lOwF)><~>C{MSvr`@JLNxvPLKXaU3u2|XG zk*7)6(Z$e|&aa~IVit~HhigM?CGfS$+Bg`6b#tZjQI;yzs+_+_@5)7BQ4&OyTls7RxG0tg_000IagfB*srAbwYkqD{MrKLQkfO}QoMHe&Qx@s~