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

142 lines
3.7 KiB
C
Raw Normal View History

2022-06-08 11:25:19 +02:00
#include "simulateFlux.h"
2022-06-09 17:34:24 +02:00
#include "initialParameters.h"
2022-06-10 15:05:04 +02:00
#include "queue.h"
2022-06-08 11:25:19 +02:00
2022-06-09 17:34:24 +02:00
int cptData = 0;
int cptFile = 1;
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;
}
2022-06-10 10:47:17 +02:00
char *createNewRawDataFileName(){
2022-06-10 15:05:04 +02:00
char *fileName = "../RawDataFiles/RawData";
2022-06-09 17:34:24 +02:00
char *extension = ".csv";
char *fileNumber = convertIntegerToChar(cptFile);
char fileNameNumber[strlen(fileName)+strlen(fileNumber)];
2022-06-10 11:13:03 +02:00
char *fullFileName = malloc( (strlen(fileNameNumber)+strlen(extension)) * sizeof(char*) );
2022-06-09 17:34:24 +02:00
strcpy( fileNameNumber, fileName );
strcat( fileNameNumber, fileNumber );
2022-06-10 11:13:03 +02:00
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
}
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;
}
2022-06-10 10:47:17 +02:00
int64_t millis()
{
struct timespec now;
timespec_get(&now, TIME_UTC);
return ((int64_t) now.tv_sec) * 1000 + ((int64_t) now.tv_nsec) / 1000000;
}
2022-06-10 11:13:03 +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];
quartet value;
2022-06-10 10:47:17 +02:00
if(fread(&buff, 26, 1, stdin)) {
2022-06-10 11:13:03 +02:00
fprintf(rawDataFile , "%d,", millis());
2022-06-08 11:25:19 +02:00
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);
}
}
2022-06-10 10:47:17 +02:00
int maxCApteurNb = maxInArray(selectionCaptors,sizeSelectionArray);
2022-06-08 11:25:19 +02:00
for (int i = 1; i < 9; i++){
2022-06-09 17:34:24 +02:00
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));
2022-06-10 10:47:17 +02:00
if(i==maxCApteurNb){
2022-06-10 11:13:03 +02:00
fprintf(rawDataFile, "%d\n", values[i]/256);
2022-06-09 17:34:24 +02:00
}
else{
2022-06-10 11:13:03 +02:00
fprintf(rawDataFile, "%d,", values[i]/256);
2022-06-09 17:34:24 +02:00
}
2022-06-08 11:25:19 +02:00
}
}
2022-06-09 17:34:24 +02:00
cptData++;
2022-06-10 15:50:27 +02:00
//sleep(0.004); //simul la freq ech
2022-06-09 17:34:24 +02:00
return true;
2022-06-08 11:25:19 +02:00
}
else {
2022-06-09 17:34:24 +02:00
return false;
2022-06-08 11:25:19 +02:00
}
}
2022-06-10 10:47:17 +02:00
2022-06-09 17:34:24 +02:00
void *simulateFlux(void *vargp){
2022-06-10 11:13:03 +02:00
char *fileName = createNewRawDataFileName();
FILE *rawDataFile = fopen(fileName,"w+");
2022-06-10 15:05:04 +02:00
Pqueue rawDataQueue = createE(strlen(fileName));
setTabChar(rawDataQueue , strlen(fileName) , fileName);
firstRawDataQueue = rawDataQueue;
printE(rawDataQueue);
2022-06-10 11:13:03 +02:00
while(writeOneRawData(rawDataFile)){
2022-06-09 17:34:24 +02:00
if(cptData == nRow){
2022-06-10 11:13:03 +02:00
fclose(rawDataFile);
2022-06-09 17:34:24 +02:00
cptData = 0;
cptFile++;
2022-06-10 11:13:03 +02:00
//create struct here
char *fileName = createNewRawDataFileName();
FILE *rawDataFile = fopen(fileName,"w+");
2022-06-10 15:05:04 +02:00
rawDataQueue = addLastQ(rawDataQueue , fileName , strlen(fileName));
printE(rawDataQueue);
2022-06-08 12:29:34 +02:00
}
2022-06-08 11:25:19 +02:00
}
2022-06-10 15:05:04 +02:00
printf("\n");
printE(firstRawDataQueue);
2022-06-10 11:13:03 +02:00
}