Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
7.1 kB
0
Indexable
Never
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "structs.h"
//void get_operations(void **operations);

void print_tire_sensor(tire_sensor *t) {
	printf("Tire Sensor\n");
	printf("Pressure: %.2f\n", t->pressure);
	printf("Temperature: %.2f\n", t->temperature);
	printf("Wear Level: %d\n", t->wear_level);
	if (t->performace_score >= 1) {
		printf("Performance Score: %d\n", t->performace_score);
	} else {
		printf("Performance Score: Not Calculated\n");
	}
}

void print_pmu_sensor(power_management_unit *p) {
	printf("Power Management Unit\n");
	printf("Voltage: %.2f\n", p->voltage);
	printf("Current: %.2f\n", p->current);
	printf("Power Consumption: %.2f\n", p->power_consumption);
	printf("Energy Regen: %d\n", p->energy_regen);
	printf("Energy Storage: %d\n", p->energy_storage);
}

	//afisez fiecare senzor din vectorul sensors
void print_sensors(sensor *sensors, int nr_sensors){
	for (int i = 0; i < nr_sensors; i++) {
		sensor s = sensors[i];
		if (s.sensor_type == TIRE) {
			tire_sensor *t = (tire_sensor *)s.sensor_data;
			print_tire_sensor(t);
		} else if (s.sensor_type ==PMU) {
			power_management_unit *p =(power_management_unit *)s.sensor_data;
			print_pmu_sensor(p);
		}
	}
}

void print_complex(sensor *sensors, int index) {
    

    // Iterez prin vectorul sortat si afisam senzorii
    for (int i = 0; i < index; i++) {
        printf("Sensor %d:\n", i + 1);

        if (sensors[i].sensor_type == PMU) {
            print_pmu_sensor((power_management_unit *) sensors[i].sensor_data);
        } else if (sensors[i].sensor_type == TIRE) {
            print_tire_sensor((tire_sensor *) sensors[i].sensor_data);
        }
}

int compare_sensors(const void *s1, const void *s2) {
    const sensor *sensor1 = (const sensor *) s1;
    const sensor *sensor2 = (const sensor *) s2;

    // Senzorii PMU au prioritate in fata senzorilor Tire
    if (sensor1->sensor_type == PMU && sensor2->sensor_type == TIRE) {
        return -1;
    } else if (sensor1->sensor_type == TIRE && sensor2->sensor_type == PMU) {
        return 1;
    } else {
        return 0;
    }
}

//efectuez operatiile senzorului
/*void perform_operations(sensor *s) {
   void **operations = (void **)malloc(sizeof(void *) * s->nr_operations);
   memcpy(operations, s->operations_idxs, sizeof(void *) * s->nr_operations);

   for (int i=0; i < s->nr_operations; i++) {
	   void (*operation)(void *) = (void (*)(void *))operations[i];
	   operation(s->sensor_data);
   }
    free(operations);
}*/

//functia pentru stergerea din vector
void clear_sensors(sensor *sensors, int nr_sensors) {
	for (int i = 0; i < nr_sensors; i++) {
		sensor s = sensors[i];

		if (s.sensor_type == TIRE) {
			tire_sensor *tire_data = (tire_sensor *)s.sensor_data;
			if (tire_data->pressure <19 || tire_data->pressure > 28 || 
			    tire_data->temperature < 0 || tire_data->temperature >120 ||
				tire_data->wear_level <0 || tire_data->wear_level > 100 ) {
					//sterg senzorul
					for (int j = i; j < nr_sensors - 1; j++){
						sensors[j] = sensors[j+1];
					}
					nr_sensors--;
					i--;
				}
		} else if (s.sensor_type == PMU) {
			power_management_unit *pmu_data = (power_management_unit *)s.sensor_data;
			if (pmu_data->voltage < 10 || pmu_data->voltage > 20 ||
			    pmu_data->current < -100 || pmu_data->current > 100 ||
				pmu_data->power_consumption < 0 || pmu_data->power_consumption > 1000 ||
				pmu_data->energy_regen < 0 || pmu_data->energy_regen > 100 ||
				pmu_data->energy_storage < 0 || pmu_data->energy_storage >100) {
					//sterg senzorul
				for (int j = i; j < nr_sensors - 1; j++){
						sensors[j] = sensors[j+1];
					}
					nr_sensors--;
					i--;
			}
		}
	}
}

int main(int argc, char const *argv[]){
	FILE *file = fopen(argv[1], "rb"); //deschid fisierul binar


void *operations[8];
get_operations(operations);
    
    int nr_sensors;
	fread(&nr_sensors, sizeof(int), 1, file); //citesc nr de senzori
	/*printf("%d\n",&nr_sensors); */

    //aloc memorie pentru vectorul de senzori
	sensor *sensors = (sensor*)malloc(nr_sensors * sizeof(sensor));
	for(int i=0; i < nr_sensors; i++) {
		int sensor_type;
		fread(&sensor_type, sizeof(int), 1, file); //citesc tipul senzorului

        //initializez structura de tip senzor
        sensor s;
		s.sensor_type = (enum sensor_type)sensor_type;

		//citesc datele senzorului din fisier
		if (s.sensor_type ==  TIRE) {
			tire_sensor *t = (tire_sensor *)malloc(sizeof(tire_sensor));
		    fread(t, sizeof(tire_sensor), 1, file);
		    int nr_operations;
fread(&nr_operations, sizeof(int), 1, file);
int *operations_idxs = (int *)malloc(nr_operations * sizeof(int));
fread(operations_idxs, sizeof(int), nr_operations, file);
sensors[i].sensor_type = sensor_type;
			sensors[i].sensor_data = t;
			sensors[i].nr_operations = nr_operations;
			sensors[i].operations_idxs = operations_idxs;
	    } else if (s.sensor_type == PMU) {
			power_management_unit *p = (power_management_unit *)malloc(sizeof(power_management_unit));
			fread(p, sizeof(power_management_unit), 1, file);
int nr_operations;
fread(&nr_operations, sizeof(int), 1, file);
int *operations_idxs = (int *)malloc(nr_operations * sizeof(int));
fread(operations_idxs, sizeof(int), nr_operations, file);
sensors[i].sensor_type = sensor_type;
			sensors[i].sensor_data = p;
			sensors[i].nr_operations = nr_operations;
			sensors[i].operations_idxs = operations_idxs;
			
		}  
		//free(sensors[i].sensor_data);
		//perform_operations(&s);
	}
	//free(sensors);
    //exit(0);
        
		//citesc numarul de operatii pentru senzorul curent
		int nr_operations;
		sensor s;
		fread(&nr_operations, sizeof(int), 1, file);
		s.nr_operations = nr_operations;

		//initializez vectorul de indecsi pentru operatii
		int *operations_idxs = (int *)malloc(nr_operations * sizeof(int));
		int i=0;
		//citesc vectorul de indecsi pentru operatii
		fread(operations_idxs, sizeof(int), nr_operations, file);
		s.operations_idxs = operations_idxs;

		//adaug senzorul la vectorul de senzori
		sensors[i] = s;
	}
	// Sortez vectorul de senzori in functie de prioritate
	int nr_sensors;
    qsort(sensors, nr_sensors, sizeof(sensor), compare_sensors);

	while (1) {
    char command[10];
    int index;
    //printf("Enter command: ");
    scanf("%s", command);
    if (strcmp(command, "print") == 0) {
        scanf("%d", &index);
        if (index < 0 || index >= nr_sensors) {
            printf("Index not in range!\n");
        } else {
            print_complex(&sensor, index);
        }
    } /*else if (strcmp(command, "analyze") == 0) {
        scanf("%d", &index);
        if (index < 0 || index >= num_sensors) {
            printf("Index not in range!\n");
        } else {
            analyze_sensor(&sensors[index]);
        }
    }*/else if (strcmp(command, "clear") == 0) {
        clear_sensors(sensors, &nr_sensors);
    } else if (strcmp(command, "exit") == 0) {
        free(sensors);
        break;
    }
}

	//free(operations_idxs);
    //exit(0);


	//print_sensors(sensors, nr_sensors);
	//clear_sensors(sensors, nr_sensors);

	//fclose(file); //inchid fisierul