Skip to content
SoundModemCli.java 4.08 KiB
Newer Older
xtof's avatar
xtof committed
/**
 * 
 */
package org.josast.ModuleSoundModem;

xtof's avatar
xtof committed
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
xtof's avatar
xtof committed
import java.nio.file.Paths;
xtof's avatar
xtof committed
import java.util.logging.Logger;
xtof's avatar
xtof committed
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

/**
 * @author christophe
 *
 */
public class SoundModemCli {

xtof's avatar
xtof committed
	private Logger log = Logger.getLogger("AmsatLogger");
xtof's avatar
xtof committed
	private static String version = "0.2";
xtof's avatar
xtof committed
	private String configurationfile = "defaultConfigFile.ini";
xtof's avatar
xtof committed
	 * Utilisation d'un répertoire par défaut
	 */
	private String generateRepository = "SoundModemOuput";
xtof's avatar
xtof committed

xtof's avatar
xtof committed
	/**
xtof's avatar
xtof committed
	 *
xtof's avatar
xtof committed
	 */
	public SoundModemCli() {
xtof's avatar
xtof committed
	}

	public String getConfigurationfile() {
		return configurationfile;
	}

	public void setConfigurationfile(String configurationfile) {
		this.configurationfile = configurationfile;
	}

	public String getGenerateRepository() {
		return generateRepository;
	}

	public void setGenerateRepository(String generateRepository) {
		this.generateRepository = generateRepository;
	}

	private static Options configParameters() {

		final Option versionOption = Option.builder("V")

				.longOpt("Version") //

				.desc("Return application version")

				.hasArg(false)

				.required(false)

				.build();

		final Option configFileOption = Option.builder("C")

				.longOpt("Config") //

				.desc("Return application version")

				.hasArg(true)

				.argName("configFile")

				.required(false)

				.build();
xtof's avatar
xtof committed

xtof's avatar
xtof committed
		final Option generateFileOption = Option.builder("G")

				.longOpt("Generate") //

				.desc("generate binary file for each frame received")

				.hasArg(true)

				.argName("GenerateRepository")

				.required(false)

				.build();

		final Option helpFileOption = Option.builder("h")

				.longOpt("help")

				.desc("display help message")

				.build();

		final Options options = new Options();

		options.addOption(versionOption);

		options.addOption(configFileOption);

		options.addOption(generateFileOption);
xtof's avatar
xtof committed

xtof's avatar
xtof committed
		options.addOption(helpFileOption);

		return options;

	}
xtof's avatar
xtof committed

	public void execute() {

		// use soudmodem configuration file 
xtof's avatar
xtof committed
		SoundModemClient sm = new SoundModemClient(configurationfile);
xtof's avatar
xtof committed
		// tant que connecté à SoundModem => reçoit des trames 
		int cpt = 0;
		Path path = Paths.get(generateRepository);
	       //if directory exists?
	        if (!Files.exists(path)) {
	            try {
	                Files.createDirectories(path);
	            } catch (IOException e) {
	               //fail to create directory
	                e.printStackTrace();
	            }
	        }

xtof's avatar
xtof committed
		while (true) {

xtof's avatar
xtof committed
			byte[] temp =	sm.receivedKissData();

xtof's avatar
xtof committed

			try {
xtof's avatar
xtof committed
				Files.write(Paths.get(generateRepository + "\\Frame_" + cpt++ + ".bin"), temp);
xtof's avatar
xtof committed
			} catch (IOException e) {
xtof's avatar
xtof committed
				log.severe(e.toString());
xtof's avatar
xtof committed
			}
			System.out.println("compteur" + cpt++);
		}

	}

xtof's avatar
xtof committed
	/**
	 * @param args
xtof's avatar
xtof committed
	 * @throws ParseException
xtof's avatar
xtof committed
	 */
	public static void main(String[] args) throws ParseException {
xtof's avatar
xtof committed

xtof's avatar
xtof committed
		final Options options = configParameters();

		final CommandLineParser parser = new DefaultParser();

		final CommandLine line = parser.parse(options, args);

xtof's avatar
xtof committed
		SoundModemCli smc = new SoundModemCli();
		
		System.out.println(SoundModemCli.class.getSimpleName() + " " + version);
xtof's avatar
xtof committed

xtof's avatar
xtof committed
		// Si mode aide

		boolean helpMode = line.hasOption("help");

		if (helpMode) {

			final HelpFormatter formatter = new HelpFormatter();

			formatter.printHelp(args[0], options, true);

			System.exit(0);

		}

		boolean versionpMode = line.hasOption("Version");

		if (versionpMode) {

xtof's avatar
xtof committed
			System.out.println(SoundModemCli.class.getSimpleName() + " " + version);
xtof's avatar
xtof committed

			System.exit(0);

		}

		boolean configMode = line.hasOption("Config");

		if (configMode) {

xtof's avatar
xtof committed
			smc.setConfigurationfile(line.getOptionValue("Config", ""));
xtof's avatar
xtof committed

		}
xtof's avatar
xtof committed

xtof's avatar
xtof committed
		boolean generateMode = line.hasOption("Generate");
xtof's avatar
xtof committed

xtof's avatar
xtof committed
		if (generateMode) {

			smc.setGenerateRepository(line.getOptionValue("Generate", ""));

		}
xtof's avatar
xtof committed
		smc.execute();
	}

}