public void UploadPLCtoDatabase(MQCVariable mqcchanged, List <string> listChanged, string line)
        {
            try
            {
                UploadLocalPLCDB uploadLocal = new UploadLocalPLCDB();
                Upload2Mes       upload2Mes  = new Upload2Mes();
                if (listChanged.Contains("OPQTY"))
                {
                    uploadLocal.InsertMQCMESUpdateRealtime(mqcchanged, "OPQTY", line, SettingClass);

                    upload2Mes.IsSendData2MES(mqcchanged, line);
                    //Upload OP QTY change
                }
                if (listChanged.Contains("NGQTY"))
                {
                    uploadLocal.InsertMQCMESUpdateRealtime(mqcchanged, "NGQTY", line, SettingClass);
                    upload2Mes.IsSendData2MES(mqcchanged, line);
                }
                if (listChanged.Contains("RWQTY"))
                {
                    uploadLocal.InsertMQCMESUpdateRealtime(mqcchanged, "RWQTY", line, SettingClass);
                    upload2Mes.IsSendData2MES(mqcchanged, line);
                }

                if (listChanged.Contains("Reset"))
                {
                    uploadLocal.InsertMQCMESUpdateRealtime(mqcchanged, "Reset", line, SettingClass);
                }
            }
            catch (Exception ex)
            {
                SystemLog.Output(SystemLog.MSG_TYPE.Err, "UploadPLCtoDatabase", ex.Message);
            }
        }
        private void btn_GetValue_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                SystemLog.Output(SystemLog.MSG_TYPE.War, "Start read data from PLC", "START");
                stopwatch = new Stopwatch();
                MachineItem machine = new MachineItem();
                machine.IP   = "172.16.1.145";
                machine.Line = "L03";
                int ConnectionPLC                 = -1;
                Sharp.ReadVariablePLC pLC         = new Sharp.ReadVariablePLC(machine.IP, 0, 0, out ConnectionPLC);
                MQCVariable           mQCVariable = new MQCVariable();
                mQCVariable.DicSPLCtatus = pLC.ReadStatusPLCMQC();

                mQCVariable.QRMES           = pLC.ReadAreaByteToString(181, 100, 100);
                mQCVariable.QRID            = pLC.ReadAreaByteToString(181, 0, 100);
                mQCVariable.ListMQCQty      = pLC.ReadQuantityMQC();
                mQCVariable.ListNG38        = pLC.ReadAreaIntToListInt(3, 4, 76);
                mQCVariable.ListRW38        = pLC.ReadAreaIntToListInt(4, 4, 76);
                mQCVariable.ListQtyProduced = pLC.ReadQuantityMQCProduced();
                SystemLog.Output(SystemLog.MSG_TYPE.War, "ConnectionPLC", ConnectionPLC.ToString());
                SystemLog.Output(SystemLog.MSG_TYPE.War, VariablePLC.FlagKT, mQCVariable.DicSPLCtatus[VariablePLC.FlagKT].ToString());
                SystemLog.Output(SystemLog.MSG_TYPE.War, VariablePLC.IsReset, mQCVariable.DicSPLCtatus[VariablePLC.IsReset].ToString());
                SystemLog.Output(SystemLog.MSG_TYPE.War, VariablePLC.OnOFF, mQCVariable.DicSPLCtatus[VariablePLC.OnOFF].ToString());
                SystemLog.Output(SystemLog.MSG_TYPE.War, VariablePLC.WriteReadyStart, mQCVariable.DicSPLCtatus[VariablePLC.WriteReadyStart].ToString());

                SystemLog.Output(SystemLog.MSG_TYPE.War, "QR MES", mQCVariable.QRMES);
                SystemLog.Output(SystemLog.MSG_TYPE.War, "QR ID", mQCVariable.QRID);

                SystemLog.Output(SystemLog.MSG_TYPE.War, "Output", mQCVariable.ListMQCQty[0].ToString());
                SystemLog.Output(SystemLog.MSG_TYPE.War, "NG", mQCVariable.ListMQCQty[1].ToString());
                SystemLog.Output(SystemLog.MSG_TYPE.War, "RW", mQCVariable.ListMQCQty[2].ToString());

                for (int i = 0; i < mQCVariable.ListNG38.Count; i++)
                {
                    SystemLog.Output(SystemLog.MSG_TYPE.War, "NG" + (i + 1).ToString(), mQCVariable.ListNG38[i].ToString());
                }
                for (int i = 0; i < mQCVariable.ListRW38.Count; i++)
                {
                    SystemLog.Output(SystemLog.MSG_TYPE.War, "RW" + (i + 1).ToString(), mQCVariable.ListRW38[i].ToString());
                }

                SystemLog.Output(SystemLog.MSG_TYPE.War, "Output Produced", mQCVariable.ListQtyProduced[0].ToString());
                SystemLog.Output(SystemLog.MSG_TYPE.War, "NG Produced", mQCVariable.ListQtyProduced [1].ToString());
                SystemLog.Output(SystemLog.MSG_TYPE.War, "RW Produced", mQCVariable.ListQtyProduced[2].ToString());



                SystemLog.Output(SystemLog.MSG_TYPE.War, "Start read data from PLC", "END");
            }
            catch (Exception ex)
            {
                SystemLog.Output(SystemLog.MSG_TYPE.Err, ex.Source, ex.Message);
            }
        }
        private MQCVariable GetMQCVariableRealtime(string IP, MQCVariable mQCOld)
        {
            MQCVariable qCVariable = new MQCVariable();

            try
            {
                int ConnectionPLC         = -1;
                Sharp.ReadVariablePLC pLC = new Sharp.ReadVariablePLC(IP, 0, 0, out ConnectionPLC);
                qCVariable.Connection = ConnectionPLC;
                if (ConnectionPLC == 0)
                {
                    qCVariable.DicSPLCtatus = pLC.ReadStatusPLCMQC();

                    if (qCVariable.DicSPLCtatus[VariablePLC.FlagKT] == true)
                    {
                        if (qCVariable.DicSPLCtatus[VariablePLC.WriteReadyStart] == false)
                        {
                            string QRMES = pLC.ReadAreaByteToString(181, 100, 100);
                            qCVariable.QRMES = QRMES.Trim();

                            string QRID = pLC.ReadAreaByteToString(181, 0, 100);
                            qCVariable.QRID = QRID.Trim();

                            var ResultValidationQRCode = SubFunction.IsValidationQRCode(qCVariable.QRMES, qCVariable.QRID);
                            pLC.WriteDinttoPLC(ResultValidationQRCode, 181, 206, 2);//write message error to PLC
                            if (ResultValidationQRCode == 0)
                            {
                                SQLUpload.SQLQRUpdate sQLQR      = new SQLQRUpdate();
                                DataTable             dtQRRecord = sQLQR.GetQuanityFromQRMES(qCVariable.QRMES);

                                QRMQC_MES qRMQC_MES = QRSpilittoClass.QRstring2MQCFormat(qCVariable.QRMES);
                                if (dtQRRecord.Rows.Count == 1)
                                {
                                    var OutputQty = Int16.Parse(dtQRRecord.Rows[0]["OutputQty"].ToString());
                                    var NGQty     = Int16.Parse(dtQRRecord.Rows[0]["NGQty"].ToString());
                                    var RWQty     = Int16.Parse(dtQRRecord.Rows[0]["RWQty"].ToString());
                                    var Total     = Int16.Parse(dtQRRecord.Rows[0]["TotalQty"].ToString());
                                    if (qRMQC_MES.quantity > (OutputQty + NGQty + RWQty) && qRMQC_MES.quantity > Total)
                                    {
                                        pLC.WriteMQCProducedQuantitytoPLC(OutputQty, NGQty, RWQty);
                                        pLC.WritebittoPLC(true, 181, 204, 1);//Write FlagKT to PLC
                                    }
                                    else
                                    {
                                        //Write to PLC values to know QR production finished
                                        pLC.WriteDinttoPLC(3, 181, 206, 2);//Completed QR MES
                                    }
                                }
                                else if (dtQRRecord.Rows.Count == 0)
                                {
                                    pLC.WriteMQCProducedQuantitytoPLC(0, 0, 0);
                                    pLC.WritebittoPLC(true, 181, 204, 1);//Write FlagKT to PLC
                                }
                            }
                            else
                            {
                                pLC.WritebittoPLC(false, 181, 204, 1);//Write FlagKT to PLC
                            }

                            //get values from db and write value PLC



                            qCVariable.ListMQCQty = pLC.ReadQuantityMQC();

                            qCVariable.ListQtyProduced = pLC.ReadQuantityMQCProduced();

                            qCVariable.ListNG38 = pLC.ReadAreaIntToListInt(3, 4, 76);
                            qCVariable.ListRW38 = pLC.ReadAreaIntToListInt(4, 4, 76);
                        }
                        else
                        {
                            qCVariable.QRMES      = mQCOld.QRMES;
                            qCVariable.QRID       = mQCOld.QRID;
                            qCVariable.ListMQCQty = pLC.ReadQuantityMQC();
                            if (qCVariable.ListMQCQty[1] > mQCOld.ListMQCQty[1])
                            {
                                qCVariable.ListNG38 = pLC.ReadAreaIntToListInt(3, 4, 76);
                            }
                            else
                            {
                                qCVariable.ListNG38 = mQCOld.ListNG38;
                            }

                            if (qCVariable.ListMQCQty[2] > mQCOld.ListMQCQty[2])
                            {
                                qCVariable.ListRW38 = pLC.ReadAreaIntToListInt(4, 4, 76);
                            }
                            else
                            {
                                qCVariable.ListRW38 = mQCOld.ListRW38;
                            }
                        }
                    }



                    pLC.Diconnect();
                }
            }
            catch (Exception ex)
            {
                SystemLog.Output(SystemLog.MSG_TYPE.Err, "Get MQC variable realtime fail", ex.Message);
            }
            return(qCVariable);
        }
        public void LoadDataMQCStarting()
        {
            try
            {
                stopwatch.Start();
                LoadAdress();
                if (ListMachines.Count > 0)
                {
                    DicMQCVariableIP = new Dictionary <string, MQCVariable>();

                    foreach (var machine in ListMachines)
                    {
                        int                   ConnectionPLC = -1;
                        MQCVariable           mQCVariable   = new MQCVariable();
                        QRMQC_MES             qRMQC_MES     = new QRMQC_MES();
                        QRIDMES               qRIDMES       = new QRIDMES();
                        Sharp.ReadVariablePLC pLC           = new Sharp.ReadVariablePLC(machine.IP, 0, 0, out ConnectionPLC);
                        mQCVariable.Connection = ConnectionPLC;
                        if (ConnectionPLC == 0)
                        {
                            Dictionary <string, bool> DicStatusPLC = new Dictionary <string, bool>();

                            DicStatusPLC             = pLC.ReadStatusPLCMQC();
                            mQCVariable.DicSPLCtatus = DicStatusPLC;

                            if (DicStatusPLC.Count == 4)
                            {
                                if (DicStatusPLC[VariablePLC.FlagKT])
                                {
                                    string QRMES = pLC.ReadAreaByteToString(181, 100, 100);
                                    mQCVariable.QRMES = QRMES;

                                    string QRID = pLC.ReadAreaByteToString(181, 0, 100);
                                    mQCVariable.QRID = QRID;

                                    var ResultValidationQRCode = SubFunction.IsValidationQRCode(QRMES, QRID);
                                    pLC.WriteDinttoPLC(ResultValidationQRCode, 181, 206, 2);//write message error to PLC
                                    if (ResultValidationQRCode == 0)
                                    {
                                        pLC.WritebittoPLC(true, 181, 204, 1);//Write FlagKT to PLC
                                    }
                                    else
                                    {
                                        pLC.WritebittoPLC(false, 181, 204, 1);//Write FlagKT to PLC
                                    }

                                    qRMQC_MES = QRSpilittoClass.QRstring2MQCFormat(mQCVariable.QRMES);
                                    //  get values from db and write value PLC
                                    SQLUpload.SQLQRUpdate sQLQR      = new SQLQRUpdate();
                                    DataTable             dtQRRecord = sQLQR.GetQuanityFromQRMES(mQCVariable.QRMES);

                                    if (dtQRRecord.Rows.Count == 1)
                                    {
                                        var OutputQty = Int16.Parse(dtQRRecord.Rows[0]["OutputQty"].ToString());
                                        var NGQty     = Int16.Parse(dtQRRecord.Rows[0]["NGQty"].ToString());
                                        var RWQty     = Int16.Parse(dtQRRecord.Rows[0]["RWQty"].ToString());
                                        var Total     = Int16.Parse(dtQRRecord.Rows[0]["TotalQty"].ToString());
                                        if (qRMQC_MES.quantity > (OutputQty + NGQty + RWQty) && qRMQC_MES.quantity > Total)
                                        {
                                            pLC.WriteMQCProducedQuantitytoPLC(OutputQty, NGQty, RWQty);
                                        }
                                        else
                                        {
                                            //Write to PLC values to know QR production finished
                                            pLC.WriteDinttoPLC(4, 181, 206, 2);//Completed QR MES
                                        }
                                    }
                                    else if (dtQRRecord.Rows.Count == 0)
                                    {
                                        pLC.WriteMQCProducedQuantitytoPLC(0, 0, 0);
                                    }

                                    List <int> ListMQCQty = pLC.ReadQuantityMQC();
                                    mQCVariable.ListMQCQty = ListMQCQty;
                                    List <int> ListMQCProduced = pLC.ReadQuantityMQCProduced();
                                    mQCVariable.ListQtyProduced = ListMQCProduced;
                                    List <int> ListNG38 = pLC.ReadAreaIntToListInt(3, 4, 76);
                                    mQCVariable.ListNG38 = ListNG38;
                                    List <int> ListRW38 = pLC.ReadAreaIntToListInt(4, 4, 76);
                                    mQCVariable.ListRW38 = ListRW38;
                                }
                            }
                        }

                        DicMQCVariableIP.Add(machine.IP, mQCVariable);
                        pLC.Diconnect();
                        qRMQC_MES = new QRMQC_MES();
                        qRMQC_MES = QRSpilittoClass.QRstring2MQCFormat(mQCVariable.QRMES);
                        qRIDMES   = QRSpilittoClass.QRstring2IDFormat(mQCVariable.QRID);
                        MachineOperation operation = new MachineOperation();
                        operation.IP        = machine.IP;
                        operation.Line      = machine.Line;
                        operation.Lot       = qRMQC_MES.PO;
                        operation.Inspector = qRIDMES.FullName;
                        operation.product   = qRMQC_MES.Product;

                        if (mQCVariable.ListMQCQty.Count == 3)
                        {
                            operation.Output = mQCVariable.ListMQCQty[0];
                            operation.NG     = mQCVariable.ListMQCQty[1];
                            operation.Rework = mQCVariable.ListMQCQty[2];
                            operation.Status = "Updating";
                        }
                        else if (mQCVariable.ListMQCQty.Count == 0)
                        {
                            operation.Output = 0;
                            operation.NG     = 0;
                            operation.Rework = 0;
                            operation.Status = "Waiting";
                        }
                        machineOperations.Add(operation);
                    }
                    datagridMachines.ItemsSource = machineOperations;
                }
            }
            catch (Exception ex)
            {
                SystemLog.Output(SystemLog.MSG_TYPE.Err, "Read PLC fail", ex.Message);
            }
        }
        public MQCVariable GetMQCVariableDisCrepancy(MQCVariable mQCPLC, MQCVariable mQCOld, ref bool Ischanged, ref List <string> typeChange)
        {
            MQCVariable qCVariableChanged = new MQCVariable();

            qCVariableChanged.QRMES = mQCOld.QRMES;
            qCVariableChanged.QRID  = mQCOld.QRID;

            try
            {
                if (mQCPLC.DicSPLCtatus[VariablePLC.IsReset] != mQCOld.DicSPLCtatus[VariablePLC.IsReset])
                {
                    Ischanged = true;
                    typeChange.Add("Reset");

                    return(qCVariableChanged);
                }

                if (mQCPLC.DicSPLCtatus[VariablePLC.FlagKT] != mQCOld.DicSPLCtatus[VariablePLC.FlagKT])
                {
                    Ischanged = true;
                    typeChange.Add("QRCheck");
                }
                if (mQCOld.DicSPLCtatus[VariablePLC.FlagKT] == true)
                {
                    qCVariableChanged.ListMQCQty.Add(0);
                    qCVariableChanged.ListMQCQty.Add(0);
                    qCVariableChanged.ListMQCQty.Add(0);
                    Ischanged  = false;
                    typeChange = new List <string>();;

                    if (mQCPLC.QRMES != mQCOld.QRMES)
                    {
                        Ischanged = true;
                        typeChange.Add("QR_MES");
                    }
                    if (mQCPLC.QRID != mQCOld.QRID)
                    {
                        Ischanged = true;
                        typeChange.Add("QR_ID");
                    }

                    if (mQCPLC.ListMQCQty[0] > mQCOld.ListMQCQty[0])
                    {
                        qCVariableChanged.ListMQCQty[0] = mQCPLC.ListMQCQty[0] - mQCOld.ListMQCQty[0];
                        Ischanged = true;
                        typeChange.Add("OPQTY");
                    }
                    if (mQCPLC.ListMQCQty[1] > mQCOld.ListMQCQty[1])
                    {
                        qCVariableChanged.ListMQCQty[1] = mQCPLC.ListMQCQty[1] - mQCOld.ListMQCQty[1];
                        for (int i = 0; i < mQCPLC.ListNG38.Count; i++)
                        {
                            qCVariableChanged.ListNG38.Add(mQCPLC.ListNG38[i] - mQCOld.ListNG38[i]);
                        }
                        Ischanged = true;
                        typeChange.Add("NGQTY");
                    }
                    if (mQCPLC.ListMQCQty[2] > mQCOld.ListMQCQty[2])
                    {
                        qCVariableChanged.ListMQCQty[2] = mQCPLC.ListMQCQty[2] - mQCOld.ListMQCQty[2];
                        for (int i = 0; i < mQCPLC.ListRW38.Count; i++)
                        {
                            qCVariableChanged.ListRW38.Add(mQCPLC.ListRW38[i] - mQCOld.ListRW38[i]);
                        }
                        Ischanged = true;
                        typeChange.Add("RWQTY");
                    }
                }
            }
            catch (Exception ex)
            {
                SystemLog.Output(SystemLog.MSG_TYPE.Err, "GetMQCVariableDisCrepancy", ex.Message);
            }
            return(qCVariableChanged);
        }
        void bg_DoWork(object sender, DoWorkEventArgs e)
        {
            // does a job like writing to serial communication, webservices etc
            var worker = sender as BackgroundWorker;

            stopwatch = new Stopwatch();
            try
            {
                stopwatch.Start();
                //    if (ListMachines != null)
                {
                    machineOperations = new List <MachineOperation>();
                    //MachineItem machine = new MachineItem();
                    //machine.IP = "172.16.1.145";
                    //machine.Line = "L03";
                    LoadAdress();//load list PLC machine
                    foreach (var machine in ListMachines)
                    {
                        try
                        {
                            //     machine.IP =
                            MQCVariable mQCOld = new MQCVariable();
                            if (DicMQCVariableIP.ContainsKey(machine.IP))
                            {
                                mQCOld = DicMQCVariableIP[machine.IP];
                            }


                            MQCVariable mQCPLC     = GetMQCVariableRealtime(machine.IP, mQCOld);
                            MQCVariable mQCChanged = new MQCVariable();
                            if (mQCPLC.Connection == 0)
                            {
                                bool          isChanged   = false;
                                List <string> listChanged = new List <string>();
                                //   if (mQCPLC.DicSPLCtatus[VariablePLC.FlagKT] == true)
                                {
                                    mQCChanged = GetMQCVariableDisCrepancy(mQCPLC, mQCOld, ref isChanged, ref listChanged);
                                    if (DicMQCVariableIP.ContainsKey(machine.IP))
                                    {
                                        DicMQCVariableIP[machine.IP] = mQCPLC;
                                    }
                                    else
                                    {
                                        DicMQCVariableIP.Add(machine.IP, mQCPLC);
                                    }
                                    if (listChanged.Contains("Reset"))
                                    {
                                        //MachineOperation operation = new MachineOperation();
                                        //operation.IP = machine.IP;
                                        //operation.Line = machine.Line;
                                        //operation.Lot = qRMQC_MES.PO;
                                        //operation.Inspector = qRIDMES.FullName;
                                        //operation.product = qRMQC_MES.Product;

                                        //operation.Output = mQCPLC.ListMQCQty[0];
                                        //operation.NG = mQCPLC.ListMQCQty[1];
                                        //operation.Rework = mQCPLC.ListMQCQty[2];
                                        //operation.Status = "Reset";
                                        SQLUpload.SQLQRUpdate sQLQRUpdate = new SQLQRUpdate();
                                        sQLQRUpdate.UpdateOrInsertQRRecordTable(mQCOld, machine.Line);

                                        SystemRecord.Output(SystemRecord.MSG_TYPE.Nor, "QR MES Reset", mQCOld.QRMES);
                                        SystemRecord.Output(SystemRecord.MSG_TYPE.Nor, "QR Inspector", mQCOld.QRID);
                                        SystemRecord.Output(SystemRecord.MSG_TYPE.Nor, "Ouput Quantity", mQCOld.ListMQCQty[0].ToString());
                                        SystemRecord.Output(SystemRecord.MSG_TYPE.Nor, "NG Quantity", mQCOld.ListMQCQty[1].ToString());
                                        SystemRecord.Output(SystemRecord.MSG_TYPE.Nor, "RW Quantity", mQCOld.ListMQCQty[2].ToString());
                                        return;
                                    }
                                    UploadPLCtoDatabase(mQCChanged, listChanged, machine.Line);

                                    QRMQC_MES qRMQC_MES = QRSpilittoClass.QRstring2MQCFormat(mQCPLC.QRMES);
                                    QRIDMES   qRIDMES   = QRSpilittoClass.QRstring2IDFormat(mQCPLC.QRID);

                                    MachineOperation operation = new MachineOperation();
                                    operation.IP        = machine.IP;
                                    operation.Line      = machine.Line;
                                    operation.Lot       = qRMQC_MES.PO;
                                    operation.Inspector = qRIDMES.FullName;
                                    operation.product   = qRMQC_MES.Product;
                                    if (mQCPLC.ListMQCQty.Count == 3)
                                    {
                                        operation.Output = mQCPLC.ListMQCQty[0];
                                        operation.NG     = mQCPLC.ListMQCQty[1];
                                        operation.Rework = mQCPLC.ListMQCQty[2];
                                        operation.Status = "Updating";
                                    }
                                    else if (mQCPLC.ListMQCQty.Count == 0)
                                    {
                                        operation.Output = 0;
                                        operation.NG     = 0;
                                        operation.Rework = 0;
                                        operation.Status = "Waiting";
                                    }
                                    //if (listChanged.Contains("Reset"))
                                    //{
                                    //    operation.Status = "Reset";
                                    //    //SQLUpload.SQLQRUpdate sQLQRUpdate = new SQLQRUpdate();
                                    //    //sQLQRUpdate.UpdateOrInsertQRRecordTable(mQCPLC, machine.Line);
                                    //    SystemRecord.Output(SystemRecord.MSG_TYPE.Nor, "QR MES Reset", mQCPLC.QRMES);
                                    //    SystemRecord.Output(SystemRecord.MSG_TYPE.Nor, "QR Inspector", mQCPLC.QRID);
                                    //    SystemRecord.Output(SystemRecord.MSG_TYPE.Nor, "Ouput Quantity", mQCPLC.ListMQCQty[0].ToString());
                                    //    SystemRecord.Output(SystemRecord.MSG_TYPE.Nor, "NG Quantity", mQCPLC.ListMQCQty[1].ToString());
                                    //    SystemRecord.Output(SystemRecord.MSG_TYPE.Nor, "RW Quantity", mQCPLC.ListMQCQty[2].ToString());

                                    //}
                                    //else
                                    //{
                                    //    operation.Status = "Updating";
                                    //}

                                    machineOperations.Add(operation);
                                }
                            }
                            else
                            {
                                MachineOperation operation = new MachineOperation();
                                operation.IP   = machine.IP;
                                operation.Line = machine.Line;
                                Sharp7.S7Client client = new Sharp7.S7Client();
                                operation.Status = client.ErrorText(mQCPLC.Connection);
                                machineOperations.Add(operation);
                            }
                        }
                        catch (Exception ex)
                        {
                            SystemLog.Output(SystemLog.MSG_TYPE.Err, "PLC IP : " + machine.IP, ex.Message);
                        }
                    }
                }


                stopwatch.Stop();
            }
            catch (Exception ex)
            {
                SystemLog.Output(SystemLog.MSG_TYPE.Err, "Read PLC fail", ex.Message);
            }

            System.Threading.Thread.Sleep(100);
        }