#include "maintenance.h"
#include <stdio.h>

/* --- FUNCTIONS --- */

/* Exercise 2.1 */
bool isSensorWorn(tSensor sensor)
{
    return sensor.residualLife < WEAR_CRITICAL_THRESHOLD;
}

/* Exercise 2.2 */
bool isSensorInAlert(tSensor sensor)
{
    return sensor.temperature > CRITICAL_TEMPERATURE ||
           sensor.vibration > VIBRATION_ALERT_LEVEL ||
           isSensorWorn(sensor);
}

/* Exercise 3.1 */
char *getSensorAxleName(int position)
{
    if (position <= SENSORS_PER_AXLE)
    {
        return "AXLE1";
    }

    return "AXLE2";
}

/* Exercise 2.3: returns a 0-based vector index */
int getComponentTypeIndex(tComponentType compType)
{
    switch (compType)
    {
        case BEARING:
            return 0;

        case WHEEL:
            return 1;

        case BRAKE:
            return 2;

        case AXLE:
            return 3;

        default:
            return -1;
    }
}

/* Exercise 3.2 */
char *getComponentName(tComponentType compType)
{
    switch (compType)
    {
        case BEARING:
            return "BEARING";

        case WHEEL:
            return "WHEEL";

        case BRAKE:
            return "BRAKE";

        case AXLE:
            return "AXLE";

        default:
            return "UNKNOWN";
    }
}

/* --- ACTIONS --- */

/* Exercise 1 */
void loadWagonFromFile(char filename[], tWagonSensors sensors, bool *isRead)
{
    FILE *fileToRead;
    int i;
    int componentTypeValue;

    *isRead = false;
    fileToRead = fopen(filename, "r");

    if (fileToRead == NULL)
    {
        return;
    }

    for (i = 0; i < SENSORS_PER_WAGON; i++)
    {
        if (fscanf(fileToRead, "%d %f %f %f",
                   &componentTypeValue,
                   &sensors[i].temperature,
                   &sensors[i].vibration,
                   &sensors[i].residualLife) != 4)
        {
            fclose(fileToRead);
            return;
        }

        if (componentTypeValue < BEARING || componentTypeValue > AXLE)
        {
            fclose(fileToRead);
            return;
        }

        sensors[i].componentType = (tComponentType) componentTypeValue;
    }

    fclose(fileToRead);
    *isRead = true;
}

/* Exercise 2 */
void inspectWagon(tWagonSensors sensors, tInspectionResult *result)
{
    int i;
    int componentIndex;
    float sumTemperature;

    sumTemperature = 0.0f;
    result->maxTemp = sensors[0].temperature;
    result->peakPos = 1;
    result->alertCount = 0;
    result->wornCount = 0;

    for (i = 0; i < COMPONENT_TYPES; i++)
    {
        result->alertCounts[i] = 0;
    }

    for (i = 0; i < SENSORS_PER_WAGON; i++)
    {
        sumTemperature += sensors[i].temperature;

        if (sensors[i].temperature > result->maxTemp)
        {
            result->maxTemp = sensors[i].temperature;
            result->peakPos = i + 1;
        }

        if (isSensorWorn(sensors[i]))
        {
            result->wornCount++;
        }

        if (isSensorInAlert(sensors[i]))
        {
            result->alertCount++;
            componentIndex =
                getComponentTypeIndex(sensors[i].componentType);

            result->alertCounts[componentIndex]++;
        }
    }

    result->avgTemp = sumTemperature / SENSORS_PER_WAGON;
}

/* Exercise 3 */
void displaySummary(tWagonSensors sensors, tInspectionResult result)
{
    int i;

    printf("----------------------------------------\n");
    printf("INSPECTION SUMMARY\n");
    printf("----------------------------------------\n");
    printf("COMPONENT TYPES: 1-BEARING, 2-WHEEL, 3-BRAKE, 4-AXLE\n");

    printf("AVERAGE TEMPERATURE (CELSIUS): %.2f\n",
           result.avgTemp);

    printf("PEAK TEMPERATURE (CELSIUS): %.2f AT POSITION %d "
           "(AXLE: %s, TYPE: %s)\n",
           result.maxTemp,
           result.peakPos,
           getSensorAxleName(result.peakPos),
           getComponentName(sensors[result.peakPos - 1].componentType));

    if (result.alertCount > 0)
    {
        printf("COMPONENTS IN ALERT: %d\n", result.alertCount);

        for (i = 0; i < SENSORS_PER_WAGON; i++)
        {
            if (isSensorInAlert(sensors[i]))
            {
                printf("POSITION %d - TYPE: %s\n",
                       i + 1,
                       getComponentName(sensors[i].componentType));
            }
        }

        printf("ALERTS BY COMPONENT TYPE:\n");
        printf("- BEARING: %d\n",
               result.alertCounts[getComponentTypeIndex(BEARING)]);
        printf("- WHEEL: %d\n",
               result.alertCounts[getComponentTypeIndex(WHEEL)]);
        printf("- BRAKE: %d\n",
               result.alertCounts[getComponentTypeIndex(BRAKE)]);
        printf("- AXLE: %d\n",
               result.alertCounts[getComponentTypeIndex(AXLE)]);
    }
    else
    {
        printf("NO COMPONENTS IN ALERT\n");
    }

    printf("WORN COMPONENTS: %d\n", result.wornCount);

    if (result.wornCount >= MIN_WORN_FOR_WARNING)
    {
        printf("WAGON STATUS: NOT RELIABLE\n");
    }
    else
    {
        printf("WAGON STATUS: RELIABLE\n");
    }

    printf("----------------------------------------\n");
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: