Traitement-signal-plantes/Code-C/simulateFlux.c
2022-06-14 10:46:12 +02:00

157 lines
3.9 KiB
C

#include "simulateFlux.h"
#include "initialParameters.h"
#include "queue.h"
/**
* @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);
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;
}
/**
* @brief Create a New Raw Data File Name
*
* @return char*
*/
char *createNewRawDataFileName(){
char *fileName = "../RawDataFiles/RawData";
char *extension = ".csv\0";
char *fileNumber = convertIntegerToChar(cptFile);
char fileNameNumber[strlen(fileName)+strlen(fileNumber)];
char *fullFileName = malloc((strlen(fileNameNumber)+strlen(extension)) * sizeof(char*));
strcpy( fileNameNumber, fileName );
strcat( fileNameNumber, fileNumber );
strcpy( fullFileName, fileNameNumber );
strcat( fullFileName, extension );
return fullFileName;
}
int intInArray(int number , int *array , int N){
for(int i = 0 ; i < N ; i++){
if(array[i] == number) return 0;
}
return -1;
}
int maxInArray(int *array , int N){
int max = 0;
for(int i = 0 ; i < N ; i++){
if(array[i]>max) max = array[i];
}
return max;
}
/**
* @brief return the unix time in millisecond
*
* @return int64_t
*/
int64_t millis()
{
struct timespec now;
timespec_get(&now, TIME_UTC);
return ((int64_t) now.tv_sec) * 1000 + ((int64_t) now.tv_nsec) / 1000000;
}
/**
* @brief write one lign of rawData in the file @param rawDataFile (simulate of freq of the Vegetal Signals Captor)
*
* @param rawDataFile
* @return true if the lign is correctly write , else :
* @return false
*/
bool writeOneRawData(FILE *rawDataFile){
char buff[26];
char buff2[18];
int32_t values[8];
uint32_t valbin[8];
quartet value;
if(fread(&buff, 26, 1, stdin)) {
fprintf(rawDataFile , "%d,", millis());
if (strncmp(buff, "#################\n", (size_t)18) == 0) {
if (!(fread(&buff2, 18, 1, stdin))) {
fprintf(stderr, "Erreur lecture après ###...#");
return 2;
} else {
strncpy(buff, &buff[18], 8);
strncpy(&buff[8], buff2, 18);
}
}
int maxCApteurNb = maxInArray(selectionCaptors,sizeSelectionArray);
for (int i = 1; i < 9; i++){
if(intInArray(i,selectionCaptors,sizeSelectionArray)==0){
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));
if(i==maxCApteurNb){
fprintf(rawDataFile, "%d\n", values[i]/256);
}
else{
fprintf(rawDataFile, "%d,", values[i]/256);
}
}
}
cptData++;
//sleep(0.004); //simul freq here
return true;
}
else {
return false;
}
}
void *threadSimulateFlux(void *vargp){
char *fileName = createNewRawDataFileName();
FILE *rawDataFile = fopen(fileName,"w+");
while(writeOneRawData(rawDataFile)){
if(cptData == nRow){
fclose(rawDataFile);
cptData = 0;
cptFile++;
//create struct here
queueAddLastQ(firstRawDataQueue , fileName , strlen(fileName));
//prepare next file now
fileName = createNewRawDataFileName();
FILE *rawDataFile = fopen(fileName,"w+");
}
}
rawDataWriteFlag = false;
}