##// END OF EJS Templates

File last commit:

r46:47
r48:49
Show More
server.py
335 lines | 11.6 KiB | text/x-python | PythonLexer
import os
import library
import time
class ABSServer:
def __init__(self,ipSource="localhost", ipDestino="192.168.1.255", 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.UDPComm(self.ipSource, self.ipDestino, self.portDestino, asServer)
self.commClientObj = library.UDPComm(self.ipSource, self.ipDestino2, self.portDestino2)
#self.ftpCommObj = library.FTPComm(self.ipSource, self.ipDestino, self.ftpPortDestino)
def sendFile(self, base):
#Needed for the loop
rx_frame_list = self.datarx.split('\n',2)
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")
#Setting variables of the loop
i =3
module = 1
number_of_modules = 16
number_of_beams = int (experiment_number)
module_list = self.connection_status(10)
#Loop for creating and sending the control module files
while module <= number_of_modules:
#Generating the control modules files
fobj = open(experiment_name,"w")
fobj.write(experiment_name + "\n")
fobj.write("------\n")
# tmp_list = rx_frame_list[i:i + number_of_beams + 1]
fobj.write(lst_control_modules[module-1])
fobj.write("------\n")
fobj.close()
if module_list[module -1] == "1":
#Preparing and doing the tftp command
cmd = "tftp -m binary 192.168.1."+ str(base + module) +" 69 -c put " + experiment_name
print cmd
os.system(cmd)
#Updating variables of the loop
i = i + number_of_beams + 2
module += 1
#Working with the UDP socket
# self.commClientObj.sendData2ControlModules("CARGA:" + experiment_name + ":")
# self.commClientObj.sendData2ControlModules("CAMBIA:0:")
#Another tftp way
# self.ftpCommObj.sendFile(filename)
# rpta = self.commClientObj.sendTxRxCommand(cmd# filename2 = "report.txt"
#fobj2 = open(filename2,"w")
#fobj2.write("Verification_file\n")
#fobj2.write("-----------------\n")
#fobj2.close()='CARGA', data=filename)
self.experiment_name = experiment_name
def __sendAnswer(self, cmd):
data = "OK"
if cmd == "SNDF":
self.commServerObj.sendData2(cmd="SNDF", data=data)
if cmd == "CHGB":
self.commServerObj.sendData2(cmd="CHGB", data=data)
if cmd == "ANST":
data = self.tx_buffer
self.commServerObj.sendData2(cmd="ANST", data=data)
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(10, mode=2)
datarpta = self.tx_buffer
self.commServerObj.sendData2(cmd=cmd, data=datarpta)
return cmd
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 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 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 __loadFile(self):
#Working with the UDP socket
self.commClientObj.sendData2ControlModules("CARGA:" + self.experiment_name + ":")
self.commClientObj.sendData2ControlModules("CAMBIA:0:")
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 sendFile2Modules(self):
#Needed for the loop
rx_frame_list = self.datarx.split('\n',2)
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(experiment_name, lst_control_modules[address-1])
cmd = "tftp -m binary 192.168.1."+ str(address) +" 69 -c put " + experiment_name
print cmd
os.system(cmd)
self.experiment_name = experiment_name
self.__loadFile()
def changeBeam(self):
#rpta = self.commClientObj.sendTxRxCommand(cmd='CAMBIA', data="0")
self.commClientObj.sendData2ControlModules("CAMBIA:" + self.datarx + ":")
def __getStsMode1(self):
#rpta = self.commClientObj.sendTxRxCommand(cmd='CHEQUEO', data="0")
self.commClientObj.sendData2ControlModules("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.sendData2ControlModules("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"
line1 = "Verification_file\n"
line2 = "-----------------\n"
report_list = [line1, line2]
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)
#Sub_header
if base == 10:
report_list.append("S" + str(module) + "\n")
else:
report_list.append("N" + str(module) + "\n")
# Content
fobj1 = open(filename1,"r")
file_list_1 = fobj1.readlines()
fobj1.close()
content = ''.join(file_list_1[2:-1])
# content = "010101\n"
report_list.append(content)
#Ending
report_list.append("------\n")
module = module + 1
#print "\nFinalizado"
self.tx_buffer = ''.join(report_list)
def __getStsMode3(self):
#rpta = self.commClientObj.sendTxRxCommand(cmd='CHEQUEO', data="0")
self.commClientObj.sendData2ControlModules("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"
line1 = "Verification_file\n"
line2 = "-----------------\n"
report_list = [line1, line2]
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)
#Sub_header
if base == 10:
report_list.append("S" + str(module) + "\n")
else:
report_list.append("N" + str(module) + "\n")
# Content
fobj1 = open(filename1,"r")
file_list_1 = fobj1.readlines()
fobj1.close()
content = ''.join(file_list_1[2:-1])
# content = "010101\n"
report_list.append(content)
#Ending
report_list.append("------\n")
module = module + 1
#print "\nFinalizado"
self.tx_buffer = ''.join(report_list)
def getStatus(self, base, mode):
if mode == 1:
self.__getStsMode1()
else:
self.__getStsMode2()
if __name__ == '__main__':
absObj = ABSServer()
while 1:
cmd = absObj.waitRequest()
absObj.__sendAnswer(cmd)