package TestApp01_call;

import TestApp01_call.respMsg.*;
import TestApp01_call.reqMsg.*;


import org.eclipse.swt.widgets.Text;
import com.global.api.entities.*;
import com.global.api.entities.enums.BaudRate;
import com.global.api.entities.enums.ConnectionModes;
import com.global.api.entities.enums.DataBits;
import com.global.api.entities.enums.DeviceType;
import com.global.api.entities.enums.Parity;
import com.global.api.entities.enums.StopBits;
import com.global.api.entities.exceptions.ApiException;
import com.global.api.services.*;
import com.global.api.terminals.*;
import com.global.api.network.abstractions.*;
import com.global.api.terminals.ingenico.*;

import com.global.api.services.DeviceService;
import com.global.api.terminals.abstractions.IDeviceInterface;

import TestApp01_call.*;
import java.util.Observable;

public class comPort extends Observable {
	
	String baudrateText = "9600";
	String comportText = " ";
	Boolean connection = false;
	IDeviceInterface _device;
	ConnectionConfig _config;
	reqMsg _reqReqMsg;
	respMsg _resRespMsg;
	
	
	public void setConnection(Boolean connection) {
		synchronized (this) {
			this.connection = connection;
		}
		setChanged();
		notifyObservers(this);
	}
	
	public synchronized Boolean getConnection() {
		return connection;
	}

	public comPort() {
		testapp01_call observer = new testapp01_call();
	}
	
	public String getComPortText() {
		return comportText;
	}
	public void setComportText(String comportText) {
		this.comportText = comportText;
	}

	public String getBaudrateText() {
		return baudrateText;
	}
	public void setBaudrateText(String baudrateText) {
		this.baudrateText = baudrateText;
	}

	public IDeviceInterface open () {
		
		_config = new ConnectionConfig();
		_config.setDeviceType(DeviceType.INGENICO_L3000);
		_config.setConnectionMode(ConnectionModes.SERIAL);
		_config.setPort(comportText);
//		_config.setBaudRate(BaudRate.valueOf(baudrateText));
		_config.setBaudRate(BaudRate.r9600);
		_config.setDataBits(DataBits.Eight);
		_config.setParity(Parity.Even);
		_config.setStopBits(StopBits.One);
		_config.setTimeout(65000);		
			try {
		_device = DeviceService.create(_config);
		if (_device != null) {
			setConnection(true);
		}else {
			setConnection(false);
				
		}
		} catch (ApiException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return _device;
	}
	public IDeviceInterface close () {
		if 	(_device != null) {
			 setConnection(false);
			_device.dispose(); 
			_device = null;
		}
		return _device;
	}
	void clear() {
		return;
	}
	void sendRqst() {
		try {
			new reqMsg(_device,_config);
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ApiException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return;
	}
}
