#!/usr/bin/env python

from cpphlib import *
import sys
       
class SSOFFCONTROLCLASS:

    def __init__(self, CAEIF, IOIF):
        """Initializing class of the control program"""
        self.CAEIF = CAEIF
        self.IOIF = IOIF
        self.FINGERPROTECT = PPPASSIVETIMER()
        self.CAEIF.commandstatemachine("POLLON:1\r\n",">",500)
        self.CAEIF.commandstatemachine("ENABLEPOLL\r\n",">",500)
        self.runworkingloop()        
        self.CAEIF.commandstatemachine("DISABLEPOLL\r\n",">",500)
        self.CAEIF.commandstatemachine("POLLOFF:1\r\n",">",500)
          
    def runworkingloop(self):
        while(1):
            if self.IOIF.getinputflag():
                data = self.IOIF.getinputdata()
                if "a1b1d80" in data: #exit button press
                    break
                if "a1b0d01" in data: #switch turn ON by finger
                    self.FINGERPROTECT.set(0.6)
                if "a1b0d00" in data: #switch turn OFF by servo
                    self.IOIF.send("a2b0dB0\r\n")
            if self.FINGERPROTECT.isexpired():
                self.IOIF.send("a2b0d78\r\n")      
                
if __name__ == '__main__': 
    """Main program"""
    PPDriver = PPDRIVER();
    PPDriver.registerCAEinterface("localhost", 8000) 
    PPDriver.registerIOinterface("localhost", 12000)
    PPDriver.connectinterfaces()
    CAEIF, IOIF, MONIF = PPDriver.getinterfaces()
    SsoffInstance = SSOFFCONTROLCLASS(CAEIF, IOIF)
    PPDriver.disconnectinterfaces()
    sys.exit(0) 