Example #1
0
        public async static Task ProcessNetworkCoins(int NetworkNumber, bool ChangeANS = true)
        {
            IFileSystem FS = FileSystem;

            FileSystem.LoadFileSystem();
            FileSystem.DetectPreProcessing();

            var predetectCoins = FS.LoadFolderCoins(FS.PreDetectFolder);

            predetectCoins = (from x in predetectCoins
                              where x.nn == NetworkNumber
                              select x).ToList();

            IFileSystem.predetectCoins = predetectCoins;

            #region Check Existing coins and skip them

            IEnumerable <CloudCoin> bankCoins     = IFileSystem.bankCoins;
            IEnumerable <CloudCoin> frackedCoins1 = IFileSystem.frackedCoins;

            var bCoins = bankCoins.ToList();
            bCoins.AddRange(frackedCoins1);
            //bankCoins.ToList().AddRange(frackedCoins1);

            var totalBankCoins = bCoins;

            var snList = (from x in totalBankCoins
                          where x.nn == NetworkNumber
                          select x.sn).ToList();

            var newCoins      = from x in predetectCoins where !snList.Contains(x.sn) select x;
            var existingCoins = from x in predetectCoins where snList.Contains(x.sn) select x;

            foreach (var coin in existingCoins)
            {
                updateLog("Found coin SN:" + coin.sn + " in folders. Skipping detection of the coin SN:" + coin.sn);
                FS.MoveFile(FS.PreDetectFolder + coin.FileName + ".stack", FS.TrashFolder + coin.FileName + ".stack", IFileSystem.FileMoveOptions.Replace);
            }

            predetectCoins = newCoins.ToList();

            #endregion

            RAIDA raida = (from x in networks
                           where x.NetworkNumber == NetworkNumber
                           select x).FirstOrDefault();
            if (raida == null)
            {
                return;
            }
            // Process Coins in Lots of 200. Can be changed from Config File
            int LotCount = predetectCoins.Count() / Config.MultiDetectLoad;
            if (predetectCoins.Count() % Config.MultiDetectLoad > 0)
            {
                LotCount++;
            }
            ProgressChangedEventArgs pge = new ProgressChangedEventArgs();

            int CoinCount      = 0;
            int totalCoinCount = predetectCoins.Count();
            for (int i = 0; i < LotCount; i++)
            {
                //Pick up 200 Coins and send them to RAIDA
                var coins = predetectCoins.Skip(i * Config.MultiDetectLoad).Take(Config.MultiDetectLoad);
                try
                {
                    raida.coins = coins;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
                if (i == LotCount - 1)
                {
                    updateLog("\tDetecting Coins " + (i * CloudCoinCore.Config.MultiDetectLoad + 1) +
                              " to " + totalCoinCount);
                }
                else
                {
                    updateLog("\tDetecting Coins " + (i * CloudCoinCore.Config.MultiDetectLoad + 1) +
                              " to " + (i + 1) * CloudCoinCore.Config.MultiDetectLoad);
                }
                var tasks = raida.GetMultiDetectTasks(coins.ToArray(), Config.milliSecondsToTimeOut, ChangeANS);
                try
                {
                    string requestFileName = Utils.RandomString(16).ToLower() + DateTime.Now.ToString("yyyyMMddHHmmss") + ".stack";
                    // Write Request To file before detect
                    FS.WriteCoinsToFile(coins, FS.RequestsFolder + requestFileName);
                    await Task.WhenAll(tasks.AsParallel().Select(async task => await task()));

                    int j = 0;
                    foreach (var coin in coins)
                    {
                        coin.pown = "";
                        for (int k = 0; k < CloudCoinCore.Config.NodeCount; k++)
                        {
                            coin.response[k] = raida.nodes[k].MultiResponse.responses[j];
                            coin.pown       += coin.response[k].outcome.Substring(0, 1);
                        }
                        int countp = coin.response.Where(x => x.outcome == "pass").Count();
                        int countf = coin.response.Where(x => x.outcome == "fail").Count();
                        coin.PassCount = countp;
                        coin.FailCount = countf;
                        CoinCount++;


                        //    MainWindow.UpdateCELog("No. " + CoinCount + ". Coin Deteced. S. No. - " + coin.sn + ". Pass Count - " + coin.PassCount + ". Fail Count  - " + coin.FailCount + ". Result - " + coin.DetectionResult + "." + coin.pown);


                        pge.MinorProgress = (CoinCount) * 100 / totalCoinCount;
                        Debug.WriteLine("Minor Progress- " + pge.MinorProgress);
                        raida.OnProgressChanged(pge);
                        coin.doPostProcessing();
                        j++;
                    }
                    pge.MinorProgress = (CoinCount - 1) * 100 / totalCoinCount;
                    Debug.WriteLine("Minor Progress- " + pge.MinorProgress);
                    raida.OnProgressChanged(pge);
                    FS.WriteCoin(coins, FS.DetectedFolder, true);
                    FS.RemoveCoinsByFileName(coins, FS.PreDetectFolder);

                    // MainWindow.UpdateCELog(pge.MinorProgress + " % of Coins on Network " + NetworkNumber + " processed.");
                    //FS.WriteCoin(coins, FS.DetectedFolder);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }
            pge.MinorProgress = 100;
            Debug.WriteLine("Minor Progress- " + pge.MinorProgress);
            raida.OnProgressChanged(pge);
            var detectedCoins = FS.LoadFolderCoins(FS.DetectedFolder);
            //detectedCoins.ForEach(x => x.pown= "ppppppppppppppppppppppppp");

            // Apply Sort to Folder to all detected coins at once.
            updateLog("Starting Grading Coins..");
            //detectedCoins.ForEach(x => x.doPostProcessing());
            detectedCoins.ForEach(x => x.SortToFolder());
            updateLog("Grading Coins Completed.");

            // detectedCoins.ForEach(x => x.folder = FS.SuspectFolder);


            var passedCoins = (from x in detectedCoins
                               where x.folder == FS.BankFolder
                               select x).ToList();

            var frackedCoins = (from x in detectedCoins
                                where x.folder == FS.FrackedFolder
                                select x).ToList();

            var failedCoins = (from x in detectedCoins
                               where x.folder == FS.CounterfeitFolder
                               select x).ToList();
            var lostCoins = (from x in detectedCoins
                             where x.folder == FS.LostFolder
                             select x).ToList();
            var suspectCoins = (from x in detectedCoins
                                where x.folder == FS.SuspectFolder
                                select x).ToList();
            var dangerousCoins = (from x in detectedCoins
                                  where x.folder == FS.DangerousFolder
                                  select x).ToList();



            Debug.WriteLine("Total Passed Coins - " + (passedCoins.Count() + frackedCoins.Count()));
            Debug.WriteLine("Total Failed Coins - " + failedCoins.Count());

            // Move Coins to their respective folders after sort
            FS.MoveCoins(passedCoins, FS.DetectedFolder, FS.BankFolder);
            FS.MoveCoins(frackedCoins, FS.DetectedFolder, FS.FrackedFolder);

            if (failedCoins.Count() > 0)
            {
                FS.WriteCoin(failedCoins, FS.CounterfeitFolder, false, true);
            }
            FS.MoveCoinsByFileName(lostCoins, FS.DetectedFolder, FS.LostFolder);
            FS.MoveCoinsByFileName(suspectCoins, FS.DetectedFolder, FS.SuspectFolder);

            // Clean up Detected Folder
            FS.RemoveCoinsByFileName(failedCoins, FS.DetectedFolder);
            FS.RemoveCoinsByFileName(lostCoins, FS.DetectedFolder);
            FS.RemoveCoinsByFileName(suspectCoins, FS.DetectedFolder);

            FS.MoveImportedFiles();

            //after = DateTime.Now;
            //ts = after.Subtract(before);

            //Debug.WriteLine("Detection Completed in - " + ts.TotalMilliseconds / 1000);
            //updateLog("Detection Completed in - " + ts.TotalMilliseconds / 1000);

            updateLog("Detection and Import of the CloudCoins completed.");
            updateLog("Coin Detection finished.");
            updateLog("Total Passed Coins - " + (passedCoins.Count() + frackedCoins.Count()) + "");
            updateLog("Total Counterfeit Coins - " + failedCoins.Count() + "");
            updateLog("Total Lost Coins - " + lostCoins.Count() + "");
            updateLog("Total Suspect Coins - " + suspectCoins.Count() + "");
            updateLog("Total Dangerous Coins - " + dangerousCoins.Count() + "");
            updateLog("Total Skipped Coins - " + existingCoins.Count() + "");

            pge.MinorProgress = 100;
            Debug.WriteLine("Minor Progress- " + pge.MinorProgress);
        }