// CLEAN
// LISTCLEAN

/***************************************************************************
                          rack.cpp  -  description
                             -------------------
    begin                : Mon Apr 15 2002
    copyright            : (C) 2002 by Ross Andrews
    email                : randrews@geekfu.org
***************************************************************************/


#include "rack.h"

rack::rack(){
  name="";
  partner="";
  mod=0;
}

rack::rack(MYSQL& conn,string namep){
#ifndef RELEASE
  cout<<"Entered rack::rack(MYSQL& conn,string namep)\n"<<flush;
  cout<<"\tnamep="<<namep<<'\n'<<flush;
#endif
  name=namep;
  ResultSet res(&conn);
  res.query<<"select racks.*,locations.RFCA as RFCA,modules.acronym as modname from racks,locations,modules where racks.acronym=\""<<name<<"\" and racks.location=locations.idx and locations.module=modules.idx";
  res.store();res.next();

  modname=(string)res["modname"];
  partner=(string)res["partner"];

  cycles_per_increment=res["cycles_per_increment"];
  hrs_per_cycle=res["hrs_per_cycle"];

  upmass_resupply=res["upmass_resupply"];
  mid_resupply=res["mid_resupply"];
  stowage=res["stowage"];

  power=res["power"];
  keepalive_power=res["keepalive_power"];

  crew_training=res["crew_training"];
  crew_time=res["crew_time"];

  MTL_flow=res["MTL_flow"];
  MTL_flow_keepalive=res["MTL_flow_keepalive"];
  MTL_inlet=res["MTL_inlet"];
  MTL_outlet=res["MTL_outlet"];

  LTL_flow=res["LTL_flow"];
  LTL_flow_keepalive=res["LTL_flow_keepalive"];
  LTL_inlet=res["LTL_inlet"];
  LTL_outlet=res["LTL_outlet"];

  video_uplink=res["video_uplink"];
  video_downlink=res["video_downlink"];
  video_rate=res["video_rate"];

  nominal_uplink=res["nominal_uplink"];
  peak_uplink=res["peak_uplink"];
  peak_uplink_duration=res["peak_uplink_duration"];

  nominal_downlink=res["nominal_downlink"];
  peak_downlink=res["peak_downlink"];
  peak_downlink_duration=res["peak_downlink_duration"];

  nominal_1553=res["nominal_1553"];
  peak_1553=res["peak_1553"];
  peak_1553_duration=res["peak_1553_duration"];

  nominal_ethernet=res["nominal_ethernet"];
  peak_ethernet=res["peak_ethernet"];
  peak_ethernet_duration=res["peak_ethernet_duration"];

  nominal_HRL=res["nominal_HRL"];
  peak_HRL=res["peak_HRL"];
  peak_HRL_duration=res["peak_HRL_duration"];

  cs_p4_asc=res["cs_p4_asc"];
  cs_p4_orb=res["cs_p4_orb"];
  cs_p4_desc=res["cs_p4_desc"];
  cs_m20_asc=res["cs_m20_asc"];
  cs_m20_orb=res["cs_m20_orb"];
  cs_m20_desc=res["cs_m20_desc"];
  cs_m80_asc=res["cs_m80_asc"];
  cs_m80_orb=res["cs_m80_orb"];
  cs_m80_desc=res["cs_m80_desc"];
  cs_m180_asc=res["cs_m180_asc"];
  cs_m180_orb=res["cs_m180_orb"];
  cs_m180_desc=res["cs_m180_desc"];

  RFCA=((int)res["RFCA"]==1);

  // Sums
  ResultSet sumsres(&conn);
  sumsres.query<<"select inputfield from sums order by name";
  sumsres.store();
  if(sumsres.size()!=0)
    while(sumsres.next())
      {
	ResultSet field(&conn);
	field.query<<"select "<<sumsres["inputfield"]<<" as field from racks where idx="<<res["idx"];
	field.store();field.next();
	sums.push_back(field["field"]);
      }
  //

#ifndef RELEASE
  cout<<"Returned from rack::rack(MYSQL& conn,string namep)\n"<<flush;
#endif
}

rack::~rack(){
}


double rack::calc_runtime(){
  return cycles_per_increment*hrs_per_cycle;
}

double rack::calc_MTL_flow(){
  if(MTL_flow==0)return 0;
  if(RFCA)
    {
      if(MTL_flow < mod->MTL_RFCA_min) return mod->MTL_RFCA_min;
      if(MTL_flow > mod->MTL_RFCA_max) return mod->MTL_RFCA_max;
    }
  else
    {
      if(MTL_flow < mod->MTL_min) return mod->MTL_min;
      if(MTL_flow > mod->MTL_max) return mod->MTL_max;
    }
  return MTL_flow;
}

double rack::calc_MTL_flow_keepalive(){
  if(MTL_flow_keepalive==0)return 0;
  if(RFCA)
    {
      if(MTL_flow_keepalive < mod->MTL_RFCA_min) return mod->MTL_RFCA_min;
      if(MTL_flow_keepalive > mod->MTL_RFCA_max) return mod->MTL_RFCA_max;
    }
  else
    {
      if(MTL_flow_keepalive < mod->MTL_min) return mod->MTL_min;
      if(MTL_flow_keepalive > mod->MTL_max) return mod->MTL_max;
    }
  return MTL_flow_keepalive;
}

double rack::calc_LTL_flow(){
  if(LTL_flow==0)return 0;
  if(RFCA)
    {
      if(LTL_flow < mod->LTL_RFCA_min) return mod->LTL_RFCA_min;
      if(LTL_flow > mod->LTL_RFCA_max) return mod->LTL_RFCA_max;
    }
  else
    {
      if(LTL_flow < mod->LTL_min) return mod->LTL_min;
      if(LTL_flow > mod->LTL_max) return mod->LTL_max;
    }
  return LTL_flow;
}

double rack::calc_LTL_flow_keepalive(){
  if(LTL_flow_keepalive==0)return 0;
  if(RFCA)
    {
      if(LTL_flow_keepalive < mod->LTL_RFCA_min) return mod->LTL_RFCA_min;
      if(LTL_flow_keepalive > mod->LTL_RFCA_max) return mod->LTL_RFCA_max;
    }
  else
    {
      if(LTL_flow_keepalive < mod->LTL_min) return mod->LTL_min;
      if(LTL_flow_keepalive > mod->LTL_max) return mod->LTL_max;
    }
  return LTL_flow_keepalive;
}

double rack::calc_energy(){
  if(!mod)return 0;
  return power*calc_runtime()+keepalive_power*(mod->stn->inclength*24-calc_runtime());
}
