package moos.devices.mdTest;

import java.rmi.RemoteException;
import java.io.IOException;
import java.lang.Math;
import gnu.io.SerialPort;
import gnu.io.UnsupportedCommOperationException;
import org.mbari.isi.interfaces.Device;
import org.mbari.isi.interfaces.Instrument;
import org.mbari.isi.interfaces.SensorDataPacket;
import org.mbari.isi.interfaces.NoDataException;
import org.mbari.isi.interfaces.ScheduleSpecifier;
import org.mbari.isi.interfaces.ScheduleParseException;
import moos.deployed.DebugMessage;
import moos.deployed.PowerPort;
import moos.deployed.RangeException;
import moos.deployed.InstrumentService;
import moos.deployed.SerialPortParameters;
import org.mbari.isi.interfaces.RecordTypeManager;
import org.mbari.isi.interfaces.SSDSTestRecordTypeManagerImpl;

/**
   Fake instrument for testing purposes.
*/
public class mdTest
    extends InstrumentService implements Instrument {
    private static int sampleNo=1;
    public static final long RECORDTYPE_ALPHA=1;
    public static final long RECORDTYPE_BETA=2;

    public mdTest() 
	throws RemoteException {
    }

    /** Specify compass device startup delay (millisec) */
    protected int initInstrumentStartDelay() {
    	return 500;
    }
    
    /** Specify compass prompt string. */
    protected byte[] initPromptString() {
	return ">".getBytes();
    }

    /** Specify sample terminator. */
    protected byte[] initSampleTerminator() {
	return "\r\n".getBytes();
    }

    /** Specify maximum bytes in raw compass sample. */
    protected int initMaxSampleBytes() {
	return 32;
    }

    /** Specify current limit. */
    protected int initCurrentLimit() {
	return 500;
    }

    /** Return initial value of instrument power policy. */
    protected int initInstrumentPowerPolicy() {
	return Instrument.POWER_ALWAYS;
    }

    /** Return initial value of communication power policy. */
    protected int initCommunicationPowerPolicy() {
	return Instrument.POWER_ALWAYS;
    }

    /** Create appropriate RecordTypeManager for this service */
    protected RecordTypeManager initRecordTypeManager(InstrumentService parent){
	return new SSDSTestRecordTypeManagerImpl(parent);
    }

    /** Request a data sample from the compass. */
    protected void requestSample() throws IOException {
	DebugMessage.println("mdTest.requestSample()");
    }

    /** Get attention of the instrument. */
    protected void getAttention(int maxTries) 
	throws Exception {
	return;
    }

    /** Return metadata. */
    protected byte[] getInstrumentMetadata() {

	return ("mdTest system time is "+System.currentTimeMillis()).getBytes();
    }


    /** Get a dummy SensorDataPacket. */
    public synchronized SensorDataPacket acquireSample(boolean logSample) 
	throws NoDataException {

	_logger.debug("mdTest.acquireSample: entry, logSample="+logSample);
	_logger.debug("mdTest.acquireSample: getting packet");
	SensorDataPacket packet = new SensorDataPacket(getId(), 100);
	_logger.debug("mdTest.acquireSample: setting time");
	packet.setSystemTime(System.currentTimeMillis());

	
	_logger.debug("mdTest.acquireSample: filling out data packet");
	if(((sampleNo++)%3)!=0){
	    packet.setDataBuffer(("AA"+sampleNo+",AB"+Math.random()+",AC"+Math.random()).getBytes());
	    setRecordType(RECORDTYPE_ALPHA);
	}else{
	    packet.setDataBuffer(("BA"+sampleNo+",BB"+Math.random()).getBytes());
	    setRecordType(RECORDTYPE_BETA);
	}

	_logger.debug("mdTest.acquireSample: logging sample "+packet+"\n_recordType="+_recordType);
	if (logSample) {
	    sendPacket(packet);
	}
	_logger.debug("mdTest.acquireSample: exit");

	return packet;
    }

    /** No internal clock. */
    public void setClock(long t) {
	return;
    }

    /** Self-test not implemented. */
    public int test() {
	return Device.OK;
    }

    /** Return specifier for default sampling schedule. */
    protected ScheduleSpecifier createDefaultSampleSchedule() 
	throws ScheduleParseException {
	// Sample every 20 seconds by default
	return new ScheduleSpecifier(20000);
    }

    /** Return parameters to use on serial port. */
    public SerialPortParameters getSerialPortParameters() 
	throws UnsupportedCommOperationException {

	return new SerialPortParameters(9600, 
					SerialPort.DATABITS_8,
					SerialPort.PARITY_NONE,
					SerialPort.STOPBITS_1);
    }

    /*
    public static void main(String args[]){
	try{
	mdTest foo = new mdTest();
	System.out.println(foo.createMetadataPacket("because we care",MDATA_ALL,false).toString());
	}catch(RemoteException e){}
    }
    */
}
