Exemple #1
0
        public IHttpActionResult PostNewBp([FromBody] BpViewModel bp)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Invalid data."));
            }

            using (var ctx = new EMSEntities())
            {
                int _trnno;
                //int _sr = 1;
                //db.Users.OrderByDescending(u => u.UserId).FirstOrDefault();
                if (bp.TRNNO == 0)
                {
                    _trnno = Convert.ToInt32(ctx.BPs.OrderByDescending(t => t.TRNNO).FirstOrDefault().TRNNO);
                    _trnno = _trnno + 1;
                    //_trnno = Convert.ToInt32(ctx.EMs.OrderByDescending(t => t.TRNNO).First().ToString());
                }
                else
                {
                    _trnno = Convert.ToInt32(bp.TRNNO) + 1;
                }
                // int totalConunt= ctx.BPs.Count<BP>();
                bp.TRNNO = _trnno;
                ctx.BPs.Add(new BP()
                {
                    TRNNO  = bp.TRNNO,
                    BPNAME = bp.BPNAME,
                    STATUS = bp.STATUS
                });
                try {
                    ctx.SaveChanges();
                }
                catch (DbEntityValidationException ex)
                {
                    // Retrieve the error messages as a list of strings.
                    var errorMessages = ex.EntityValidationErrors
                                        .SelectMany(x => x.ValidationErrors)
                                        .Select(x => x.ErrorMessage);

                    // Join the list to a single string.
                    var fullErrorMessage = string.Join("; ", errorMessages);

                    // Combine the original exception message with the new one.
                    var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage);

                    // Throw a new DbEntityValidationException with the improved exception message.
                    throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors);
                }
            }

            return(Ok());
        }
Exemple #2
0
        public IHttpActionResult Put(BpViewModel bp)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Not a valid model"));
            }

            using (var ctx = new EMSEntities())
            {
                try
                {
                    var existingBp = ctx.BPs.Where(s => s.TRNNO == bp.TRNNO)
                                     .FirstOrDefault <BP>();

                    if (existingBp != null)
                    {
                        existingBp.BPNAME = bp.BPNAME;
                        existingBp.STATUS = bp.STATUS;

                        ctx.SaveChanges();
                    }
                    else
                    {
                        return(NotFound());
                    }
                }
                catch (DbEntityValidationException e)
                {
                    foreach (var eve in e.EntityValidationErrors)
                    {
                        Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                          eve.Entry.Entity.GetType().Name, eve.Entry.State);
                        foreach (var ve in eve.ValidationErrors)
                        {
                            Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                              ve.PropertyName, ve.ErrorMessage);
                        }
                    }
                    throw;
                }
            }

            return(Ok());
        }
Exemple #3
0
        public IHttpActionResult GetBpById(int id)
        {
            BpViewModel bps = null;

            using (var ctx = new EMSEntities())
            {
                bps = ctx.BPs
                      .Where(s => s.TRNNO == id)
                      .Select(s => new BpViewModel()
                {
                    TRNNO  = s.TRNNO,
                    BPNAME = s.BPNAME,
                    STATUS = s.STATUS
                }).FirstOrDefault <BpViewModel>();
            }

            if (bps == null)
            {
                return(NotFound());
            }

            return(Ok(bps));
        }
        public async Task ScanLabelAsync(List <int> ids, BpViewModel bpViewModel, ScanSide side, CancellationToken cancellationToken)
        {
            try
            {
                OcrAsyncChecker.CheckThread(OcrAsyncChecker.ScanLabelAsyncChecker);

                var finder = new Finder();

                float rotation = side == ScanSide.Left ? (float)29.7 : (float)-29.7;

                var bpVm       = new Dictionary <int, HeroSelectorViewModel>();
                var checkedDic = new Dictionary <int, bool>();
                foreach (var id in ids)
                {
                    var vm = bpViewModel.HeroSelectorViewModels.First(v => v.Id == id);
                    if (vm == null)
                    {
                        return;
                    }

                    bpVm[id] = vm;
                }

                for (var i = 0; i < ids.Count; ++i)
                {
                    checkedDic[i] = false;
                }

                var logUtil = new LogUtil(@".\logPick" + string.Join("&", ids) + ".txt");
                logUtil.Log("PickChecked");

                int  bpScreenFail = 0;
                bool warned       = false;
                bool awaitFlag    = false;
                while (checkedDic.Any(c => !c.Value))
                {
                    var sb        = new StringBuilder();
                    var sbConfirm = new StringBuilder();
                    var i         = checkedDic.FirstOrDefault(l => !l.Value).Key;

                    if (awaitFlag)
                    {
                        await Task.Delay(1000, cancellationToken).ConfigureAwait(false);

                        awaitFlag = false;
                    }

                    if (checkedDic.ContainsKey(i) && checkedDic[i])
                    {
                        continue;
                    }

                    if (bpVm[ids[i]].Selected)
                    {
                        checkedDic[i] = true;
                        continue;
                    }

                    if (cancellationToken.IsCancellationRequested)
                    {
                        return;
                    }

                    if (SuspendScanning)
                    {
                        await Task.Delay(1000).ConfigureAwait(false);

                        continue;
                    }

                    logUtil.Log("Starting detect quit");
                    var processedResult = ProcessedResult.Fail;

                    int realPositionId = ids[i];

                    // first attempt
                    lock (ImageProcessingHelper.GDILock)
                    {
                        using (var bitmap = new ImageUtils().CaptureScreen())
                        {
                            if (!warned && bpViewModel.BpStatus.CurrentStep < 11 &&
                                StageFinder.ProcessStageInfo(bitmap).Step == -1)
                            {
                                bpScreenFail++;
                                if (bpScreenFail == 5)
                                {
                                    warned = true;
                                    bpViewModel.WarnNotInBp();
                                }
                            }
                            else
                            {
                                bpScreenFail = 0;
                            }

                            if (CheckIsOverlap(side, logUtil, bitmap))
                            {
                                awaitFlag = true;
                                continue;
                            }

                            sb.Clear();
                            sbConfirm.Clear();

                            foreach (var scanSideId in IdList[side].Where(id => !ProcessedPositions.ContainsKey(id)))
                            {
                                using (var bitMap = finder.AddNewTemplateBitmap(scanSideId, bitmap))
                                {
                                    logUtil.Log("Capture Complete " + scanSideId);
                                    processedResult = _recognizer.Recognize(bitMap, rotation, sb,
                                                                            _trustableThreashold);
                                    if (processedResult != ProcessedResult.Fail)
                                    {
                                        realPositionId = scanSideId;
                                        break;
                                    }
                                }
                            }
                        }
                    }

                    logUtil.Log("Checked " + ids[i] + " (" + sb + ")");

                    if (sb.ToString() == _recognizer.PickingText || string.IsNullOrEmpty(sb.ToString()) || processedResult == ProcessedResult.Fail)
                    {
                        continue;
                    }

                    if (cancellationToken.IsCancellationRequested)
                    {
                        return;
                    }

                    if (SuspendScanning)
                    {
                        logUtil.Log("SuspendScanning delay 1000 " + ids[i]);
                        await Task.Delay(1000).ConfigureAwait(false);

                        continue;
                    }

                    // second attempt
                    FilePath tempscreenshotPath = null;
                    if (processedResult != ProcessedResult.Trustable)
                    {
                        logUtil.Log("Delay 500 " + ids[i]);
                        await Task.Delay(500).ConfigureAwait(false);

                        if (cancellationToken.IsCancellationRequested)
                        {
                            return;
                        }
                        if (SuspendScanning)
                        {
                            await Task.Delay(1000).ConfigureAwait(false);

                            continue;
                        }

                        lock (ImageProcessingHelper.GDILock)
                        {
                            tempscreenshotPath = finder.CaptureScreen();
                            using (var bitMap = finder.AddNewTemplateBitmap(realPositionId, tempscreenshotPath))
                            {
                                logUtil.Log("Capture Complete " + realPositionId);
                                _recognizer.Recognize(bitMap, rotation, sbConfirm,
                                                      _trustableThreashold);
                            }
                        }
                    }

                    logUtil.Log("Second checked " + ids[i] + " (" + sbConfirm.ToString() + ")");

                    if (SuspendScanning)
                    {
                        logUtil.Log("SuspendScanning delay 1000 " + ids[i]);
                        await Task.Delay(1000).ConfigureAwait(false);

                        continue;
                    }

                    if (bpVm[ids[i]].Selected)
                    {
                        logUtil.Log("Vm already selected delay 1000 " + ids[i]);
                        checkedDic[i] = true;
                        continue;
                    }

                    if (processedResult == ProcessedResult.Trustable)
                    {
                        bpScreenFail = 0;
                    }

                    if ((processedResult == ProcessedResult.Trustable || sb.ToString() == sbConfirm.ToString()) &&
                        !cancellationToken.IsCancellationRequested && CheckIfInFocus())
                    {
                        bpScreenFail = 0;
                        tempscreenshotPath?.DeleteIfExists();
                        var text  = sb.ToString();
                        var index = ids[checkedDic.First(l => !l.Value).Key];

                        if (ids.Contains(realPositionId) && !checkedDic[ids.IndexOf(realPositionId)])
                        {
                            index = realPositionId;
                        }

                        Execute.OnUIThread(() => { bpViewModel.ShowHeroSelector(index, text); });
                        ProcessedPositions[realPositionId] = true;
                        checkedDic[ids.IndexOf(index)]     = true;
                        logUtil.Log("Confirmed " + index + " " + text);
                    }
                }
                logUtil.Flush();
            }
            catch (Exception e)
            {
                File.WriteAllText(@".\error" + string.Join("&", ids) + ".txt", e.Message + e.StackTrace + e);
                throw;
            }
            finally
            {
                OcrAsyncChecker.CleanThread(OcrAsyncChecker.ScanLabelAsyncChecker);
            }
        }