package org.josast.ApplicationAmicalsat;

import java.util.logging.Logger;


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("AmicalSatLogger");

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

	/**
	 * @param smConfiguration fichier de configuration
	 * @param receivedFrame Frame Data model
	 */
	public TaskSoundmodemTCP(SoundModemConfiguration smConfiguration, ModelReceivedData receivedFrame) {
		this.smConfiguration = smConfiguration;
		smc = new SoundModemClient(smConfiguration);
		if (smc.isOpen() == false) {
			this.failed();
		}
		this.receivedFrame = receivedFrame;

	}

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

	/**
	 *  while the connection is open, the modem is listening. When a frame is received, 
	 *  this frame is added to the model
	 */
	@Override
	protected String call() {
		try {

			while (running == true) {
			    byte[] temp = smc.receivedRawData();
			    receivedFrame.addFrame(temp);
				this.updateValue(receivedFrame.toString());

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

		}
		return receivedFrame.toString();
	}

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