/*
 *****************************************************************************
 *                                                                           *
 *              IMPINJ CONFIDENTIAL AND PROPRIETARY                          *
 *                                                                           *
 * This source code is the sole property of Impinj, Inc. Reproduction or     *
 * utilization of this source code in whole or in part is forbidden without  *
 * the prior written consent of Impinj, Inc.                                 *
 *                                                                           *
 * (c) Copyright Impinj, Inc. 2014. All rights reserved.                     *
 *                                                                           *
 *****************************************************************************/
package com.impinj.stpexamples;

import com.impinj.stp.*;
import com.impinj.stp.Helpers;
import com.impinj.stp.StpApi.*;

public class InventoryConsole {
    final static String DEVICE_NAME = "speedwayr-XX-XX-XX";    // Please edit
    final int RUN_FOR_SEC = 1;  // run for 1 second
    final boolean debug = false;
    Device device;
    public InventoryConsole(String devStr) {
         try {
            System.out.println("Inventory console sample app");
            System.out.println("Run for "+RUN_FOR_SEC+" sec");
            // Open network connection
            device = new Device(devStr);
            device.setDebug(debug);
            // setup event listeners
            MyTagOperationReportListener myTagOperationReportListener=new MyTagOperationReportListener();
            device.addTagOperationReportListener(myTagOperationReportListener);
            device.addOperationSummaryReportListener(new MyOperationSummaryReportListener());

            device.handshake().getHandShake();   // Handshake will run a command (just a test)
            // Get and print versions
            for (Version v : device.getVersions())
                System.out.println("Version:"+v.getVersionID()+"  Major="+v.getMajor()+ "  Minor="+v.getMinor()+ "  Maintance="+v.getMaintenance()+"  Build="+v.getBuild());
            // Setup Device parameters
            device.runSetDeviceConfigCommand(SetDeviceConfigCommand.newBuilder()
                  .setDeviceConfig(
                      DeviceConfig.newBuilder()
                           .setDeviceMode(DeviceMode.DEVICE_MODE_INVENTORY)
                           .setOperationMode(OperationMode.OPERATION_MODE_CONTINUOUS)
                           .setReportConfig(
                               ReportConfig.newBuilder()
                                  .setDeviceEventConfig(
                                      DeviceEventConfig.newBuilder()
                                          .setEnableGPIEvents(false)
                                  )
                                 .setOperationSummaryReportConfig(
                                     OperationSummaryReportConfig.newBuilder()
                                        .setEnableOperationSummaryReports(true)
                                 )
                                .setTagOperationReportConfig(
                                    TagOperationReportConfig.newBuilder()
                                        .setTagOperationReportTrigger(
                                            TagOperationReportTrigger.TAG_OPERATION_REPORT_TRIGGER_IMMEDIATE)
                                            .setEnableTagOperationReports(true)
                                        )
                                )
                       ).build()
                    );
             device.runSetAntennaConfigCommand(SetAntennaConfigCommand.newBuilder()
                 .setAntennasConfig(
                     AntennasConfig.newBuilder()
                        .addAntennaConfig(
                            AntennaConfig.newBuilder()
                               .setAntennaID(1)
                               .setGen2Mode(Gen2Mode.GEN2_MODE_MAX_THROUGHPUT)
                               .setInventorySearchMode(InventorySearchMode.INVENTORY_SEARCH_MODE_DUAL_TARGET)
                               .setGen2Session(Gen2Session.GEN2_SESSION_1)
                               .setTransmitPower(2500)
                               .setReceiveSensitivity(-7000)
                               .setTagPopulationEstimate(3)
                        )
                    ).build()
                );
             device.engage();
             device.start();

             Helpers.sleepMS(RUN_FOR_SEC * 1000);
             device.removeTagOperationReportListener(myTagOperationReportListener);  // turn off listener
             device.stop();
             device.disengage();
         } catch(Exception ex) {
             System.out.println("Main: "+ex.getMessage());
         } finally {
             device.stpClose();
         }
     }
     public static void main(String[] args) {
           if (args.length == 1)
                new InventoryConsole(args[0]);
           else
                new InventoryConsole(DEVICE_NAME);
     }
     class MyTagOperationReportListener implements Device.TagOperationReportListener {
         public void reportEvent(TagOperationReport report) {
             System.out.print("TID="+Helpers.byteStringToString(report.getTID()));
             System.out.print(" Ant="+report.getAntennaID());
             System.out.print(" EPC="+Helpers.byteStringToString(report.getEPC()));
             System.out.print(" RSSI="+report.getRSSI());
             System.out.print(" "+Helpers.getDatetimeString(report.getTimestamp().getUsecondsSinceEpoch()));
             System.out.println();
         }
     }
     class MyOperationSummaryReportListener implements Device.OperationSummaryReportListener {
         public void reportEvent(OperationSummaryReport report) {
             System.out.println("Summary: Pass="+ report.getTagOperationPassCount() +
                     "  Fail="+report.getTagOperationFailCount() +
                     "  Total=" + report.getTagOperationTotalCount());
         }
     }
}
