/*
 * monitor.c
 *
 *  Created on: 2023/09/26
 *      Author: BestTechnology
 */

#include <monitor.h>

#include "monitor.h"

extern void led_toggle (void);
extern void led_on (void);
extern void led_off (void);

tmonproc MonConditions(void) {
    tmonproc result = mpc_idle;
    tmonproc MonCondition = mpc_idle;
    Tdatatype dtype;
    int32_t s_rdat;
    bool usig;

    // Condition monitoring
    for (int i = 0; i < 5; i++) {
        if (ControlTable.MonInfo[i].ItemIndex >= 0) {
            if (read_item ((uint16_t)ControlTable.MonInfo[i].ItemIndex, &dtype, &s_rdat)) {
                usig = ((dtype == dt_uint8) || (dtype == dt_uint16) || (dtype == dt_uint32));
                switch (ControlTable.MonInfo[i].DetectMode){
                    case mdm_none:
                    default:
                        MonCondition = mpc_idle;
                        ControlTable.MonInfo[i].DurationTime = 0;
                        break;
                    case mdm_withinrange:
                        if (((int64_t)ControlTable.MonInfo[i].min <= (usig ? (int64_t)(uint32_t)s_rdat : s_rdat)) && ((usig ? (int64_t)(uint32_t)s_rdat : s_rdat) <= (int64_t)ControlTable.MonInfo[i].max)) {
                            if (ControlTable.MonInfo[i].DurationTime >= ControlTable.MonInfo[i].time) {
                                MonCondition = ControlTable.MonInfo[i].ProcMode;
                            } else
                                MonCondition = mpc_idle;
                            ControlTable.MonInfo[i].DurationTime++;
                        } else {
                            MonCondition = mpc_idle;
                            ControlTable.MonInfo[i].DurationTime = 0;
                        }
                        break;
                    case mdm_outofrange:
                        if (((usig ? (int64_t)(uint32_t)s_rdat : s_rdat) <= (int64_t)ControlTable.MonInfo[i].min) || ((int64_t)ControlTable.MonInfo[i].max <= (usig ? (int64_t)(uint32_t)s_rdat : s_rdat))) {
                            if (ControlTable.MonInfo[i].DurationTime >= ControlTable.MonInfo[i].time) {
                                MonCondition = ControlTable.MonInfo[i].ProcMode;
                            } else
                                MonCondition = mpc_idle;
                            ControlTable.MonInfo[i].DurationTime++;
                        } else {
                            MonCondition = mpc_idle;
                            ControlTable.MonInfo[i].DurationTime = 0;
                        }
                        break;
                    case mdm_match:
                        if (((usig ? (int64_t)(uint32_t)s_rdat : s_rdat) == (int64_t)ControlTable.MonInfo[i].min) || ((int64_t)ControlTable.MonInfo[i].max == (usig ? (int64_t)(uint32_t)s_rdat : s_rdat))) {
                            if (ControlTable.MonInfo[i].DurationTime >= ControlTable.MonInfo[i].time) {
                                MonCondition = ControlTable.MonInfo[i].ProcMode;
                            } else
                                MonCondition = mpc_idle;
                            ControlTable.MonInfo[i].DurationTime++;
                        } else {
                            MonCondition = mpc_idle;
                            ControlTable.MonInfo[i].DurationTime = 0;
                        }
                        break;
                }
            }
        }
        if (MonCondition > result) result = MonCondition;
    }

    return result;
}
