/*
 * control_station.c
 *
 *  Created on: 1 èþí. 2020 ã.
 *      Author: Yura
 */


#include "control_station.h"

#include "global_time.h"


#pragma DATA_SECTION(control_station, ".slow_vars")
CONTROL_STATION control_station = CONTROL_STATION_DEFAULTS;
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////

void control_station_clear(CONTROL_STATION_handle cs)
{
    int i,k;

    for (i=0;i<COUNT_CONTROL_STATION;i++)
    {

       cs->count_error_modbus[i] = 0;
       cs->count_ok_modbus[i] = 0;



       cs->flag_waiting_answer[i]        = 0;
       cs->flag_message_sent[i] = 0;
       cs->active_control_station[i] = 0;
       cs->alive_control_station[i]  = 0;

       for (k=0;k<CONTROL_STATION_CMD_LAST;k++)
           cs->array_cmd[i][k] = 0;

       cs->detect_get_data_control_station[i] = 0;
       cs->period_detect_active[i] = 0;

       cs->setup_time_detect_active[i]      = CONTROL_STATION_SETUP_TIME_DETECT_ACTIVE;
       cs->setup_time_detect_active_resend_485[i] = CONTROL_STATION_SETUP_TIME_DETECT_ACTIVE_RESEND_485;
       cs->setup_time_send_cmd_after_off[i] = CONTROL_STATION_SETUP_TIME_SEND_CMD_AFTER_OFF;

       cs->time_detect_active[i] = 0;
       cs->time_detect_answer_485[i] = 0;

       for (k=0;k<CONTROL_STATION_MAX_RAW_DATA;k++)
           cs->raw_array_data[i][k].all = 0;

       cs->flag_refresh_array[i] = 0;
    }
}

///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////


void control_station_update_timers(CONTROL_STATION_handle cs)
{
    static unsigned int old_time = 0;
    volatile int i;


    if (!detect_pause_milisec(CONTROL_STATION_TIME_WAIT,&old_time))
       return;


    for (i=0;i<COUNT_CONTROL_STATION;i++)
    {

       if  (cs->flag_message_sent[i] == 1)
           (cs->time_detect_answer_485[i])++;

//          cs->time_detect_answer_485[i] = 0;




       if (cs->detect_get_data_control_station[i])
       {
           cs->period_detect_active[i] = cs->time_detect_active[i];
           cs->time_detect_active[i] = 0;
           cs->detect_get_data_control_station[i] = 0;
           cs->alive_control_station[i] = 1;
       }
       else
       {

           if (cs->time_detect_active[i]>=cs->setup_time_detect_active[i])
           {
               cs->alive_control_station[i] = 0; // òóò ñäîõ
               cs->period_detect_active[i]  = 0;
//               cs->flag_message_sent[i] = 0;
               cs->time_detect_active[i] = cs->setup_time_detect_active[i]+1;
           }
           else
           {
              cs->time_detect_active[i]++;

//              if (cs->flag_message_sent[i])
//              {
//                if (cs->flag_waiting_answer[i])
//                    cs->time_detect_active[i]++;
//                else
//                {
//                    cs->time_detect_active[i] = 0;
//                }
//              }
//              else
//                cs->time_detect_active[i]++;
           }

       }


    }



}




///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////

void control_station_detect_active_station(CONTROL_STATION_handle cs)
{






}





///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////



///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////




///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////



///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////