Untitled

mail@pastecode.io avatar
unknown
c_cpp
a year ago
3.9 kB
1
Indexable
Never
#include <stdio.h>
#include <stdlib.h>
#include "structs.h"
#include <string.h>

int opr_nr;
// char exit_code[4] = "exit\n";
int cmp(const void *a, const void *b) {
    sensor *sensorA = (sensor *)a;
    sensor *sensorB = (sensor *)b;
    if (sensorA->sensor_type == PMU && sensorB->sensor_type != PMU) {
        return -1;
    } else if (sensorA->sensor_type != PMU && sensorB->sensor_type == PMU) {
        return 1;
    } else {
        return 0;
    }
}

void PRINT(sensor *sensors, int index) {
    if (sensors[index].sensor_type == TIRE) {
        tire_sensor *Tire = (tire_sensor *)sensors[index].sensor_data;
        printf("Tire Sensor\n");
        printf("Pressure: %f\n", Tire->pressure);
        printf("Temperature: %f\n", Tire->temperature);
        printf("Wear Level: %d%%\n", Tire->wear_level);
        printf("Not Calculated\n");
    
    } else if (sensors[index].sensor_type == PMU) {
        power_management_unit *PowMU = (power_management_unit *)sensors[index].sensor_data;
        printf("Power Managment Unit\n");
        printf("Voltage: %.2f\n", PowMU->voltage);
        printf("Current: %.2f\n", PowMU->current);
        printf("Power Consumption: %.2f\n", PowMU->power_consumption);
        printf("Energy Regen: %d%%\n", PowMU->energy_regen);
        printf("Energy Storage: %d%%\n", PowMU->energy_storage);
    }
}

int main(int argc, char const *argv[]) {
    FILE *fptr = fopen(argv[1], "rb");
    int nr;
    fread(&nr, sizeof(int), 1, fptr);

    sensor *sensors = (sensor *)malloc(nr * sizeof(sensor));
    for (size_t i = 0; i < nr; i++) {
        fread(&sensors[i].sensor_type, sizeof(int), 1, fptr);
        if (sensors[i].sensor_type == TIRE)
        {
            tire_sensor *Tire = (tire_sensor *)malloc(sizeof(tire_sensor));
            sensors[i].sensor_data = (tire_sensor *)malloc(sizeof(tire_sensor));
            fread(&Tire->pressure, sizeof(float), 1, fptr);
            fread(&Tire->temperature, sizeof(float), 1, fptr);
            fread(&Tire->wear_level, sizeof(int), 1, fptr);
            fread(&Tire->performace_score, sizeof(int), 1, fptr);
            sensors[i].sensor_data = Tire;
        }
        else if (sensors[i].sensor_type == PMU)
        {
            power_management_unit *PowMU = (power_management_unit *)malloc(sizeof(power_management_unit));
            sensors[i].sensor_data = (power_management_unit *)malloc(sizeof(power_management_unit));
            fread(&PowMU->voltage, sizeof(float), 1, fptr);
            fread(&PowMU->current, sizeof(float), 1, fptr);
            fread(&PowMU->power_consumption, sizeof(float), 1, fptr);
            fread(&PowMU->energy_regen, sizeof(int), 1, fptr);
            fread(&PowMU->energy_storage, sizeof(int), 1, fptr);
            sensors[i].sensor_data = PowMU;
        }
        fread(&opr_nr, sizeof(int), 1, fptr);
        sensors[i].operations_idxs = malloc(opr_nr * sizeof(int));
        for (size_t i = 0; i < opr_nr; i++)
        {
            fread(&sensors->operations_idxs[i], sizeof(int), 1, fptr);
        }
    }
    qsort(sensors, nr, sizeof(sensors), cmp);
    fclose(fptr);
    // char string_c[20];
    // int size_string = sizeof(string_c);
    int VEC_INDEX = 0;
    char command[10];
    scanf("%s", command);
    while (strncmp(command, "exit\n", 4) != 0) {
        //scanf("%s", string_c);
        if (strcmp(command, "clear") == 0) {

        } else {
            scanf("%d", &VEC_INDEX);
            if (strcmp(command, "analyse") == 0) {
                printf("shya budet magic");
            } else if(strcmp(command, "print") == 0){
                if (VEC_INDEX < 0 || VEC_INDEX > nr) {
                    printf("Index out of range!\n");
                    continue;
                }
                PRINT(sensors, VEC_INDEX);
            }
        }
        scanf("%s", command);
    }

return 0;
}