private static int ParseArgs(string[] args, out MfgBom.CostEstimation.CostEstimationRequest Request, out AVM.DDP.MetaTBManifest Manifest, out String pathResultsFolder) { Request = null; Manifest = null; pathResultsFolder = ""; var pathManifest = args[0]; if (false == File.Exists(pathManifest)) { Console.Error.WriteLine("No file found at location: {0}", pathManifest); return(-2); } pathResultsFolder = Path.GetDirectoryName(pathManifest); Manifest = AVM.DDP.MetaTBManifest.OpenForUpdate(pathResultsFolder); if (null == Manifest) { Console.Error.WriteLine("Manifest is empty or didn't load correctly: {0}", pathManifest); return(-3); } var artifactRequest = Manifest.Artifacts.FirstOrDefault(a => a.Tag == "CyPhy2MfgBom::CostEstimationRequest"); if (null == artifactRequest) { Console.Error.WriteLine("Didn't find any artifacts in the manifest with the tag \"CyPhy2MfgBom::CostEstimationRequest\": {0}", pathManifest); return(-4); } var pathRequest = Path.Combine(pathResultsFolder, artifactRequest.Location); if (false == File.Exists(pathRequest)) { Console.Error.WriteLine("Couldn't find the file tagged with \"CyPhy2MfgBom::CostEstimationRequest\" at given path: {0}", pathRequest); return(-5); } String jsonRequest; try { jsonRequest = File.ReadAllText(pathRequest); Request = MfgBom.CostEstimation.CostEstimationRequest.Deserialize(jsonRequest); } catch (Exception ex) { Console.Error.WriteLine("Exception loading the cost estimation request at \"{0}\": {1}", pathRequest, ex); return(-6); } return(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; }
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); }