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 4c6ea17..c4345b1 100755 Binary files a/Db-Script/db and b/Db-Script/db differ diff --git a/Db-Script/robotgowest.db b/Db-Script/robotgowest.db index 96dd36c..4dd5df7 100644 Binary files a/Db-Script/robotgowest.db and b/Db-Script/robotgowest.db differ