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

221 lines
5.4 KiB
C
Raw Normal View History

2023-06-01 17:22:38 +02:00
#include "./Include/simulateFlux.h"
#include "./Include/initialParameters.h"
#include "./Include/queue.h"
2022-06-21 15:13:10 +02:00
#include <time.h>
#include <errno.h>
2022-06-08 11:25:19 +02:00
2022-06-14 10:46:12 +02:00
/**
* @brief convert an interger N into a char*
2022-06-21 15:13:10 +02:00
*
* @param N
* @return char*
2022-06-14 10:46:12 +02:00
*/
2022-06-21 15:13:10 +02:00
char *convertIntegerToChar(int N)
2022-06-09 17:34:24 +02:00
{
// Count digits in number N
int m = N;
int digit = 0;
2022-06-21 15:13:10 +02:00
while (m)
{
2022-06-09 17:34:24 +02:00
digit++;
m /= 10;
}
2022-06-21 15:13:10 +02:00
char *arr;
2022-06-09 17:34:24 +02:00
char arr1[digit];
2022-06-21 15:13:10 +02:00
arr = (char *)malloc(digit * sizeof(char));
2022-06-09 17:34:24 +02:00
int index = 0;
2022-06-21 15:13:10 +02:00
while (N)
{
2022-06-09 17:34:24 +02:00
arr1[++index] = N % 10 + '0';
N /= 10;
}
int i;
2022-06-21 15:13:10 +02:00
for (i = 0; i < index; i++)
{
2022-06-09 17:34:24 +02:00
arr[i] = arr1[index - i];
}
arr[i] = '\0';
2022-06-21 15:13:10 +02:00
return (char *)arr;
2022-06-09 17:34:24 +02:00
}
2022-06-14 10:46:12 +02:00
/**
2022-06-21 15:13:10 +02:00
* @brief Create a New Raw Data File Name
*
* @return char*
2022-06-14 10:46:12 +02:00
*/
2022-06-21 15:13:10 +02:00
char *createNewRawDataFileName()
{
char *fileName = "RawDataFiles/RawData";
2022-06-13 15:03:51 +02:00
char *extension = ".csv\0";
2022-06-09 17:34:24 +02:00
char *fileNumber = convertIntegerToChar(cptFile);
2022-06-21 15:13:10 +02:00
// char *fileNumber;
// sprintf(fileNumber, "%d", cptFile);
// printf("%s\n" , fileNumber);
2022-06-09 17:34:24 +02:00
2022-06-21 15:13:10 +02:00
char fileNameNumber[strlen(fileName) + strlen(fileNumber)];
char *fullFileName = malloc((strlen(fileNameNumber) + strlen(extension)) * sizeof(char *));
2022-06-09 17:34:24 +02:00
2022-06-21 15:13:10 +02:00
strcpy(fileNameNumber, fileName);
strcat(fileNameNumber, fileNumber);
strcpy(fullFileName, fileNameNumber);
strcat(fullFileName, extension);
2022-06-09 17:34:24 +02:00
2022-06-10 11:13:03 +02:00
return fullFileName;
2022-06-09 17:34:24 +02:00
}
2022-06-21 15:13:10 +02:00
int intInArray(int number, int *array, int N)
{
for (int i = 0; i < N; i++)
{
if (array[i] == number)
return 0;
2022-06-09 17:34:24 +02:00
}
return -1;
}
2022-06-21 15:13:10 +02:00
int maxInArray(int *array, int N)
{
int max = 0;
for (int i = 0; i < N; i++)
{
if (array[i] > max)
max = array[i];
2022-06-09 17:34:24 +02:00
}
return max;
}
2022-06-14 10:46:12 +02:00
/**
* @brief return the unix time in millisecond
2022-06-21 15:13:10 +02:00
*
* @return int64_t
2022-06-14 10:46:12 +02:00
*/
2022-06-21 15:13:10 +02:00
2022-06-10 10:47:17 +02:00
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);
2022-06-10 10:47:17 +02:00
}
2022-06-14 10:46:12 +02:00
2022-06-21 15:13:10 +02:00
int lastIndexCaptor()
{
2022-06-15 10:49:21 +02:00
int lastIndex = 0;
2022-06-21 15:13:10 +02:00
for (int i = 1; i < 8; i++)
{
if (selectionCaptors[i])
{
2022-06-15 10:49:21 +02:00
lastIndex = i;
}
}
return lastIndex;
2022-06-10 10:47:17 +02:00
}
2022-06-14 10:46:12 +02:00
/**
* @brief write one lign of rawData in the file @param rawDataFile (simulate of freq of the Vegetal Signals Captor)
2022-06-21 15:13:10 +02:00
*
* @param rawDataFile
2022-06-14 10:46:12 +02:00
* @return true if the lign is correctly write , else :
2022-06-21 15:13:10 +02:00
* @return false
2022-06-14 10:46:12 +02:00
*/
2022-06-21 15:13:10 +02:00
bool writeOneRawData(FILE *rawDataFile)
{
2022-06-08 11:25:19 +02:00
char buff[26];
char buff2[18];
int32_t values[8];
uint32_t valbin[8];
2022-06-21 15:13:10 +02:00
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());
2022-06-17 17:38:53 +02:00
fclose(timeFile);
2022-06-21 15:13:10 +02:00
fprintf(rawDataFile, "%ld,", millis());
2022-06-21 15:13:10 +02:00
if (strncmp(buff, "#################\n", (size_t)18) == 0)
{
if (!(fread(&buff2, 18, 1, stdin)))
{
2022-06-17 17:38:53 +02:00
fprintf(stderr, "Erreur lecture après ###...#");
2022-06-21 15:13:10 +02:00
}
else
{
2022-06-17 17:38:53 +02:00
strncpy(buff, &buff[18], 8);
strncpy(&buff[8], buff2, 18);
2022-06-09 17:34:24 +02:00
}
2022-06-17 17:38:53 +02:00
}
2022-06-21 17:47:27 +02:00
2022-06-17 17:38:53 +02:00
int lastIndex = lastIndexCaptor();
2022-06-21 15:13:10 +02:00
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;
2022-06-21 15:13:10 +02:00
valbin[i] = buff[3 * i + 1] * 256 * 256 * 256 + buff[3 * i + 2] * 256 * 256 + buff[3 * i + 3] * 256;
2022-06-17 17:38:53 +02:00
memcpy(&values[i], &valbin[i], sizeof(uint32_t));
2022-06-21 15:13:10 +02:00
FILE *allRawDataFile = fopen("AllRawData.csv", "a+");
if (i - 1 == lastIndex)
{
fprintf(rawDataFile, "%d\n", values[i] / 256);
fprintf(allRawDataFile, "%d\n", values[i] / 256);
2022-06-17 17:38:53 +02:00
}
2022-06-21 15:13:10 +02:00
else
{
fprintf(rawDataFile, "%d,", values[i] / 256);
fprintf(allRawDataFile, "%d,", values[i] / 256);
2022-06-17 17:38:53 +02:00
}
2022-06-21 15:13:10 +02:00
fclose(allRawDataFile);
2022-06-09 17:34:24 +02:00
}
2022-06-08 11:25:19 +02:00
}
2022-06-21 15:13:10 +02:00
cptData++;
/************** simul freq here **************/
// struct timespec ts;
// ts.tv_sec = 0;
// ts.tv_nsec = 4 * 1000000;
// nanosleep(&ts, &ts);
2022-06-17 17:38:53 +02:00
2022-06-21 15:13:10 +02:00
cptValue++;
return true;
}
else
{
2022-06-17 17:38:53 +02:00
return false;
2022-06-08 11:25:19 +02:00
}
}
2022-06-21 17:47:27 +02:00
return false;
2022-06-08 11:25:19 +02:00
}
2022-06-10 10:47:17 +02:00
2022-06-21 15:13:10 +02:00
void *threadSimulateFlux(void *vargp)
{
2022-06-13 15:03:51 +02:00
2022-06-10 11:13:03 +02:00
char *fileName = createNewRawDataFileName();
2022-06-21 15:13:10 +02:00
FILE *rawDataFile = fopen(fileName, "w+");
2022-06-10 15:05:04 +02:00
2022-06-21 15:13:10 +02:00
while (writeOneRawData(rawDataFile))
{
2022-06-10 15:05:04 +02:00
2022-06-21 15:13:10 +02:00
if (cptData == nRowRawData)
{
2022-06-13 15:19:05 +02:00
fclose(rawDataFile);
2022-06-09 17:34:24 +02:00
cptData = 0;
cptFile++;
2022-06-21 15:13:10 +02:00
// create struct here
queueAddLastQ(firstRawDataQueue, fileName, strlen(fileName));
// prepare next file now
2022-06-13 15:03:51 +02:00
fileName = createNewRawDataFileName();
2022-06-21 17:47:27 +02:00
rawDataFile = fopen(fileName, "w+");
2023-06-01 17:22:38 +02:00
// add test to get p then print it here // p is gotten from fileName file
2022-06-08 12:29:34 +02:00
}
2022-06-08 11:25:19 +02:00
}
2022-06-13 17:11:16 +02:00
rawDataWriteFlag = false;
2022-06-21 17:47:27 +02:00
return NULL;
2022-06-10 11:13:03 +02:00
}