/*
 *****************************************************************************
 *                                                                           *
 *              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. 2015. All rights reserved.                     *
 *                                                                           *
 *****************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Impinj
{
    class Program
    {
        // This example demonstrates how the serial output from an ItemEncode
        // device can be consumed. The following is required to use this code:
        //  - A serial connection to the GPIO connector serial port (NOT the
        //    CONSOLE port) on an ItemEncode device
        //  - An ItemEncode application that connects to the ItemEncode
        //    device and configures it to output serial report outputs,
        //    including EPC data.
        static void Main(string[] args)
        {
            // Initialize console window
            InitializeConsoleWindow();

            try
            {
                // Create an ItemEncodeSerialPort object
                ItemEncodeSerialPort serialPort = new ItemEncodeSerialPort();
                // Open a connection
                serialPort.Open("COM3");

                // Bind a handler to the TagOperatinReportReceived event
                serialPort.TagOperationReportReceived += TagOperationReportHandler;

                // Serial data continues to be processed until the user
                // presses a key.
                Console.WriteLine("Press <enter> to exit");
                Console.ReadKey(true);

                // Close the serial port
                serialPort.Close();
            }
            // Exception handler
            catch (Exception ex)
            {
                string ex_msg = string.Format("{0}{1}",
                    ex.Message,
                    (null == ex.InnerException) ? string.Empty : Environment.NewLine + ex.InnerException.Message
                    );
                Console.WriteLine("Exception: {0}", ex_msg);

                // Pause before exiting the application to help when
                // debugging within Visual Studio
                Console.WriteLine("\r\nPress any key to exit . . .");
                Console.ReadKey(true);
            }

        }

        // Handler for TagOperationReportReceived events
        static void TagOperationReportHandler(object sender, TagOperationReportReceivedEventArgs e)
        {
            Console.WriteLine("{0}", e.ToString());
        }

        // Utility procedure for changing console window configuration,
        // including the window title
        private static void InitializeConsoleWindow()
        {
            Console.Clear();
            Console.Title = "Impinj ItemEncode - ASCII Serial Data Tag Operation Reports";
            Console.SetWindowSize(150, 50);
            Console.BufferHeight = 1000;
            Console.TreatControlCAsInput = true;
        }

    }
}
