Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
5.5 kB
4
Indexable
Never
#include <stdio.h>
#include <stdlib.h>

#include "structs.h"
#include "operations.h"

#define MAX_SENSORS 100

void print_sensor(sensor *s) {
    if (s->sensor_type == TIRE) {
        tire_sensor *t = (tire_sensor *) s->sensor_data;
        if (t->performace_score >= 0) {
            printf("Tire Sensor\n");
            printf("Pressure: %.2f\n", t->pressure);
            printf("Temperature: %.2f\n", t->temperature);
            printf("Wear Level: %d\n", t->wear_level);
            printf("Performance Score: %d\n", t->performace_score);
        } else {
            printf("Tire Sensor\n");
            printf("Pressure: %.2f\n", t->pressure);
            printf("Temperature: %.2f\n", t->temperature);
            printf("Wear Level: %d\n", t->wear_level);
            printf("Performance Score: Not Calculated\n");
        }
    } else {
        power_management_unit *p = (power_management_unit *) s->sensor_data;
        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);
    }
}

void print_sensors(sensor **sensors, int num_sensors) {
    for (int i = 0; i < num_sensors; i++) {
        printf("\nSensor %d:\n", i);
        print_sensor(sensors[i]);
    }
}

void print_sensors_complex(sensor **sensors, int num_sensors) {
    // sort sensors by priority (TIRE -> PMU, then by index)
    sensor *sorted_sensors[MAX_SENSORS];
    int num_sorted_sensors = 0;
    for (enum sensor_type type = TIRE; type <= PMU; type++) {
        for (int i = 0; i < num_sensors; i++) {
            if (sensors[i]->sensor_type == type) {
                sorted_sensors[num_sorted_sensors++] = sensors[i];
            }
        }
    }
    // print sorted sensors
    print_sensors(sorted_sensors, num_sorted_sensors);
}

void analyze_sensor(sensor *s) {
    for (int i = 0; i < s->nr_operations; i++) {
        int op_idx = s->operations_idxs[i];
        operation op = get_operation(op_idx);
        op(s->sensor_data);
    }
}

void clear_sensors(sensor **sensors, int *num_sensors) {
    int i = 0;
    while (i < *num_sensors) {
        if (sensors[i]->sensor_type == TIRE) {
            tire_sensor *t = (tire_sensor *) sensors[i]->sensor_data;
            if (t->pressure < 19 || t->pressure > 28 ||
                t->temperature < 0 || t->temperature > 120 ||
                t->wear_level < 0 || t->wear_level > 100) {
                // invalid tire sensor, remove it from array
                free(t);

void analyze_sensor(sensor *s) {
    if (s->sensor_type == TIRE) {
        tire_sensor *t = (tire_sensor *)s->sensor_data;
        printf("Performing analysis on Tire Sensor...\n");
        calculate_performance_score(t);
        printf("Analysis complete!\n");
    } else {
        printf("Performing analysis on Power Management Unit Sensor...\n");
        // do something with PMU sensor
        printf("Analysis complete!\n");
    }
}

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 *t = (tire_sensor *)s->sensor_data;
            if (t->pressure < 19 || t->pressure > 28 || t->temperature < 0 || t->temperature > 120 || t->wear_level < 0 || t->wear_level > 100) {
                free(s->sensor_data);
                free(s->operations_idxs);
                free(s);
                sensors[i] = NULL;
            }
        } else {
            power_management_unit *p = (power_management_unit *)s->sensor_data;
            if (p->voltage < 10 || p->voltage > 20 || p->current < -100 || p->current > 100 || p->power_consumption < 0 || p->power_consumption > 1000 || p->energy_regen < 0 || p->energy_regen > 100 || p->energy_storage < 0 || p->energy_storage > 100) {
                free(s->sensor_data);
                free(s->operations_idxs);
                free(s);
                sensors[i] = NULL;
            }
        }
    }
}

void cleanup_sensors(sensor **sensors, int nr_sensors) {
    for (int i = 0; i < nr_sensors; i++) {
        if (sensors[i] != NULL) {
            free(sensors[i]->sensor_data);
            free(sensors[i]->operations_idxs);
            free(sensors[i]);
        }
    }
    free(sensors);
}

int main() {
    sensor **sensors = NULL;
    int nr_sensors = 0;
    char cmd[32];

    while (1) {
        printf("Enter command: ");
        scanf("%s", cmd);

        if (strcmp(cmd, "print") == 0) {
            int idx;
            scanf("%d", &idx);
            print_sensor(sensors, nr_sensors, idx);
        } else if (strcmp(cmd, "printc") == 0) {
            print_sensors_prioritized(sensors, nr_sensors);
        } else if (strcmp(cmd, "analyze") == 0) {
            int idx;
            scanf("%d", &idx);
            analyze_sensor(sensors[idx]);
        } else if (strcmp(cmd, "clear") == 0) {
            clear_sensors(sensors, nr_sensors);
        } else if (strcmp(cmd, "exit") == 0) {
            cleanup_sensors(sensors, nr_sensors);
            break;
        } else {
            printf("Invalid command!\n");
        }
    }

    return 0;
}