// StarCashDrawerTest.java
// This file contains sample code illustrating how to use the CashDrawer class to
// control your cash drawer.  The basic control and status querying mechanisms
// are used here.  For more advanced usage of the CashDrawer class, see the 
// JavaPOS specification.

// usage instructions - Windows
//1. add current directory to the CLASSPATH = set CLASSPATH=%CLASSPATH%;.
//2. compile from command line - javac StarCashDrawerTest
//3. execute from command line - java StarCashDrawerTest


// NOTE: CHANGE THE CASH DRAWER NAME IN THE cashDrawer.open STATEMENT BELOW TO MATCH YOUR CONFIGURED DEVICE NAME

import jpos.CashDrawer;
import jpos.JposException;
import jpos.util.JposPropertiesConst;

public class CashDrawerTest
{
    public static void main(String[] args)
    {
      	/*
	      If you want to place the jpos.xml file elsewhere on your local file system then uncomment the
	      following line and specify the full path to jpos.xml.
	   
	      If you want to place the jpos.xml file on a webserver for access over the internet then uncomment
	      the second System.setProperty line below and specify the full URL to jpos.xml.
        */
      	System.setProperty(JposPropertiesConst.JPOS_POPULATOR_FILE_PROP_NAME, "c:\\jpos.xml");
      	//System.setProperty(JposPropertiesConst.JPOS_POPULATOR_FILE_URL_PROP_NAME, "http://some-where-remote.com/jpos.xml");
  
        // instantiate a new jpos.CashDrawer object
        CashDrawer cashDrawer = new CashDrawer();
        
        try
        {
            // open the cashDrawer object according to the entry names defined in jpos.xml
            cashDrawer.open("Star TSP100 Cutter (TSP143)_CashDrawer_1");
            
            // claim exclsive usage of the cashDrawer object
            cashDrawer.claim(1);
            
            // enable the device for input and output
            cashDrawer.setDeviceEnabled(true);
            
            // check if the drawer is currently opened
            // if this check returns true when the drawer is ACTUALLY CLOSED,
            //    set the drawerClosedOnSignalLow property of this device's entry
            //    in the jpos.xml file to true
            if (cashDrawer.getDrawerOpened() == true)
            {
                System.out.println("cashDrawer.getDrawerOpened() == true");
            }
            else
            {
                System.out.println("cashDrawer.getDrawerOpened() == false");
            }
            
            // open the cash drawer
            //   if your cash drawer is wired to the printer's control circuit
            //   number 2, set the cashDrawer1 proprty of this device's entry in
            //   jpos.xml file to true
            cashDrawer.openDrawer();
        }
        catch(JposException e)
        {
            // display any errors that come up
            e.printStackTrace();
        }
        finally
        {
            // close the cashDrawer object
            try
            {
                cashDrawer.close();
            }
            catch (Exception e) {}
        }
        
        System.out.println("StarCashDrawerTest finished.");
        System.exit(0);
    }
}
