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


import javax.comm.SerialPort;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.lang.Exception;
import nmc.common.*;

public abstract class StdEnvironmentalSensorDriver implements EnvironmentalSensorDriver {

    String _name;
    //static Adc _adc= new Adc();
    private static Adc adc = Adc.getInstance();
    int _adcChannel;
    int _timeout;
    int _maxTries;
    int _maxAttnBytes;
    int _maxSampleBytes;
    int _initMaxTries;
    int _maxSkipBytes;
    int _cfgBufSize;

    
    /** Unique sensor ID */
    long _sensorID = 9999; // does this ID apply to, say, MicroCat,
                           // or to MicroCat serial# 149236 ?
    /** Outbound Message Queue */
    public OutboundMessageQ _outbound;


    /** Inbound Message Queue */
    public DriverMessageQ _inbound;

   public void handleMessage(Message m){
      int msgType = m.getMessageType();
	switch (msgType) {
	    case Message.POLL_CMD: {
		DebugMessage.println("detected POLL_CMD");
		PollCmd p = (PollCmd)m;
		try{
            	   poll(p);
		}catch(Exception e){
		  DebugMessage.println(e);
		}	
		}
		break;

	   case Message.INITIALIZE_INSTRUMENT_CMD: {
		DebugMessage.println("detected INITIALIZE_INSTRUMENT_CMD");

		InitializeInstrumentCmd iic = (InitializeInstrumentCmd)m;
		DebugMessage.println("    for channel number "
			   + iic.channelNumber);
            	// This is a placeholder in case the SideArm really
            	// wants to control the initialization.
            	}
		break;

               case Message.CONFIGURATION_CMD:
               	DebugMessage.println("detected CONFIGURATION_CMD");
               	ConfigurationCmd cc = (ConfigurationCmd)m;
               	try{
               	    configure(cc);
               	}catch(Exception e){DebugMessage.println(e);}
                	break;
	    	
	    default:
		DebugMessage.println("unknown message received");
	 }// end switch

   }// end handleMessage()

public void setOutboundMessageQ(OutboundMessageQ q){
	_outbound = q;
	return;
}

public void initialize(){
	   DebugMessage.println("StdEnvSensorDrvr Initializing ADC...");
	   adc.initAdcCsN();
	    
	   DebugMessage.println("Starting Inbound Message Queue...");
	   // May want to do this outside of initialize
	   try{
   	   _inbound = new DriverMessageQ(this,5);
	   _inbound.startInboundMessaging();
	   }catch(IOException e){DebugMessage.println(e);}

	DebugMessage.println("Done with base class initialization");

}

public void enqueueMessage(Message m){
  // Implement policy for multiple messages here...
  _inbound.put(m);
  return;
}// end enqueueMessage()

public void poll(PollCmd m) throws Exception{
        int tries = 0;
        TimestampedData retVal;

        while (tries < _maxTries) {
            tries++;
            try {
                byte[] theSample = getSample();
                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 + ")");
}

// needs to be protected for other classes to override (klh)
private byte[] getSample() throws Exception {
     DebugMessage.println("StdEnvironmentalSensorDriver: doing nothing...");
     byte [] retval = "StdEnvironmentalSensorDriver: Default getSample()".getBytes();
     return retval;    
}


public void configure(ConfigurationCmd m) throws Exception{
        DebugMessage.println("We are in the driver \""
                           + _name
                           + "\" with configuration data="
                           + new String(m.data));

        TimestampedData retval = new TimestampedData( m.data );
         // 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;
}


    public Object getResourceType(IoResourceType aType) {
		if (aType == IoResourceType.DPA_BOARD)
	    	return null;
		else if (aType == IoResourceType.INSTRUMENT)
	    	return this;
		else
	   		return null; // probably should throw exception
    }
    
    public double parseDouble(String s) throws NumberFormatException{
	int sign=1,esign=1,intpart=0,fracpart=0,exp=0;
	int fracdigits=0,intdigits=0,expdigits=0;
    	char c,signchar='+',esignchar='+';
    	boolean gotSign=false,gotDecimal=false,gotE=false,gotESign=false;
    	boolean gotInt=false,gotFrac=false,gotExp=false;
    	double fresult;
	double result;
		
	for(int i=0;i<s.length();i++){
	  c=s.charAt(i);

	  if(!gotSign){
	    	if(c=='+'){
			sign=1;
			signchar='+';
	      		gotSign=true;
	      		//DebugMessage.println("1 gotSign");
	    	}else
	  	  if(c=='-'){
			sign= (-1);
			signchar='-';
			gotSign=true;
			//DebugMessage.println("1 gotSign");				
  		}else
		if(c=='.'){
			sign=1;
			signchar='+';
			gotSign=true;
			//DebugMessage.println("1 gotSign");
			
			intpart=0;
			gotInt=true;
			//DebugMessage.println("1 gotInt");
			
			gotDecimal=true;
			//DebugMessage.println("1 gotDec");
		}else
		if(isDigit(c)){
			sign=1;
			signchar='+';
			gotSign=true;
			//DebugMessage.println("1 gotSign");
			
			intpart*=10;
			intpart+=(c-0x30);
			intdigits++;					
		}else{
			throw new NumberFormatException();
		}
	}else
	if(!gotInt){
		if(c=='.'){
			gotDecimal=true;
			//DebugMessage.println("2 gotDecimal");
			gotInt=true;
			//DebugMessage.println("2 gotInt");					
		}else
		if(c=='e' || c=='E'){
			if(intdigits==0 && fracdigits==0)
				throw new NumberFormatException();
			gotDecimal=true;
			//DebugMessage.println("2 gotDecimal");
			gotE=true;
			//DebugMessage.println("2 gotE");
			gotInt=true;
			//DebugMessage.println("2 gotInt");
			fracpart=0;
			gotFrac=true;
			//DebugMessage.println(" gotFrac");
		}else
		if(isDigit(c)){
			if(!gotSign){
				sign=1;
				signchar='+';
				gotSign=true;
				//DebugMessage.println("2 gotSign");
			}			
			intpart*=10;
			intpart+=(c-0x30);
			intdigits++;
		}else{
			throw new NumberFormatException();
		}
	}else
	if(!gotFrac){
		if(c=='e' || c=='E'){
			if( fracdigits==0 && intdigits==0 )
				throw new NumberFormatException();
			gotFrac=true;
			//DebugMessage.println("3 gotFrac");
			gotE=true;
			//DebugMessage.println("3 gotE");
		}else
		if(isDigit(c)){
			fracpart*=10;
			fracpart+= (c-0x30);
			fracdigits++;
		}else{
			throw new NumberFormatException();
		}
	}else
	if(!gotESign){
	
		if(!gotE){
			throw new NumberFormatException();
		}else
		if(c=='+'){
			esign=1;
			esignchar='+';
			gotESign=true;
			//DebugMessage.println("4 gotESign");
		}else
		if(c=='-'){
			esign= (-1);
			esignchar='-';
			gotESign=true;	
			//DebugMessage.println("4 gotESign");			
		}else
		if(isDigit(c)){
			esign=1;
			esignchar='+';
			gotESign=true;
			//DebugMessage.println("4 gotESign");
			exp*=10;
			exp+= (c-0x30);
			expdigits++;
		}else{
			throw new NumberFormatException();
		}
	}else
	if(!gotExp){
		if(isDigit(c)){
			exp*=10;
			exp+= (c-0x30);
			expdigits++;
		}else{
			throw new NumberFormatException();			
		}
	}else{
		throw new NumberFormatException();
	}
}// end for

if(intdigits==0 && fracdigits==0)
	throw new NumberFormatException();
if(gotE && expdigits==0)
	throw new NumberFormatException();
	
fresult=fracpart;			
for(int j=0;j<fracdigits;j++)
	fresult/=10;
			
result=intpart+fresult;	
for(int k=0;k<exp;k++){
	if(esign==1)
		result*=10;
	if(esign==-1)
		result/=10;
}
DebugMessage.println("parseDouble: "+signchar+" "+intpart+" "+fracpart+" "+fracdigits+" "+esignchar+" "+exp);		
return result;
}// end parseDouble()

    public boolean isDouble(String s) throws NumberFormatException{
	int sign=1,esign=1,intpart=0,fracpart=0,exp=0;
	int fracdigits=0,intdigits=0,expdigits=0;
    	char c,signchar='+',esignchar='+';
    	boolean gotSign=false,gotDecimal=false,gotE=false,gotESign=false;
    	boolean gotInt=false,gotFrac=false,gotExp=false;
    	double fresult;
	double result;
		
	for(int i=0;i<s.length();i++){
	  c=s.charAt(i);

	  if(!gotSign){
	    	if(c=='+'){
			sign=1;
			signchar='+';
	      		gotSign=true;
	      		//DebugMessage.println("1 gotSign");
	    	}else
	  	  if(c=='-'){
			sign= (-1);
			signchar='-';
			gotSign=true;
			//DebugMessage.println("1 gotSign");				
  		}else
		if(c=='.'){
			sign=1;
			signchar='+';
			gotSign=true;
			//DebugMessage.println("1 gotSign");
			
			intpart=0;
			gotInt=true;
			//DebugMessage.println("1 gotInt");
			
			gotDecimal=true;
			//DebugMessage.println("1 gotDec");
		}else
		if(isDigit(c)){
			sign=1;
			signchar='+';
			gotSign=true;
			//DebugMessage.println("1 gotSign");
			
			intpart*=10;
			intpart+=(c-0x30);
			intdigits++;					
		}else{
			return false;
		}
	}else
	if(!gotInt){
		if(c=='.'){
			gotDecimal=true;
			//DebugMessage.println("2 gotDecimal");
			gotInt=true;
			//DebugMessage.println("2 gotInt");					
		}else
		if(c=='e' || c=='E'){
			if(intdigits==0 && fracdigits==0)
				return false;
			gotDecimal=true;
			//DebugMessage.println("2 gotDecimal");
			gotE=true;
			//DebugMessage.println("2 gotE");
			gotInt=true;
			//DebugMessage.println("2 gotInt");
			fracpart=0;
			gotFrac=true;
			//DebugMessage.println(" gotFrac");
		}else
		if(isDigit(c)){
			if(!gotSign){
				sign=1;
				signchar='+';
				gotSign=true;
				//DebugMessage.println("2 gotSign");
			}			
			intpart*=10;
			intpart+=(c-0x30);
			intdigits++;
		}else{
			return false;
		}
	}else
	if(!gotFrac){
		if(c=='e' || c=='E'){
			if( fracdigits==0 && intdigits==0 )
				return false;
			gotFrac=true;
			//DebugMessage.println("3 gotFrac");
			gotE=true;
			//DebugMessage.println("3 gotE");
		}else
		if(isDigit(c)){
			fracpart*=10;
			fracpart+= (c-0x30);
			fracdigits++;
		}else{
			return false;
		}
	}else
	if(!gotESign){
	
		if(!gotE){
			return false;
		}else
		if(c=='+'){
			esign=1;
			esignchar='+';
			gotESign=true;
			//DebugMessage.println("4 gotESign");
		}else
		if(c=='-'){
			esign= (-1);
			esignchar='-';
			gotESign=true;	
			//DebugMessage.println("4 gotESign");			
		}else
		if(isDigit(c)){
			esign=1;
			esignchar='+';
			gotESign=true;
			//DebugMessage.println("4 gotESign");
			exp*=10;
			exp+= (c-0x30);
			expdigits++;
		}else{
			return false;
		}
	}else
	if(!gotExp){
		if(isDigit(c)){
			exp*=10;
			exp+= (c-0x30);
			expdigits++;
		}else{
			return false;			
		}
	}else{
		return false;
	}
    }// end for

  if(intdigits==0 && fracdigits==0)
	return false;
  if(gotE && expdigits==0)
	return false;
	
  DebugMessage.println("isDouble: "+signchar+" "+intpart+" "+fracpart+" "+fracdigits+" "+esignchar+" "+exp);		
  return true;
} // end isDouble


    public boolean isDigit(char c){
    	if(c>=0x30 && c<=0x39)
    		return true;
    	return false;
    }

void delay(int ms){
	try{
	 DebugMessage.println("delaying "+ms+" ms");
	  Thread.sleep(ms);
	}catch(InterruptedException e){DebugMessage.println("Should never see this InterruptedException");}    
}
}