def received_message(self, msg): # Parse the message s = str(msg.data) if s.startswith('['): s = s[1:-1] # Remove brackets if necessary else: return # Ignore Geophone ID message (eg: Geophone_AABBBCC) # Add the timestamp column timestamp = datetime.now().strftime('%H-%M-%S:%f') vals = s.split(",") s = "" for val in vals: s += timestamp + "," + val + "\n" # Check if we need to start a new file if datetime.now() > self.deadline_new_file: # Close existing file if necessary if self.output_file_handle: self.output_file_handle.close() logger.verbose("Closed file: '{}' (it's been {}s)".format(self.output_filename, self.DELTA_NEW_FILE.total_seconds())) # And create a new one self.generate_new_filename() self.output_file_handle = open(self.output_filename, 'w') # Write the parsed message to the file try: # In case the file has been closed (user stopped data collection), surround by try-except self.output_file_handle.write(s + '\n') except Exception as e: logger.error("Couldn't write to '{}'. Error: {}".format(self.output_filename, e)) logger.debug("Received data from '{}'!".format(self.url))
def run(self): # Run forever until event_stop tells us to stop while not self.data_collection.event_stop.is_set(): # Initialize the websocket WebSocketBaseClient.__init__(self, self.url, *self.init_args, **self.init_kwargs) self.sock.settimeout( self.TIMEOUT ) # Set the socket timeout so if a host is unreachable it doesn't take 60s (default) to figure out logger.notice("Connecting to '{}'...".format(self.url)) try: self.connect() # Attempt to connect to the Arduino except Exception as e: logger.error( "Unable to connect to '{}' (probably timed out). Reason: {}" .format(self.url, e)) else: # If we were able to connect, then run the websocket (received_message will get called appropriately) while self.once(): pass # self.once() will return False on error/close -> Only stop when the connection is lost or self.close() is called self.terminate() time.sleep( 2 ) # Wait for a couple of seconds for Arduino to reboot/connect or just to avoid network overload logger.success("Thread in charge of '{}' exited :)".format(self.url))
def close(self, code=1000, reason=''): try: super(DataReceiver, self).close(code, reason) except socket.error as e: logger.error( "Error closing the socket '{}' (probably the host was unreachable). Reason: {}" .format(self.url, e))
def received_message(self, msg): if msg.is_text: return # Ignore Geophone ID message (eg: Geophone_AABBBCC) # Parse the message: '<' for Little-Endian, 'H' for uint16_t msg_format = '<' + 'H'*(len(msg.data)/2) msg_vals = unpack(msg_format, msg.data) cvs_vals = ','.join(map(str, msg_vals)) # Convert each item to str then join with ',' # Check if we need to start a new file if datetime.now() > self.deadline_new_file: # Close existing file if necessary if self.output_file_handle: self.output_file_handle.close() logger.verbose("Closed file: '{}' (it's been {}s)".format(self.output_filename, self.DELTA_NEW_FILE.total_seconds())) # And create a new one self.generate_new_filename() self.output_file_handle = open(self.output_filename, 'w') # Write the parsed message to the file try: # In case the file has been closed (user stopped data collection), surround by try-except self.output_file_handle.write(cvs_vals + ',') except Exception as e: logger.error("Couldn't write to '{}'. Error: {}".format(self.output_filename, e)) logger.debug("Received data from '{}'!".format(self.url))
def find_avail_cams(): video_capture = cv2.VideoCapture() for i in range(1501): if video_capture.open(i): logger.info("\tCAMERA {} OPENED!".format(i)) for j in range(3): # Read a couple frames, sometimes cameras return a full-green frame on the first read() ret, frame = video_capture.read() video_capture.release() # Close the camera if ret: cv2.imwrite("cam_{}.jpg".format(i), frame) else: logger.error("Ooops, something went wrong accessing the frame! :S") else: logger.debug("Nothing on {}...".format(i))
def unhandled_error(self, error): logger.error("Unhandled websocket error: {}".format(error))