/*
 * pump_management.c
 *
 *  Created on: 28 дек. 2020 г.
 *      Author: stud
 */

#include <edrk_main.h>
#include <pump_control.h>

#include "control_station.h"
#include "digital_filters.h"
#include "sbor_shema.h"

#pragma DATA_SECTION(pumps, ".slow_vars")
PUMP_CONTROL pumps = PUMP_CONTROL_DEFAULTS;

void turn_on_nasos_1_2(unsigned int without_time_wait);
int select_pump(void);

void pump_control(void)
{
    unsigned int pump_off_without_time_wait;


    if (control_station.active_array_cmd[CONTROL_STATION_CMD_MODE_PUMP] == 0)
    {
//        edrk.SelectPump1_2 = 0;
        pumps.SelectedPump1_2 = 0;
        edrk.ManualStartPump = 0;
   //     edrk.AutoStartPump = 1;
        pump_off_without_time_wait = 0;
    }

    if (control_station.active_array_cmd[CONTROL_STATION_CMD_MODE_PUMP] == 1)
    {
//        edrk.SelectPump1_2 = 1;
        pumps.SelectedPump1_2 = 1;
        edrk.ManualStartPump = 0;
    //    edrk.AutoStartPump = 1;
        pump_off_without_time_wait = 0;
    }

    if (control_station.active_array_cmd[CONTROL_STATION_CMD_MODE_PUMP] == 2)
    {
//        edrk.SelectPump1_2 = 2;
        pumps.SelectedPump1_2 = 2;
        edrk.ManualStartPump = 0;
    //    edrk.AutoStartPump = 1;
        pump_off_without_time_wait = 0;
    }

    if (control_station.active_array_cmd[CONTROL_STATION_CMD_MODE_PUMP] == 3)
    {
//        edrk.SelectPump1_2 = 1;
        pumps.SelectedPump1_2 = 1;
        edrk.ManualStartPump = 1;
        edrk.AutoStartPump = 0;
        pump_off_without_time_wait = 1;
    }

    if (control_station.active_array_cmd[CONTROL_STATION_CMD_MODE_PUMP] == 4)
    {
//        edrk.SelectPump1_2 = 2;
        pumps.SelectedPump1_2 = 2;
        edrk.ManualStartPump = 1;
        edrk.AutoStartPump = 0;
        pump_off_without_time_wait = 1;
    }

    if (control_station.active_array_cmd[CONTROL_STATION_CMD_MODE_PUMP] == 5)
    {
//        edrk.SelectPump1_2 = 0;
        pumps.SelectedPump1_2 = 0;
        edrk.ManualStartPump = 0;
        edrk.AutoStartPump = 0;
        pump_off_without_time_wait = 1;
    }

    if (control_station.active_array_cmd[CONTROL_STATION_CMD_MODE_PUMP] == 6)
    {
//        edrk.SelectPump1_2 = 1;
        pumps.SelectedPump1_2 = 0;
        edrk.ManualStartPump = 1;
        edrk.AutoStartPump = 0;
        pump_off_without_time_wait = 1;
    }

    edrk.SumStartPump = edrk.AutoStartPump || edrk.ManualStartPump;

    turn_on_nasos_1_2(pump_off_without_time_wait);

    set_status_pump_fan();

}


#define TIME_WAIT_PUMP_ON 30 //3 sec
#define TIME_WAIT_ALL_PUMP_OFF 600 //30 sec

///////////////////////////////////////////////
void turn_on_nasos_1_2(unsigned int without_time_wait)
{
static unsigned int time_wait_on_pump1=0;
static unsigned int time_wait_on_pump2=0;

static unsigned int time_wait_off_all_pump1=0;
static unsigned int time_wait_off_all_pump2=0;

static unsigned int prev_without_time_wait = 0;



int cmd_p1 = 0,
    cmd_p2 = 0,
    add_cmd_without_time_wait1 = 0,
    add_cmd_without_time_wait2 = 0;

int fast_stop_pump=0;


//   if ()
//(prev_start_nasos!=edrk.StartPump) &&


   cmd_p1 = 0;
   cmd_p2 = 0;

   fast_stop_pump = ( edrk.errors.e5.bits.ERROR_ISOLATE || edrk.errors.e5.bits.FAN ||
                      edrk.errors.e5.bits.PRE_READY_PUMP || edrk.errors.e5.bits.UTE4KA_WATER || edrk.errors.e2.bits.P_WATER_INT_MIN ||
                      edrk.errors.e2.bits.P_WATER_INT_MAX || edrk.errors.e5.bits.PUMP_1 || edrk.errors.e5.bits.PUMP_2);

   if ( edrk.SumStartPump==1 && edrk.errors.e5.bits.PRE_READY_PUMP == 0 && fast_stop_pump==0)
   {
       if (pumps.SelectedPump1_2 == 0 /*&& edrk.ManualStartPump == 0*/)
       {
           //Выбор насоса исходя из отработаного времени
           switch (select_pump()) {
           case 1:
                   cmd_p1 = 1;
                   cmd_p2 = 0;
               break;
               case 2:
                   cmd_p1 = 0;
                   cmd_p2 = 1;
               break;
           default:
                   cmd_p1 = 0;
                   cmd_p2 = 0;
           }
           pumps.lock_pump = 1;
       } else
           if (pumps.SelectedPump1_2==1 && edrk.errors.e5.bits.PUMP_1==0)
            cmd_p1 = 1;
       else if (pumps.SelectedPump1_2==2 && edrk.errors.e5.bits.PUMP_2==0)
            cmd_p2 = 1;
   }
   else
   {
        cmd_p1 = 0;
        cmd_p2 = 0;
   }


   if (cmd_p1)
   {
       edrk.SelectPump1_2 = 1;
       if (pause_detect_error(&time_wait_on_pump1,TIME_WAIT_PUMP_ON,1))
        {
          edrk.to_ing.bits.NASOS_1_ON = 1;
          time_wait_off_all_pump1     = 0;
        }
   }
   else
   {
        time_wait_on_pump1 = 0;

        if (without_time_wait==0 && prev_without_time_wait != without_time_wait)
            add_cmd_without_time_wait1 = 1;

        if (cmd_p1!=cmd_p2 || fast_stop_pump || without_time_wait || add_cmd_without_time_wait1) {
            edrk.to_ing.bits.NASOS_1_ON = 0;
        }
        else
        {
            if (pause_detect_error(&time_wait_off_all_pump1,TIME_WAIT_ALL_PUMP_OFF,1)) {
                edrk.to_ing.bits.NASOS_1_ON = 0;
            }
        }

   }


   if (cmd_p2)
   {
       edrk.SelectPump1_2 = 2;
       if (pause_detect_error(&time_wait_on_pump2,TIME_WAIT_PUMP_ON,1))
        {
          edrk.to_ing.bits.NASOS_2_ON = 1;
          time_wait_off_all_pump2     = 0;
        }
   }
   else
   {
        time_wait_on_pump2 = 0;

        if (without_time_wait==0 && prev_without_time_wait != without_time_wait)
            add_cmd_without_time_wait2 = 1;

        if (cmd_p1!=cmd_p2 || fast_stop_pump || without_time_wait || add_cmd_without_time_wait2) {
            edrk.to_ing.bits.NASOS_2_ON = 0;
        }
        else
        {
            if (pause_detect_error(&time_wait_off_all_pump2,TIME_WAIT_ALL_PUMP_OFF,1)) {
                edrk.to_ing.bits.NASOS_2_ON = 0;
            }
        }
   }

   if (edrk.to_ing.bits.NASOS_1_ON == 0 && edrk.to_ing.bits.NASOS_2_ON == 0) {
       pumps.lock_pump = 0;
   }

   prev_without_time_wait = without_time_wait;

}

int select_pump() {
    int p_n = 0;
    if (pumps.lock_pump == 0) {

        p_n = calc_auto_moto_pump();
        if (p_n == 0)
            p_n = 1;
//        if (pumps.pump1_engine_minutes > pumps.pump2_engine_minutes) {
//            p_n = 2;
//        } else {
//            p_n = 1;
//        }

    } else {
        p_n = edrk.SelectPump1_2;
    }
    if ((edrk.errors.e5.bits.PUMP_1 == 1) && (edrk.errors.e5.bits.PUMP_2 == 1)) {
        p_n = 0;
    }
    if (p_n == 1 && edrk.warnings.e5.bits.PUMP_1 == 1) {
        p_n = 2;
    }
    if (p_n == 2 && edrk.warnings.e5.bits.PUMP_2 == 1) {
        p_n = 1;
    }
    return p_n;
}
