/*
 * monitor.c
 *
 *  Created on: 2023/10/11
 */

/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2023 Takayoshi Uemitsu (BestTechnology CO.,LTD.)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 */

#include <monitor.h>

#include "packet.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, &read_item_callback)) {
                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;
}
