Untitled
unknown
plain_text
a year ago
5.5 kB
3
Indexable
Never
#include <stdio.h> #include "operations.c" #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); } } } //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(operatins_idx, sizeof(int), nr_operatins, file); sensors[i].sensor_type = sensor_type; sensors[i].sensor_data = t; sensors[i].nr_operations = nr_operations; sensors[i].operations_idxs = operatins_idx; } 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(operatins_idx, sizeof(int), nr_operatins, file); sensors[i].sensor_type = sensor_type; sensors[i].sensor_data = p; sensors[i].nr_operations = nr_operations; sensors[i].operations_idxs = operatins_idx; } //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; } //free(operations_idxs); //exit(0); //print_sensors(sensors, nr_sensors); //clear_sensors(sensors, nr_sensors); //fclose(file); //inchid fisierul