Ask Question

Name:
Title:
Your Question:

Answer Question

Name:
Your Answer:
User Submitted Source Code!


Description:
  aa
Language: PASCAL
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Data.SqlClient;
using System.Net.NetworkInformation;
using System.Net;
namespace Finger_Teck_Program
{
    public partial class Form1 : Form
    {     
        bool IsReset;
        List<sctMemberDeviceId> lstMemberDeviceId = new List<sctMemberDeviceId>();
        SqlConnection cnn = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=delta6;User ID=sa;Password=123456789");
        SqlCommand cmd = new SqlCommand();
        FingerNarmgostaran.Finger _device;
        long LastLogTime;
        long LastLogMemberid;
        struct sctMemberDeviceId
        {
            public Int64 Deviceid;
            public Int64 memberid;
        };
        struct sctDbLog
        {
            public Int64 id;
            public Int64 memberid;
            public String DateEnter;
            public String DateExit;
        };
        struct sctDeviceLog
        {
            public Int64 Deviceid;
            public Int64 memberid;
            public Int64 Time;
        };
        List<sctDeviceLog> lstDevicelog = new List<sctDeviceLog>();
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                _device = new FingerNarmgostaran.Finger();
                Cursor = Cursors.WaitCursor;

                Ping pingSender = new Ping();
                IPAddress address = IPAddress.Parse(txtIp.Text);
                PingReply reply = pingSender.Send(address);

                if (reply.Status == IPStatus.Success)
                {
                    if (_device.Connect(txtIp.Text, txtPort.Text) == true)
                    {
                        btnreadlog.Enabled = true;
                        btnerasecode.Enabled = true;
                        txtstatus.Text = "Connect";
                        btnConnect.Enabled = false;
                        btnDisconnect.Enabled = true;
                        _device.OnRecive += _device_OnRecive;
                    }
                }
                Cursor = Cursors.Default;
            }
            catch (Exception er)
            {
                Cursor = Cursors.Default;
                List1.Items.Add(er.Message);
                List1.SelectedIndex = List1.Items.Count - 1;
            }
        }

        void _device_OnRecive(object source, FingerNarmgostaran.MyEventArgs e)
        {            
            if (e.Date == "")
            {
                if (e.Log.IndexOf("TCount=") == 0)
                {
                    progressBar1.Invoke(new Action(() => progressBar1.Value = 0));
                    int i = Convert.ToInt32(e.Log.Substring(7));
                    progressBar1.Invoke(new Action(() => progressBar1.Maximum = i));
                    lblPerson.Invoke(new Action(() => lblPerson.Text = "0 %"));
                }
                else
                {
                    List1.Invoke(new Action(() => List1.Items.Add(e.Log)));
                    btnreadlog.Invoke(new Action(() => btnreadlog.Enabled = true));
                    btnerasecode.Invoke(new Action(() => btnerasecode.Enabled = true));
                }
            }
            else
            {
                try
                {
                    sctDeviceLog tmp = new sctDeviceLog();
                    DateTime dt = new DateTime(Convert.ToInt16(e.Date.Substring(0, 4)), Convert.ToInt16(e.Date.Substring(5, 2)), Convert.ToInt16(e.Date.Substring(8, 2)), Convert.ToInt16(e.Time.Substring(0, 2)), Convert.ToInt16(e.Time.Substring(3, 2)), Convert.ToInt16(e.Time.Substring(6, 2)));
                    tmp.Deviceid = Convert.ToInt64(e.Userid);
                    tmp.Time = dt.Ticks;
                    tmp.memberid = lstMemberDeviceId.Where(p => p.Deviceid == tmp.Deviceid).FirstOrDefault().memberid;

                    lstDevicelog.Add(tmp);
                    List1.Invoke(new Action(() => List1.Items.Add("ID:" + e.Userid + " MId: " + tmp.memberid + " " + Convert.ToString(e.Date) + "  " + e.Time)));
                    txtTotalLog.Invoke(new Action(() => txtTotalLog.Text = (Convert.ToInt64(txtTotalLog.Text) + Convert.ToInt64(lstDevicelog.Count())).ToString()));

                    SaveLog();//Save Log To DataBase
                    if (progressBar1.Value < progressBar1.Maximum)
                        progressBar1.Invoke(new Action(() => progressBar1.Value = progressBar1.Value + 1));
                    lblPerson.Invoke(new Action(() => lblPerson.Text = (progressBar1.Value * 100 / progressBar1.Maximum).ToString() + " %"));
                    if (progressBar1.Value == progressBar1.Maximum)
                    {
                        btnreadlog.Invoke(new Action(() => btnreadlog.Enabled = true));
                        btnerasecode.Invoke(new Action(() => btnerasecode.Enabled = true));
                    }
                }
                catch (Exception er)
                {
                    MessageBox.Show(er.Message);
                }
            }            
        }        
        private void button2_Click(object sender, EventArgs e)
        {
            btnerasecode.Enabled = false;
            btnreadlog.Enabled = false;
            IsReset = true;            
            txtTotalLog.Text = "0";
            TxtEnterLog.Text = "0";
            txtExitLog.Text = "0";
            txtErrorCount.Text = "0";
            txtDuplicate.Text = "0";
            _device.ReadLog();            
        }
        private void SaveLog()
        {           
            foreach (var item in lstDevicelog)
            {
                string dtEnter="";
                string dtExit="";
                string dtid = "";
                string prid = "";
                
                cmd.Connection = cnn;
                DateTime dt2 = new DateTime(Convert.ToInt64(item.Time));                
                DateTime dt3 = new DateTime(dt2.Year,dt2.Month,dt2.Day);
                cmd.CommandText = "select * from Device_EnterExit where MemberId=" + item.memberid + " and DateEnter>='" + dt3.Ticks.ToString() + "' and DateExit=''";
                SqlDataReader dr = null;
                dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    dtEnter = dr["DateEnter"].ToString();
                    dtExit = dr["DateExit"].ToString();
                    dtid = dr["MemberId"].ToString();
                    prid = dr["id"].ToString();
                }
                dr.Close();               

                if (item.memberid.ToString() == dtid && (dtEnter == item.Time.ToString() || item.Time.ToString() == dtExit) )
                {
                    txtDuplicate.Invoke(new Action(() => txtDuplicate.Text = (Convert.ToInt64(txtDuplicate.Text) + 1).ToString()));
                    continue;
                }
                if (dtEnter == "" && dtExit=="")
                {
                    if (item.memberid != 0)
                    {
                        TxtEnterLog.Invoke(new Action(() => TxtEnterLog.Text = (Convert.ToInt64(TxtEnterLog.Text) + 1).ToString()));
                        cmd.CommandText = "insert into Device_EnterExit (DateEnter,DateExit,MemberId,del) OUTPUT INSERTED.id values('" + item.Time + "',''," + item.memberid + ",0)";
                        cmd.ExecuteNonQuery();
                        //Int64 newId = (Int64)cmd.ExecuteScalar();
                    }
                    else
                    {
                        txtErrorCount.Invoke(new Action(() => txtErrorCount.Text = (Convert.ToInt64(txtErrorCount.Text) + 1).ToString()));
                        cmd.CommandText = "insert into device_tblerrorforeignkey (memberid,deviceid,date) values(" + item.memberid + "," + item.Deviceid + ",'" + item.Time + "')";
                        cmd.ExecuteNonQuery();
                    }
                    continue;
                }
                else 
                {
                    DateTime dt1 = new DateTime(Convert.ToInt64(dtEnter));                    
                    if (txtDelayTime.Text == "")
                        txtDelayTime.Text = "0";
                    if ((dt2 - dt1).TotalSeconds > Convert.ToInt32(txtDelayTime.Text))
                    {
                        txtExitLog.Invoke(new Action(() => txtExitLog.Text = (Convert.ToInt64(txtExitLog.Text) + 1).ToString()));
                        cmd.CommandText = "update Device_EnterExit set DateExit='" + item.Time + "' where id=" + prid;
                        cmd.ExecuteNonQuery();
                    }
                    else
                    {
                        txtErrorCount.Invoke(new Action(() => txtErrorCount.Text = (Convert.ToInt64(txtErrorCount.Text) + 1).ToString()));                        
                    }
                    
                }                
            }
            lstDevicelog.Clear();
        }
        private void button3_Click(object sender, EventArgs e)
        {
            btnerasecode.Enabled = false;
            btnreadlog.Enabled = false;
            _device.DelLog();
        }
        private void button4_Click(object sender, EventArgs e)
        {
            List1.Items.Clear();
        }
        private void btnGetTime_Click(object sender, EventArgs e)
        {
            
        }

        private void btnSetTime_Click(object sender, EventArgs e)
        {
            
        }

        private void btnSyncTime_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void txtstatus_TextChanged(object sender, EventArgs e)
        {
            if (txtstatus.Text == "Connect")
            {
                gprLog.Enabled = true;
                gprtime.Enabled = true;
            }
            else
            {
                gprLog.Enabled = false;
                gprtime.Enabled = false;
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                cnn.Open();
                cmd.Connection = cnn;
                cmd.CommandText = "select * from Device_Setting";
                SqlDataReader dr = null ;
                dr=cmd.ExecuteReader();
                if (dr.Read())
                {
                    txtIp.Text = dr["Ip"].ToString();
                    txtPort.Text = dr["Port"].ToString();
                    txtReadTime.Text = dr["LiveTime"].ToString();
                    txtDelayTime.Text = dr["DelayTime"].ToString();
                }
                dr.Close();


                //Load Member And Device Id From Db                
                cmd.CommandText = "select * from Device_MembersMemberDevice";
                SqlDataReader rd = null;
                rd = cmd.ExecuteReader();
                while (rd.Read())
                {
                    sctMemberDeviceId tmp = new sctMemberDeviceId();
                    tmp.memberid = Convert.ToInt64(rd["MemberId"]);
                    tmp.Deviceid = Convert.ToInt64(rd["DeviceId"]);
                    lstMemberDeviceId.Add(tmp);
                }
                rd.Close();
                //Load Member And Device Id From Db                                               
            }
            catch (Exception er)
            {
                List1.Items.Add(er.Message);
                List1.SelectedIndex = List1.Items.Count - 1;
            }            
        }
        private void btnDisconnect_Click(object sender, EventArgs e)
        {
            try
            {
                if (_device.Disconnect() == true)
                {
                    btnreadlog.Enabled = false;
                    btnerasecode.Enabled = false;
                    txtstatus.Text = "Disconnect";
                    btnConnect.Enabled = true;
                    btnDisconnect.Enabled = false;
                }
                _device = null;
            }
            catch (Exception er)
            {
                List1.Items.Add(er.Message);
                List1.SelectedIndex = List1.Items.Count - 1;
            }
        }
        private void btnSaveChange_Click(object sender, EventArgs e)
        {
            try
            {
                cmd.CommandText = "update Device_Setting set LiveTime=" + txtReadTime.Text  + ",Ip='" + txtIp.Text +"',Port='" + txtPort.Text + "',DelayTime="+ txtDelayTime.Text;
                cmd.ExecuteNonQuery();                
                List1.Items.Add("Save Successful...");
                List1.SelectedIndex = List1.Items.Count - 1;
            }
            catch (Exception er)
            {
                List1.Items.Add(er.Message);
                List1.SelectedIndex = List1.Items.Count - 1;
            }
        }
        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            try
            {
                Environment.Exit(0);
                _device = null;
                btnDisconnect_Click(sender,e);
            }
            catch{}
        }
        Boolean IsFirst = true;
        private void Form1_Activated(object sender, EventArgs e)
        {
            if(IsFirst==true)
                button1_Click(sender, e);//Connect To Device
            IsFirst = false;
        }
    }
}
Comments: