<commit_before>package org.chargecar.serial.streaming;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;
import edu.cmu.ri.createlab.serial.SerialDeviceIOHelper;
import edu.cmu.ri.createlab.serial.SerialPortEnumerator;
import edu.cmu.ri.createlab.serial.SerialPortException;
import edu.cmu.ri.createlab.serial.config.BaudRate;
import edu.cmu.ri.createlab.serial.config.CharacterSize;
import edu.cmu.ri.createlab.serial.config.FlowControl;
import edu.cmu.ri.createlab.serial.config.Parity;
import edu.cmu.ri.createlab.serial.config.SerialIOConfiguration;
import edu.cmu.ri.createlab.serial.config.StopBits;
import edu.cmu.ri.createlab.util.commandline.BaseCommandLineApplication;
import org.apache.log4j.Logger;

/**
 * @author <NAME> (<EMAIL>)
 */
@SuppressWarnings({"UseOfSystemOutOrSystemErr"})
public final class StreamingSerialPortRecorder extends BaseCommandLineApplication
   {
   private static final Logger LOG = Logger.getLogger(StreamingSerialPortRecorder.class);

   public static void main(final String[] args)
      {
      final BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

      new StreamingSerialPortRecorder(in).run();
      }

   private StreamingSerialPortRecorder(final BufferedReader in)
      {
      super(in);

      registerAction("r", recordAction);
      registerAction(QUIT_COMMAND, quitAction);
      }

   @Override
   protected void menu()
      {
      println("COMMANDS -----------------------------------");
      println("");
      println("r         Record data from a serial port");
      println("");
      println("q         Quit");
      println("");
      println("--------------------------------------------");
      }

   private final Runnable recordAction =
         new Runnable()
         {
         public void run()
            {
            try
               {
               // prompt user for port to connect to
               final String serialPortName = getSerialPortName();

               if (serialPortName == null)
                  {
                  return;
                  }

               // prompt the user for the baud rate
               final String baudStr = readString("Baud: ");
               final BaudRate baudRate = BaudRate.findByName(baudStr);

               if (baudRate == null)
                  {
                  println("Invalid baud rate");
                  return;
                  }

               // prompt the user for the number of seconds to record
               final Integer numSecondsToRead = readInteger("Num seconds to record: ");
               if (numSecondsToRead == null || numSecondsToRead <= 0)
                  {
                  println("Invalid duration");
                  return;
                  }

               // prompt the user for the number of seconds to record
               final String outputFilename = readString("Output filename: ");
               if (outputFilename == null || outputFilename.length() <= 0)
                  {
                  println("Invalid output filename");
                  return;
                  }

               // prompt the user for the output filename
               final File outputFile = new File(outputFilename);
               if (outputFile.exists())
                  {
                  println("Output file already exists or does not denote a valid file");
                  return;
                  }

               // set up the serial port config
               final SerialIOConfiguration config = new SerialIOConfiguration(serialPortName,
                                                                              baudRate,
                                                                              CharacterSize.EIGHT,
                                                                              Parity.NONE,
                                                                              StopBits.ONE,
                                                                              FlowControl.NONE);

               // connect to the serial port
               final SerialIOManager serialIOManager = new DefaultSerialIOManager(StreamingSerialPortRecorder.class.getSimpleName(), config);
               if (serialIOManager.connect())
                  {
                  // create an output file stream<commit_msg>corrected comment<commit_after>package org.chargecar.serial.streaming;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;
import edu.cmu.ri.createlab.serial.SerialDeviceIOHelper;
import edu.cmu.ri.createlab.serial.SerialPortEnumerator;
import edu.cmu.ri.createlab.serial.SerialPortException;
import edu.cmu.ri.createlab.serial.config.BaudRate;
import edu.cmu.ri.createlab.serial.config.CharacterSize;
import edu.cmu.ri.createlab.serial.config.FlowControl;
import edu.cmu.ri.createlab.serial.config.Parity;
import edu.cmu.ri.createlab.serial.config.SerialIOConfiguration;
import edu.cmu.ri.createlab.serial.config.StopBits;
import edu.cmu.ri.createlab.util.commandline.BaseCommandLineApplication;
import org.apache.log4j.Logger;

/**
 * @author <NAME> (<EMAIL>)
 */
@SuppressWarnings({"UseOfSystemOutOrSystemErr"})
public final class StreamingSerialPortRecorder extends BaseCommandLineApplication
   {
   private static final Logger LOG = Logger.getLogger(StreamingSerialPortRecorder.class);

   public static void main(final String[] args)
      {
      final BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

      new StreamingSerialPortRecorder(in).run();
      }

   private StreamingSerialPortRecorder(final BufferedReader in)
      {
      super(in);

      registerAction("r", recordAction);
      registerAction(QUIT_COMMAND, quitAction);
      }

   @Override
   protected void menu()
      {
      println("COMMANDS -----------------------------------");
      println("");
      println("r         Record data from a serial port");
      println("");
      println("q         Quit");
      println("");
      println("--------------------------------------------");
      }

   private final Runnable recordAction =
         new Runnable()
         {
         public void run()
            {
            try
               {
               // prompt user for port to connect to
               final String serialPortName = getSerialPortName();

               if (serialPortName == null)
                  {
                  return;
                  }

               // prompt the user for the baud rate
               final String baudStr = readString("Baud: ");
               final BaudRate baudRate = BaudRate.findByName(baudStr);

               if (baudRate == null)
                  {
                  println("Invalid baud rate");
                  return;
                  }

               // prompt the user for the number of seconds to record
               final Integer numSecondsToRead = readInteger("Num seconds to record: ");
               if (numSecondsToRead == null || numSecondsToRead <= 0)
                  {
                  println("Invalid duration");
                  return;
                  }

               // prompt the user for the output filename
               final String outputFilename = readString("Output filename: ");
               if (outputFilename == null || outputFilename.length() <= 0)
                  {
                  println("Invalid output filename");
                  return;
                  }

               // make sure the output filename is valid and doesn't already exist
               final File outputFile = new File(outputFilename);
               if (outputFile.exists())
                  {
                  println("Output file already exists or does not denote a valid file");
                  return;
                  }

               // set up the serial port config
               final SerialIOConfiguration config = new SerialIOConfiguration(serialPortName,
                                                                              baudRate,
                                                                              CharacterSize.EIGHT,
                                                                              Parity.NONE,
                                                                              StopBits.ONE,
                                                                              FlowControl.NONE);

               // connect to the serial port
               final SerialIOManager serialIOManager = new DefaultSerialIOManager(StreamingSerialPortRecorder.class.getSimpleName(), config);
               if (serialIOManager.connect())
                  {
                  // create an output file stream
{'fraction_non_alphanumeric': 0.09644449276938132, 'fraction_numerical': 0.0006523866478199413, 'mean_word_length': 1.5290074237008524, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 30, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '145338', 'n_tokens_mistral': 2171, 'n_tokens_neox': 2099, 'n_words': 515}