'''
Created on Jun 14, 2012

@author: manuel
'''
from PhysioMonitor import SyringePump
from PhysioMonitor.SyringePump import invalidCommandException,\
    unknownCommandException, valueOORException

class DummyPump(SyringePump):
    def __init__(self, inPort, inBaudRate):
        self.port = inPort
        self.baud = inBaudRate
        self.currState = 0
        self.nextState = 1
        self.currDiameter = 10
        self.currRate = 20
        self.currUnits = 0
    
    def __del__(self):
        pass
    
    def getInfo(self):
        retVal = '''fake pump on serial port %s (%s baud)
        syringe diameter: %.2f
        current rate: %.2f %s
        current state: '''%(self.port,\
                                  self.baud,self.getDiameter(),self.getRate(),\
                                  self.getTextualUnits())
        if self.isRunning():
            if self.getDirection()==1:
                retVal+='Infusing...'
            else:
                retVal+='Withdrawing...'
        else:
            retVal += 'Stopped'
        return retVal
    
    def start(self):
        if self.currState==0:
            self.currState=self.nextState
        else:
            raise invalidCommandException("Pump already started")
    
    def stop(self):
        if self.currState==1 or self.currState==2:
            self.nextState=self.currState
            self.currState=0
        else:
            raise invalidCommandException("Pump already stopped")
    
    def reverse(self):
        if not self.currState==0:
            if self.currState==1:
                self.currState=2
            else:
                self.currState=1
        else:
            if self.nextState==1:
                self.nextState=2
            else:
                self.nextState=1
    
    def isRunning(self):
        return not self.currState==0
    
    def clearAccumulatedVolume(self):
        pass
    
    def clearTargetVolume(self):
        pass
    
    def setDirection(self, inValue):
        if inValue>0 and inValue<=len(self._directions)-1:
            if self.isRunning():
                self.currState = inValue
            else:
                self.nextState = inValue
        else:
            raise unknownCommandException("direction should be either INFUSION or WITHDRAWAL")
    
    def setSyringeDiameter(self, inValue):
        if inValue<=0:
            raise valueOORException("Diameter must be a positive value")
        else:
            self.currDiameter = inValue;
    
    def setRate(self, inValue, inUnits):
        if inValue<=0:
            raise valueOORException("Rate must be a positive value")
        if inUnits<0 or inUnits>len(self._units)-1:
            raise valueOORException("Units must be an integer between %d and %d"%(0,len(self._units)-1))
        self.currUnits=inValue
        self.currRate = inValue
            
    def setTargetVolume(self, inValue):
        pass
    
    def getDiameter(self):
        return self.diameter
    
    def getRate(self):
        return self.rate
    
    def getUnits(self):
        return self.units
    
    def getAccumulatedVolume(self):
        return 0
    
    def getTargetVolume(self):
        return 0
    
    def getDirection(self):
        if self.isRunning():
            return self.currState
        else:
            return self.nextState
    
    def getPossibleUnits(self):
        return self._units