Traitement-signal-plantes/Code-C/simulateFlux.c

221 lines
5.4 KiB
C

#include "include/simulateFlux.h"
#include "include/initialParameters.h"
#include "include/queue.h"
#include <time.h>
#include <errno.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 * 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;
}
/**
* @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 *fileNumber;
// sprintf(fileNumber, "%d", cptFile);
// printf("%s\n" , fileNumber);
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)((int64_t)now.tv_sec) * 1000 + ((int64_t)now.tv_nsec) / 1000000);
}
int lastIndexCaptor()
{
int lastIndex = 0;
for (int i = 1; i < 8; i++)
{
if (selectionCaptors[i])
{
lastIndex = i;
}
}
return lastIndex;
}
/**
* @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];
if (fread(&buff, 26, 1, stdin))
{
// printf("%d\n",cptValue);
if (cptValue < nbRowBinFile - nbRowIgnore)
{
FILE *timeFile = fopen("timeFile.csv", "a+");
fprintf(timeFile, "%ld\n", millis());
fclose(timeFile);
fprintf(rawDataFile, "%ld,", millis());
if (strncmp(buff, "#################\n", (size_t)18) == 0)
{
if (!(fread(&buff2, 18, 1, stdin)))
{
fprintf(stderr, "Erreur lecture après ###...#");
}
else
{
strncpy(buff, &buff[18], 8);
strncpy(&buff[8], buff2, 18);
}
}
int lastIndex = lastIndexCaptor();
for (int i = 1; i < 9; i++)
{
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;
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));
FILE *allRawDataFile = fopen("AllRawData.csv", "a+");
if (i - 1 == lastIndex)
{
fprintf(rawDataFile, "%d\n", values[i] / 256);
fprintf(allRawDataFile, "%d\n", values[i] / 256);
}
else
{
fprintf(rawDataFile, "%d,", values[i] / 256);
fprintf(allRawDataFile, "%d,", values[i] / 256);
}
fclose(allRawDataFile);
}
}
cptData++;
/************** simul freq here **************/
// struct timespec ts;
// ts.tv_sec = 0;
// ts.tv_nsec = 4 * 1000000;
// nanosleep(&ts, &ts);
cptValue++;
return true;
}
else
{
return false;
}
}
return false;
}
void *threadSimulateFlux(void *vargp)
{
char *fileName = createNewRawDataFileName();
FILE *rawDataFile = fopen(fileName, "w+");
while (writeOneRawData(rawDataFile))
{
if (cptData == nRowRawData)
{
fclose(rawDataFile);
cptData = 0;
cptFile++;
// create struct here
queueAddLastQ(firstRawDataQueue, fileName, strlen(fileName));
// prepare next file now
fileName = createNewRawDataFileName();
rawDataFile = fopen(fileName, "w+");
//add test to get p then print it here // p is gotten from fileName file
}
}
rawDataWriteFlag = false;
return NULL;
}