Skip to content
SoundModemClient.java 4.69 KiB
Newer Older
xtof's avatar
xtof committed
package org.josast.ModuleSoundModem;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.text.SimpleDateFormat;
xtof's avatar
xtof committed
import java.util.Arrays;
import java.util.Date;
xtof's avatar
xtof committed
import java.util.logging.Level;
import java.util.logging.Logger;

import org.josast.AX25.AX25Frame;
import org.josast.AX25.KissData;
import org.josast.AX25.KissFrame;
xtof's avatar
xtof committed
public class SoundModemClient {
xtof's avatar
xtof committed
	private Logger log = Logger.getLogger("AmsatLogger");
xtof's avatar
xtof committed
	private SoundModemConfiguration smConfiguration = null;
	private Socket skt = null;
	private InputStream in = null;
	private boolean open = false;
	private int bufferSize = 1024;
	private Path filepath;

xtof's avatar
xtof committed
	/**
	 * Initialise connection to soundmodem application with default value
xtof's avatar
xtof committed
	 *
xtof's avatar
xtof committed
	 */
xtof's avatar
xtof committed
	public SoundModemClient() {
		smConfiguration = new SoundModemConfiguration("defaultConfigFile.ini");
xtof's avatar
xtof committed
		openSocket();
		initialiseFile();
xtof's avatar
xtof committed
	}
xtof's avatar
xtof committed
	/**
	 * Initialise connection to soundmodem application with configuration
xtof's avatar
xtof committed
	 *
xtof's avatar
xtof committed
	 */
xtof's avatar
xtof committed
	public SoundModemClient(final SoundModemConfiguration  smConfig) {

		smConfiguration = smConfig;
xtof's avatar
xtof committed
		openSocket();
		initialiseFile();

xtof's avatar
xtof committed
	}
xtof's avatar
xtof committed
	/**.
xtof's avatar
xtof committed
	 * Initialise connection to soundmodem application with configuration file
xtof's avatar
xtof committed
	 *
xtof's avatar
xtof committed
	 */
xtof's avatar
xtof committed
	public SoundModemClient(final String filename) {
xtof's avatar
xtof committed
		smConfiguration = new SoundModemConfiguration(filename);
		openSocket();
		initialiseFile();

xtof's avatar
xtof committed
	}
xtof's avatar
xtof committed
	private void openSocket() {
			skt = new Socket(smConfiguration.getSmIPadress(), smConfiguration.getSmPort());
			in = skt.getInputStream();
			open = true;
		} catch (IOException e) {
			log.log(Level.SEVERE, "create stream " + e.toString());
		}

	}

	private void initialiseFile() {

		if (smConfiguration.isSmGenerateBinarieFile()) {

			Path path = Paths.get(smConfiguration.getSmBinarieOutputDirectory());
			// if directory exists?
			if (!Files.exists(path)) {
				try {
					Files.createDirectories(path);
				} catch (IOException e) {
					log.log(Level.SEVERE, "cannot create outpout directorie " + e.toString());
				}
			}

			String creationdate = "";
			if (smConfiguration.isSmBinarieFilePrefixDate()) {
				SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd-HHmmss");
				Date aujourdhui = new Date();
				creationdate = formater.format(aujourdhui) + "-";
xtof's avatar
xtof committed
			}
			filepath = Paths.get(smConfiguration.getSmBinarieOutputDirectory() + "\\" + creationdate
					+ smConfiguration.getSmBinarieOutputFile());
			byte[] temp = new byte[] {};
			try {
				Files.write(filepath, temp);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
xtof's avatar
xtof committed
	}
xtof's avatar
xtof committed
	/**.
	 * wait bytes from sound modem
xtof's avatar
xtof committed
	 *
	 * @return bytes received in KISS format
xtof's avatar
xtof committed
	 */
	public byte[] receivedKissData() {
xtof's avatar
xtof committed
		byte[] cbuf = new byte[bufferSize];
		int nbbyteread = 0;
			try {
				nbbyteread = in.read(cbuf);
			} catch (IOException e) {
				log.log(Level.SEVERE, "Error Read data " + e.toString());
				try {
					in.close();
				} catch (IOException e1) {
					log.log(Level.SEVERE, "Closing " + e1.toString());
				}
xtof's avatar
xtof committed
			}
			byte[] temp = Arrays.copyOf(cbuf, nbbyteread);

			if (smConfiguration.isSmGenerateBinarieFile()) {

				try {
					Files.write(filepath, temp, StandardOpenOption.APPEND);
				} catch (IOException e) {
					log.severe(e.toString());
				}

			return Arrays.copyOf(cbuf, nbbyteread);
		} else {
			log.severe("Connection not open, return null");
			return null;
xtof's avatar
xtof committed
	}
xtof's avatar
xtof committed

	@Deprecated
xtof's avatar
xtof committed
	public int receiveData(final byte[] cbuf) {
xtof's avatar
xtof committed
		try {
			return in.read(cbuf);
		} catch (IOException e) {

			log.log(Level.SEVERE, "Error Read data " + e.toString());
xtof's avatar
xtof committed

xtof's avatar
xtof committed
			try {
				in.close();
			} catch (IOException e1) {
				log.log(Level.SEVERE, "Closing " + e1.toString());
			}
		}
		return 0;
xtof's avatar
xtof committed
	}

	@Deprecated
xtof's avatar
xtof committed
	public AX25Frame decode(final byte[] frame, final int nb) {
		KissFrame kf = new KissFrame();
xtof's avatar
xtof committed
		KissData kissData = kf.extactMessage(frame, nb);

//		if (type==kf.AX25)
//		{
		AX25Frame ax = new AX25Frame();
		ax.decodeAX25Frame(kissData.getData(), kissData.getSize());
xtof's avatar
xtof committed
//		}
		System.out.println(kissData.toString());
		System.out.println(ax.toString());
		return ax;

xtof's avatar
xtof committed
	}

	public void close() {
xtof's avatar
xtof committed
		try {
			in.close();
		} catch (IOException e1) {
			log.log(Level.SEVERE, "Closing " + e1.toString());
		}
	}
xtof's avatar
xtof committed

	public boolean isOpen() {
		return open;
	}
	public byte[] receivedRawData() {
		byte[] temp = receivedKissData();
xtof's avatar
xtof committed
		if (temp != null) {

		kiss k = new kiss(temp);
		try {
			temp = k.toRaw();
		} catch (KissException e) {
			log.log(Level.SEVERE, "Extract Raw data " + e.toString());
		}
		return temp;
xtof's avatar
xtof committed
		else {
			log.severe("Buffer read null, return null");
			return null;
		}
xtof's avatar
xtof committed
}