示例#1
0
        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            // check timing (keystrokes within 100 ms)
            TimeSpan elapsed = (DateTime.Now - _lastKeystroke);

            if (!manualMode)
            {
                if (elapsed.TotalMilliseconds > 150)
                {
                    _barcode.Clear();
                }
            }

            if (manualMode && e.KeyChar == Convert.ToChar(Keys.Back))
            {
                if (_barcode.Count > 0)
                {
                    _barcode.RemoveAt(_barcode.Count - 1);
                    string msg = new String(_barcode.ToArray());
                    lblId.Text = msg;
                }
                return;
            }

            if (e.KeyChar != 13)
            {
                if (_barcode.Count < 6)
                {
                    _barcode.Add(e.KeyChar);
                }
                _lastKeystroke = DateTime.Now;

                if (manualMode)
                {
                    string msg = new String(_barcode.ToArray());
                    lblId.Text = msg;
                }
                if (_barcode.Count == 1)
                {
                    lblName.Text         = txtRole.Text = lblTimeIn.Text = lblTimeOut.Text = lblCheckinStatus.Text = lblMotorNum.Text = "";
                    picBoxEmployee.Image = null;
                }

                clearImageWindow();
            }
            else if (e.KeyChar == 13 && _barcode.Count > 0) //MAIN EVENT
            {
                string empId = new String(_barcode.ToArray());
                //MessageBox.Show(msg);
                lblId.Text = empId;
                _barcode.Clear();

                // Check if the ID is valid: contains number
                int tmp;
                if (!Int32.TryParse(empId, out tmp))
                {
                    Console.WriteLine("Invalid ID: " + empId);
                    lblId.Text = "";
                    return;
                }

                // Employee exist or not
                EmployeeData empData = getEmployeeDataIfExist(empId);
                if (!empData.exist)
                {
                    lblId.Text           = lblName.Text = txtRole.Text = lblTimeIn.Text = lblTimeOut.Text = lblCheckinStatus.Text = lblMotorNum.Text = "";
                    picBoxEmployee.Image = null;
                    Console.WriteLine("Employee does not exist: " + empId);
                    return;
                }

                // Check recent activities
                if (scaningState.ContainsKey(empId))
                {
                    DateTime last     = scaningState[empId];
                    DateTime now      = DateTime.Now;
                    double   timeDiff = (now - last).TotalSeconds;
                    Console.WriteLine("TimeDiff for " + empId + ": " + timeDiff.ToString());

                    if (timeDiff < minTimeBetweenScanSteps)
                    {
                        int remain = (int)(minTimeBetweenScanSteps - timeDiff);
                        Console.WriteLine("Duplicated activities");
                        lblId.Text   = "Đã thực hiện!";
                        lblName.Text = "Thử lại sau: " + remain.ToString() + " giây!";
                        return;
                    }
                    else
                    {
                        Console.WriteLine("Remove key from activities");
                        scaningState.Remove(empId);
                    }
                }

                // Save activities timestamp
                scaningState[empId] = DateTime.Now;


                // Actual event to update GUI and DB
                string absFrontImageDir = captureCamera(1);
                string absRearImageDir  = captureCamera(2);
                try
                {
                    string date = DateTime.Now.ToString("yyyy-MM-dd");
                    string frontImageCheckIn = "";
                    string rearImageCheckIn  = "";
                    string timeCheckin       = "tuan";
                    bool   rowExist          = isParkingRowExist(empId, date, out frontImageCheckIn, out rearImageCheckIn, out timeCheckin);
                    Console.WriteLine(rowExist);
                    Console.WriteLine(frontImageCheckIn + "_" + rearImageCheckIn);
                    updateTimeInOut(empId, absFrontImageDir, absRearImageDir, rowExist);
                    displayNameAndImage(empData);

                    if (!rowExist) //checkin
                    {
                        displayTimeIn();
                        //displayImageWindow(absFrontImageDir, absRearImageDir);
                        //It has already done at captureCamera 1/2
                    }
                    else //checkout
                    {
                        displayImageWindowStoredInDb(frontImageCheckIn, rearImageCheckIn);
                        displayTimeOut(timeCheckin);
                    }

                    // Timer to clean up screen
                    if (!backgroundTimer.Enabled)
                    {
                        backgroundTimer.Stop();
                    }
                    backgroundTimer.Interval = screenTimeoutValue * 1000;

                    backgroundTimer.Start();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Connection ERROR: " + ex.Message, "Lost database connection!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            // check timing (keystrokes within 100 ms)
            TimeSpan elapsed = (DateTime.Now - _lastKeystroke);

            if (!manualMode)
            {
                if (elapsed.TotalMilliseconds > 150)
                {
                    _barcode.Clear();
                }
            }

            if (manualMode && e.KeyChar == Convert.ToChar(Keys.Back))
            {
                if (_barcode.Count > 0)
                {
                    _barcode.RemoveAt(_barcode.Count - 1);
                    string msg = new String(_barcode.ToArray());
                    lblId.Text = msg;
                }
                return;
            }

            if (e.KeyChar != 13)
            {
                if (_barcode.Count < 6)
                {
                    _barcode.Add(e.KeyChar);
                }
                _lastKeystroke = DateTime.Now;

                if (manualMode)
                {
                    string msg = new String(_barcode.ToArray());
                    lblId.Text = msg;
                }

                if (_barcode.Count == 1)
                {
                    lblName.Text         = txtRole.Text = lblTime.Text = lblCheckinStatus.Text = lblCheckInOutStatus.Text = "";
                    picBoxEmployee.Image = null;
                    txtWarning.Visible   = false;
                }
            }
            else if (e.KeyChar == 13 && _barcode.Count > 0) //MAIN EVENT
            {
                string empId = new String(_barcode.ToArray());
                //MessageBox.Show(msg);
                lblId.Text = empId;
                _barcode.Clear();

                // Check if the ID is valid: contains number
                int tmp;
                if (!Int32.TryParse(empId, out tmp))
                {
                    Console.WriteLine("Invalid ID: " + empId);
                    string errorLine = DateTime.Now.ToString("HH:mm:ss") + ": Mã nhân viên chưa đúng: " + empId;
                    txtConsole.Text         += "\n" + errorLine;
                    lblCheckInOutStatus.Text = errorLine;
                    lblId.Text = "";
                    return;
                }

                // Employee exist or not
                EmployeeData empData = getEmployeeDataIfExist(empId);
                if (!empData.exist)
                {
                    lblId.Text           = lblName.Text = txtRole.Text = lblTime.Text = lblCheckinStatus.Text = "";
                    picBoxEmployee.Image = null;
                    txtWarning.Visible   = false;
                    string errorLine = DateTime.Now.ToString("HH:mm:ss") + ": Nhân viên không tồn tại: " + empId;
                    txtConsole.Text         += "\n" + errorLine;
                    lblCheckInOutStatus.Text = errorLine;
                    Console.WriteLine("Employee does not exist: " + empId);
                    return;
                }

                // Check recent activities
                if (scaningState.ContainsKey(empId))
                {
                    DateTime last     = scaningState[empId];
                    DateTime now      = DateTime.Now;
                    double   timeDiff = (now - last).TotalSeconds;
                    Console.WriteLine("TimeDiff for " + empId + ": " + timeDiff.ToString());

                    if (timeDiff < minTimeBetweenScanSteps)
                    {
                        int remain = (int)(minTimeBetweenScanSteps - timeDiff);
                        Console.WriteLine("Duplicated activities");
                        lblId.Text   = "Đã thực hiện!";
                        lblName.Text = "Vui lòng đợi: " + remain.ToString() + " giây!";
                        return;
                    }
                    else
                    {
                        Console.WriteLine("Remove key from activities");
                        scaningState.Remove(empId);
                    }
                }

                // Save activities timestamp
                scaningState[empId] = DateTime.Now;

                // Actual event to update GUI and DB
                string absImageDir = captureCamera();
                try
                {
                    updateTimeInOut(empId, absImageDir);
                    displayNameAndImage(empData);
                    displayTime();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Exception when updating the checkin/out: " + ex.Message);
                    string errorLine = DateTime.Now.ToString("HH:mm:ss") + ": Exception when updating the checkin/out!: " + ex.Message;
                    txtConsole.Text         += "\n" + errorLine;
                    lblCheckInOutStatus.Text = errorLine;
                }
            }
        }