import library3
import os.path

class ABSClient(object):
    
    def __init__(self,ipSource="192.168.1.117", iDSource="Clnt_01", ipDestino="192.168.1.117", iDDestino = "CeCnMod", portDestino=7000):
        
        self.ipSource = ipSource
        self.iDSource = iDSource 
        self.ipDestino = ipDestino
        self.iDDestino = iDDestino 
        self.portDestino = portDestino
        
        self.createObjects()
        
    def createObjects(self):
        
        self.commObj = library3.TCPComm(self.ipSource, self.iDSource, self.ipDestino, self.iDDestino, self.portDestino)
        #self.wFiles = library3.FilesStuff()
        
    def __ConnectionWithCentralControl(self, cmd, data):

        self.commObj.open_socket()
        self.commObj.sendData(cmd = cmd, data = data, ipDestino = self.ipDestino)
        ipSource, ipDestino, cmd, output = self.commObj.waitData()
        self.commObj.close_socket()
        
        return output
    
#    def abs2ControlModuleFormatFile(self, filename):
#        
#        #From matriz to control module format
#        self.wFiles.toCentralControlFormat(filename)
#        FileName = "CentralControlFormat.txt"
#        F_Obj = open(FileName,"r")
#        FileList = F_Obj.readlines()
#        F_Obj.close()
#        FileStr = "".join(FileList)
#        
#        return FileStr
     
    def sendFile(self, filename):
        
        data = self.__readFile(filename)
        answer = self.__ConnectionWithCentralControl(cmd = "SNDF", data = data)
        return answer
        
    def changeBeam(self, newBeam):

        answer = self.__ConnectionWithCentralControl(cmd = "CHGB", data = newBeam)
        return answer
    
    def __writeFile(self, filename, data):

        fobj = open(filename,"w")
        fobj.writelines(data)
        fobj.close()

    def __readFile(self, filename):

        fobj = open(filename,"r")
        listData = fobj.readlines()
        fobj.close()        
        tmp = "".join(listData)
        #Adding filename to the begining of the data
        newfilename = os.path.split(filename)[1]
        #data = filename + '\n' + tmp
        data = newfilename + '\n' + tmp
        return data
        
        
    def getControlModuleStatus(self, data):

        data = self.__ConnectionWithCentralControl(cmd = "ANST", data = data)
        self.__writeFile("report.txt", data)

    def getControlModulePhase(self, opt, u = "50", pw = "10"):

        if opt == '0':
            data = self.__ConnectionWithCentralControl(cmd = "LWPH", data = u + '/' + pw + '/')
        elif opt ==  '1':
            data = self.__ConnectionWithCentralControl(cmd = "BGPH", data = u + '/' + pw + '/')
        elif opt == '2':
            data = self.__ConnectionWithCentralControl(cmd = "cBPH", data = u + '/' + pw + '/')
        else:
            data = self.__ConnectionWithCentralControl(cmd = "cLPH", data = u + '/' + pw + '/')
#        self.__writeFile("report.txt", data)
    
    def getConnectionStatus(self):

        data = self.__ConnectionWithCentralControl(cmd = "NTST", data = "none")
        self.__writeFile("connection_status.txt", data)

if __name__ == '__main__':
    
    filename = "experimento1.abs"
    
    absObj = ABSClient()
    print absObj.sendFile(filename)
