# SVN changeset patch # User imanay # Date 2012-12-19 22:28:19.437120 # Revision 63 Index: trunk/absroot/source/abspy/abscontrol/client2.py =================================================================== diff --git a/trunk/absroot/source/abspy/abscontrol/client2.py b/trunk/absroot/source/abspy/abscontrol/client2.py new file mode 10644 --- /dev/null (revision 0) +++ b/trunk/absroot/source/abspy/abscontrol/client2.py (revision 63) @@ -0,0 +1,488 @@ +# imports needed for the file convertion +import os +import sys +import time +import numpy as np + +import library + +class ABSClient: + + def __init__(self,ipSource="localhost", ipDestino="192.168.1.100", portDestino=7000): + + self.ipSource = ipSource + self.ipDestino = ipDestino + self.portDestino = portDestino + + self.createObjects() + + def createObjects(self): + + self.commObj = library.TCPComm(self.ipSource, self.ipDestino, self.portDestino) + + def sendFile(self, filename): + + #From matriz to control module format + self.FuncionMaestra_GeneraFormatoControlCentral(filename) + FileName = "FormatoControlCentral.txt" + F_Obj = open(FileName,"r") + FileList = F_Obj.readlines() + F_Obj.close() + FileStr = "".join(FileList) + data = FileStr + + self.commObj.sendData(cmd="SNDF", data=data) + self.commObj.waitData() + + def changeBeam(self, newBeam): + + self.commObj.sendData(cmd="CHGB", data=newBeam) + self.commObj.waitData() + + def __writeFile(self, filename, data): + + fobj = open(filename,"w") + fobj.writelines(data) + fobj.close() + + def getStatus(self, data): + + self.commObj.sendData(cmd="ANST", data = data) + ipSource, ipDestino, cmd, data = self.commObj.waitData() + self.__writeFile("report.txt", data) + +########## + + def FuncionMaestra_GeneraFormatoControlCentral(self,archivo): + """ Funcion que genera un archivo para el control central""" + + # CarpetaDeTrabajo='/home/redes/ABS_Control_2012_09_24/Control_Module_v1_Client_09_24/' + CarpetaDeTrabajo = os.getcwd() + '/' + #print CarpetaDeTrabajo + #CarpetaDeTrabajo='/home/redes/workspace/ABS_Client_v2/Debug/' + + def lst2string(lst): + string='' + for i in lst: + string=string+i + return string + + def string2lst(string): + lst = [] + for i in string: + lst.append(i) + return lst + + + def file1(string, type): + lst = string2lst(archivo) + fin = -1 + t = len(lst) + for i in np.arange(-1,-t,-1): + if lst[i]=='/': + fin=i + break + if type == '1': + nombre2 = lst[fin+1:] + nombre2[-1]='s' + nombre2 = lst2string(nombre2) + return nombre2 + if type == '2': + nombre2 = lst[fin+1:] + nombre2[-1]='1' + nombre2 = lst2string(nombre2) + return nombre2 + + + def EliminaSaltoDeLinea(cadena): + i = 0 + for elemento in cadena: + if elemento =='\n' or elemento =='\r': + pass + else: + i=i+1 + return cadena [:i] + + def NumeroDeExperimentos(path): + fichero1=open(path,'r') + cont=0 + for cadena in fichero1: + cont=cont+1 + if cont==3: + nexp='' + pos=0 + for elemento in cadena: + pos=pos+1 + if elemento=='=': + nexp=int(cadena[pos:]) + return nexp + fichero1.close() + + def Paridad(numero): + if numero%2==0: return 'par' + elif numero%2==1: return 'impar' + + def EvaluaCadena(cadena): + if len(cadena)>35: + if cadena[-1]=='$': + return cadena[-35:-2] + elif cadena[-1]==']': + return cadena[-34:-1] + else: + return None + + def GuardaEnLista(path): + fichero=open(path,'r') + lista=[] + for cadena in fichero: + cadena = EliminaSaltoDeLinea(cadena) + cadena = EvaluaCadena(cadena) + if cadena != None: + lista.append(cadena) + fichero.close() + return lista + + def CreaFicherosPrevios(): + vector = GuardaEnLista(archivo) + for i in range(1,NumeroDeExperimentos(archivo)+1): + fichero =open(CarpetaDeTrabajo+str(i)+'.txt','w') + for j in range(0,16): + fichero.write(vector[j+16*(i-1)]+'\n') + fichero.close() + + def CapturaValoresEnArchivo(path,polarizacion='up'): + fichero =open(path,'r') + cnt=0 + lstup=[] + lstdw=[] + for cadena in fichero: + cnt=cnt+1 + if cnt==5: + su01=cadena[17:20] + su02=cadena[21:24] + su03=cadena[25:28] + su04=cadena[29:32] + if cnt==6: + su05=cadena[17:20] + su06=cadena[21:24] + su07=cadena[25:28] + su08=cadena[29:32] + if cnt==7: + su09=cadena[17:20] + su10=cadena[21:24] + su11=cadena[25:28] + su12=cadena[29:32] + if cnt==8: + su13=cadena[17:20] + su14=cadena[21:24] + su15=cadena[25:28] + su16=cadena[29:32] + if cnt==13: + sd01=cadena[17:20] + sd02=cadena[21:24] + sd03=cadena[25:28] + sd04=cadena[29:32] + if cnt==14: + sd05=cadena[17:20] + sd06=cadena[21:24] + sd07=cadena[25:28] + sd08=cadena[29:32] + if cnt==15: + sd09=cadena[17:20] + sd10=cadena[21:24] + sd11=cadena[25:28] + sd12=cadena[29:32] + if cnt==16: + sd13=cadena[17:20] + sd14=cadena[21:24] + sd15=cadena[25:28] + sd16=cadena[29:32] + lstup=[su01,su02,su03,su04,su05,su06,su07,su08,su09,su10,su11,su12,su13,su14,su15,su16] + lstdw=[sd01,sd02,sd03,sd04,sd05,sd06,sd07,sd08,sd09,sd10,sd11,sd12,sd13,sd14,sd15,sd16] + if polarizacion=='up': + return lstup + elif polarizacion=='dw': + return lstdw + fichero.close() + + def CapturaValoresEnArchivo2(path,polarizacion='up'): + fichero =open(path,'r') + cnt=0 + lstup=[] + lstdw=[] + for cadena in fichero: + cnt=cnt+1 + if cnt==1: + nu01=cadena[1:4] + nu02=cadena[5:8] + nu03=cadena[9:12] + nu04=cadena[13:16] + eu01=cadena[17:20] + eu02=cadena[21:24] + eu03=cadena[25:28] + eu04=cadena[29:32] + if cnt==2: + nu05=cadena[1:4] + nu06=cadena[5:8] + nu07=cadena[9:12] + nu08=cadena[13:16] + eu05=cadena[17:20] + eu06=cadena[21:24] + eu07=cadena[25:28] + eu08=cadena[29:32] + if cnt==3: + nu09=cadena[1:4] + nu10=cadena[5:8] + nu11=cadena[9:12] + nu12=cadena[13:16] + eu09=cadena[17:20] + eu10=cadena[21:24] + eu11=cadena[25:28] + eu12=cadena[29:32] + if cnt==4: + nu13=cadena[1:4] + nu14=cadena[5:8] + nu15=cadena[9:12] + nu16=cadena[13:16] + eu13=cadena[17:20] + eu14=cadena[21:24] + eu15=cadena[25:28] + eu16=cadena[29:32] + if cnt==5: + wu01=cadena[1:4] + wu02=cadena[5:8] + wu03=cadena[9:12] + wu04=cadena[13:16] + su01=cadena[17:20] + su02=cadena[21:24] + su03=cadena[25:28] + su04=cadena[29:32] + if cnt==6: + wu05=cadena[1:4] + wu06=cadena[5:8] + wu07=cadena[9:12] + wu08=cadena[13:16] + su05=cadena[17:20] + su06=cadena[21:24] + su07=cadena[25:28] + su08=cadena[29:32] + if cnt==7: + wu09=cadena[1:4] + wu10=cadena[5:8] + wu11=cadena[9:12] + wu12=cadena[13:16] + su09=cadena[17:20] + su10=cadena[21:24] + su11=cadena[25:28] + su12=cadena[29:32] + if cnt==8: + wu13=cadena[1:4] + wu14=cadena[5:8] + wu15=cadena[9:12] + wu16=cadena[13:16] + su13=cadena[17:20] + su14=cadena[21:24] + su15=cadena[25:28] + su16=cadena[29:32] + if cnt==9: + nd01=cadena[1:4] + nd02=cadena[5:8] + nd03=cadena[9:12] + nd04=cadena[13:16] + ed01=cadena[17:20] + ed02=cadena[21:24] + ed03=cadena[25:28] + ed04=cadena[29:32] + if cnt==10: + nd05=cadena[1:4] + nd06=cadena[5:8] + nd07=cadena[9:12] + nd08=cadena[13:16] + ed05=cadena[17:20] + ed06=cadena[21:24] + ed07=cadena[25:28] + ed08=cadena[29:32] + if cnt==11: + nd09=cadena[1:4] + nd10=cadena[5:8] + nd11=cadena[9:12] + nd12=cadena[13:16] + ed09=cadena[17:20] + ed10=cadena[21:24] + ed11=cadena[25:28] + ed12=cadena[29:32] + if cnt==12: + nd13=cadena[1:4] + nd14=cadena[5:8] + nd15=cadena[9:12] + nd16=cadena[13:16] + ed13=cadena[17:20] + ed14=cadena[21:24] + ed15=cadena[25:28] + ed16=cadena[29:32] + if cnt==13: + wd01=cadena[1:4] + wd02=cadena[5:8] + wd03=cadena[9:12] + wd04=cadena[13:16] + sd01=cadena[17:20] + sd02=cadena[21:24] + sd03=cadena[25:28] + sd04=cadena[29:32] + if cnt==14: + wd05=cadena[1:4] + wd06=cadena[5:8] + wd07=cadena[9:12] + wd08=cadena[13:16] + sd05=cadena[17:20] + sd06=cadena[21:24] + sd07=cadena[25:28] + sd08=cadena[29:32] + if cnt==15: + wd09=cadena[1:4] + wd10=cadena[5:8] + wd11=cadena[9:12] + wd12=cadena[13:16] + sd09=cadena[17:20] + sd10=cadena[21:24] + sd11=cadena[25:28] + sd12=cadena[29:32] + if cnt==16: + wd13=cadena[1:4] + wd14=cadena[5:8] + wd15=cadena[9:12] + wd16=cadena[13:16] + sd13=cadena[17:20] + sd14=cadena[21:24] + sd15=cadena[25:28] + sd16=cadena[29:32] + lst_n_up=[nu01,nu02,nu03,nu04,nu05,nu06,nu07,nu08,nu09,nu10,nu11,nu12,nu13,nu14,nu15,nu16] + lst_n_dw=[nd01,nd02,nd03,nd04,nd05,nd06,nd07,nd08,nd09,nd10,nd11,nd12,nd13,nd14,nd15,nd16] + lst_s_up=[su01,su02,su03,su04,su05,su06,su07,su08,su09,su10,su11,su12,su13,su14,su15,su16] + lst_s_dw=[sd01,sd02,sd03,sd04,sd05,sd06,sd07,sd08,sd09,sd10,sd11,sd12,sd13,sd14,sd15,sd16] + lst_w_up=[wu01,wu02,wu03,wu04,wu05,wu06,wu07,wu08,wu09,wu10,wu11,wu12,wu13,wu14,wu15,wu16] + lst_w_dw=[wd01,wd02,wd03,wd04,wd05,wd06,wd07,wd08,wd09,wd10,wd11,wd12,wd13,wd14,wd15,wd16] + lst_e_up=[eu01,eu02,eu03,eu04,eu05,eu06,eu07,eu08,eu09,eu10,eu11,eu12,eu13,eu14,eu15,eu16] + lst_e_dw=[ed01,ed02,ed03,ed04,ed05,ed06,ed07,ed08,ed09,ed10,ed11,ed12,ed13,ed14,ed15,ed16] + + lstup = lst_s_up + lst_w_up + lst_n_up + lst_e_up + lstdw = lst_s_dw + lst_w_dw + lst_n_up + lst_e_up + + if polarizacion=='up': + return lstup + elif polarizacion=='dw': + return lstdw + fichero.close() + + + def CreaFormatoFinal(): + ne=NumeroDeExperimentos(archivo) + + #nombre01 = file1(archivo,'1') + nombre02 = file1(archivo,'2') + fichero=open(CarpetaDeTrabajo+'FormatoControlCentral.txt','w') + fichero.write(nombre02+'\n') + fichero.write(str(ne)+'\n') + for i in range(1,17): + + if i<10: + nmod = '0'+str(i) + else: nmod = str(i) + + + fichero.write('S'+nmod+'\n') + for j in range(1,ne+1): + ruta=CarpetaDeTrabajo+str(j)+'.txt' + lu=CapturaValoresEnArchivo(ruta,polarizacion='up') + ld=CapturaValoresEnArchivo(ruta,polarizacion='dw') + part1='' + part2='' + if lu[i-1]=='1.0': part1='000' + if lu[i-1]=='2.0': part1='001' + if lu[i-1]=='3.0': part1='010' + if lu[i-1]=='0.0': part1='011' + if lu[i-1]=='0.5': part1='100' + if lu[i-1]=='1.5': part1='101' + if lu[i-1]=='2.5': part1='110' + if lu[i-1]=='3.5': part1='111' + if ld[i-1]=='1.0': part2='000' + if ld[i-1]=='2.0': part2='001' + if ld[i-1]=='3.0': part2='010' + if ld[i-1]=='0.0': part2='011' + if ld[i-1]=='0.5': part2='100' + if ld[i-1]=='1.5': part2='101' + if ld[i-1]=='2.5': part2='110' + if ld[i-1]=='3.5': part2='111' + fichero.write(part1+part2+'\n') + fichero.write('------'+'\n') + fichero.close() + + def CreaFormatoFinal2(): + ne=NumeroDeExperimentos(archivo) + + #nombre01 = file1(archivo,'1') + nombre02 = file1(archivo,'2') + fichero=open(CarpetaDeTrabajo+'FormatoControlCentral.txt','w') + fichero.write(nombre02+'\n') + fichero.write(str(ne)+'\n') + + for i in range(1,65): + + if i<10: + nmod = '0'+str(i) + else: nmod = str(i) + + fichero.write("ABS_" + nmod+'\n') + + for j in range(1,ne+1): + ruta=CarpetaDeTrabajo+str(j)+'.txt' + lu=CapturaValoresEnArchivo2(ruta,polarizacion='up') + ld=CapturaValoresEnArchivo2(ruta,polarizacion='dw') + part1='' + part2='' + if lu[i-1]=='1.0': part1='000' + if lu[i-1]=='2.0': part1='001' + if lu[i-1]=='3.0': part1='010' + if lu[i-1]=='0.0': part1='011' + if lu[i-1]=='0.5': part1='100' + if lu[i-1]=='1.5': part1='101' + if lu[i-1]=='2.5': part1='110' + if lu[i-1]=='3.5': part1='111' + if ld[i-1]=='1.0': part2='000' + if ld[i-1]=='2.0': part2='001' + if ld[i-1]=='3.0': part2='010' + if ld[i-1]=='0.0': part2='011' + if ld[i-1]=='0.5': part2='100' + if ld[i-1]=='1.5': part2='101' + if ld[i-1]=='2.5': part2='110' + if ld[i-1]=='3.5': part2='111' + fichero.write(part1+part2+'\n') + fichero.write('------'+'\n') + fichero.close() + + def EliminaArchivosEnLaCarpeta(): + ne=NumeroDeExperimentos(archivo) + for i in range(1,ne+1): + os.remove(CarpetaDeTrabajo+str(i)+'.txt') + + CreaFicherosPrevios() + CreaFormatoFinal2() + EliminaArchivosEnLaCarpeta() + +########## + +if __name__ == '__main__': + + filename = "experimento1.abs" + + absObj = ABSClient() + absObj.sendFile(filename) +# absObj.changeBeam("0") +# absObj.changeBeam("1") +# absObj.changeBeam("2") +# absObj.changeBeam("3") +# absObj.changeBeam("4") +# absObj.changeBeam("5") +# absObj.changeBeam("6") +# absObj.changeBeam("7") +# absObj.getStatus(5) \ No newline at end of file Index: trunk/absroot/source/abspy/abscontrol/server2.py =================================================================== diff --git a/trunk/absroot/source/abspy/abscontrol/server2.py b/trunk/absroot/source/abspy/abscontrol/server2.py new file mode 10644 --- /dev/null (revision 0) +++ b/trunk/absroot/source/abspy/abscontrol/server2.py (revision 63) @@ -0,0 +1,280 @@ +import os +import library +import time + +class ABSServer: + + def __init__(self,ipSource="localhost", ipDestino="192.168.1.100", portDestino=7000, ipDestino2="192.168.1.255", portDestino2=5500, ftpPortDestino=None): + + self.ipSource = ipSource + self.ipDestino = ipDestino + self.portDestino = portDestino + + self.ipDestino2 = ipDestino2 + self.portDestino2 = portDestino2 + + self.ftpPortDestino = ftpPortDestino + self.experiment_name = "default" + self.tx_buffer = "default" + + self.createObjects() + + def createObjects(self): + + asServer = True + self.commServerObj = library.TCPComm(self.ipSource, self.ipDestino, self.portDestino, asServer) + self.commClientObj = library.TCPComm(self.ipSource, self.ipDestino2, self.portDestino2) + #self.ftpCommObj = library.FTPComm(self.ipSource, self.ipDestino, self.ftpPortDestino) + + + def waitRequest(self): + + ipSource, ipDestino, cmd, self.datarx = self.commServerObj.waitData() + + datarpta = "OK" + + if cmd == "SNDF": + self.sendFile2Modules() + + if cmd == "CHGB": + self.changeBeam() + + if cmd == "ANST": + self.getStatus(mode=3) + datarpta = self.tx_buffer + + self.commServerObj.sendData(cmd=cmd, data=datarpta) + + def checkModule(self, address): + + cmd = "ping -c 1 -w 1 192.168.1."+ str(address) + " >> /dev/null" + status = os.system(cmd) + + if status == 256: + return False + + return True + + def __writeReport(self, enaModules): + + status_array = ["Status of modules\n"] + status_array.append("----------------\n") + + for address in range(1,65): + if address in enaModules: +# status_array.append("192.168.1." + str(base + i + 1) + " [1 1]\n") + status_array.append("192.168.1." + str(address) + " [1 1]\n") + else: + status_array.append("192.168.1." + str(address) + " [0 0]\n") + + f = open("module_status.txt","w") + f.writelines(status_array) + f.close() + + def checkAntenna(self): + + """ + Direccion de los modulos de las antenas: + + Norte : 01-16 + Este : 17-32 + Oeste: : 33-48 + Sur : 49-64 + + """ + + enaModules = [] + + for address in range(1,65): + if self.checkModule(address): + enaModules.append(address) + + self.__writeReport(enaModules) + return enaModules + + def sendFile2Modules(self): + + #Needed for the loop + rx_frame_list = self.datarx.split('\n',2) + + self.experiment_name = rx_frame_list[0] + experiment_number = rx_frame_list[1] + str_control_modules = rx_frame_list[2] + + lst_control_modules = str_control_modules.split("------\n") + + enaModules = self.checkAntenna() + + for address in range(1,65): + + if address not in enaModules: + continue + + self.__writeModuleFile(self.experiment_name, lst_control_modules[address-1]) + + cmd = "tftp -m binary 192.168.1."+ str(address) +" 69 -c put " + self.experiment_name + print cmd + os.system(cmd) + + self.__loadFile() + + def __writeModuleFile(self, filename, str): + + fobj = open(filename,"w") + fobj.write(filename + "\n") + fobj.write("------\n") + fobj.write(str) + fobj.write("------\n") + fobj.close() + + def __readModuleFile(self, filename): + + fobj1 = open(filename,"r") + file_list_1 = fobj1.readlines() + fobj1.close() + content_str = ''.join(file_list_1[2:-1]) + + return content_str + + def __loadFile(self): + + #Working with the UDP socket + self.commClientObj.sendData("none", "CARGA:" + self.experiment_name + ":") + self.commClientObj.sendData("none", "CAMBIA:0:") + + def changeBeam(self): + + #rpta = self.commClientObj.sendTxRxCommand(cmd='CAMBIA', data="0") + self.commClientObj.sendData("none", "CAMBIA:" + self.datarx + ":") + + def getStatus(self,mode): + + if mode == 1: + self.__getStsMode1() + elif mode == 2: + self.__getStsMode2() + else: + self.__getStsMode3() + + + def __getStsMode1(self): + #rpta = self.commClientObj.sendTxRxCommand(cmd='CHEQUEO', data="0") + self.commClientObj.sendData("CHEQUEO:" + self.datarx + ":") + seconds = int (self.datarx) + # Give 5 seconds to the control modules + time.sleep(seconds) + # Checking the module connection + module_list = self.connection_status(10) + #Generating the complete report + module = 1 + number_of_modules = 16 + filename1 = "Verificacion" + filename2 = "report.txt" + fobj2 = open(filename2,"w") + fobj2.write("Verification_file\n") + fobj2.write("-----------------\n") + fobj2.close() + while module <= number_of_modules: + if module_list[module -1] == "1": + #Preparing and doing the tftp command + cmd = "tftp -m binary 192.168.1."+ str(base + module) +" 69 -c get " + filename1 + print cmd + os.system(cmd) + # Getting data from the control module file + fobj1 = open(filename1,"r") + file_list_1 = fobj1.readlines() + fobj1.close() + content = file_list_1[2:-1] + # + fobj2 = open(filename2,"a") + if base == 10: + fobj2.write("S" + str(module) + "\n") + else: + fobj2.write("N" + str(module) + "\n") + fobj2.writelines(content) + fobj2.write("------\n") + fobj2.close() + module = module + 1 + + def __getStsMode2(self): + + #rpta = self.commClientObj.sendTxRxCommand(cmd='CHEQUEO', data="0") + self.commClientObj.sendData("CHEQUEO:" + self.datarx + ":") + seconds = int (self.datarx) + # Give 5 seconds to the control modules + time.sleep(seconds) + # Checking the module connection + enaModules = self.checkAntenna() + #Generating the complete report + filename1 = "Verificacion" + line1 = "Verification_file\n" + line2 = "-----------------\n" + report_list = [line1, line2] + + for address in range(1,65): + + if address not in enaModules: + continue + #Preparing and doing the tftp command + cmd = "tftp -m binary 192.168.1."+ str(address) +" 69 -c get " + filename1 + print cmd + os.system(cmd) + #Sub_header + report_list.append("ABS_" + str(address) + "\n") + # Content + fobj1 = open(filename1,"r") + file_list_1 = fobj1.readlines() + fobj1.close() + content = ''.join(file_list_1[2:-1]) + report_list.append(content) + #Ending + report_list.append("------\n") + #print "\nFinalizado" + self.tx_buffer = ''.join(report_list) + + def __AddingHeader(self,content_list, title): + + line1 = title + "\n" + line2 = "-----------------\n" + header_list = [line1, line2] + verification_list = header_list + content_list + # Arming the frame + self.tx_buffer = ''.join(verification_list) + + def __getModuleFile(self, filename): + + enaModules = self.checkAntenna() + content_list = [] + for address in range(1,65): + + if address not in enaModules: + continue + #Preparing and doing the tftp command + cmd = "tftp -m binary 192.168.1."+ str(address) +" 69 -c get " + filename + print cmd + os.system(cmd) + #Sub_header + content_list.append("ABS_" + str(address) + "\n") + # From module file to list + content_str = self.__readModuleFile(filename) + content_list.append(content_str) + content_list.append("------\n") + + self.__AddingHeader(content_list, title = "Verification_file") + + def __getStsMode3(self): + + self.commClientObj.sendData("none", "CHEQUEO:" + self.datarx + ":") + seconds = int (self.datarx) + # Give 5 seconds to the control modules + time.sleep(seconds) + + self.__getModuleFile(filename = "Verificacion") + + +if __name__ == '__main__': + + absObj = ABSServer() + + while 1: + absObj.waitRequest() \ No newline at end of file