 1Content-type: application/x-Unknown; charset=UTF8package nmc.sidekick;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.lang.Exception;

import nmc.common.*;

//(klh)
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Date;
import com.ajile.jem.rawJEM;

public class SKPressureTemp extends StdEnvironmentalSensorDriver {
    
    private static PressureTempSensor ptSensor = PressureTempSensor.getInstance();
    
    SKPressureTemp () {

	_name			= "TheSKPressureTempDriverRev1.0";
	_timeout		= 15000;
	_maxTries		= 3;
	_maxAttnBytes		= 250;
	_maxSampleBytes	= 1000;
	_initMaxTries		= 3;
	_maxSkipBytes		= 512;
	_cfgBufSize		= 512;

    }
	
    public void initialize(){
	super.initialize();
	DebugMessage.println("Initializing PressureTemp Sensor");
	ptSensor.configureAjileForPressureTemperatureChip();
    }


    /** Read a data sample from device communications interface. */
    //public TimestampedData poll() throws Exception {
    public void poll(PollCmd m) throws Exception{
        int tries = 0;
        TimestampedData retVal;

        while (tries < _maxTries) {
            tries++;
            try {
                byte[] theSample = getPTSample();
                TimestampedData retval = new TimestampedData(theSample);
	        // Now we put the message into an outbound MessageQ, 
	        // instead of returning the TimestampedData.
	        // We may need some Message packetizing here...
                
                PolledDataReply pdr = new PolledDataReply(m,retval);
                _outbound.put(pdr);

                return;

            } catch (TimeoutException e) {
                DebugMessage.println("Driver: '"
                                   + _name
                                   + "' got timeout");
            } // other Exceptions fly past into the caller
        }
        
        throw new Exception("Retry limit exceeded (retries=" + tries + ")");
    }//end poll()

    private byte[] getPTSample() throws Exception {
       String result="PTSensor Error";
       synchronized(ptSensor){
          if (! ptSensor.takeReading()) {
            DebugMessage.println("pressure temperature reading failed!!!");
          }else{
            ptSensor.calculatePresTemp();
            result=("Temp="+ptSensor.iTemp+" Pressure="+ptSensor.iPressure);
          }
       }
 
      // copy the sample into the exactly-right-sized byte array
      byte[] retval = result.getBytes();	
	DebugMessage.println(_name+" Returning..."+retval);
      
      return retval;
    }//end getSKHumiditySample()

    //public TimestampedData configure(byte[] cfgData) throws Exception {
    public void configure(ConfigurationCmd m) throws Exception{
        DebugMessage.println("We are in the driver \""
                           + _name
                           + "\" with configuration data="
                           + new String(m.data));
	  // The SKPressureTemp has no configuration and does not return
	  // and status/meta data...
        byte[] result = "SKPressureTemp has no configurable options".getBytes();
        TimestampedData retval = new TimestampedData( result );

         // Now we put the message into an outbound MessageQ, 
         // instead of returning the TimestampedData.
         // We may need some Message packetizing here...   
         ConfigurationReply cr=new ConfigurationReply(m,retval);
        _outbound.put(cr);
      
         return;
    }

}