package demo;

import junit.framework.TestCase;

import com.jlrfid.service.AntStruct;
import com.jlrfid.service.MainHandler;
import com.jlrfid.service.RFIDException;

public class Demo extends TestCase{

	public void testGetAnt() {
		MainHandler handler = new MainHandler();
		if(handler.dllInit("R2k.dll")){
			if(handler.deviceInit("",4, 115200)){
				AntStruct struct = handler.GetAnt();
				for(int i=0; i<4; i++){
					System.out.println("antenna" + (i+1) +(struct.antEnable[i]==1?"！！connected":"！！disconnected") + "！！work time:" + struct.dwellTime[i] + "ms！！power:" + struct.power[i].longValue()/10 +"dBm");
				}
			}
		}
	}
	
	public void testSetAnt() throws RFIDException {
		MainHandler handler = new MainHandler();
		if(handler.dllInit("R2k.dll")){
			if(handler.deviceInit("",4, 115200)){
				byte[] antEnable = new byte[]{1,0,0,0};
				long[] dwellTime = new long[]{2000,2000,4000,2000};
				long[] power = new long[]{300,300,250,200};
				if(handler.SetAnt(antEnable,dwellTime,power)){
					System.out.println("succeed to set antenna parameter");
					AntStruct struct = handler.GetAnt();
					for(int i=0; i<4; i++){
						System.out.println("antenna" + (i+1) +(struct.antEnable[i]==1?"！！connected":"！！disconnected") + "！！work time:" + struct.dwellTime[i] + "ms！！power:" + struct.power[i].longValue()/10 +"dBm");
					}
				}else{
					System.out.println("failed to set antenna parameter");
				}
				
			}
		}
	}
	
	public void testGetDevVersion() {
		MainHandler handler = new MainHandler();
		if(handler.dllInit("R2k.dll")){
			if(handler.deviceInit("",4, 115200)){
				System.out.println(handler.GetDevVersion());
			}
		}
	}
	
	public void testGetDI() {
		MainHandler handler = new MainHandler();
		if(handler.dllInit("R2k.dll")){
			if(handler.deviceInit("",4, 115200)){
				System.out.println(handler.GetDI());
			}
		}
	}
	
	public void testSetDO() throws RFIDException {
		MainHandler handler = new MainHandler();
		if(handler.dllInit("R2k.dll")){
			if(handler.deviceInit("",4, 115200)){
				System.out.println(handler.SetDO((byte) 1, (byte) 0));
			}
		}
	}

	public void testLockTag() throws RFIDException {
		MainHandler handler = new MainHandler();
		if(handler.dllInit("R2k.dll")){
			if(handler.deviceInit("",4, 115200)){
				System.out.println(handler.lockTag(2, 2, "00000000"));
			}
		}
	}
	
	public void testReadCard() throws RFIDException{
		MainHandler handler = new MainHandler();
		if(handler.dllInit("R2k.dll")){
			if(handler.deviceInit("",4, 115200)){
				String data = handler.ReadTagData(2, 0, 4, "\0\0\0\0");
				System.out.println(data);
			}
		}
	}
	
	
	public void testWriteCard() throws RFIDException{
		MainHandler handler = new MainHandler();
		if(handler.dllInit("R2k.dll")){
			if(handler.deviceInit("",4, 115200)){
				System.out.println(handler.WriteTagData((char)1, (char)2, (char)2, "A2E2E2E2", ""));
				System.out.println(handler.ReadTagData(1, 2, 6, ""));
			}
		}
	}
	
	public void testSetAlive() throws RFIDException{
		MainHandler handler = new MainHandler();
		if(handler.dllInit("R2k.dll")){
			if(handler.deviceInit("",4, 115200)){
				System.out.println(handler.SetAlive((byte)2));
			}
		}
	}
	
	public void testStopInv() throws RFIDException{
		MainHandler handler = new MainHandler();
		if(handler.dllInit("R2k.dll")){
			if(handler.deviceInit("",4, 115200)){
				System.out.println(handler.StopInv());
			}
		}
	}
}
