示例#1
0
    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))
示例#2
0
	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 create_symlinks(ARDUINO_DIR, subdir):
    ACTUAL_DIR = os.path.join(ARDUINO_DIR, subdir)  # Make it generic so we can symlink Arduino/libraries as well as Arduino/hardware

    # Create directory if it doesn't exist
    if not os.path.isdir(ACTUAL_DIR):
        logger.debug("Arduino '{}' directory does not exist - Creating".format(subdir))
        os.makedirs(ACTUAL_DIR)

    # Update all libraries (using git submodule)
    logger.notice("Making sure you have the latest version of each submodule/library...")
    call(["git", "submodule", "init"])
    call(["git", "submodule", "update"])
    logger.success("All submodules updated :)")

    # Create symbolic links
    src_paths, dst_paths = get_src_and_dst_paths(ARDUINO_DIR, subdir)
    for src, dst in zip(src_paths, dst_paths):
        if os.path.exists(dst):  # If dst library folder already exists, decide between:
            if not os.path.islink(dst):  # If the folder is not a symlink and already existed, leave it as is
                logger.warning("{} exists and is not a symbolic link - not overwriting".format(dst))
                continue
            else:  # If it was a symlink, just "refresh" (update) it
                logger.verbose("Unlinking {} first".format(dst))
                os.unlink(dst)
        # Create symbolic link
        logger.debug("Creating new symbolic link {}".format(dst))
        os.symlink(src, dst)

    logger.success("Done! :)")
    return True
 def closed(self, code, reason=None):
     self.deadline_new_file = datetime.now(
     )  # Even if a reconnect happens before the current deadline, force the creation of a new file, instead of the 'reconnected' data being appended to the current file
     if self.output_file_handle:
         self.output_file_handle.close()
         self.output_file_handle = None
         logger.verbose(
             "Data was saved at '{}' after closing the socket".format(
                 self.output_filename))
示例#5
0
def make_symlink(src, dst):
    if os.path.exists(dst):  # If dst file/folder already exists, decide between:
        if not os.path.islink(dst):  # If the file/folder is not a symlink and already existed, leave it as is
            logger.warning("{} exists and is not a symbolic link - not overwriting".format(dst))
            return
        else:  # If it was a symlink, just "refresh" (update) it
            logger.verbose("Unlinking {} first".format(dst))
            os.unlink(dst)

    # Create symbolic link
    logger.debug("Creating new symbolic link {}".format(dst))
    os.symlink(src, dst)
示例#6
0
 def closed(self, code, reason=None):
     if self.output_file_handle:
         self.output_file_handle.close()
         self.output_file_handle = None
         logger.verbose("Data was saved at '{}' after closing the socket".format(self.output_filename))