Form1.cs 2.78 KB
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using HttpUtils;
using System.Threading;

namespace BarcodeScannerClient
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        BarCodeScanner scanner = new BarCodeScanner();
        private void Form1_Load(object sender, EventArgs e)
        {
            scanner.BarcodeReceived += new BarCodeScanner.Barcode(scanner_BarcodeReceived);
            string[] ports = SerialPort.GetPortNames();
            foreach (string port in ports)
            {
                PortsComboBox.Items.Add(port);
            }
            PortsComboBox.SelectedItem = Properties.Settings.Default.Port;
           // apiKeyTextBox.Text = Properties.Settings.Default.ApiKey;
<<<<<<< HEAD
            apiUrlTextBox.Text = Properties.Settings.Default.ApiURL;
            if (apiUrlTextBox.Text == "")
            {
                apiUrlTextBox.Text = "http://my.event.ltd/shop/rfidListener.jsf?reader=demo_reader&tag=%";
            }
=======
>>>>>>> b730dd7f982f556ecbc4adf437995c3832096919
            
        }
        string ret = "";
        void scanner_BarcodeReceived(string data)
        {
            string url = Properties.Settings.Default.ApiURL.Replace("%", data);
            BarcodeRestClient client = new BarcodeRestClient(url);
            
            try
            {
                ret = client.MakeRequest();
                
            }
            catch (Exception e)
            {
                ret = e.Message;
                
            }
            setRet();

        }

        private void setRet()
        {
            if (this.InvokeRequired){
                this.Invoke(new ThreadStart(setRet));
                return;
            }
            status.Text = ret;
        }
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            scanner.Close();
            if (PortsComboBox.SelectedItem != null)
            {
                Properties.Settings.Default.Port = PortsComboBox.SelectedItem.ToString();
            }
            
            Properties.Settings.Default.Save();
        }

        private void PortsComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            scanner.Close();
            if (PortsComboBox.SelectedItem != null)
            {
                scanner.Port = PortsComboBox.SelectedItem.ToString();
            }
            scanner.Listen();
        }

        private void settingsToolStripButton_Click(object sender, EventArgs e)
        {
            ApiSettings frm = new ApiSettings();
            frm.ShowDialog();
        }
    }
}