private void gridViewPBIScans_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e) { for (var i = e.RowIndex; i < e.RowCount; i++) { BaleEntity b = (BaleEntity)gridViewPBIScans.Rows[i].DataBoundItem; if (b.OutOfSequence) { foreach (DataGridViewCell c in gridViewPBIScans.Rows[i].Cells) { c.Style.BackColor = Color.Pink; } } } }
public static BaleEntityData BaleEntityToData(BaleEntity entity) { var data = new BaleEntityData() { BaleId = entity.BaleId, BaleNumber = entity.BaleNumber, AccountId = entity.AccountId, DockId = entity.DockId, LoadId = entity.LoadId, WarehouseWeight = entity.WarehouseWeight, InitWeight = entity.InitWeight, FinalWeight = entity.FinalWeight, RecycleTypeId = entity.RecycleTypeId, Notes = entity.Notes, Property = entity.Account.Name, Dock = entity.Dock.DockName, RecycleType = entity.RecycleType.Name }; return(data); }
private void btnRun_Click(object sender, EventArgs e) { decimal turnOutRatio = 0.00M; decimal overragePercent = 0.00M; if (string.IsNullOrWhiteSpace(txtTurnOutMultiplier.Text) || !decimal.TryParse(txtTurnOutMultiplier.Text, out turnOutRatio) || turnOutRatio < 0.00M || turnOutRatio > 1.00M) { MessageBox.Show("Please enter a valid turn out as a decimal between 0 and 1."); return; } if (string.IsNullOrWhiteSpace(txtOverrageThreshold.Text) || !decimal.TryParse(txtOverrageThreshold.Text, out overragePercent) || overragePercent < 0.00M || overragePercent > 100.00M) { MessageBox.Show("Please enter a valid overrage percentage as a decimal between 0 and 100."); return; } List <ModuleEntity> modules = new List <ModuleEntity>(); List <BaleEntity> bales = new List <BaleEntity>(); int startModuleIndex = int.MaxValue; int endModuleIndex = -1; int startBaleIndex = int.MaxValue; //find index of first and last selected module rows foreach (DataGridViewRow row in gridViewModules.SelectedRows) { if (row.Index < startModuleIndex) { startModuleIndex = row.Index; } if (row.Index > endModuleIndex) { endModuleIndex = row.Index; } } //get list of modules we are going to accumulate bale weights for for (int i = startModuleIndex; i <= endModuleIndex; i++) { ModuleEntity m = (ModuleEntity)gridViewModules.Rows[i].DataBoundItem; m.Bales.Clear(); // need to clear bales because we will be re-assigning modules.Add(m); } //get index of first bale to use as starting point for accumulation foreach (DataGridViewRow row in gridViewPBIScans.SelectedRows) { if (row.Index < startBaleIndex) { startBaleIndex = row.Index; } } var firstModule = modules.First(); var lastModule = modules.Last(); //get list of bales we are working with for (var i = startBaleIndex; i < gridViewPBIScans.Rows.Count; i++) { BaleEntity b = (BaleEntity)gridViewPBIScans.Rows[i].DataBoundItem; bales.Add(b); } var firstBale = bales.First(); if (MessageBox.Show("Accumulation will start with module " + firstModule.Name + " and end with module " + lastModule.Name + ". Lint weight accumulation will begin with bale " + firstBale.PbiNumber + " using a turnout ratio of " + turnOutRatio.ToString() + ". Would you like to continue? Any previous mappings for this data set will be overwritten.", "Continue?", MessageBoxButtons.YesNo) == DialogResult.Yes) { BusyMessage.Show("Step 1 - Calculating estimated load and module lint turn out.", this.FindForm()); Task.Run(() => { try { using (IUnitOfWork uow = UnitOfWorkFactory.CreateUnitOfWork()) { uow.ModuleRepository.DisableChangeTracking(); int moduleCount = 1; foreach (var m in modules) { m.GinLoad.LintWeight = m.GinLoad.NetWeight * turnOutRatio; if (m.HIDModuleWeightLBS.HasValue) { m.NetSeedCottonWeight = m.HIDModuleWeightLBS.Value; } else if (m.DiameterApproximatedWeight.HasValue) { m.NetSeedCottonWeight = m.DiameterApproximatedWeight.Value; } else { m.NetSeedCottonWeight = m.LoadAvgModuleWeight.Value; } m.LintWeight = m.NetSeedCottonWeight * turnOutRatio; uow.GinLoadRepository.QuickUpdate(m.GinLoad, false); uow.ModuleRepository.QuickUpdate(m, false); if (moduleCount % 25 == 0) { BusyMessage.UpdateMessage("Step 1 - Calculating lint turn out for module " + moduleCount.ToString() + " of " + modules.Count().ToString()); } moduleCount++; } BusyMessage.UpdateMessage("Step 1 - Saving lint weight estimates."); uow.SaveChanges(); } BusyMessage.UpdateMessage("Step 2 - Accumulating bale lint weights"); using (IUnitOfWork uow = UnitOfWorkFactory.CreateUnitOfWork()) { uow.ModuleRepository.DisableChangeTracking(); uow.BalesRepository.DisableChangeTracking(); decimal overrage = 0.00M; decimal carryOver = 0.00M; int currentBaleIndex = 0; decimal accumulatedLintWeight = 0.00M; bool startNewAccumulation = true; for (int i = 0; i < modules.Count(); i++) { if (i % 25 == 0) { BusyMessage.UpdateMessage("Step 2 - Accumulating for module " + (i + 1).ToString() + " of " + modules.Count().ToString()); } var m = modules[i]; startNewAccumulation = false; while (!startNewAccumulation && currentBaleIndex < bales.Count()) { var b = bales[currentBaleIndex]; Logging.Logger.Log("INFO", "Accumulating weight for bale: " + b.PbiNumber); b.OverrageThreshold = overragePercent; b.LintTurnout = turnOutRatio; if (accumulatedLintWeight + bales[currentBaleIndex].NetWeight + carryOver <= m.LintWeight) { Logging.Logger.Log("INFO", "Bale weight under module weight"); //map bale to current module/gin load accumulatedLintWeight += b.NetWeight + carryOver; b.ModuleId = m.Id; b.Module = null; b.ModuleSerialNumber = m.Name; b.GinLoadId = m.GinLoadId; b.GinTicketLoadNumber = m.GinLoad.GinTagLoadNumber; b.AccumWeight = accumulatedLintWeight; b.OverrageAdjustment = carryOver; b.LintTurnout = turnOutRatio; carryOver = 0.00M; uow.BalesRepository.QuickUpdate(b, true); } else { startNewAccumulation = true; overrage = accumulatedLintWeight + b.NetWeight - m.LintWeight.Value; accumulatedLintWeight += b.NetWeight + carryOver; //if > overrage % of weight belongs to next module map to next and subtract off portion belonging to current if ((overrage / b.NetWeight) > (1.00M - (overragePercent / 100.00M))) { Logging.Logger.Log("INFO", "Bale will be assigned to next module"); int nextModuleIndex = i + 1; if (nextModuleIndex < modules.Count()) { Logging.Logger.Log("INFO", "Bale ModuleID: " + b.ModuleId); b.ModuleId = modules[nextModuleIndex].Id; b.Module = null; b.ModuleSerialNumber = modules[nextModuleIndex].Name; b.GinLoadId = modules[nextModuleIndex].GinLoadId; b.GinTicketLoadNumber = modules[nextModuleIndex].GinLoad.GinTagLoadNumber; //adjust accumulation for next module by subtracting off weight from bale //applied to current module carryOver = 0.00M - (b.NetWeight - overrage); accumulatedLintWeight = b.NetWeight + carryOver; b.AccumWeight = accumulatedLintWeight; b.OverrageAdjustment = carryOver; uow.BalesRepository.QuickUpdate(b, true); carryOver = 0; } } else //bale belongs to current module { Logging.Logger.Log("INFO", "Bale assigned to current module"); b.ModuleId = m.Id; b.Module = null; b.ModuleSerialNumber = m.Name; b.GinLoadId = m.GinLoadId; b.GinTicketLoadNumber = m.GinLoad.GinTagLoadNumber; b.AccumWeight = accumulatedLintWeight; b.OverrageAdjustment = carryOver; uow.BalesRepository.QuickUpdate(b, true); carryOver = overrage; accumulatedLintWeight = 0.00M; } } if (b.Module != null && b.ModuleId != b.Module.Id) { Logging.Logger.Log("INFO", "MISMATCH IDS"); } currentBaleIndex++; } if (i % 25 == 0) //save every 25 modules { uow.SaveChanges(); } } uow.SaveChanges(); } BusyMessage.Close(); } catch (Exception exc) { BusyMessage.Close(); this.Invoke((MethodInvoker) delegate { MessageBox.Show("An exception occurred: " + exc.Message); }); Logging.Logger.Log(exc); } }); } }
private static void createFeederScans() { //create feeder scan for each load DateTime currentModuleScan = new DateTime(2019, 9, 15, 8, 0, 0); currentModuleScan = currentModuleScan.ToUniversalTime(); int feederToBaleMaxMinutes = 10; int feedetToBaleMinMunutes = 4; int minBaleWeight = 475; int maxBaleWeight = 510; int minModuleTurnout = 39; int maxModuleTurnout = 45; int lastBaleNumber = 0; try { //get each load in order using (var uow = UnitOfWorkFactory.CreateUnitOfWork()) { var loads = uow.GinLoadRepository.GetAll(new string[] { "Modules.ModuleHistory" }).OrderBy(x => x.ScaleBridgeLoadNumber); foreach (var l in loads) { Console.WriteLine("CREATING SCANS FOR LOAD: " + l.GinTagLoadNumber); foreach (var affectedModule in l.Modules) { affectedModule.ModuleStatus = ModuleStatus.ON_FEEDER; affectedModule.LastBridgeId = "BRIDGE2"; Console.WriteLine("CURRENT FEEDER SCAN TIME: " + currentModuleScan.ToString("MM/dd/yyyy HH:mm:ss")); ModuleHistoryEntity historyItem = new ModuleHistoryEntity(); historyItem.Id = Guid.NewGuid().ToString(); historyItem.Created = currentModuleScan; historyItem.Driver = ""; historyItem.TruckID = ""; historyItem.BridgeId = "BRIDGE2"; historyItem.ModuleId = affectedModule.Id; historyItem.Latitude = affectedModule.Latitude; historyItem.Longitude = affectedModule.Longitude; historyItem.GinTagLoadNumber = affectedModule.GinTagLoadNumber; if (!string.IsNullOrEmpty(affectedModule.BridgeLoadNumber)) { historyItem.BridgeLoadNumber = int.Parse(affectedModule.BridgeLoadNumber); } else { historyItem.BridgeLoadNumber = null; } affectedModule.ModuleHistory.Add(historyItem); historyItem.ModuleStatus = ModuleStatus.ON_FEEDER; historyItem.ModuleStatus = affectedModule.ModuleStatus; historyItem.ModuleEventType = ModuleEventType.BRIDGE_SCAN; affectedModule.SyncedToCloud = false; uow.ModuleRepository.Save(affectedModule); uow.SaveChanges(); //create PBI scans for module decimal accumulatedWeight = 0.00M; decimal currentModuleTurnout = Convert.ToDecimal(rand.Next(minModuleTurnout, maxModuleTurnout)) / 100.00M; decimal selectedWeight = 0.00M; if (affectedModule.HIDModuleWeightLBS.HasValue) { selectedWeight = affectedModule.HIDModuleWeightLBS.Value; } else if (affectedModule.DiameterApproximatedWeight.HasValue) { selectedWeight = affectedModule.DiameterApproximatedWeight.Value; } else { selectedWeight = affectedModule.LoadAvgModuleWeight.Value; } decimal currentModuleLintWeight = selectedWeight * currentModuleTurnout; DateTime currentPBIScanTime = currentModuleScan.AddMinutes(rand.Next(feedetToBaleMinMunutes, feederToBaleMaxMinutes)); currentPBIScanTime = currentPBIScanTime.AddSeconds(rand.Next(1, 30)); while (accumulatedWeight < currentModuleLintWeight) { lastBaleNumber++; var affectedBale = new BaleEntity(); affectedBale.Id = Guid.NewGuid().ToString(); affectedBale.Name = int.Parse(affectedModule.Name).ToString().PadLeft(5, '0') + lastBaleNumber.ToString().PadLeft(6, '0').ToString(); affectedBale.PbiNumber = affectedBale.Name; affectedBale.Created = currentPBIScanTime; affectedBale.SyncedToCloud = false; affectedBale.WeightFromScale = rand.Next(minBaleWeight, maxBaleWeight); affectedBale.TareWeight = 3.0M; affectedBale.ScanNumber = lastBaleNumber; affectedBale.SyncedToCloud = false; affectedBale.NetWeight = affectedBale.WeightFromScale - affectedBale.TareWeight; accumulatedWeight += affectedBale.NetWeight; if (accumulatedWeight > currentModuleLintWeight) //only apply weight to equal lint weight { affectedBale.NetWeight = affectedBale.NetWeight - (accumulatedWeight - currentModuleLintWeight); if (rand.Next(10) > 5) { affectedBale.NetWeight += rand.Next(1, 3); } else { affectedBale.NetWeight -= rand.Next(1, 3); } affectedBale.WeightFromScale = affectedBale.NetWeight + 3.0M; } Console.WriteLine("CREATING PBI: " + affectedBale.PbiNumber + " " + affectedBale.Created.ToString("MM/dd/yyyy HH:mm:ss")); uow.BalesRepository.Add(affectedBale); currentPBIScanTime = currentPBIScanTime.AddMinutes(rand.Next(1, 3)); } uow.SaveChanges(); //set next module scan half way between last module scan and current pbi scan var timeSpan = currentPBIScanTime.Subtract(currentModuleScan); currentModuleScan = currentPBIScanTime.AddSeconds(feedetToBaleMinMunutes * 60 / 2 * -1); } } } } catch (Exception exc) { Console.WriteLine(exc); } }
private static void createPBIData() { FieldEntity field = null; ModuleEntity module = null; using (var uow = UnitOfWorkFactory.CreateUnitOfWork()) { #region Create Modules and Gin Loads var client = new ClientEntity(); client.Name = "PBI TEST CLIENT"; client.Farms = new List <FarmEntity>(); var farm = new FarmEntity(); farm.Id = Guid.NewGuid().ToString(); farm.Name = "PBI TEST FARM"; farm.Fields = new List <FieldEntity>(); field = new FieldEntity(); field.Id = Guid.NewGuid().ToString(); field.Name = "PBI TEST FIELD"; field.Modules = new List <ModuleEntity>(); field.Latitude = 0.00; field.Longitude = 0.00; field.Created = DateTime.UtcNow; farm.Fields.Add(field); client.Farms.Add(farm); uow.ClientRepository.Save(client); uow.SaveChanges(); } //add modules from CSV file string fileName = "C:\\Users\\mbohn\\Documents\\PBI_TestData.csv"; string[] moduleContents = System.IO.File.ReadAllLines(fileName); List <GinLoadEntity> ginLoadEntities = new List <GinLoadEntity>(); int bridgeLoadNumber = 0; for (int moduleCount = 1; moduleCount < moduleContents.Length; moduleCount++) { var fields = moduleContents[moduleCount].Split(','); using (var uow = UnitOfWorkFactory.CreateUnitOfWork()) { module = new ModuleEntity(); module.Id = Guid.NewGuid().ToString(); module.Name = fields[0]; module.FieldId = field.Id; module.TruckID = ""; module.Driver = ""; module.LoadNumber = fields[3]; module.HIDDiameter = Decimal.Parse(fields[1]) * 2.54M; module.HIDModuleWeight = Decimal.Parse(fields[2]) * 0.453592M; module.Latitude = 0.00; module.Longitude = 0.00; module.ModuleStatus = ModuleStatus.IN_FIELD; module.Created = DateTime.UtcNow; module.ModuleHistory = new List <ModuleHistoryEntity>(); var historyItem = new ModuleHistoryEntity { Id = Guid.NewGuid().ToString(), Created = DateTime.UtcNow, Driver = module.Driver, TruckID = module.TruckID, Latitude = module.Latitude, Longitude = module.Longitude, ModuleEventType = ModuleEventType.IMPORTED_FROM_FILE }; module.FieldId = field.Id; module.ModuleHistory.Add(historyItem); uow.ModuleRepository.Add(module); uow.SaveChanges(); if (!ginLoadEntities.Any(g => g.GinTagLoadNumber == module.LoadNumber)) { //create gin load var affectedGinLoad = new GinLoadEntity(); ginLoadEntities.Add(affectedGinLoad); affectedGinLoad.Created = DateTime.UtcNow; affectedGinLoad.SyncedToCloud = false; affectedGinLoad.Name = module.LoadNumber; affectedGinLoad.GinTagLoadNumber = module.LoadNumber; affectedGinLoad.ScaleBridgeLoadNumber = bridgeLoadNumber++; affectedGinLoad.NetWeight = Convert.ToDecimal(fields[4]); affectedGinLoad.GrossWeight = affectedGinLoad.NetWeight + 8200; affectedGinLoad.SplitWeight1 = affectedGinLoad.NetWeight; affectedGinLoad.SplitWeight2 = 0.00M; affectedGinLoad.ScaleBridgeId = "BRIDGE1"; affectedGinLoad.ScaleBridgeLoadNumber = ginLoadEntities.Count(); affectedGinLoad.Source = InputSource.GIN; affectedGinLoad.SyncedToCloud = false; affectedGinLoad.TruckID = "TRUCK1"; affectedGinLoad.YardLocation = "SOMEWHERE"; affectedGinLoad.SubmittedBy = "TEST HARNESS"; //add client/farm/field information affectedGinLoad.FieldId = field.Id; uow.GinLoadRepository.Save(affectedGinLoad); uow.SaveChanges(); module.GinTagLoadNumber = affectedGinLoad.GinTagLoadNumber; module.GinLoadId = affectedGinLoad.Id; } else { var load = ginLoadEntities.Single(g => g.GinTagLoadNumber == module.LoadNumber); module.GinTagLoadNumber = load.GinTagLoadNumber; module.GinLoadId = load.Id; } //create feeder scan of module module.ModuleStatus = ModuleStatus.ON_FEEDER; module.LastBridgeId = "BRIDGE2"; // uow.ModuleRepository.Save(module); //uow.SaveChanges(); Console.WriteLine("CREATING FEEDER SCAN FOR MODULE : " + module.Name); ModuleHistoryEntity feederHistoryItem = new ModuleHistoryEntity(); feederHistoryItem.Id = Guid.NewGuid().ToString(); feederHistoryItem.Created = DateTime.Parse(fields[5] + " " + fields[6]).ToUniversalTime(); feederHistoryItem.Created = feederHistoryItem.Created.AddSeconds(int.Parse(fields[7])); feederHistoryItem.Driver = ""; feederHistoryItem.TruckID = ""; feederHistoryItem.BridgeId = "BRIDGE2"; feederHistoryItem.ModuleId = module.Id; feederHistoryItem.Latitude = module.Latitude; feederHistoryItem.Longitude = module.Longitude; feederHistoryItem.GinTagLoadNumber = module.GinTagLoadNumber; if (!string.IsNullOrEmpty(module.BridgeLoadNumber)) { feederHistoryItem.BridgeLoadNumber = bridgeLoadNumber; } else { feederHistoryItem.BridgeLoadNumber = null; } module.ModuleHistory.Add(feederHistoryItem); feederHistoryItem.ModuleStatus = ModuleStatus.ON_FEEDER; feederHistoryItem.ModuleStatus = module.ModuleStatus; feederHistoryItem.ModuleEventType = ModuleEventType.BRIDGE_SCAN; module.SyncedToCloud = false; uow.ModuleRepository.Save(module); uow.SaveChanges(); } } #endregion #region Create PBIs using (var uow = UnitOfWorkFactory.CreateUnitOfWork()) { string pbiFileName = "C:\\Users\\mbohn\\Documents\\PBI_Test_Bales.csv"; string[] pbiLines = System.IO.File.ReadAllLines(pbiFileName); for (int lineCount = 1; lineCount < pbiLines.Length; lineCount++) { string line = pbiLines[lineCount]; var pbiFields = line.Split(','); var affectedBale = new BaleEntity(); affectedBale.Id = Guid.NewGuid().ToString(); affectedBale.Name = pbiFields[0]; affectedBale.PbiNumber = pbiFields[0]; affectedBale.Created = DateTime.Parse(pbiFields[1] + " " + pbiFields[2]).ToUniversalTime(); affectedBale.Created = affectedBale.Created.AddMilliseconds(lineCount); affectedBale.SyncedToCloud = false; affectedBale.NetWeight = decimal.Parse(pbiFields[3]); affectedBale.WeightFromScale = affectedBale.NetWeight + 3.0M; affectedBale.TareWeight = 3.0M; affectedBale.ScanNumber = lineCount; affectedBale.SyncedToCloud = false; affectedBale.NetWeight = decimal.Parse(pbiFields[3]); Console.WriteLine("CREATING PBI: " + affectedBale.PbiNumber + " " + affectedBale.Created.ToString("MM/dd/yyyy HH:mm:ss")); uow.BalesRepository.Add(affectedBale); } uow.SaveChanges(); } #endregion }