public DefectsController(RaportareDbContext context, DefectService defectService, PlcService plcService, ReportService reportService)
 {
     _context       = context;
     _defectService = defectService;
     _plcService    = plcService;
     _reportService = reportService;
 }
 public TimedService(IServiceProvider services, ILogger <TimedService> logger)
 {
     _logger     = logger;
     Services    = services;                                                        // Get Service Provider
     Scope       = Services.CreateScope();                                          // Create Scope
     _context    = Scope.ServiceProvider.GetRequiredService <RaportareDbContext>(); // Get DbContext
     _plcService = Scope.ServiceProvider.GetRequiredService <PlcService>();         // Get PlcService
     // log
     _logger.LogInformation("{data}<=>{Messege}", DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss"), "A pornit TimedkService din TimedService Constructor");
 }
 // Send report on mail
 public bool SendReportOnMail(RaportareDbContext context)
 {
     if (!string.IsNullOrEmpty(MailList))
     {
         TimeOfReport = ChangeDateKeepTime(TimeOfReport); // Add curent Date to Time Of report Variable, used to Save excel file for the previous day
         MailSubject  = "Raport stationari utilaje ajustaj";
         MailBodyText = string.Format("Buna dimineata. <br>Atasat gasiti raport stationari utilaje ajustaj <br>O zi buna.");
         MailFilePath = SaveExcelFileToDisk(TimeOfReport.AddDays(-1), TimeOfReport, context);
         SendMail(MailList, MailFilePath, MailSubject, MailBodyText);
         return(true);
     }
     return(false);
 }
        // Function make report and send mail if it is time
        public bool MakeReport(RaportareDbContext context, PlcService plcService)
        {
            int nrOfAvailablePlc = plcService.ListPlcs.Where(p => p.IsEnable).Count();

            SetVarIsReportTime(nrOfAvailablePlc); // Set Variable for report tile
            SetNumberOfCheckedPlc();

            if (NumberOfCheckedPlc == nrOfAvailablePlc)
            {
                SendReportOnMail(context); // Send Mail with report
            }
            return(VarIsReportTime);
        }
Example #5
0
 // 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();
 }
 // Update Database Context Tag Values
 public Task UpdateDbContextTagsValue(RaportareDbContext context, List <TagModel> tags)
 {
     foreach (TagModel tag in tags)
     {
         try
         {
             context.Update(tag); // update tag value in DbContext
         }
         catch (InvalidOperationException exEFUpdate)
         {
             _logger.LogError("{data} {exMessege} PlcName: {name}", DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss"), exEFUpdate.Message, tag.PlcModel.Name);
         }
     }
     return(Task.CompletedTask);
 }
Example #7
0
        // Function return List of Dfect By PlcModelId from Database
        public async Task <List <Defect> > GetListOfDefectsByPlcModelIdAsync(RaportareDbContext _context, int PlcModelID)
        {
            List <Defect> raportareDbContext;

            // Show data between dates and selected by Plc
            if (PlcModelID == 0 || PlcModelID == 7)
            {
                raportareDbContext = await _context.Defects.Include(d => d.PlcModel).ToListAsync();
            }
            else
            {
                raportareDbContext = await _context.Defects.Include(d => d.PlcModel).Where(item => item.PlcModelID == PlcModelID).ToListAsync();
            }
            return(raportareDbContext);
        }
        // Initiate List of Plcs from DbContext
        public async Task InitializeListOfPlcAsync(RaportareDbContext context)
        {
            ListPlcs.Clear();
            IEnumerable <PlcModel> listPlcs = await context.Plcs.ToListAsync();

            IEnumerable <TagModel> tagList = await context.Tags.ToListAsync();

            // Add list of Plcs(with Plc object and TagList) from DbContext To PlcService PLCList
            foreach (var plcModel in listPlcs)
            {
                plcModel.PlcObject        = GetNewPlcFromPlcModel(plcModel); // Create Plc object
                plcModel.TagsList         = tagList.Where(t => t.PlcModelID == plcModel.PlcModelID).ToList();
                plcModel.PingRequestsFail = 0;                               // Initiate with 0 ping requests fail
                ListPlcs.Add(plcModel);
            }
        }
Example #9
0
        // 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);
        }
Example #10
0
        // 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);
        }
        // Functie creare fisier excel cu defecte pentru ziua precedenta
        // Functie exportare data to excel file and save to disk
        // returneaza FilePath
        public string SaveExcelFileToDisk(DateTime dataFrom, DateTime dataTo, RaportareDbContext _context)
        {
            List <Defect> listaDeAfisat = new List <Defect>(); // List to be writen in excel
            //return Content(dataFrom + "<==>" + dataTo);
            // Collect Data between given dates
            IEnumerable <Defect> listaSql = _context.Defects.Where(model => model.TimpStartDefect.CompareTo(dataFrom) >= 0 && model.TimpStartDefect.CompareTo(dataTo) <= 0);

            // Get List grouped by PLC name
            var listaExcel = GetListGroupBySingleProperty(listaSql);

            // Create Excel file
            using (var pck = new ExcelPackage())
            {
                ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Lista defecte");
                ws.Cells["A1:Z1"].Style.Font.Bold = true;

                ws.Cells["A1"].Value = "Denumire utilaj";
                ws.Cells["B1"].Value = "Timp Start Defect";
                ws.Cells["C1"].Value = "Timp Stop Defect";
                ws.Cells["D1"].Value = "Motiv Stationare";
                ws.Cells["E1"].Value = "Interval Stationare";

                int rowStart = 2;
                foreach (var elem in listaExcel)
                {
                    ws.Cells[string.Format("A{0}", rowStart)].Value = elem.PlcModel.Name;
                    ws.Cells[string.Format("B{0}", rowStart)].Value = elem.TimpStartDefect.ToString("dd.MM.yyyy HH:mm");
                    ws.Cells[string.Format("C{0}", rowStart)].Value = elem.TimpStopDefect.ToString("dd.MM.yyyy HH:mm");
                    ws.Cells[string.Format("D{0}", rowStart)].Value = elem.MotivStationare;
                    ws.Cells[string.Format("E{0}", rowStart)].Value = elem.IntervalStationare.ToString("hh\\:mm\\:ss");
                    rowStart++;
                }

                ws.Cells["A:AZ"].AutoFitColumns();

                //Write the file to the disk
                string   excelName = "RaportStationariAjustaj.xlsx";
                string   filePath  = string.Format("{0}/{1}", CreareFolderRaportare(), excelName);
                FileInfo fi        = new FileInfo(filePath);
                pck.SaveAs(fi);
                //pck.Save();
                return(filePath);
            }
        }
Example #12
0
        // 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);
        }
Example #13
0
        // Update last not finished Defect from given Plc to finished defect
        public void UpdateLastNotFinishedDefect(RaportareDbContext context, Defect defect)
        {
            // Add MotivStationare, TimpStop Defect, interval Stationare and Defect finalizat
            defect.TimpStopDefect = DateTime.Now;
            // Catch overflow error to IntervalStationare
            try
            {
                // Limit the Interval Stationare Max Value
                defect.IntervalStationare = LimitMaxTimeSpan(defect.TimpStartDefect, defect.TimpStopDefect);
            }
            catch (OverflowException ex)
            {
                defect.IntervalStationare = new TimeSpan(23, 59, 00);
                Console.WriteLine(String.Format("{0} <=> {1} <=> PlcaName: {2}", DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss"), ex.Message, defect.PlcModel.Name));
            }

            defect.DefectFinalizat = true;
            // Update Database
            context.Update(defect);
            context.SaveChanges();
        }
Example #14
0
 public ProbaModelsController(RaportareDbContext context)
 {
     _context = context;
 }
 public IndexModelsEltiController(RaportareDbContext context)
 {
     _context = context;
 }
 // Constructor
 public PlcServiceController(RaportareDbContext context, ILogger <PlcsController> logger, PlcService plcService)
 {
     _logger     = logger;
     _context    = context;
     _plcService = plcService;
 }
Example #17
0
 public HomeController(RaportareDbContext context)
 {
     _context = context;
 }
 public TrackingUsersController(RaportareDbContext context)
 {
     _context = context;
 }
 public HomeController(RaportareDbContext context, IHostedService backGroundService)
 {
     _context           = context;
     _backGroundService = backGroundService as BackgroundService;
 }
Example #20
0
 public IndexModelsGaddaF4(RaportareDbContext context)
 {
     _context = context;
 }
Example #21
0
        // 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
                }
            }
        }
 // Constructor
 public PlcsController(RaportareDbContext context, ILogger <PlcsController> logger)
 {
     _context = context;
     _logger  = logger;
 }
Example #23
0
 public IndexModelsCuptorController(RaportareDbContext context)
 {
     _context = context;
 }
 public TagsController(RaportareDbContext context)
 {
     _context = context;
 }