public void Initialize() { //initialize using ActiveListID to lookup info ModulesOnTruck = new ObservableCollection <ModuleViewModel>(); BadSerials = new ObservableCollection <ModuleViewModel>(); modulesScanned = new List <string>(); using (var uow = SimpleIoc.Default.GetInstance <IUnitOfWorkFactory>().CreateUnitOfWork()) { activeList = uow.PickupListRepository.GetById(ActiveListID, new string[] { "AssignedModules" }); } var snsOnTruck = AggregateDataProvider.SerialNumbersOnTruck; foreach (var sn in snsOnTruck) { if (!ModulesOnTruck.Any(m => m.SerialNumber == sn)) { ModulesOnTruck.Add(new ViewModels.ModuleViewModel { SerialNumber = sn }); } } GalaSoft.MvvmLight.Messaging.Messenger.Default.Register <QuadratureStateChangeMessage>(this, action => ProcessQuadratureMessage(action)); Messenger.Default.Register <ManualUnloadCorrectionMessage>(this, (action) => ProcessManualCorrection(action)); t = new System.Timers.Timer(250); t.Elapsed += (sender, e) => this.HandleRefreshTimer(); lastIntervalStart = DateTime.UtcNow; t.Start(); }
private void WriteArchiveFileAsync() { try { string invalid = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars()); string path = ConfigHelper.ArchivePath; string fileName = string.Format("{0}_{1}_{2}_{3}_{4}.csv", clientSelector.ExistingOrNewClientName, farmSelector.ExistingOrNewFarmName, fieldSelector.ExistingOrNewFieldName, DateTime.Now.ToString("yyyyy_MM_dd_HH_mm_ss"), tbListname.Text.Trim()); fileName = path + FileHelper.CleanFilename(fileName); using (var uow = UnitOfWorkFactory.CreateUnitOfWork()) { dataObj = uow.PickupListRepository.GetById(dataObj.Id, "AssignedModules", "DownloadedToTrucks", "AssignedTrucks", "Field.Farm.Client"); } using (StreamWriter sw = File.CreateText(fileName)) { //write header sw.WriteLine("Client,Farm,Field,List,SerialNumber,AssignedTruckIDs,Latitude,Longitude"); string latString = ""; string longString = ""; foreach (var module in dataObj.AssignedModules) { latString = ""; longString = ""; if (module.Latitude > 0 || module.Latitude < 0) { latString = module.Latitude.ToString(); } if (module.Longitude > 0 || module.Longitude < 0) { longString = module.Longitude.ToString(); } sw.WriteLine(string.Format("{0},{1},{2},{3},{4},{5},{6},{7}", FileHelper.EscapeForCSV(dataObj.ClientName), FileHelper.EscapeForCSV(dataObj.FarmName), FileHelper.EscapeForCSV(dataObj.Field.Name), FileHelper.EscapeForCSV(dataObj.Name), FileHelper.EscapeForCSV(module.Name), FileHelper.EscapeForCSV(dataObj.DownloadedByTruckNames), latString, longString )); } } } catch (Exception exc) { Logging.Logger.Log(exc); MessageBox.Show("An error occurred writing archive file for pick up list."); } }
public void LoadForm() { try { using (var uow = UnitOfWorkFactory.CreateUnitOfWork()) { if (dataObj != null) { //need to refresh pickup list document because it could have changed dataObj = uow.PickupListRepository.GetById(dataObj.Id, new string[] { "Field.Farm.Client", "AssignedModules", "DownloadedToTrucks", "AssignedTrucks" }); if (ReadOnly) { farmSelector.Enabled = false; fieldSelector.Enabled = false; clientSelector.Enabled = false; btnSaveAndClose.Enabled = false; availableModulesGridView.ReadOnly = true; gridViewTrucks.ReadOnly = true; checkAllModules.Visible = false; checkAllTrucks.Visible = false; tbListname.ReadOnly = true; } } if (dataObj != null) { tbListname.Text = dataObj.Name; BindingHelper.BindPickupListDestinationCombo(cboDestination, dataObj.Destination); } else { tbListname.Text = string.Empty; BindingHelper.BindPickupListDestinationCombo(cboDestination); } bindingDestList = false; clientSelector.Initialize(errorProvider, true, false, (dataObj != null) ? dataObj.Field.Farm.Client.Id : ""); farmSelector.FormErrorProvider = errorProvider; fieldSelector.FormErrorProvider = errorProvider; clearErrors(); } } catch (Exception exc) { Logging.Logger.Log(exc); throw new Exception("Error occurred loading pickup list form.", exc); } }
public async Task <DialogResult> ShowEdit(PickupListEntity doc) { dataObj = doc; this.Text = "Update Pickup List"; await RefreshDownloadedTrucks(); //has list already been downloaded if so prevent removing modules using (var uow = UnitOfWorkFactory.CreateUnitOfWork()) { var thisList = uow.PickupListRepository.GetById(doc.Id, "AssignedModules", "AssignedTrucks", "DownloadedToTrucks"); if (downloadedByTrucks.Count() > 0) { modulesDownloaded.AddRange(thisList.AssignedModules.Select(s => s.Id).ToList()); } } return(this.ShowDialog()); }
public void Initialize() { //initialize using ActiveListID to lookup info ModulesOnTruck = new ObservableCollection <ModuleViewModel>(); BadSerials = new ObservableCollection <ModuleViewModel>(); using (var uow = SimpleIoc.Default.GetInstance <IUnitOfWorkFactory>().CreateUnitOfWork()) { activeList = uow.PickupListRepository.GetById(ActiveListID, new string[] { "AssignedModules", "Field.Farm.Client" }); Farm = activeList.FarmName; Field = activeList.FieldName; Client = activeList.ClientName; } var snsOnTruck = AggregateDataProvider.SerialNumbersOnTruck; foreach (var sn in AggregateDataProvider.SerialNumbersOnTruck) { if (!ModulesOnTruck.Any(m => m.SerialNumber == sn)) { var moduleViewModel = new ViewModels.ModuleViewModel { SerialNumber = sn, Selected = false }; if (!activeList.AssignedModules.Any(l => l.Name == sn)) { moduleViewModel.BackgroundColor = "Red"; moduleViewModel.ForegroundColor = "White"; } else { moduleViewModel.BackgroundColor = "Green"; moduleViewModel.ForegroundColor = "White"; } ModulesOnTruck.Add(moduleViewModel); } } }
public void LoadData(string listId) { try { //LOAD DATA BASED ON LIST ID using (var uow = SimpleIoc.Default.GetInstance <IUnitOfWorkFactory>().CreateUnitOfWork()) { pickupList = uow.PickupListRepository.GetById(listId, "Field.Farm.Client", "AssignedModules"); syncedSettings = uow.SyncedSettingsRepo.GetAll().FirstOrDefault(); if (pickupList.Destination == PickupListDestination.GIN_YARD && pickupList.AssignedModules.Count(m => m.ModuleStatus == ModuleStatus.IN_FIELD) > 0) { var firstModule = pickupList.AssignedModules.First(); FieldLat = firstModule.Latitude; FieldLong = firstModule.Longitude; } else if (pickupList.Destination == PickupListDestination.GIN_FEEDER && pickupList.AssignedModules.Count(m => m.ModuleStatus == ModuleStatus.AT_GIN) > 0) { var firstModule = pickupList.AssignedModules.First(); FieldLat = firstModule.Latitude; FieldLong = firstModule.Longitude; } if (FieldLat == 0.000 || FieldLong == 0.000) { FieldLat = pickupList.Latitude; FieldLong = pickupList.Longitude; } ListName = pickupList.Name; Client = pickupList.ClientName; Farm = pickupList.FarmName; Field = pickupList.Field.Name; if (syncedSettings == null) { pickupList.ModulesPerLoad = 4; } else { pickupList.ModulesPerLoad = syncedSettings.ModulesPerLoad; } ModulesInField = pickupList.ModulesRemaining; ModulesLoaded = pickupList.AssignedModules.Count(c => AggregateDataProvider.SerialNumbersOnTruck.Contains(c.Name)); //adjust modules in field by modules on truck if (pickupList.Destination == PickupListDestination.GIN_YARD) { foreach (var module in pickupList.AssignedModules.Where(s => s.ModuleStatus == ModuleStatus.IN_FIELD && AggregateDataProvider.SerialNumbersOnTruck.Any(x => x == s.Name))) { ModulesInField--; } } else if (pickupList.Destination == PickupListDestination.GIN_FEEDER) { foreach (var module in pickupList.AssignedModules.Where(s => s.ModuleStatus == ModuleStatus.AT_GIN && AggregateDataProvider.SerialNumbersOnTruck.Any(x => x == s.Name))) { ModulesInField--; } } ModulesOnList = pickupList.TotalModules; LoadsCompleted = pickupList.LoadsCompleted; LoadsRemaining = pickupList.LoadsRemaining; DestinationName = pickupList.DestinationName; setLoadsRemaining(); HasCheckedModules = false; System.Windows.Application.Current.Dispatcher.Invoke(new Action(() => { foreach (var m in ListModules) { m.PropertyChanged -= MVm_PropertyChanged; } Modules.Clear(); ListModules.Clear(); foreach (var m in pickupList.AssignedModules.OrderBy(s => s.Name)) { //using not thread safe version of serial numbers on truck because this event gets fired //inside a lock on the _aggregateEvents in the provider //using the threadsafe property will cause a thread lock on the same property var mVm = new ModuleViewModel { Loaded = AggregateDataProvider.SerialNumbersOnTruckNotThreadSafe.Contains(m.Name), Latitude = m.Latitude, Longitude = m.Longitude, SerialNumber = m.Name }; mVm.ShowOnMap = (m.ModuleStatus == ModuleStatus.IN_FIELD && pickupList.Destination == PickupListDestination.GIN_YARD) || (m.ModuleStatus == ModuleStatus.AT_GIN && pickupList.Destination == PickupListDestination.GIN_FEEDER); Modules.Add(mVm); if (mVm.ShowOnMap || mVm.Loaded) { mVm.PropertyChanged += MVm_PropertyChanged; ListModules.Add(mVm); } } })); } } catch (Exception exc) { Logging.Logger.Log(exc); } }
private async void btnSaveAndClose_Click(object sender, EventArgs e) { var selectedTrucks = new List <TruckEntity>(); await RefreshDownloadedTrucks(); foreach (DataGridViewRow row in gridViewTrucks.Rows) { var truckItem = (TruckEntity)row.DataBoundItem; if (row.Cells[0].Value != null && (bool)row.Cells[0].Value) { selectedTrucks.Add(truckItem); } } if (selectedTrucks.Count() < 1) { MessageBox.Show("Please check at least one truck to send this list to."); } else { //check to see to make sure ids weren't remove that may have been downloaded while dialog was open if (downloadedByTrucks.Count() > 0 && !selectedTrucks.Any(t => downloadedByTrucks.Contains(t.Id)) && dataObj != null) { MessageBox.Show("A truck downloaded this list after you began editting. Close this list and re-open to edit."); return; } try { using (var uow = UnitOfWorkFactory.CreateUnitOfWork()) { ClientEntity client = clientSelector.GetOrCreateClientEntity(); FarmEntity farm = farmSelector.GetOrCreateFarmEntity(client); FieldEntity field = fieldSelector.GetOrCreateFieldEntity(farm); BaseEntity dest = (BaseEntity)cboDestination.SelectedItem; if (modulesSelected.Count() < 1) { field.Latitude = selectedLat; field.Longitude = selectedLng; uow.FieldRepository.Save(field); } PickupListEntity existingList = null; if (dataObj == null) { existingList = new PickupListEntity(); existingList.Id = Guid.NewGuid().ToString(); existingList.DownloadedToTrucks = new List <TruckEntity>(); existingList.AssignedModules = modulesSelected; existingList.Created = DateTime.UtcNow; existingList.AssignedTrucks = selectedTrucks; existingList.FieldId = field.Id; existingList.Name = tbListname.Text.Trim(); existingList.Destination = (PickupListDestination)Convert.ToInt32(dest.Id); existingList.OriginalSerialNumbers = ""; foreach (var s in modulesSelected) { existingList.OriginalSerialNumbers += "," + s.Name; } existingList.OriginalSerialNumbers.TrimEnd(','); existingList.OriginalModuleCount = modulesSelected.Count(); if (modulesSelected.Count() > 0) { var m = modulesSelected.First(); existingList.Latitude = m.Latitude; existingList.Longitude = m.Longitude; } else { existingList.Latitude = selectedLat; existingList.Longitude = selectedLng; } uow.PickupListRepository.Add(existingList); } else { existingList = uow.PickupListRepository.GetById(dataObj.Id, "AssignedModules", "AssignedTrucks", "DownloadedToTrucks"); existingList.FieldId = field.Id; existingList.Name = tbListname.Text.Trim(); existingList.Latitude = selectedLat; existingList.Longitude = selectedLng; existingList.Destination = (PickupListDestination)Convert.ToInt32(dest.Id); existingList.OriginalSerialNumbers = ""; foreach (var s in modulesSelected) { existingList.OriginalSerialNumbers += "," + s.Name; } existingList.OriginalSerialNumbers.TrimEnd(','); existingList.OriginalModuleCount = modulesSelected.Count(); uow.PickupListRepository.Update(existingList, modulesSelected.Select(m => m.Id).ToList(), selectedTrucks.Select(t => t.Id).ToList()); } uow.SaveChanges(); dataObj = existingList; WriteArchiveFileAsync(); this.DialogResult = DialogResult.OK; this.Close(); } } catch (Exception exc) { Logging.Logger.Log(exc); MessageBox.Show("An error occurred trying to save pickup list. " + exc.Message); } } }
private void ExecuteSaveCommand() { refreshErrorState(); if (!HasErrors) { try { using (var uow = SimpleIoc.Default.GetInstance <IUnitOfWorkFactory>().CreateUnitOfWork()) { //get this truck var syncedSetting = uow.SyncedSettingsRepo.GetAll().FirstOrDefault(); TruckEntity thisTruck = uow.SettingsRepository.GetCurrentTruck(); string clientName = ""; if (_selectedProducer.ID == "") { clientName = _NewProducerText.Trim(); } else { clientName = _selectedProducer.DisplayText.Trim(); } string farmName = ""; if (ShowNewFarmText) { farmName = _NewFarmText.Trim(); } else { farmName = _selectedFarm.DisplayText.Trim(); } string fieldName = ""; if (ShowFieldText) { fieldName = _NewFieldText.Trim(); } else { fieldName = _selectedField.DisplayText.Trim(); } ClientEntity client = uow.ClientRepository.EnsureClientCreated(clientName, InputSource.TRUCK); uow.SaveChanges(); FarmEntity farm = uow.FarmRepository.EnsureFarmCreated(client, farmName, InputSource.TRUCK); uow.SaveChanges(); FieldEntity field = uow.FieldRepository.EnsureFieldCreated(farm, fieldName, InputSource.TRUCK); uow.SaveChanges(); var coords = GPSDataProvider.GetLastCoords(); double lat = CottonDBMS.DataModels.Helpers.GPSHelper.SafeLat(coords); double lng = CottonDBMS.DataModels.Helpers.GPSHelper.SafeLong(coords); PickupListEntity list = new PickupListEntity(); list.Id = Guid.NewGuid().ToString(); list.Name = _listName.Trim(); list.Source = InputSource.TRUCK; list.AssignedModules = new List <ModuleEntity>(); list.AssignedTrucks = new List <TruckEntity>(); list.DownloadedToTrucks = new List <TruckEntity>(); list.FieldId = field.Id; list.PickupListStatus = PickupListStatus.OPEN; list.ModulesPerLoad = (syncedSetting != null) ? syncedSetting.ModulesPerLoad : 4; if (uow.SettingsRepository.CoordsOnGinYard(lat, lng) || uow.SettingsRepository.CoordsAtFeeder(lat, lng)) { list.Destination = PickupListDestination.GIN_FEEDER; } else { list.Destination = PickupListDestination.GIN_YARD; } if (thisTruck != null) { list.AssignedTrucks.Add(thisTruck); list.DownloadedToTrucks.Add(thisTruck); } uow.PickupListRepository.Add(list); uow.SaveChanges(); GalaSoft.MvvmLight.Messaging.Messenger.Default.Send <PickupListAddedMessage>(new PickupListAddedMessage { Id = list.Id }); } _windowService.CloseModalWindow(WindowType.AddFieldWindow); } catch (Exception exc) { Logging.Logger.Log(exc); System.Windows.MessageBox.Show("An error occured trying to save list. " + exc.Message); } } }
private void ExecuteSaveCommand() { refreshErrorState(); if (!HasErrors) { using (var uow = SimpleIoc.Default.GetInstance <IUnitOfWorkFactory>().CreateUnitOfWork()) { string clientName = ""; if (ShowNewProducerText) { clientName = NewProducerText.Trim(); } else { clientName = SelectedProducer.DisplayText; } string farmName = ""; if (ShowNewFarmText) { farmName = NewFarmText.Trim(); } else { farmName = SelectedFarm.DisplayText; } string fieldName = ""; if (ShowFieldText) { fieldName = NewFieldText.Trim(); } else { fieldName = SelectedField.DisplayText.Trim(); } var coords = GPSDataProvider.GetLastCoords(); double lat = CottonDBMS.DataModels.Helpers.GPSHelper.SafeLat(coords); double lng = CottonDBMS.DataModels.Helpers.GPSHelper.SafeLong(coords); var client = uow.ClientRepository.EnsureClientCreated(clientName, InputSource.TRUCK); uow.SaveChanges(); var farm = uow.FarmRepository.EnsureFarmCreated(client, farmName, InputSource.TRUCK); uow.SaveChanges(); var field = uow.FieldRepository.EnsureFieldCreated(farm, fieldName, InputSource.TRUCK); field.Latitude = lat; field.Longitude = lng; uow.SaveChanges(); var currentTruck = uow.SettingsRepository.GetCurrentTruck(); PickupListEntity list = null; if (ShowListText) { list = new PickupListEntity(); list.Id = Guid.NewGuid().ToString(); list.Created = DateTime.UtcNow; list.FieldId = field.Id; list.Latitude = lat; list.Longitude = lng; list.Name = ListName; list.PickupListStatus = PickupListStatus.OPEN; list.Source = InputSource.TRUCK; list.SyncedToCloud = false; list.AssignedModules = new List <ModuleEntity>(); list.AssignedTrucks = new List <TruckEntity>(); list.DownloadedToTrucks = new List <TruckEntity>(); if (uow.SettingsRepository.CoordsOnGinYard(lat, lng) || uow.SettingsRepository.CoordsAtFeeder(lat, lng)) { list.Destination = PickupListDestination.GIN_FEEDER; } else { list.Destination = PickupListDestination.GIN_YARD; } if (currentTruck != null) { list.AssignedTrucks.Add(currentTruck); list.DownloadedToTrucks.Add(currentTruck); } uow.PickupListRepository.Add(list); uow.SaveChanges(); } else { list = uow.PickupListRepository.GetById(SelectedPickupList.ID, "Field.Farm.Client", "AssignedModules", "DownloadedToTrucks", "AssignedTrucks"); } GalaSoft.MvvmLight.Messaging.Messenger.Default.Send <PickupListAddedMessage>(new PickupListAddedMessage { Id = list.Id }); //assign all serial numbers on truck to the new list uow.PickupListRepository.MoveModulesToList(list, AggregateDataProvider.SerialNumbersOnTruck, lat, lng); _windowService.CloseModalWindow(WindowType.ChangeListWindow); _windowService.CloseModalWindow(WindowType.LoadingIncorrectModuleWindow); _windowService.CloseModalWindow(WindowType.PickupWindow); //re-open pickup window with new list assignment PickUpListViewModel vm = SimpleIoc.Default.GetInstance <PickUpListViewModel>(); vm.ListID = list.Id; vm.InitiatingEvent = null; _windowService.ShowModalWindow(WindowType.PickupWindow, vm); } } }