Select Git revision
server_unb_data_logger.py
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
server_unb_data_logger.py 3.63 KiB
##server.py
from socket import * #import the socket library
import cPickle as pickle
import time
from commands import getstatusoutput
import read_datarates
from decimal import *
##let's set up some constants
HOST = '' #we are the host
PORT = 44330 #arbitrary port not currently in use
ADDR = (HOST,PORT) #we need a tuple for the address
BUFSIZE = 4096 #reasonably sized buffer for data
## now we create a new socket object (serv)
## see the python docs for more information on the socket types/flags
serv = socket( AF_INET,SOCK_STREAM)
serv.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
##bind our socket to the address
serv.bind((ADDR)) #the double parens are to create a tuple with one element
serv.listen(1) #5 is the maximum number of queued connections we'll allow
conn,addr = serv.accept() #accept the connection
unbnr = conn.recv(BUFSIZE)
node_list = ['FN-0','FN-1','FN-2','FN-3','BN-0','BN-1','BN-2','BN-3']
label_list = ['Time', 'ETH PHY temp', 'UNB current', 'UNB voltage'] + node_list
data_dict = {}
datarate = read_datarates.datarate_reader(unbnr)
def calc_datarates_Gbps(exec_time):
t0 = time.time()
datarates_dict = datarate.get_data_rates()
devide_time = time.time() - t0 + exec_time
getcontext().prec = 9
for key in datarates_dict.keys():
try:
datarates_dict[key] = Decimal(datarates_dict[key] / devide_time * 8 / 1000000000)
except:
datarates_dict[key] = None
return datarates_dict
# Write None to all dict keys
for i in range(len(label_list)):
data_dict[label_list[i]] = None
cmd = "python $UPE/peripherals/util_unb_sens.py --unb {unbnr} --fn 0:3 --bn 0:3 -n 0".format(unbnr=unbnr)
# First time datarate reading doesn't return anything
datarate.get_data_rates()
startTime_sleep = time.time()
startTime_process = time.time()
while True:
output = getstatusoutput(cmd)[1] # Get statusoutput
linelist = []
# Filter output and get timestamp
b = True
for line in output.rstrip().split('\n'):
if 'SENS' in line:
b = False
timestamp = line[12:20]
linelist.append(line)
if b:
conn.send('ERROR')
break
data_dict['Time'] = timestamp
# FPGA tempertures
s = 'FPGA temperature ='
e = ' [degrees]'
for i in range(8):
for line in linelist:
if s in line and node_list[i] in line:
data_dict['{node}_temp'.format(node = node_list[i])] = int(line[line.find(s)+len(s):line.find(e)])
# ETH PHY temperture
s = 'ETH PHY temperature ='
for line in linelist:
if s in line:
eth_phy = int(line[line.find(s)+len(s):line.find(e)])
data_dict['ETH PHY temp'] = eth_phy
# UNB supply curent
s = 'UNB supply current ='
e = ' [A]'
for line in linelist:
if s in line:
I = float(line[line.find(s)+len(s):line.find(e)])
data_dict['UNB current'] = I
# UNB supply voltage
s = 'UNB supply voltage ='
e = ' [V]'
for line in linelist:
if s in line:
U = float(line[line.find(s)+len(s):line.find(e)])
data_dict['UNB voltage'] = U
# get data rates
process_time = time.time() - startTime_process
data_dict.update(calc_datarates_Gbps(process_time))
startTime_process = time.time()
sleep_time = time.time() - startTime_sleep
if sleep_time < 1:
time.sleep(1-sleep_time)
startTime_sleep = time.time()
# Write data_dict to client
data = pickle.dumps(data_dict, -1)
conn.send( data )
# check if stopped
if conn.recv(BUFSIZE) == 'STOP':
break
conn.close()