/*
 *****************************************************************************
 *                                                                           *
 *              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 SingleEncodeConsole {
    final static String DEVICE_NAME = "speedwayr-XX-XX-XX";   // Please edit
    String epc =     "3074257BF7191E4000000000";              // Please edit
    String epcMask = "FFFFFFFFFFFFFFFFFFFFFFFF";

    final boolean debug = false;
    Device device;
    public SingleEncodeConsole(String devStr) {
         try {
            System.out.println("Single encode console sample app");
            System.out.println();
            // Open network connection
            device = new Device(devStr);
            device.setDebug(debug);
            // setup event listeners
            device.addTagOperationReportListener(new MyTagOperationReportListener());
            device.addOperationSummaryReportListener(new MyOperationSummaryReportListener());
            HandShake hs = device.handshake().getHandShake();
            System.out.println("API Name="+hs.getAPIName()+ "  API version="+hs.getAPIVersion());
            // Device Config
            device.runSetDeviceConfigCommand(
                        SetDeviceConfigCommand.newBuilder()
                            .setDeviceConfig(
                                DeviceConfig.newBuilder()
                                    .setDeviceMode(DeviceMode.DEVICE_MODE_ENCODE)
                                    .setOperationMode(OperationMode.OPERATION_MODE_SINGLE)
                                    .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)
                                                    .setEnableTagOperationRetryReports(false)
                                                    .setEnableEpcInSerialReport(false)
                                                    .setEnableReportingOverSerialPort(false)
                                                    .setSendSerialReportAfterGPODeassert(false)
                                            )
                                    )
                            ).build()
                    );
            // Setup Antenna parameters
            device.runSetAntennaConfigCommand(
                    SetAntennaConfigCommand.newBuilder()
                        .setAntennasConfig(
                            AntennasConfig.newBuilder()
                                .addAntennaConfig(
                                    AntennaConfig.newBuilder()
                                        .setAntennaID(1)
                                        .setGen2Mode(Gen2Mode.GEN2_MODE_MAX_THROUGHPUT)
                                        .setInventorySearchMode(InventorySearchMode.INVENTORY_SEARCH_MODE_SINGLE_TARGET_A_TO_B)
                                        .setGen2Session(Gen2Session.GEN2_SESSION_0)
                                        .setTagPopulationEstimate(5)
                                        .setTransmitPower(2000)
                                        .setReceiveSensitivity(-5500)
                                        .setTagPopulationEstimate(0)
                                )
                         ).build()
             );
             // Setup Serialization Job
             device.runAddSerializationJobCommand(
                   AddSerializationJobCommand.newBuilder()
                       .setSerializationJob(
                           SerializationJob.newBuilder()
                               .setJobId(1)
                               .setJobType(SerializationJobType.SERIALIZATION_JOB_TYPE_STATIC)
                               .setJobCompletionCriteria(SerializationJobCompletionCriteria.SERIALIZATION_JOB_COMPLETION_CRITERIA_NONE)
                               .setEPCMemory(
                                   EPCMemory.newBuilder()
                                      .setData(Helpers.stringToByteString(epc))
                                      .setMask(Helpers.stringToByteString(epcMask))
                                      .setSerialization(
                                          Serialization.newBuilder()
                                              .setSerializationBits(38)
                                              .setMonzaSelfSerialization(
                                                   MonzaSelfSerialization.newBuilder()
                                                       .setSerializationPrefix("101")
                                               )
                                      )
                               )
                      ).build()
             );
             device.engage();
             device.start();

             Helpers.sleepMS(1000);

             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 SingleEncodeConsole(args[0]);
          else
              new SingleEncodeConsole(DEVICE_NAME);
     }

     class MyTagOperationReportListener implements Device.TagOperationReportListener {
         public void reportEvent(TagOperationReport report) {
              System.out.print("EPC: "+Helpers.byteStringToString(report.getEPC()));
              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());
         }
     }
}
