# *****************************************************************************
# 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>
# *****************************************************************************

"""interlocks for furnance"""

import time
from frappy.core import Module, Writable, Attached, Parameter, FloatRange, Readable, BoolType, ERROR, IDLE

class Interlocks(Module):
    input = Attached(Readable, 'the input module')
    vacuum = Attached (Readable, 'the vacuum pressure')
    wall_T = Attached (Readable, 'the wall temperature')
    control = Attached(Module, 'the control module')
    relais = Attached(Writable, 'the interlock relais')
    wall_limit = Parameter('maximum wall temperature', FloatRange(0, unit='degC'),
                           default = 50, readonly = False)
    vacuum_limit = Parameter('maximum vacuum pressure', FloatRange(0, unit='mbar'),
                             default = 0.1, readonly = False)
    
    def doPoll(self):
        super().doPoll()
        if self.input.status[0] >= ERROR:
            self.control.status = self.input.status
        elif self.vacuum.value > self.vacuum_limit:
            self.control.status = ERROR, 'bad vacuum'
        elif self.wall_T.value > self.wall_limit:
            self.control.status = ERROR, 'wall overheat'
        else:
            return
        self.control.write_control_active(False)
        self.relais.write_target(False)
        
      