#utilites for the collection stage

require 'sampler'

def pullReagent chemical, volume=2, airVol=2.5, holdTime=5 
  Log.record "Pulling #{volume}ml of #{chemical}"
  CSV.dial :CSR
  CSR.select chemical
  CS.to volume
  delay holdTime
  CSR.select :air
  CS.pull airVol
end

def addReagent chemical,fixDelay=15,volume=2,airVol=2.5,holdTime=5
  CBV.dialBetween :CSV, :waste1
  CTV.dial :CSV, :up
  pullReagent chemical, volume, airVol, holdTime
  CSV.dial :CTV
  CS.to 0.5, CSfast
  delay fixDelay
  CBV.dial :waste1
  CS.empty
  delay 10
  CTV.to :air, :down  #vent residual pressure
end

def pullProcessReagent chemical, volume=1, holdTime=5 
  Log.record "Pulling #{volume}ml of #{chemical}"
  PSV.to :PSR
  PSR.select chemical
  PS.to volume
  delay holdTime
  PSR.select :air
  PS.fill
end

def addProcessReagent chemical,volume=1,holdTime=5
  PSR.select :air
  pullProcessReagent chemical, volume, holdTime
  PTV.to :puck
  PSV.to :PTV
  PBV.to :waste1
  PS.push 0.5
  delay 3
  PBV.advance 0.5
  PS.to 0
end

def recoverProcessReagent waste=:waste1
  Log.record "Drying Process Puck to #{waste}"
  PSV.to :PBV
  PBV.to :PSV
  PS.fill
  delay 3
  PSR.to :air
  PSV.to :PSR
  delay 2
  PSV.to waste
  PS.to 0, PSfast
end

def wcrEvac #bubblePt
# pressurize puck with Air to bubblePt then release to waste
#TODO --- Add multi-threading, use pressure sensors
#Use bubblePt parameter and both pressure sensors
  ambientPSI = Float Sampler::CanPSI
  if CS.volume > .02
    CSV.to :waste
    CS.empty
  end
  CBV.connect SSV
  CSV.to :air
  CS.fill   #filling with air
  CSV.to :CTV
  CS.to 2
  delay 10
  CTV.to :air  #vent pressure
  CSV.to :air
  CS.to 3
  CSV.to :CTV  #vent puck top pressure
  delay 2
  CSV.to :air
  CS.to 0
  
  SS.to 10, SSslow  #try to dry dribble off puck bottom
  delay 15
  SSV.to :spit
  SS.to 0, SSfast
  SS.to 5
  SSV.to :CBV
  delay 2
  SSV.to :spit
  SS.to 0, SSfast
  delay 1
  SS.coast
end


class << CSR

  def purge flushVolume=5
  # Flush just the CSR
    raise (CC.name + " is not closed!") unless CC.closed?
    raise (CS.name + " is not empty!") unless CS.volume < .1
    pullReagent :flush, flushVolume
    CSV.to :waste
    CS.to 0
  end
  
end


class << CS

  def pullFlush flushVol=5, airVol=2
    raise (CC.name + " is not closed!") unless CC.closed?
    raise (CS.name + " is not empty!") unless CS.volume < .1
    CSV.to :flush
    CS.to flushVol, CSfast
    delay 5
    if airVol
      CSV.to :air
      CS.pull airVol, CSfast
    end
    self
  end
  
  def purgeLower destination=:waste1
  #Flush just the lower section
    pullFlush
    CSV.connect CTV
    CBV.to destination
    CS.to 0, CSfast
    self
  end
  
  def purgeUpper
    pullFlush maxVolume, nil
    CSV.connect CBV
    CTV.to :intake
    begin
      Intake.open
      CS.to 0, CSfast
    ensure
      Intake.close
    end
    CTV.to :air
    pullFlush
    CSV.connect CBV
    CS.to 0, CSfast
    delay 7
    CS.to 3, CSfast
    CSV.to :waste
    CS.to 0, CSfast
    pullFlush 3, 2
    CSV.to :air
    CS.to 0, CSfast
    delay 7
    CS.to 3, CSfast
    CSV.to :waste
    CS.to 0, CSfast
    self
  end
  
  def killIntake flushVol=8, airVol=2
    raise (CC.name + " is not closed!") unless CC.closed?
    raise (CS.name + " is not empty!") unless CS.volume < .1
    CSV.to :CSR
    CSR.to :kill
    CS.to flushVol, CSfast
    delay 5
    if airVol
      CSR.to :air
      CS.to flushVol+airVol, CSfast
    end
    CSV.connect CBV
    CTV.to :intake
    begin
      Intake.open
      CS.to airVol, CSfast
    ensure
      Intake.close
    end
    CTV.to :air
    CS.to 0
    CSV.to :CSR
    CSR.to :flush
    CS.to 5
    delay 5
    CSR.to :air
    CS.fill
    CSV.to :CBV
    CS.to 0
    self
  end
 
end

