package org.josast.EntrysatDecoder;

import java.util.logging.Logger;

import org.josast.AX25.KissData;
import org.josast.AX25.KissFrame;
import org.josast.ModuleSoundModem.SoundModemClient;
import org.josast.ModuleSoundModem.SoundModemConfiguration;

import javafx.concurrent.Task;

public class TaskSoundmodemTCP extends Task<String> {
	
	private Logger log = Logger.getLogger("EntrySatLogger");

	private volatile boolean running = true;
	
	SoundModemConfiguration smConfiguration = null;
	SoundModemClient smc = null;
	ModelFrame receivedFrame = null;

	public TaskSoundmodemTCP(SoundModemConfiguration smConfiguration, ModelFrame receivedFrame) {
		this.smConfiguration = smConfiguration;
		smc = new SoundModemClient(smConfiguration);
		if (smc.isOpen() == false) {
			this.failed();
		}
		this.receivedFrame = receivedFrame;

	}

	public TaskSoundmodemTCP(ModelFrame receivedFrame) {
		// update from soundmodem
		smConfiguration = new SoundModemConfiguration("default");
		smc = new SoundModemClient(smConfiguration);
		if (smc.isOpen() == false) {
			this.failed();
		}
		this.receivedFrame = receivedFrame;
	}

	@Override
	protected String call() {
		try {
			KissFrame kf=new KissFrame();
			byte cbuf[] = new byte[1060];
			while (running == true) {
				int nb = smc.receiveData(cbuf);
				KissData kissData = kf.extactMessage(cbuf, nb);
				receivedFrame.addFrame(kissData.getSize(), kissData.getData());
				this.updateValue(receivedFrame.toString());

			}
		} catch (Exception e) {
			log.severe("Erreur SoundModem not available");
			log.severe(e.toString());
			e.printStackTrace();
			this.failed();

		}
		return receivedFrame.toString();
	}

	
	
	public void arreter() {
	 
		this.running = false;
	 
		}
	
	public void redemarrer()
	{
		 
		this.running = true;
	 
		}
	
}
