public void GetByID_OneItem_ReturnsClientLogEventType() { // Arrange ModuleEventType entity = new ModuleEventType { ID = 1 }; // Act MessageHub.Instance.Publish(new OnCacheObjectSet <ModuleEventType>(entity)); // Assert Assert.AreNotSame(entity, _cache.GetByID(1)); }
public void GetByID_TwoItems_ReturnsCorrectObject() { // Arrange ModuleEventType entity1 = new ModuleEventType { ID = 1 }; ModuleEventType entity2 = new ModuleEventType { ID = 2 }; // Act MessageHub.Instance.Publish(new OnCacheObjectSet <ModuleEventType>(entity1)); MessageHub.Instance.Publish(new OnCacheObjectSet <ModuleEventType>(entity2)); // Assert Assert.AreNotSame(entity1, _cache.GetByID(1)); Assert.AreNotSame(entity2, _cache.GetByID(2)); }
public void GetByID_RemovedItem_ReturnsCorrectObject() { // Arrange ModuleEventType entity1 = new ModuleEventType { ID = 1 }; ModuleEventType entity2 = new ModuleEventType { ID = 2 }; // Act MessageHub.Instance.Publish(new OnCacheObjectSet <ModuleEventType>(entity1)); MessageHub.Instance.Publish(new OnCacheObjectSet <ModuleEventType>(entity2)); MessageHub.Instance.Publish(new OnCacheObjectDeleted <ModuleEventType>(entity1)); // Assert Assert.Throws <KeyNotFoundException>(() => { _cache.GetByID(1); }); Assert.AreNotSame(entity2, _cache.GetByID(2)); }
public void GetByID_NoItems_ThrowsKeyNotFoundException() { // Arrange ModuleEventType entity1 = new ModuleEventType { ID = 1 }; ModuleEventType entity2 = new ModuleEventType { ID = 2 }; // Act MessageHub.Instance.Publish(new OnCacheObjectSet <ModuleEventType>(entity1)); MessageHub.Instance.Publish(new OnCacheObjectSet <ModuleEventType>(entity2)); MessageHub.Instance.Publish(new OnCacheObjectDeleted <ModuleEventType>(entity1)); MessageHub.Instance.Publish(new OnCacheObjectDeleted <ModuleEventType>(entity2)); // Assert Assert.Throws <KeyNotFoundException>(() => { _cache.GetByID(1); }); Assert.Throws <KeyNotFoundException>(() => { _cache.GetByID(2); }); }
private bool processRow(CSVRow row, CSVFileParser parser, string filename) { try { using (var uow = UnitOfWorkFactory.CreateUnitOfWork()) { //create a new module document ModuleEntity doc = new ModuleEntity(); doc.Name = row.GetColumnValue(serialNumberColIndex); string clientName = row.GetColumnValue(clientColIndex); string farmName = row.GetColumnValue(farmColIndex); string fieldName = row.GetColumnValue(fieldColIndex); doc.ImportedLoadNumber = row.GetColumnValue(loadColIndex); doc.Driver = row.GetColumnValue(driverColIndex); doc.TruckID = row.GetColumnValue(truckColIndex); doc.Latitude = row.GetDoubleColumnValue(latitudeColIndex); doc.Longitude = row.GetDoubleColumnValue(longitudeColIndex); doc.Notes = row.GetColumnValue(commentsColIndex); doc.GinTagLoadNumber = row.GetColumnValue(ginTicketColIndex).TrimStart('0'); doc.ModuleId = row.GetColumnValue(moduleIdColIndex); doc.HIDDiameter = row.GetDecimalColumnValueNullabe(hidDiameterIndex); doc.HIDFieldArea = row.GetDecimalColumnValueNullabe(hidFieldArea); doc.HIDIncrementalArea = row.GetDecimalColumnValueNullabe(hidIncrementalAreaIndex); doc.HIDModuleWeight = row.GetDecimalColumnValueNullabe(hidModuleWeightIndex); doc.HIDMoisture = row.GetDecimalColumnValueNullabe(hidMoistureIndex); doc.HIDOperator = row.GetColumnValue(hidOperatorIndex); doc.HIDProducerID = row.GetColumnValue(hidProducerIDIndex); doc.HIDGMTDate = row.GetColumnValue(hidGMTDateIndex); doc.HIDGMTTime = row.GetColumnValue(hidGMTTimeIndex); DateTime ts = DateTime.Now; if (DateTime.TryParse(doc.HIDGMTDate + " " + doc.HIDGMTTime, out ts)) { doc.HIDTimestamp = ts; } else { doc.HIDTimestamp = null; } doc.HIDSeasonTotal = row.GetIntColumnValueNullabe(hidSeasonTotalIndex); doc.HIDFieldTotal = row.GetIntColumnValueNullabe(hidFieldTotalIndex); doc.HIDVariety = row.GetColumnValue(hidVarietyIndex); doc.HIDLat = row.GetDoubleColumnValue(latitudeColIndex); doc.HIDLong = row.GetDoubleColumnValue(longitudeColIndex); doc.HIDDropLat = row.GetDoubleColumnValue(hidDropLat); doc.HIDDropLong = row.GetDoubleColumnValue(hidDropLong); doc.HIDWrapLat = row.GetDoubleColumnValue(hidWrapLat); doc.HIDWrapLong = row.GetDoubleColumnValue(hidWrapLon); doc.HIDGinID = row.GetColumnValue(hidGinID); doc.HIDMachinePIN = row.GetColumnValue(hidMachinePIN); doc.GinTagLoadNumber = row.GetColumnValue(ginTicketColIndex).TrimStart('0'); doc.Name = doc.Name.Trim(); string clientID = row.GetColumnValue(clientIDColIndex); string farmID = row.GetColumnValue(farmIDColIndex); string fieldID = row.GetColumnValue(fieldIDColIndex); ModuleEventType eventType = ModuleEventType.IMPORTED_FROM_FILE; DateTime created = DateTime.Now; if (filename.ToLower().Contains("transmission")) { eventType = ModuleEventType.IMPORTED_FROM_RFID_MODULESCAN; if (!DateTime.TryParse(row.GetColumnValue(timestampIndex), out created)) { if (doc.HIDTimestamp.HasValue) { created = doc.HIDTimestamp.Value; } } else { created = created.ToUniversalTime(); } } else { eventType = ModuleEventType.IMPORTED_FROM_HID; if (doc.HIDTimestamp.HasValue) { created = doc.HIDTimestamp.Value; } } ModuleHistoryEntity historyItem = new ModuleHistoryEntity { Id = Guid.NewGuid().ToString(), Created = created, Driver = doc.Driver, TruckID = doc.TruckID, BridgeId = string.Empty, BridgeLoadNumber = null, GinTagLoadNumber = doc.GinTagLoadNumber, Latitude = doc.Latitude, Longitude = doc.Longitude, ModuleEventType = eventType }; doc.ModuleHistory.Add(historyItem); //determine if serial number exists if so skip bool canSave = !string.IsNullOrWhiteSpace(doc.Name) && !existingSerialNumbers.ContainsKey(doc.Name); bool canUpdate = !string.IsNullOrWhiteSpace(doc.Name) && existingSerialNumbers.ContainsKey(doc.Name); if (canSave) { existingSerialNumbers.Add(doc.Name, true); ClientEntity existingClient = uow.ClientRepository.FindSingle(c => c.Name == clientName.Trim(), "Farms.Fields"); FarmEntity existingFarm = null; FieldEntity existingField = null; if (existingClient == null) { //add client/farm/field and module if (!string.IsNullOrWhiteSpace(clientName)) { existingClient = new ClientEntity(); existingClient.Farms = new List <FarmEntity>(); existingClient.Name = clientName.Trim(); uow.ClientRepository.CreateWithID(existingClient, clientID); } else { Logging.Logger.Log("INFO", string.Format("Cannot save client for new module with SN {0} - name is empty.", doc.Name)); return(false); } } existingFarm = uow.FarmRepository.FindSingle(f => f.Name == farmName.Trim() && f.ClientId == existingClient.Id); if (existingFarm == null) { var canSaveFarm = !(string.IsNullOrWhiteSpace(farmName)); if (!canSaveFarm) { Logging.Logger.Log("INFO", string.Format("Cannot save farm {0} for new module with SN {1} - name empty or duplicate farm already exists for another client.", doc.FarmName, doc.Name)); return(false); } else { //add farm/field to client existingFarm = new FarmEntity(); existingFarm.Fields = new List <FieldEntity>(); existingFarm.Name = farmName.Trim(); existingFarm.ClientId = existingClient.Id; uow.FarmRepository.CreateWithID(existingFarm, farmID); } } existingField = uow.FieldRepository.FindSingle(f => f.Name.Trim() == fieldName.Trim() && f.FarmId == existingFarm.Id); if (existingField == null) { if (!string.IsNullOrWhiteSpace(fieldName)) { existingField = new FieldEntity(); existingField.Name = fieldName.Trim(); existingField.Latitude = 0.00; existingField.Longitude = 0.00; existingField.FarmId = existingFarm.Id; uow.FieldRepository.CreateWithID(existingField, fieldID); } else { Logging.Logger.Log("INFO", string.Format("Cannot save field for new module with SN {0} - field name empty.", doc.Name)); return(false); } } if (uow.SettingsRepository.CoordsAtFeeder(doc.Latitude, doc.Longitude)) { doc.ModuleStatus = ModuleStatus.ON_FEEDER; } else if (uow.SettingsRepository.CoordsOnGinYard(doc.Latitude, doc.Longitude)) { doc.ModuleStatus = ModuleStatus.AT_GIN; } else { doc.ModuleStatus = ModuleStatus.IN_FIELD; //change status back to field } historyItem.ModuleStatus = doc.ModuleStatus; doc.FieldId = existingField.Id; doc.SyncedToCloud = false; // Added 5/23 uow.ModuleRepository.Save(doc); uow.SaveChanges(); return(true); } else if (canUpdate) { var affectedModule = uow.ModuleRepository.FindSingle(x => x.Name == doc.Name, "ModuleHistory"); affectedModule.Latitude = doc.Latitude; affectedModule.Longitude = doc.Longitude; if (uow.SettingsRepository.CoordsAtFeeder(doc.Latitude, doc.Longitude)) { affectedModule.ModuleStatus = ModuleStatus.ON_FEEDER; } else if (uow.SettingsRepository.CoordsOnGinYard(doc.Latitude, doc.Longitude)) { affectedModule.ModuleStatus = ModuleStatus.AT_GIN; } else { affectedModule.ModuleStatus = ModuleStatus.IN_FIELD; //change status back to field } if (!string.IsNullOrWhiteSpace(doc.ImportedLoadNumber)) { affectedModule.ImportedLoadNumber = doc.ImportedLoadNumber; } historyItem.ModuleStatus = affectedModule.ModuleStatus; affectedModule.ModuleHistory.Add(historyItem); affectedModule.SyncedToCloud = false; uow.ModuleRepository.Save(affectedModule); uow.SaveChanges(); return(true); } else { Logging.Logger.Log("INFO", "Cannot save empty or duplicate serial number: " + doc.Name); return(false); } } } catch (Exception exc) { Logging.Logger.Log(exc); return(false); } }
public ModuleEventMessage(UIModule module, ModuleEventType moduleEvent) { Module = module; ModuleEvent = moduleEvent; }