// CLEAN
// LISTCLEAN

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


#include "module.h"

module::module(){
  idx=0;name="";
  stn=0;
}

module::~module(){
}

module::module(const module& other){
  *this=other;
}


const module& module::operator=(const module& other){
  if(&other==this)return *this;
  name=other.name;
  idx=other.idx;
  stn=other.stn;
  racks=other.racks;
  keepalives=other.keepalives;
  acoustics=other.acoustics;
  power=other.power;
  MTL_min=other.MTL_min;
  MTL_RFCA_min=other.MTL_RFCA_min;
  MTL_max=other.MTL_max;
  MTL_RFCA_max=other.MTL_RFCA_max;
  MTL_total=other.MTL_total;
  LTL_min=other.LTL_min;
  LTL_RFCA_min=other.LTL_RFCA_min;
  LTL_max=other.LTL_max;
  LTL_RFCA_max=other.LTL_RFCA_max;
  LTL_total=other.LTL_total;
  CAM_power=other.CAM_power;
  racks=other.racks;
  keepalives=other.keepalives;

  for(int n=0;n!=racks.size();n++)
    racks[n].mod=this;
  for(int n=0;n!=keepalives.size();n++)
    keepalives[n].mod=this;

  return *this;
}


void module::addrack(rack r){
  r.mod=this;
  racks.push_back(r);
}

void module::addkeepalive(rack r){
  r.mod=this;
  keepalives.push_back(r);
}

module::module(MYSQL& conn,string namep){
#ifndef RELEASE
  cout<<"Entered module::module(MYSQL& conn,string namep)\n"<<flush;
  cout<<"namep="<<namep<<'\n';
#endif
  name=namep;
  ResultSet res(&conn);
  res.query<<"select * from modules where acronym=\""<<name<<"\"";
  res.store();res.next();

  power=res["power"];

  acoustics=res["acoustics"];

  MTL_min=res["MTL_min"];
  MTL_RFCA_min=res["MTL_RFCA_min"];
  MTL_max=res["MTL_max"];
  MTL_RFCA_max=res["MTL_RFCA_max"];
  LTL_min=res["LTL_min"];
  LTL_RFCA_min=res["LTL_RFCA_min"];
  LTL_max=res["LTL_max"];
  LTL_RFCA_max=res["LTL_RFCA_max"];	
  MTL_total=res["MTL_total"];
  LTL_total=res["LTL_total"];
  CAM_power=res["CAM_power"];
#ifndef RELEASE
  cout<<"Returned from module::module(MYSQL& conn,string namep)\n"<<flush;
#endif
}

int module::getid() const{return idx;}
string module::getname() const{return name;}


double module::calc_power_ttl(){
  double runningtotal=0;
  for(int n=0;n!=racks.size();n++)
    runningtotal+=racks[n].power;
  return runningtotal;
}

double module::calc_energy_ttl(){
  double runningtotal=0;
  for(int n=0;n!=racks.size();n++)
    runningtotal+=racks[n].calc_energy();
  return runningtotal;
}

double module::calc_MTL_ttl(){
  double runningtotal=0;
  for(int n=0;n!=racks.size();n++)
    {
      runningtotal+=racks[n].calc_MTL_flow()*racks[n].calc_runtime();
      runningtotal+=racks[n].calc_MTL_flow_keepalive()*(stn->inclength*24-racks[n].calc_runtime());
    }
  for(int n=0;n!=keepalives.size();n++)
    {
      //runningtotal+=keepalives[n].calc_MTL_flow()*keepalives[n].calc_runtime();
      runningtotal+=keepalives[n].calc_MTL_flow_keepalive()*(stn->inclength*24-keepalives[n].calc_runtime());
    }
  return runningtotal;
}

double module::calc_LTL_ttl(){
  double runningtotal=0;
  for(int n=0;n!=racks.size();n++)
    {
      runningtotal+=racks[n].calc_LTL_flow()*racks[n].calc_runtime();
      runningtotal+=racks[n].calc_LTL_flow_keepalive()*(stn->inclength*24-racks[n].calc_runtime());
    }
  for(int n=0;n!=keepalives.size();n++)
    {
      //runningtotal+=keepalives[n].calc_LTL_flow()*keepalives[n].calc_runtime();
      runningtotal+=keepalives[n].calc_LTL_flow_keepalive()*(stn->inclength*24-keepalives[n].calc_runtime());
    }
  return runningtotal;
}


double module::calc_acoustics_ttl(){
  double runningtotal=0;
  for(int n=0;n!=racks.size();n++)
    runningtotal+=racks[n].calc_runtime();
  return runningtotal;
}

double module::calc_acoustics_crewtend_ttl(){
  double runningtotal=0;
  for(int n=0;n!=racks.size();n++)
    runningtotal+=racks[n].crew_time;
  return runningtotal;
}

double module::calc_acoustics_ttl_simul(){
  double runningtotal=0;
  for(int n=0;n!=racks.size();n++)
    runningtotal+=1; // This isn't the best, but it's the only way anyone can think of.
  return runningtotal;
}


double module::calc_middecks_ttl(){
  double runningtotal=0;
  for(int n=0;n!=racks.size();n++)
    runningtotal+=racks[n].mid_resupply;
  return runningtotal;
}


double module::calc_upmass_ttl(){
  double runningtotal=0;
  for(int n=0;n!=racks.size();n++)
    runningtotal+=racks[n].upmass_resupply*(stn->rack_overhead+1)+(stn->middeckmass*racks[n].mid_resupply);
  //	current=keepalives.begin();
  //	for(int n=0;n!=keepalives.size();n++)
  //	  {
  //	    runningtotal+=keepalives[n].upmass_resupply*(stn->rack_overhead+1)+(stn->middeckmass*keepalives[n].mid_resupply);
  //	    current++;
  //	  }
  return runningtotal;
}


double module::calc_stowage_ttl(){
  double runningtotal=0;
  for(int n=0;n!=racks.size();n++)
    runningtotal+=racks[n].stowage;
  return runningtotal;
}


double module::calc_crewtrng_ttl(){
  double runningtotal=0;
  for(int n=0;n!=racks.size();n++)
    runningtotal+=racks[n].crew_training;
  return runningtotal;
}


double module::calc_crewtime_ttl(){
  double runningtotal=0;
  for(int n=0;n!=racks.size();n++)
    runningtotal+=racks[n].crew_time;
  return runningtotal;
}

double module::calc_runtime_ttl(){
  double runningtotal=0;
  for(int n=0;n!=racks.size();n++)
    runningtotal+=racks[n].calc_runtime();
  return runningtotal;
}

double module::calc_energy_crewtend_ttl(){
  double runningtotal=0;
  for(int n=0;n!=racks.size();n++)
    runningtotal+=racks[n].crew_time*racks[n].power;
  return runningtotal;
}

double module::calc_MTL_ttl_simul(){
  double runningtotal=0;
  for(int n=0;n!=racks.size();n++)
    runningtotal+=racks[n].calc_MTL_flow();
  for(int n=0;n!=keepalives.size();n++)
    runningtotal+=keepalives[n].calc_MTL_flow_keepalive();
  return runningtotal;
}

double module::calc_LTL_ttl_simul(){
  double runningtotal=0;
  for(int n=0;n!=racks.size();n++)
    runningtotal+=racks[n].calc_LTL_flow();
  for(int n=0;n!=keepalives.size();n++)
    runningtotal+=keepalives[n].calc_LTL_flow_keepalive();
  return runningtotal;
}

double module::calc_MTL_crewtend_ttl(){
  double runningtotal=0;
  for(int n=0;n!=racks.size();n++)
    runningtotal+=racks[n].calc_MTL_flow()*racks[n].crew_time;
  for(int n=0;n!=keepalives.size();n++)
    runningtotal+=keepalives[n].calc_MTL_flow()*keepalives[n].crew_time;
  return runningtotal;
}

double module::calc_LTL_crewtend_ttl(){
  double runningtotal=0;
  for(int n=0;n!=racks.size();n++)
    runningtotal+=racks[n].calc_LTL_flow()*racks[n].crew_time;
  for(int n=0;n!=keepalives.size();n++)
    runningtotal+=keepalives[n].calc_MTL_flow()*keepalives[n].crew_time;
  return runningtotal;
}

double module::calc_keepalive_ttl(){
  double runningtotal=0;
  for(int n=0;n!=racks.size();n++)
    runningtotal+=racks[n].keepalive_power;
  for(int n=0;n!=keepalives.size();n++)
    runningtotal+=keepalives[n].keepalive_power;
  return runningtotal;
}

double module::calc_video_uplink_ttl(){
  double runningtotal=0;
  for(int n=0;n!=racks.size();n++)
    runningtotal+=racks[n].video_uplink;
  return runningtotal;
}

double module::calc_video_downlink_ttl(){
  double runningtotal=0;
  for(int n=0;n!=racks.size();n++)
    runningtotal+=racks[n].video_downlink;
  return runningtotal;
}

double module::calc_video_rate_ttl(){
  double runningtotal=0;
  for(int n=0;n!=racks.size();n++)
    runningtotal+=racks[n].video_rate;
  return runningtotal;
}

double module::calc_nominal_HRL_ttl(){
  double runningtotal=0;
  for(int n=0;n!=racks.size();n++)
    runningtotal+=racks[n].nominal_HRL;
  return runningtotal;
}

double module::calc_peak_HRL_ttl(){
  double runningtotal=0;
  for(int n=0;n!=racks.size();n++)
    runningtotal+=racks[n].peak_HRL;
  return runningtotal;
}

double module::calc_peak_HRL_duration_ttl(){
  double runningtotal=0;
  for(int n=0;n!=racks.size();n++)
    runningtotal+=racks[n].peak_HRL_duration;
  return runningtotal;
}

double module::calc_nominal_ethernet_ttl(){
  double runningtotal=0;
  for(int n=0;n!=racks.size();n++)
    runningtotal+=racks[n].nominal_ethernet;
  return runningtotal;
}

double module::calc_peak_ethernet_ttl(){
  double runningtotal=0;
  for(int n=0;n!=racks.size();n++)
    runningtotal+=racks[n].peak_ethernet;
  return runningtotal;
}

double module::calc_peak_ethernet_duration_ttl(){
  double runningtotal=0;
  for(int n=0;n!=racks.size();n++)
    runningtotal+=racks[n].peak_ethernet_duration;
  return runningtotal;
}

double module::calc_peak_1553_duration_ttl(){
  double runningtotal=0;
  for(int n=0;n!=racks.size();n++)
    runningtotal+=racks[n].peak_1553_duration;
  return runningtotal;
}

double module::calc_peak_1553_ttl(){
  double runningtotal=0;
  for(int n=0;n!=racks.size();n++)
    runningtotal+=racks[n].peak_1553;
  return runningtotal;
}

double module::calc_nominal_1553_ttl(){
  double runningtotal=0;
  for(int n=0;n!=racks.size();n++)
    runningtotal+=racks[n].nominal_1553;
  return runningtotal;
}

double module::calc_peak_downlink_duration_ttl(){
  double runningtotal=0;
  for(int n=0;n!=racks.size();n++)
    runningtotal+=racks[n].peak_downlink_duration;
  return runningtotal;
}

double module::calc_peak_uplink_duration_ttl(){
  double runningtotal=0;
  for(int n=0;n!=racks.size();n++)
    runningtotal+=racks[n].peak_uplink_duration;
  return runningtotal;
}

double module::calc_nominal_downlink_ttl(){
  double runningtotal=0;
  for(int n=0;n!=racks.size();n++)
    runningtotal+=racks[n].nominal_downlink;
  return runningtotal;
}

double module::calc_peak_uplink_ttl(){
  double runningtotal=0;
  for(int n=0;n!=racks.size();n++)
    runningtotal+=racks[n].peak_uplink;
  return runningtotal;
}

double module::calc_peak_downlink_ttl(){
  double runningtotal=0;
  for(int n=0;n!=racks.size();n++)
    runningtotal+=racks[n].peak_downlink;
  return runningtotal;
}

double module::calc_nominal_uplink_ttl(){
  double runningtotal=0;
  for(int n=0;n!=racks.size();n++)
    runningtotal+=racks[n].nominal_uplink;
  return runningtotal;
}


double module::calc_cs_p4_asc_ttl(){
  double total=0;
  for(int n=0;n!=racks.size();n++)
    total+=racks[n].cs_p4_asc;
  return total;
}

double module::calc_cs_p4_orb_ttl(){
  double total=0;
  for(int n=0;n!=racks.size();n++)
    total+=racks[n].cs_p4_orb;
  return total;
}

double module::calc_cs_p4_desc_ttl(){
  double total=0;
  for(int n=0;n!=racks.size();n++)
    total+=racks[n].cs_p4_desc;
  return total;
}

double module::calc_cs_m20_asc_ttl(){
  double total=0;
  for(int n=0;n!=racks.size();n++)
    total+=racks[n].cs_m20_asc;
  return total;
}

double module::calc_cs_m20_orb_ttl(){
  double total=0;
  for(int n=0;n!=racks.size();n++)
    total+=racks[n].cs_m20_orb;
  return total;
}

double module::calc_cs_m20_desc_ttl(){
  double total=0;
  for(int n=0;n!=racks.size();n++)
    total+=racks[n].cs_m20_desc;
  return total;
}

double module::calc_cs_m80_asc_ttl(){
  double total=0;
  for(int n=0;n!=racks.size();n++)
    total+=racks[n].cs_m80_asc;
  return total;
}

double module::calc_cs_m80_orb_ttl(){
  double total=0;
  for(int n=0;n!=racks.size();n++)
    total+=racks[n].cs_m80_orb;
  return total;
}

double module::calc_cs_m80_desc_ttl(){
  double total=0;
  for(int n=0;n!=racks.size();n++)
    total+=racks[n].cs_m80_desc;
  return total;
}

double module::calc_cs_m180_asc_ttl(){
  double total=0;
  for(int n=0;n!=racks.size();n++)
    total+=racks[n].cs_m180_asc;
  return total;
}

double module::calc_cs_m180_orb_ttl(){
  double total=0;
  for(int n=0;n!=racks.size();n++)
    total+=racks[n].cs_m180_orb;
  return total;
}

double module::calc_cs_m180_desc_ttl(){
  double total=0;
  for(int n=0;n!=racks.size();n++)
    total+=racks[n].cs_m180_desc;
  return total;
}
