#!/usr/bin/env python

from cpphlib import *

class RELAYSTEST:
    
    def __init__(self, CAEIF, IOIF):
        self.CAEIF = CAEIF
        self.IOIF = IOIF
        self.IOCG = PPIOCARDGADGETS(self.CAEIF, self.CAEIF)
        self.BM = PPBITMANIPULATOR()   
        self.ONTimer = PPPASSIVETIMER()
        self.OFFTimer = PPPASSIVETIMER()     
        self.DGOAddr = -1
        self.DGIAddr = -1
    
    def runtest(self):
        """Identify used cards address by discover dict"""
        for addr in range(16):
            if self.IOCG.DiscoverDict.get(addr) == "16 channel DIGITAL INPUT":
                self.DGIAddr = addr
            if self.IOCG.DiscoverDict.get(addr) == "16 channel DIGITAL OUTPUT":
                self.DGOAddr = addr
        if self.DGOAddr == -1 or self.DGIAddr == -1:
            print "Identify unsuccessfull!"
            return        
        """switch off pollings by discover dict"""        
        for addr in range(16):
            if self.IOCG.DiscoverDict.get(addr) != "EMPTY":
                if self.IOCG.CardIOTypes.get(self.IOCG.DiscoverDict.get(addr)) == "INP":
                    self.CAEIF.commandstatemachine("POLLOFF:%X\r\n"%addr,"CIA: POLLOFF OK at address %X\r\n>\r\n"%addr,500)                      
        """Switch On pollings on used cards"""
        self.CAEIF.commandstatemachine("POLLON:%X\r\n"%self.DGIAddr,"CIA: POLLON OK at address %X\r\n>\r\n"%addr,500)
        print " "
        print "Hardware OK."
        print 
        print "Control handles:"
        print " Color buttons on input matching unit"
        print " - push BLUE button to switch on relay 1 (red light power on)"
        print " - push RED and GREEN button to switch on relay 2 (yellow light power on)"
        print " - push YELLOW button to switch off relay 1 and 2 (red and yellow light power off)"
        print "Exit from test:"
        print " Push button 7 in block 1 on input card"
        print " "
        self.CAEIF.commandstatemachine("ENABLEPOLL\r\n",">",500)
        relaystate = "00"
        prevrelaystate = "00"
        self.IOIF.send("a%Xb0dFF\r\n"%self.DGOAddr)
        self.IOIF.send("a%Xb1dFF\r\n"%self.DGOAddr)
        while(1):   
            if self.IOIF.getinputflag():
                data = self.IOIF.getinputdata()
                if "a%Xb0"%self.DGIAddr in data:
                    if self.BM.getbitstate(data[5:7],0) == "1":
                        self.ONTimer.set(0.02)
                        relaystate = self.BM.setbitstate(relaystate,1,0)
                    if self.BM.getbitstate(data[5:7],2) == "1":
                        self.OFFTimer.set(0.02)
                        relaystate = self.BM.setbitstate(relaystate,1,1)
                        relaystate = self.BM.setbitstate(relaystate,0,2)
                    if self.BM.getbitstate(data[5:7],1) == "1" and self.BM.getbitstate(data[5:7],3) == "1":
                        relaystate = self.BM.setbitstate(relaystate,1,2)        
                if "a%Xb1"%self.DGIAddr in data:
                    if self.BM.getbitstate(data[5:7],7) == "1":
                        break             
            if self.ONTimer.isexpired():
                relaystate = self.BM.setbitstate(relaystate,0,0)           
            if self.OFFTimer.isexpired():
                relaystate = self.BM.setbitstate(relaystate,0,1)           
                        
            if prevrelaystate != relaystate:
                self.IOIF.send("a%Xb0d%s\r\n"%(self.DGOAddr, self.BM.inverse(relaystate)))
                prevrelaystate = relaystate   
                                 
        self.IOIF.send("a%Xb0dFF\r\n"%self.DGOAddr)
        self.IOIF.send("a%Xb1dFF\r\n"%self.DGOAddr)                      
        self.CAEIF.commandstatemachine("DISABLEPOLL\r\n",">",500)
        """Switch Off pollings on used cards"""
        self.CAEIF.commandstatemachine("POLLOFF:%X\r\n"%self.DGIAddr,"CIA: POLLOFF OK at address %X\r\n>\r\n"%addr,500)
        

if __name__ == "__main__":
    """Main program"""
    print
    print "PP digital output test start" 
    print
    print "Connecting to PP driver's CAE and IO interface"
    PPDriver = PPDRIVER();
    PPDriver.registerCAEinterface("localhost", 8000)
    PPDriver.registerIOinterface("localhost", 12000)
    PPDriver.connectinterfaces()
    CAEIF, IOIF, MONIF = PPDriver.getinterfaces()
    RelayTests = RELAYSTEST(CAEIF, IOIF)   
    RelayTests.runtest() 
    print "Disonnecting from PP driver's CAE and IO interface"
    PPDriver.disconnectinterfaces()
    print 
    print "PP test end" 
    print
