public void LoadDataMQCStarting() { stopwatch = new Stopwatch(); try { stopwatch.Start(); if (ListMachines != null) { foreach (var machine in ListMachines) { Plc.Instance.Connect(machine.IP, SettingClass.PLCTimeOut); DataMQC dataMQC = null; if (Plc.Instance.ConnectionState == ConnectionStates.Online) {// doc barcode truoc var barcode = Plc.Instance.ReadTagsToString(tagsbarcode); dataMQC = new DataMQC(); dataMQC.PLC_Barcode = barcode; var ListTag = Plc.Instance.ReadTags(tags); if (ListTag != null && ListTag.Count == 4) { MachineOperation ma1 = new MachineOperation(); ma1.Line = machine.Line; ma1.IP = machine.IP; ma1.Output = (ListTag[0].ItemValue.ToString() != null) ? double.Parse(ListTag[0].ItemValue.ToString()) : 0; ma1.NG = (ListTag[1].ItemValue.ToString() != null) ? double.Parse(ListTag[1].ItemValue.ToString()) : 0; ma1.Rework = (ListTag[2].ItemValue.ToString() != null) ? double.Parse(ListTag[2].ItemValue.ToString()) : 0; ma1.ONOFF = (ListTag[3].ItemValue.ToString() != null) ? ListTag[3].ItemValue.ToString() : ""; ma1.Status = Plc.Instance.ConnectionState.ToString(); ma1.Lot = barcode; dataMQC.DateTimeReset = DateTime.Now; dataMQC.Good_Products_Total = ma1.Output; dataMQC.NG_Products_Total = ma1.NG; dataMQC.RW_Products_Total = ma1.Rework; dataMQC.STARTSTOP = ma1.ONOFF; dataMQC.NG_Products_NG_ = new int[38]; int CountNG = 0; int CountRW = 0; machineOperations.Add(ma1); if (dataMQC.NG_Products_Total > 0) { var ListNG = Plc.Instance.ReadTags(tagsError); foreach (var item in ListNG) { dataMQC.NG_Products_NG_[CountNG] = int.Parse(item.ItemValue.ToString()); CountNG++; } } dataMQC.RW_Products_NG_ = new int[38]; if (dataMQC.RW_Products_Total > 0) { var ListRW = Plc.Instance.ReadTags(tagsRework); foreach (var item in ListRW) { dataMQC.RW_Products_NG_[CountRW] = int.Parse(item.ItemValue.ToString()); CountRW++; } } } else { SystemLog.Output(SystemLog.MSG_TYPE.War, "PLC readtag fail :", machine.IP); } if (dataMQC != null) { UploadLocalPLCDB uploadLocalPLCDB = new UploadLocalPLCDB(); if (dataMQC.PLC_Barcode.Contains("0010;B01;B01") || dataMQC.PLC_Barcode.Contains("0010;B01;B02")) { Uploaddata uploaddata = new Uploaddata(); if (CountRun == 0 && cb_GetFirstValues.IsChecked == true) { if (SettingClass.usingOfftlineServer) { DateTime FromDate = DateTime.Now; var InsertLocal = uploadLocalPLCDB.InsertMQCUpdateRealtime(dataMQC, machine.Line, false, SettingClass); if (InsertLocal == false) { SystemLog.Output(SystemLog.MSG_TYPE.War, "Insert local data fail", ""); } else { DateTime ToDate = DateTime.Now; LocalToServer localToServer = new LocalToServer(); var Result = localToServer.UploadLocalServertoFactoryDB(dataMQC.PLC_Barcode, FromDate, ToDate, SettingClass); if (Result == false) { SystemLog.Output(SystemLog.MSG_TYPE.War, "insert to local data not success", machine.Line); } } } else { var InsertResult = uploaddata.InsertMQCUpdateRealtime(dataMQC, machine.Line, false); if (InsertResult == false) { SystemLog.Output(SystemLog.MSG_TYPE.War, "InsertMQCUpdateRealtime", ""); } } } var StockAvaiable = uploaddata.QuantityCanRun(dataMQC.PLC_Barcode); if (StockAvaiable > 0) { Plc.Instance.Write("DB151.DBW0", (uint)StockAvaiable); } else { Plc.Instance.Write("DB151.DBW4", (uint)Math.Abs(StockAvaiable)); } } else { SystemLog.Output(SystemLog.MSG_TYPE.War, "Barcode wrong format :", dataMQC.PLC_Barcode); } } else { SystemLog.Output(SystemLog.MSG_TYPE.War, "Data MQC = null :", machine.IP); } } else { MachineOperation ma1 = new MachineOperation(); ma1.Line = machine.Line; ma1.IP = machine.IP; ma1.Status = Plc.Instance.ConnectionState.ToString(); machineOperations.Add(ma1); SystemLog.Output(SystemLog.MSG_TYPE.War, "machine is not online", machine.IP); } keyValuePairsOld.Add(machine.IP, dataMQC); } datagridMachines.ItemsSource = machineOperations; } else { SystemLog.Output(SystemLog.MSG_TYPE.War, "list machine = null", ""); } stopwatch.Stop(); } catch (Exception ex) { SystemLog.Output(SystemLog.MSG_TYPE.Err, "Read PLC fail", ex.Message); } }
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>(); foreach (var machine in ListMachines) { try { DataMQC MQCIPOld = keyValuePairsOld[machine.IP]; DataMQC mQCIP = new DataMQC(); mQCIP = GetDataMQCRealtime(machine.IP, machine.Line, MQCIPOld); if (mQCIP != null) { Uploaddata uploaddata = new Uploaddata(); UploadLocalPLCDB uploadLocalPLCDB = new UploadLocalPLCDB(); bool ischange = false; DataMQC mQCIPChanged = new DataMQC(); mQCIPChanged = uploaddata.ChangeMQCData(MQCIPOld, mQCIP, out ischange); if (mQCIP.PLC_Barcode != null) { if (mQCIP.PLC_Barcode.Contains("0010;B01;B01") || mQCIP.PLC_Barcode.Contains("0010;B01;B02")) { if (ischange) { if (SettingClass.usingOfftlineServer) { DateTime FromDate = DateTime.Now; var InsertLocal = uploadLocalPLCDB.InsertMQCUpdateRealtime(mQCIPChanged, machine.Line, false, SettingClass); if (InsertLocal == false) { SystemLog.Output(SystemLog.MSG_TYPE.War, "Insert local data fail", machine.Line); } else { keyValuePairsOld[machine.IP] = mQCIP; DateTime ToDate = DateTime.Now; LocalToServer localToServer = new LocalToServer(); var Result = localToServer.UploadLocalServertoFactoryDB(mQCIPChanged.PLC_Barcode, FromDate, ToDate, SettingClass); if (Result == false) { SystemLog.Output(SystemLog.MSG_TYPE.War, "insert to local data not success", machine.Line); } } //var InsertDb = uploaddata.InsertMQCUpdateRealtime(mQCIPChanged, machine.Line, false); //if (InsertDb == false) //{ // SystemLog.Output(SystemLog.MSG_TYPE.War, "Insert remote data fail", machine.Line); //} } else { var InsertDb = uploaddata.InsertMQCUpdateRealtime(mQCIPChanged, machine.Line, false); if (InsertDb == false) { SystemLog.Output(SystemLog.MSG_TYPE.War, "Insert remote data fail", machine.Line); } else { keyValuePairsOld[machine.IP] = mQCIP; } } } var StockAvaiable = uploaddata.QuantityCanRun(mQCIPChanged.PLC_Barcode); if (StockAvaiable > 0) { Plc.Instance.Write("DB151.DBW0", (uint)StockAvaiable); } else { Plc.Instance.Write("DB151.DBW4", (uint)Math.Abs(StockAvaiable)); } } else { Plc.Instance.Write("DB151.DBW0", (uint)0); SystemLog.Output(SystemLog.MSG_TYPE.War, "Line : " + machine.Line, "Barcode Wrong Format- Write stock available = 0 " + mQCIP.PLC_Barcode); } } else { Plc.Instance.Write("DB151.DBW0", (uint)0); SystemLog.Output(SystemLog.MSG_TYPE.War, "Line : " + machine.Line, "Barcode == null - Write stock available = 0 " + mQCIP.PLC_Barcode); } } else { SystemLog.Output(SystemLog.MSG_TYPE.War, "Line : " + machine.Line, " DATA MQC = NULL "); } } 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); }