// Check pingRequests grater than a nr and disable Plc public void IsPingRequestsFailGrateThan(PlcModel plc, int nr) { if (plc.PingRequestsFail >= nr) { plc.IsEnable = false; } }
// Functie Verificare Adresa IP public bool IsAvailableIpAdress(PlcModel plc) { Ping ping = new Ping(); try { PingReply reply = ping.Send(plc.Ip, 200); if (reply.Status.ToString() == "Success") { plc.PingRequestsFail = 0; return(true); } plc.PingRequestsFail++; return(false); } catch (System.Net.NetworkInformation.PingException exPing) { _logger.LogError("{data} {exMessege} PlcName: {name}", DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss"), exPing.Message, plc.Name); plc.PingRequestsFail++; return(false); } catch (Exception ex) { _logger.LogError("{data} {exMessege} PlcName: {name}", DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss"), ex.Message, plc.Name); plc.PingRequestsFail++; return(false); } }
public async Task <IActionResult> PutEntry([FromRoute] int id, [FromBody] PlcModel entry) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != entry.Id) { return(BadRequest()); } _Context.Entry(entry).State = EntityState.Modified; try { await _Context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!EntryExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> Edit(int id, [Bind("PlcModelID,Name,IsEnable,CpuType,Ip,Rack,Slot")] PlcModel plcModel) { if (id != plcModel.PlcModelID) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(plcModel); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!PlcModelExists(plcModel.PlcModelID)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(plcModel)); }
// Reset pingRequests Fail at a certain time public void RemakeEnablePlc(PlcModel plc) { if (DateTime.Now.Second == 10) { plc.PingRequestsFail = 0; plc.IsEnable = true; } }
// Check pingRequests grater than a nr and disable Plc public bool IsPingRequestsFailGrateThan(PlcModel plc, int nr) { if (plc.PingRequestsFail >= nr) { return(true); } return(false); }
// Reset pingRequests Fail at a certain time public void RemakeEnablePlc(PlcModel plc) { if (DateTime.Now.Second >= 10 && DateTime.Now.Second <= 15) { plc.PingRequestsFail = 0; //plc.IsEnable = true; } }
public async Task <IActionResult> Create([Bind("PlcModelID,Name,IsEnable,CpuType,Ip,Rack,Slot")] PlcModel plcModel) { if (ModelState.IsValid) { _context.Add(plcModel); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(plcModel)); }
// Add defect to DbContext for a given Plc (Start new defect) public void AddNewDefectForPlc(RaportareDbContext context, PlcModel plc) { context.Add(new Defect { TimpStartDefect = DateTime.Now, DefectFinalizat = false, PlcModelID = plc.PlcModelID, TimpStopDefect = DateTime.Now, IntervalStationare = new TimeSpan(), MotivStationare = "Start Defect" }); context.SaveChanges(); }
// Chek if brakdown is in progress (check if it is breakdown in progress or is pressed any cause; cannot be pressed any cause without breakdown) public bool IsBreakDownInProgress(PlcModel plc) { foreach (var tag in plc.TagsList) { if (Convert.ToBoolean(tag.Value)) { return(Convert.ToBoolean(tag.Value)); } } return(false); //return Convert.ToBoolean(plc.TagsList.Where(tag => tag.Name == "BreakDownInProgress").ToList().FirstOrDefault().Value); }
// Get MotivStationare from Plc Tag public string GetMotivStationare(PlcService plcService, PlcModel plc) { // Check if plc is not connected and return messege Plc DEconectat try { if (!plcService.IsAvailableIpAdress(plc)) { return("Plc Deconectat"); } // AddedException for GaddaF2, GaddaF4 and DunkePRess: not Get Motiv Stationare if (plc.Name == "GaddaF2" || plc.Name == "GaddaF4" || plc.Name == "PresaDunke") { return("Stationeaza"); } // If Plc Connected return a type of breakdown messege if (Convert.ToBoolean(plc.TagsList.Where(tag => tag.Name == "MechanicalBreakDown").ToList().FirstOrDefault().Value)) { return("Defect mecanic"); } else if (Convert.ToBoolean(plc.TagsList.Where(tag => tag.Name == "ElectricalBreakDown").ToList().FirstOrDefault().Value)) { return("Defect electric"); } else if (Convert.ToBoolean(plc.TagsList.Where(tag => tag.Name == "ProgrammedBreakDown").ToList().FirstOrDefault().Value)) { return("Oprire programata"); } else if (Convert.ToBoolean(plc.TagsList.Where(tag => tag.Name == "TechnologicalBreakDown").ToList().FirstOrDefault().Value)) { return("Oprire tehnologica"); } else if (Convert.ToBoolean(plc.TagsList.Where(tag => tag.Name == "NoCraneReadyBreakDown").ToList().FirstOrDefault().Value)) { return("Lipsa pod rulant / Lipsa material"); } } catch (PlcException exPlc) { Console.WriteLine(String.Format("{0} <=> {1} <=> PlcaName: {2}", DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss"), exPlc.Message, plc.Name)); return("Plc Deconectat"); } catch (NullReferenceException ex) { Console.WriteLine(String.Format("{0} <=> {1} <=> PlcaName: {2}. Din GetMotivStationare", DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss"), ex.Message, plc.Name)); return("Nu s-a apasat cauza"); } // If none of the predefined causes are not set return none was pressed return("Nu s-a apasat cauza"); }
[Route("api/plcController/post")] //added route public IActionResult PostEntry([FromBody] PlcModel entry) //old version which tries to sync //public async Task<IActionResult> PostEntry([FromBody] PlcModel entry) { if (!ModelState.IsValid) //checks model state { return(BadRequest(ModelState)); //no page to redirect to at the moment } _Context.PlcTb.Add(entry); var result = _Context.Add(entry); _Context.SaveChanges(); //Saves entries return(RedirectToPage("insertpagehere")); //no page to redirect to at the moment }
// Is Last Added Defect Finished public bool IsLastDefectFinshed(RaportareDbContext context, PlcModel plc) { Defect defect = null; try { // Get Last defect not finished defect of a Plc, from DbContext defect = context.Defects.Where(p => p.PlcModelID == plc.PlcModelID).Last(); } catch (InvalidOperationException ex) { Console.WriteLine(String.Format("{0} <=> {1} <=> PlcaName: {2}", DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss"), ex.Message, plc.Name)); return(false); } return(defect.DefectFinalizat); }
// Get Last not finished Defect from given Plc public Defect GetLastNotFinishedDefect(RaportareDbContext context, PlcModel plc) { Defect defect = null; try { defect = context.Defects.Where(def => def.PlcModelID == plc.PlcModelID && def.DefectFinalizat == false).ToList().Last(); } catch (InvalidOperationException ex) { Console.WriteLine(String.Format("{0} <=> {1} <=> PlcaName: {2}", DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss"), ex.Message, plc.Name)); return(null); } // Get Last defect not finished defect of a Plc, from DbContext return(defect); }
// Get Last Element by Plc from context public Defect GetLastElementByPlc(RaportareDbContext context, PlcModel plc) { Defect defect = null; try { defect = context.Defects.Include(t => t.PlcModel).Where(def => def.PlcModelID == plc.PlcModelID).ToList().Last(); } catch (InvalidOperationException ex) { Console.WriteLine(String.Format("{0} <=> {1} <=> Din GetLast Element By PlcName: {2}", DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss"), ex.Message, plc.Name)); return(null); } if (defect != null) { return(defect); } return(null); }
// Update Tag List Values of Plc public void RefreshTagValues(PlcModel plc) { // Read Tag values from Plc foreach (TagModel tag in plc.TagsList) { try { var _cancelTasks = new CancellationTokenSource(); var task = Task.Run(() => { if (plc.PlcObject.IsConnected) { //_lastScanTime = DateTime.Now; GetTagValueFromPlc(tag); // Citire semnale Plc //ScanTime = DateTime.Now - _lastScanTime; // Determinare ScanTime Plc } else { return; }; }, _cancelTasks.Token); if (!task.Wait(TimeSpan.FromSeconds(0.2))) { _cancelTasks.Cancel(); // Daca nu mai raspunde in timp util se opreste Task } } catch (PlcException exPlc) { _logger.LogError("{data} {exMessege} IP: {ip}", DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss"), exPlc.Message, plc.Name); } catch (OperationCanceledException ex) { _logger.LogError("{data} {exMessege} IP: {ip}", DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss"), ex.Message, plc.Name); } catch (Exception ex) { _logger.LogError("{data} {exMessege} IP: {ip}", DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss"), ex.Message, plc.Name); } } }
// Try to reconnect Plc in automatic mode if it is available // Disable Plc if PingRequestsFail => nr // return true if it is connected public bool IsConnectedPlc(PlcModel plc) { // If Plc RequestsFail is grater than a nr, disable read tag signals if (IsPingRequestsFailGrateThan(plc, 5)) { return(false); } // Try to reconnect plc if it is available try { if (IsAvailableIpAdress(plc)) { // Check if it is connected if (!plc.PlcObject.IsConnected) { plc.PlcObject.Open(); // if it is not connected => reconnect } if (plc.PlcObject.IsConnected) { return(true); // if is connected return true } return(false); } else { return(false); } } catch (PlcException exPlc) { //Console.WriteLine("{data} {exMessege} IP: {ip}", DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss"), exPlc.Message, plc.Ip); return(false); } catch (Exception ex) { //Console.WriteLine("{data} {exMessege} IP: {ip}", DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss"), ex.Message, plc.Ip); return(false); } }
// Chek if second brakdown is in progress public bool IsBreakDown2InProgress(PlcModel plc) { return(Convert.ToBoolean(plc.TagsList.Where(tag => tag.Name == "BreakDown2InProgress").ToList().FirstOrDefault().Value)); }
// Logic IfBrackDownInProgrss Add Defect, Update It, Add Second Defect public void LogicBrackDowns(RaportareDbContext context, PlcModel plc, PlcService plcService, ReportService reportService) { Defect lastDefect = GetLastElementByPlc(context, plc); // Get Last defect from Plc // If is time of report finalise last defect and send mail (added for report excel file) if (lastDefect != null && lastDefect.DefectFinalizat == false) { //if list is not empty and last defect is not finalised // make report if it is time TODO if (reportService.MakeReport(context, plcService)) // make report to excel { UpdateLastNotFinishedDefect(context, lastDefect); // finished not finalised defect } } // Add PlcViewModel foreach (var plcViewModel in plcService.ListPlcViewModels) { if (lastDefect != null) { if (plcViewModel.PlcModel.Name == plc.Name) { plcViewModel.MapDefect(plcService, lastDefect, plc, context.Defects.Where(def => def.PlcModelID == plc.PlcModelID).ToList()); break; } } } // If is Breakdown in progress and list of defects is empty or last defect is finished Add Defect to list if (IsBreakDownInProgress(plc)) { // if is brackdown if (lastDefect == null) // if list is empty { AddNewDefectForPlc(context, plc); // Add Defect } else if (lastDefect.DefectFinalizat == true) // if list is not empty and last defect is finalised { AddNewDefectForPlc(context, plc); // Add defect } else if (lastDefect.DefectFinalizat == false) // if last defect is not finalised add Motiv Stationare { lastDefect.MotivStationare = GetMotivStationare(plcService, plc); // Add Motiv Stationare to lastDefect when it is pressed the button lastDefect.TimpStopDefect = DateTime.Now; // Add Stop time defect dynamic // Catch overflow error to IntervalStationare try { lastDefect.IntervalStationare = LimitMaxTimeSpan(lastDefect.TimpStartDefect, lastDefect.TimpStopDefect);// Add dynamic interval stationare // Limit the Interval Stationare Max Value } catch (OverflowException ex) { Console.WriteLine(String.Format("{0} <=> {1} <=> PlcaName: {2}", DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss"), ex.Message, lastDefect.PlcModel.Name)); lastDefect.IntervalStationare = new TimeSpan(23, 59, 00); } context.Update(lastDefect); // Update DbContext with motiv stationare } } else // when machine start work again finished defect { if (lastDefect != null && lastDefect.DefectFinalizat == false) //if list is not empty and last defect is not finalised { UpdateLastNotFinishedDefect(context, lastDefect); // finished not finalised defect } } }
// Get New Plc object by PlcModel public Plc GetNewPlcFromPlcModel(PlcModel plcModel) { return(new Plc(plcModel.CpuType, plcModel.Ip, plcModel.Rack, plcModel.Slot)); }