def __init__(self, options): """ Initialize class with options passed in from option parser, durations of events, start the labjack, etc. """ self.port_num = options.port_num self.debug = options.debug # initialize arduino #arduino = serial.Serial('/dev/tty.usbserial',9600) # # initialize labjack # try: # self.labjack = u6.U6() # except: # print "--------------------------------------------------------------" # print " Labjack could not be initialized!" # print "--------------------------------------------------------------" # self.labjack = None # initialize json logging self.json_logger = JsonLogThread() self.json_logger.set_filename(options.output_path) self.json_logger.recording = True self.json_logger.start() # initialize labjack logger self.labjack_logger = LabjackLogger(options.config_file, self.json_logger) self.labjack_logger.start()
def __init__(self, options): """ Initialize class with options passed in from option parser, durations of events, start the labjack, etc. """ self.port_num = options.port_num self.debug = options.debug # initialize json logging self.json_logger = JsonLogThread() self.json_logger.set_filename(options.output_path) self.json_logger.recording = True self.json_logger.start() # initialize labjack logger self.labjack_logger = LabjackLogger(options.config_file, self.json_logger) self.labjack_logger.start()
class EventServer(object): """ This class is able to generate a TCP/IP server that persists and listens for transient clients. Once a client is contacted, the server listens for instructions from the client and dispatched commands to other devices conditional on the client data. It also manages time locking with other machines. """ def __init__(self, options): """ Initialize class with options passed in from option parser, durations of events, start the labjack, etc. """ self.port_num = options.port_num self.debug = options.debug # initialize arduino #arduino = serial.Serial('/dev/tty.usbserial',9600) # # initialize labjack # try: # self.labjack = u6.U6() # except: # print "--------------------------------------------------------------" # print " Labjack could not be initialized!" # print "--------------------------------------------------------------" # self.labjack = None # initialize json logging self.json_logger = JsonLogThread() self.json_logger.set_filename(options.output_path) self.json_logger.recording = True self.json_logger.start() # initialize labjack logger self.labjack_logger = LabjackLogger(options.config_file, self.json_logger) self.labjack_logger.start() def spawn_server(self): """ Start a TCP server that listens for connections from clients, collects events, logs them, and potentially triggers labjack events. """ # Create a TCP/IP socket sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Bind the socket to the port server_address = ('localhost', self.port_num) if self.debug: print >> sys.stderr, 'starting up on %s port %s' % server_address sock.bind(server_address) # Listen for incoming connections sock.listen(1) # while True: # Wait for a connection if self.debug: print >> sys.stderr, 'waiting for a connection' try: self.connection, client_address = sock.accept() except KeyboardInterrupt: print "Session ended via keypress." # break try: if self.debug: print >> sys.stderr, 'connection from', client_address # Receive the data. # Run as long as data is streaming. while True: # Note: if Unity messages increase in length, # data recv size may need to increase data = self.connection.recv(1024) if self.debug: pass #print >>sys.stderr, 'received "%s"' % data if data: try: # check if there is a data hangover split_data = data.split("}")[:-1] if len(split_data) > 1: # if so run and log each split separately for d in data: current_data = d + '}' self.json_logger.log(current_data) self.run_event(current_data) else: current_data = split_data[0] + '}' self.json_logger.log(current_data) self.run_event(current_data) except Exception, e: print "Lost event", data, "due to error:" print e except KeyboardInterrupt: print "Run ended via ESC keypress." break else:
class EventServer( object ): """ This class is able to generate a TCP/IP server that persists and listens for transient clients. Once a client is contacted, the server listens for instructions from the client and dispatched commands to other devices conditional on the client data. It also manages time locking with other machines. """ def __init__(self, options): """ Initialize class with options passed in from option parser, durations of events, start the labjack, etc. """ self.port_num = options.port_num self.debug = options.debug # initialize arduino #arduino = serial.Serial('/dev/tty.usbserial',9600) # # initialize labjack # try: # self.labjack = u6.U6() # except: # print "--------------------------------------------------------------" # print " Labjack could not be initialized!" # print "--------------------------------------------------------------" # self.labjack = None # initialize json logging self.json_logger = JsonLogThread() self.json_logger.set_filename(options.output_path) self.json_logger.recording = True self.json_logger.start() # initialize labjack logger self.labjack_logger = LabjackLogger(options.config_file, self.json_logger) self.labjack_logger.start() def spawn_server(self): """ Start a TCP server that listens for connections from clients, collects events, logs them, and potentially triggers labjack events. """ # Create a TCP/IP socket sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Bind the socket to the port server_address = ('localhost', self.port_num) if self.debug: print >>sys.stderr, 'starting up on %s port %s' % server_address sock.bind(server_address) # Listen for incoming connections sock.listen(1) # while True: # Wait for a connection if self.debug: print >>sys.stderr, 'waiting for a connection' try: self.connection, client_address = sock.accept() except KeyboardInterrupt: print "Session ended via keypress." # break try: if self.debug: print >>sys.stderr, 'connection from', client_address # Receive the data. # Run as long as data is streaming. while True: # Note: if Unity messages increase in length, # data recv size may need to increase data = self.connection.recv(1024) if self.debug: pass #print >>sys.stderr, 'received "%s"' % data if data: try: # check if there is a data hangover split_data = data.split("}")[:-1] if len(split_data) > 1: # if so run and log each split separately for d in data: current_data = d + '}' self.json_logger.log(current_data) self.run_event(current_data) else: current_data = split_data[0] + '}' self.json_logger.log(current_data) self.run_event(current_data) except Exception, e: print "Lost event", data, "due to error:" print e except KeyboardInterrupt: print "Run ended via ESC keypress." break else: