/*
 *****************************************************************************
 *                                                                           *
 *              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.inline;

import com.impinj.stp.*;
import java.nio.ByteBuffer;

public class TidTableEntryForwarder implements Device.TagOperationReportListener {
    private int epc1to4, epc5to8;
    protected TidTableSender tidTableSender;
    protected int currCount;

    public TidTableEntryForwarder(TidTableSender tidTableSender, int epc1to4, int epc5to8) {
        this.epc1to4 = epc1to4;
        this.epc5to8 = epc5to8;
        this.tidTableSender = tidTableSender;
        currCount = 0;
    }
    public void HookUpToTagOperationReports(Device localizerDevice) {
        localizerDevice.addTagOperationReportListener(this);
    }
    public void reportEvent(StpApi.TagOperationReport tor) {
        StpApi.TIDTableEntries.Builder entriesBuilder =  StpApi.TIDTableEntries.newBuilder();
        StpApi.TIDTableEntry.Builder entryBuilder = entriesBuilder.addTidTableEntriesBuilder();

        // This is where any interesting lookup would live
        byte [] epcBytes = new byte[12];
        ByteBuffer epcBuffer = ByteBuffer.wrap(epcBytes);
        epcBuffer.putInt(epc1to4);
        epcBuffer.putInt(epc5to8);
        epcBuffer.putInt(currCount);
        entryBuilder.setTID(tor.getTID());
        entryBuilder.getEPCMemoryBuilder().setData(com.google.protobuf.ByteString.copyFrom(epcBytes));
        try {
            tidTableSender.addTidTableEntries(entriesBuilder.build());
        } catch (java.io.IOException ioe) {
            ioe.printStackTrace();
        }
        currCount++;
    }
}
