public static CostEstimationResult ProcessRequest(CostEstimationRequest request)
        {
            CostEstimationResult result = new CostEstimationResult()
            {
                request = request,
                // Get copy of BOM by serializing and deserializing
                result_bom = MfgBom.Bom.MfgBom.Deserialize(request.bom.Serialize())                
            };

            
            // Update the Octopart data for each one
            Random rnd = new Random();
            foreach (var part in result.result_bom.Parts)
            {
                int numTries = 10;
                int i = 0;
                while (i++ < numTries)
                {
                    try
                    {
                        part.QueryOctopartData();

                        // Break the while loop
                        break;
                    }
                    catch (MfgBom.OctoPart.OctopartQueryRateException)
                    {
                        // Cool our heels a little
                        int interval = rnd.Next(3000, 5000);
                        System.Threading.Thread.Sleep(interval);
                    }
                    catch (MfgBom.OctoPart.OctopartQueryServerException ex)
                    {
                        throw ex;
                    }
                    if (i >= numTries)
                    {
                        throw new OctoPart.OctopartQueryException("Exceeded 10 tries");
                    }
                }
                
                System.Threading.Thread.Sleep(500);
            }


            // Choose a supplier for each
            foreach (var p in result.result_bom
                                    .Parts
                                    .Where(p => String.IsNullOrWhiteSpace(p.octopart_mpn) == false))
            {
                p.SelectSupplier(request.design_quantity);
            }
            
            // Try to calculate the cost
            result.per_design_parts_cost = result.result_bom
                                                 .Parts
                                                 .Sum(p => (p.SelectedSupplierPartCostPerUnit.HasValue)
                                                           ? p.SelectedSupplierPartCostPerUnit.Value * p.quantity
                                                           : 0);


            return result;
        }
示例#2
0
        public static CostEstimationResult ProcessRequest(CostEstimationRequest request)
        {
            CostEstimationResult result = new CostEstimationResult()
            {
                request = request,
                // Get copy of BOM by serializing and deserializing
                result_bom = MfgBom.Bom.MfgBom.Deserialize(request.bom.Serialize())
            };


            // Update the Octopart data for each one
            Random rnd = new Random();

            foreach (var part in result.result_bom.Parts)
            {
                int numTries = 10;
                int i        = 0;
                while (i++ < numTries)
                {
                    try
                    {
                        part.QueryOctopartData();

                        // Break the while loop
                        break;
                    }
                    catch (MfgBom.OctoPart.OctopartQueryRateException)
                    {
                        // Cool our heels a little
                        int interval = rnd.Next(3000, 5000);
                        System.Threading.Thread.Sleep(interval);
                    }
                    catch (MfgBom.OctoPart.OctopartQueryServerException ex)
                    {
                        throw ex;
                    }
                    if (i >= numTries)
                    {
                        throw new OctoPart.OctopartQueryException("Exceeded 10 tries");
                    }
                }

                System.Threading.Thread.Sleep(500);
            }


            // Choose a supplier for each
            foreach (var p in result.result_bom
                     .Parts
                     .Where(p => String.IsNullOrWhiteSpace(p.octopart_mpn) == false))
            {
                p.SelectSupplier(request.design_quantity);
            }

            // Try to calculate the cost
            result.per_design_parts_cost = result.result_bom
                                           .Parts
                                           .Sum(p => (p.SelectedSupplierPartCostPerUnit.HasValue)
                                                           ? p.SelectedSupplierPartCostPerUnit.Value * p.quantity
                                                           : 0);


            return(result);
        }