/*
 *****************************************************************************
 *                                                                           *
 *              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.google.protobuf.*;
import com.impinj.stp.*;
import com.impinj.stp.StpApi.*;

public class TidTableConsole {
    final static String DEVICE_NAME = "speedwayr-XX-XX-XX";    // Please edit
    String tids[]  = { "e2801130200028616732000b", "e2801130200024bf306a013a" };  // Please edit
    String epcs[]  = { "E2801130200024BF306A013A", "E2801130200024DF306A013A" };  // Please edit
    String epcMask =  "FFFFFFFFFFFFFFFFFFFFFFFF";

    Device device;
    boolean debug = true;
    public TidTableConsole(String devStr) {
        try {
            device = new Device(devStr);
            device.setDebug(debug);
            // Device Setup
            Command.Builder command = Command.newBuilder();
            // Make use of the TextFormat.merge feature to read configuration parameters from a file
            TextFormat.merge(Helpers.readConfigFile("all_encoder.proto.txt"), command);
            // SetDeviceConfig
            if (command.hasSetDeviceConfig())
                device.runSetDeviceConfigCommand(command.getSetDeviceConfig());
            else
                System.out.println("Error: Missing SetDeviceConfig");
            // Set Antenna
            if (command.hasSetAntennaConfig())
                device.runSetAntennaConfigCommand(command.getSetAntennaConfig());
            else
                System.out.println("Error: Missing SetAntennaConfig");
            // Serialization Job
            if (command.hasAddSerializationJob())
                device.runAddSerializationJobCommand(command.getAddSerializationJob());
            else
                System.out.println("Error: Missing Serialization Job");
            // Tid table
            TidTableSender tidSender = new TidTableSender();
            tidSender.addTidTableListener(devStr);
            TIDTableEntries entries = TIDTableEntries.newBuilder()
                    .addTidTableEntries(
                        TIDTableEntry.newBuilder()
                            .setTID(Helpers.stringToByteString(tids[0]))
                            .setEPCMemory(
                                EPCMemory.newBuilder()
                                     .setData(Helpers.stringToByteString(epcs[0]))
                                     .setMask(Helpers.stringToByteString(epcMask))
                            ).build()
                    )
                    .addTidTableEntries(
                         TIDTableEntry.newBuilder()
                            .setTID(Helpers.stringToByteString(tids[1]))
                            .setEPCMemory(
                                EPCMemory.newBuilder()
                                    .setData(Helpers.stringToByteString(epcs[1]))
                                    .setMask(Helpers.stringToByteString(epcMask))
                            ).build()
                    ).build();

             device.engage();
             tidSender.addTidTableEntries(entries);  // add tid entries after engage but before start
             device.start();

             Helpers.sleepMS(1000);    // run for a second

             device.stop();
             device.disengage();
        } catch(Exception ex) {
             System.out.println("TidTableConsole: "+ex.getMessage());
        } finally {
            device.stpClose();
        }
    }
    public static void main(String[] args) {
        if (args.length == 1)
            new TidTableConsole(args[0]);
        else
            new TidTableConsole(DEVICE_NAME);
    }

}
