# *****************************************************************************
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# Module authors:
#   Markus Zolliker <markus.zolliker@psi.ch>
#
# *****************************************************************************

from frappy.core import BoolType, FloatRange, Parameter, Readable, Writable, Attached, EnumType, nopoll
from frappy_psi.trinamic import Motor
from frappy_psi.ccu4 import Pressure, NeedleValveFlow


class ValveMotor(Motor):
    has_inputs = True


class HePump(Writable):
    valvemotor = Attached(Motor)
    valve = Attached(Writable)
    value = Parameter(datatype=BoolType())
    target = Parameter(datatype=BoolType())
    pump_type = NeedleValveFlow.pump_type
    eco_mode = Parameter('eco mode', BoolType(), readonly=False)
    has_feedback = Parameter('feedback works', BoolType(), readonly=False, default=True)

    def write_target(self, value):
        self.valvemotor.write_output0(value)

    def read_target(self):
        if self.valvemotor.io.is_connected:
            return self.valvemotor.read_output0()
        return self.target

    def read_value(self):
        if self.has_feedback and self.valvemotor.io.is_connected:
            return not self.valvemotor.read_input3()
        return self.target

    def read_eco_mode(self):
        if self.pump_type == 'xds35' and self.valvemotor.io.is_connected:
            return self.valvemotor.read_output1()
        return False

    def write_eco_mode(self, value):
        if self.pump_type == 'xds35':
            return self.valvemotor.write_output1(value)
        # else silently ignore






