 1Content-type: application/x-Unknown; charset=UTF8package msgtestbed;
import nmc.common.*;
import nmc.platform.*;
import java.util.Date;

public class MsgTestBed {
	SystemHardwiredInfo sysinfo= SystemHardwiredInfo.Current; 
	OutboundMessageQ outq;
	InboundMessageQ inq;
	public static final int NORMAL= 0;
	public static final int SA= 1;
	public static final int SIDEARM= 2;
	
	public static int mode = 	NORMAL;
	
	// Setup for NORMAL mode
	boolean Handshake             = false;  // done only once
	boolean HRHSample            = false;
	boolean HRHConfig              = false;	
	int HRHCfgModulus              = 3; 
	boolean MicrocatSample      = false;	
	boolean MicrocatConfig        = false;
	int MicrocatCfgModulus        = 3;
	boolean EmeterSample        = false;	
	boolean EmeterConfig          = false;	
	boolean SKHumiditySample  = false;	
	boolean SKHumidityConfig    = false;
	boolean SKPTSample           = false;	
	boolean SKPTConfig             = false;
	boolean FooSample              = false;
	boolean SetTime                   = false;
	boolean SetTimeOnce           = true;
	boolean GetTime                   = true;
	int loopDelay                         = 10000;
	
	// DPA Channel Assignments	
	int chMicrocat=7;//7//6;
	int chAsimet=0;
	int chEmeter=6;//6//7;
	int chFoo=1;
	int chSKHumidity=12;
	int chSKPressureTemp=13;
	long i=0;
	long cfgModulus=3,j=0,k=0;
	int loopsToDo=(-1);// set to -1 to loop forever

	long msTime=System.currentTimeMillis();//System.currentTimeMillis();
	Date now = new Date(msTime);
	
	PollCmd pA = new PollCmd(chAsimet);
	PollCmd pE = new PollCmd(chEmeter);
	PollCmd pM = new PollCmd(chMicrocat);
	PollCmd pFoo = new PollCmd(chFoo);
	PollCmd pSKH = new PollCmd(chSKHumidity);
	PollCmd pSKPT = new PollCmd(chSKPressureTemp);
	
	TestPollCmd tA = new TestPollCmd(chAsimet,30000);
	TestPollCmd tE = new TestPollCmd(chEmeter,30000);
	TestPollCmd tM = new TestPollCmd(chMicrocat,45000);
	TestPollCmd tFoo = new TestPollCmd(chFoo,10000);
	TestPollCmd tSKH = new TestPollCmd(chSKHumidity,450000);
	TestPollCmd tSKPT = new TestPollCmd(chSKPressureTemp,20000);
	//TestPollCmd tCmds[]= { tM,  tE, tSKH, tSKPT };
	//TestPollCmd tCmds[]= { tSKH, tSKPT };
	TestPollCmd tCmds[]= { tSKH};

	AliveReply rA = new AliveReply();
	SetAbsoluteTimeCmd sA = new SetAbsoluteTimeCmd(now.getTime());
	GetAbsoluteTimeCmd gA = new GetAbsoluteTimeCmd();
	
	byte[] asimetCfg_0 = "MODADR=\"HRH01\"\r\nCAL_1E=\"1.23456\"\r\nSMPCMD=\"R\"\r\n".getBytes();
	byte[]asimetCfg_1 = "MODADR=\"HRH02\"\r\nCAL_1E=\"2.46810\"\r\nSMPCMD=\"B\"\r\n".getBytes();
	byte[] microcatCfg_0 = "SAMPLENUM=0\r\nINTERVAL=60\r\nFORMAT=2\r\nSTORETIME=N\r\nSMODE=\"SL\"\r\n".getBytes();
	byte[] microcatCfg_1 = "SAMPLENUM=0\r\nINTERVAL=30\r\nFORMAT=1\r\nSTORETIME=N\r\nSMODE=\"TS\"\r\n".getBytes();
	
	Message m;

private class MsgSender extends Thread{
	TestCommand testCmds[];
	int interval=30000; // default is to send at 30 s intervals
	protected int messagesSent=0;
	public MsgSender(TestCommand[] c, int i){
		System.out.println("MsgSender ctor invoked");
		testCmds=c;
		if(i>1000) // interval must be > 1 sec
			interval=i;
	}
	
	public void run(){
		System.out.println("Starting MsgSender");
		this.send();
	}
	
	public void send(){
	  if(Handshake)
	   doHandshake();
	  int loops=0;
	  boolean cfgSel=false;
	   while(loopsToDo-- < 0){
	 	 if(SetTime){
	 	   System.out.println("Setting time to "+msTime+" "+now.toString());
		    outq.put(sA);
		    if(SetTimeOnce==true)
		      SetTime=false;
		  }
	   	
	   	// Send poll commands
		for(int i=0; i < testCmds.length ; i++){
			System.out.println("sending " + testCmds[i]);
			outq.put(testCmds[i]);
		}
		
		// Send any configuration commands , per modulo and enable status
		cfgSel=sendConfig(loops,cfgSel);
		
		loops++;		 
		delay(interval);
	   }// end while
	}// end send()
	
}// end class

private class MsgReceiver extends Thread{
	public MsgReceiver(){
		System.out.println("MsgReceiver ctor invoked");
	}
	
	public void run(){
		System.out.println("Starting MsgReceiver");
		this.receive();
	}
	
	public void receive(){
	   while(true){
	   	receiveMessage();
	   }
	}
	public void receive(int t){
	   while(true){
	   	receiveMessage(t);
	   }
	}
}

public static void main(String[] args){
	System.out.println("MsgTestBed starting up");
	MsgTestBed app=new MsgTestBed();
	try{
	  app.outq= new OutboundMessageQ(app.sysinfo.sidekickIpAddr, app.sysinfo.sidekickRcvPort);
	  app.inq = new InboundMessageQ(app.sysinfo.sidearmRcvPort);
	  app.outq.startOutboundMessaging();
	  app.inq.startInboundMessaging();
	  
	  switch(mode){
	  	case NORMAL:
	  	app.testLoop();
	  	System.exit(0);
	  	break;
	  	case SA:
		app.altTestLoop();
		System.exit(0);	  	
	  	break;
	  	case SIDEARM:
	  	MsgReceiver r=app.new MsgReceiver();
	  	r.start();
	  	MsgSender s=app.new MsgSender(app.tCmds,60000);
	  	s.start();
	  	break;
	  	default:
	  	System.out.println("What should I do?");
	  	break;
	  }
	}catch (Exception e) {
		System.out.println("caught exception " + e);
	}
	System.out.println("Main done");
}// end main()

public void altTestLoop(){
	int i=0;
	System.out.println("There are "+tCmds.length+" commands to send");

	doHandshake();

	while(loopsToDo-- < 0){
 	  	// Print loop count, etc.
	  	Date t = new Date(System.currentTimeMillis());
              	System.out.println("Loop "+i+" @ "+t.toString());

	 	 if(SetTime){
	 	   System.out.println("Setting time to "+msTime+" "+now.toString());
		    m=sendReceiveMessage(sA,30000);
		    if(SetTimeOnce==true)
		      SetTime=false;
		  }
              	
		for(i=0;i<tCmds.length;i++){
			System.out.println("sending " + tCmds[i]);
			outq.put(tCmds[i]);
		}
		for(i=0;i<tCmds.length;i++){
			Message m=null;
			m=receiveMessage(tCmds[i].getTimeout());
		}
		delay(loopDelay);
	}	
}

public void testLoop(){
	
	doHandshake();

	while( loopsToDo-- < 0){
 	  // Print loop count, etc.
	  Date t = new Date(System.currentTimeMillis());
              System.out.println("Loop "+i+" @ "+t.toString());

	   
	  if(SetTime){
	    System.out.println("Setting time to "+msTime+" "+now.toString());
	    m=sendReceiveMessage(sA,30000);
	    if(SetTimeOnce==true)
	      SetTime=false;
	  }
	  if(GetTime){
	    System.out.println("Getting time  ");
	    m=sendReceiveMessage(gA,30000);
	    if(m!=null){
		AbsoluteTimeReply r = (AbsoluteTimeReply)m;
		long tm = r.getAbsoluteTime();
		System.out.println("NMC Time is currently "+new Date(tm)+" ("+tm+")");
	    } 
 	  }
	
	  if(HRHSample){
	    // Sample ASIMET HRH
	    m=sendReceiveMessage(pA,30000);
  	  }

	  if(HRHConfig){
	    // Configure ASIMET HRH
	    ConfigurationCmd pCA;
	    if( (i%cfgModulus) == 0 ){
		// Change around the config string each time...
		if(j%2==0)
		  pCA = new ConfigurationCmd(chAsimet,2,asimetCfg_1);
		else
		  pCA = new ConfigurationCmd(chAsimet,2,asimetCfg_0);

		j++;
		
		sendReceiveMessage(pCA,60000);
		pCA=null;
	    }//end if
	  }

	  if(EmeterSample){
	    // Sample emeter
	    sendReceiveMessage(pE,5000);
	  }

	  if(MicrocatSample){
	    // Sample microcat
	    sendReceiveMessage(pM,30000);
	  }

	  if(MicrocatConfig){
	    // Configure Microcat CTD
	    if( (i%cfgModulus) == 0 ){
		// Change around the config string each time...
		ConfigurationCmd pCM;
		if(k%2==0)
		  pCM = new ConfigurationCmd(chMicrocat,2,microcatCfg_1);
		else
		  pCM = new ConfigurationCmd(chMicrocat,2,microcatCfg_0);

		k++;
		sendReceiveMessage(pCM,120000);
		pCM=null;
	    }//end if
	  }

	  if(SKHumiditySample){
	    // Sample SKHumidity
	    sendReceiveMessage(pSKH,45000);
	  }

	  if(SKPTSample){
	    // Sample SKPressureTemp
	    sendReceiveMessage(pSKPT,5000);
	  }

	  if(FooSample){
	    // Sample foo
	    sendReceiveMessage(pFoo,15000);
	  }
	  // Null message (debugging memory leak)
	  m=null;
	 delay(loopDelay);
	
      	  i++;
	}// end while
}//end testLoop()

public boolean sendConfig(int loops,boolean csel){
	System.out.println("Sending configuration packets; loops= "+loops+" csel="+csel);
	if(loops>0){
	  if( HRHConfig && (loops%HRHCfgModulus == 0) ){
	    ConfigurationCmd h;
	    if(csel==true)
	      h= new ConfigurationCmd(chAsimet,2,asimetCfg_0);
	    else
 	      h = new ConfigurationCmd(chAsimet,2,asimetCfg_1);
		    
	    System.out.println("sending " + h);
	    outq.put(h);
	    h=null;
	  }// end HRH
	  
	  if( MicrocatConfig && (loops%MicrocatCfgModulus == 0) ){
	    ConfigurationCmd m;
	    if(csel==true)
	      m= new ConfigurationCmd(chMicrocat,2,microcatCfg_0);
	    else
 	      m= new ConfigurationCmd(chMicrocat,2,microcatCfg_1);
		    
	    System.out.println("sending " + m);
	    outq.put(m);
	    m=null;
	  }// end Microcat
	  	  
	}// end if loops>0
	
	return !csel;
}

public Message sendReceiveMessage(Message msg, int timeout){
	// Send message
	System.out.println("sending " + msg);
	outq.put(msg);
	
	Message m=receiveMessage(timeout);
	return m;
}//end sendReceiveMessage()

public Message receiveMessage(int timeout){
	Message m=null;
	{
		int t2 = 2;
		System.out.println("receiving...");
		while (t2<timeout) {
			m = inq.blockingGet(t2);
			if (m == null) {
				System.out.print(".");
				t2 *= 2;
			} else {
				System.out.println("done");
				break; // out of while loop
			}
		}
		if (m == null) {
			System.out.println("A timeout occurred");
		}else
			System.out.println("received " + m);
	}
	return m;
}//end receiveMessage()

public Message receiveMessage(){
	Message m=null;
	{
		System.out.println("receiving...");
		m = inq.blockingGet();
		System.out.println("received " + m);
	}
	return m;
}//end receiveMessage()

public void delay(int ms){
	  try{
	    if(ms>0)	
 	      Thread.sleep(ms);
	  }catch(InterruptedException e){System.out.println("achoo!");}// Take a break...

}

public void doHandshake(){
	if(Handshake){
	    // Get AliveMessage from Sidekick and send AliveReply
	    System.out.println("Waiting for AliveNotice");
	    Message m=inq.blockingGet();
	    System.out.println("Sending AliveReply");
	    outq.put(pSKH);
	      Handshake=false;
 	}
}
}// end class