/// <summary> /// Create entity object at URI in application settings. /// </summary> /// <returns>Entities if successful, null otherwise.</returns> private GatewayEntities GetGatewayEntities() { const string tag = ClassName + "GetGatewayService:"; Gateway.GatewayEntities entities = null; IdUtility.Properties.Settings settings = new IdUtility.Properties.Settings(); string gatewayServiceUriString = settings.GatewayServiceUri; if (string.IsNullOrEmpty(gatewayServiceUriString)) { string errorText = string.Format( Local.Resources.GatewayUriAppSettingNotFound, GatewayServiceUriKey); Trace.TraceError(tag + errorText); MessageBox.Show(errorText, Local.Resources.Error, MessageBoxButton.OK, MessageBoxImage.Error); } else { entities = new Gateway.GatewayEntities(new Uri(gatewayServiceUriString)); } return(entities); }
/// <summary> /// Add a tracker and serial number to the database. /// </summary> /// <param name="gatewayEntities"></param> /// <param name="macAddress">Expected to represent a 32-bit integer in hex format.</param> /// <param name="serialNumber"></param> /// <exception cref="System.FormatException">One or both parameters could not be parsed.</exception> /// <exception cref="System.OverflowException">One or both parameters represented numbers that were too large.</exception> static void AddTracker( Gateway.GatewayEntities gatewayEntities, DateTime batchDate, int tagNodeId, string macAddress, string serialNumber) { UInt64 macDummy; macDummy = UInt64.Parse(macAddress, NumberStyles.AllowHexSpecifier); // The parsed value is not used but we want the exception thrown for invalid format and overflow. var tracker = new Gateway.Tracker(); tracker.MobileId = macAddress; gatewayEntities.AddToTrackers(tracker); gatewayEntities.SaveChanges(); var serial = new Gateway.SerialNumber(); serial.BatchDate = batchDate; serial.TheSerialNumber = serialNumber; serial.SerialNumberTypeId = tagNodeId; serial.TrackerId = tracker.Id; gatewayEntities.AddToSerialNumbers(serial); gatewayEntities.SaveChanges(); }
/// <summary> /// Load tracker asset information from a text reader (probably attached to a file) into the database. /// </summary> /// <param name="source">Text reader attached to a file or some other stream.</param> /// <param name="gatewayEntities">Entities linked to database definitions.</param> /// <param name="batchDateTime">Timestamp to associate with the whole batch.</param> public static void LoadTrackers(TextReader source, Gateway.GatewayEntities gatewayEntities, DateTime batchDateTime) { const string tag = ClassName + ".LoadTrackers:"; Trace.TraceInformation(tag + string.Format("batchDateTime={0}", batchDateTime)); int tagNodeId = TagNodeId(gatewayEntities); string line; int linesProcessed = 0; using (var scope = new TransactionScope()) // Transaction may not be supported for ODATA sources. { try { while ((line = source.ReadLine()) != null) { Trace.TraceInformation(tag + "Processing \"" + line + "\""); linesProcessed++; string[] splitLine = line.Split(LineDelimiters, StringSplitOptions.None); if (splitLine.Length != 2) { string errorText = string.Format("Format error on line {0}.", linesProcessed); Trace.TraceError(errorText); throw new FormatException(errorText); } AddTracker( gatewayEntities, batchDateTime, tagNodeId, splitLine[MacAddressColumn], splitLine[SerialNumberColumn]); } scope.Complete(); } catch (FormatException) { Trace.TraceError(tag + "Format error."); } catch (Exception) { Trace.TraceError(tag + "Unknown error."); } } Trace.TraceInformation(tag + ExitText); }
/// <summary> /// Find the integer type to be used for database relationships in the list of all node types. /// </summary> /// <param name="gatewayEntities"></param> /// <returns></returns> private static int TagNodeId(Gateway.GatewayEntities gatewayEntities) { var nodeTypes = gatewayEntities.SerialNumberTypes; int tagNodeId = -1; foreach (var nodeType in nodeTypes) { if (nodeType.Name == TagSerialNumberType) { tagNodeId = nodeType.Id; break; } } return(tagNodeId); }
/// <summary> /// Open a CSV file with serial numbers and MAC address, read it and create new entries in the database. /// </summary> /// <param name="sender">Ignored.</param> /// <param name="e">Ignored.</param> void mainWindow_SendToDatabase(object sender, RoutedEventArgs e) { const string tag = ClassName + ".LoadTrackersFromFile:"; string filename = ViewModel.FileSelectionViewModel.FileSelectionModel.Filename; Trace.TraceInformation(tag + string.Format("filename={0}", filename)); string message = string.Format( Local.Resources.LoadToDatabaseConfirmation, filename); MessageBoxResult confirmation = MessageBox.Show( message, Local.Resources.LoadToDatabaseConfirmationCaption, MessageBoxButton.OKCancel, MessageBoxImage.Question); if (confirmation == MessageBoxResult.Cancel) { Trace.TraceInformation(tag + "Cancelled by the user."); return; } try { var reader = new StreamReader(filename); DateTime batchDateTime = File.GetCreationTime(filename); Gateway.GatewayEntities gatewayEntities = GetGatewayEntities(); TrackerLoader.LoadTrackers(reader, gatewayEntities, batchDateTime); reader.Close(); } catch (FileNotFoundException) { Trace.TraceError(tag + "File not found."); } catch (ArgumentException) { Trace.TraceError(tag + "Empty filename."); } Trace.TraceInformation(tag + ExitText); }
/// <summary> /// Look for and load serial, tracker information given a serial number. /// </summary> /// <returns>True if data was found, False otherwise.</returns> bool Search() { const string tag = ClassName + "Search:"; string searchSerial = ViewModel.SearchViewModel.SerialNumberSearch.SerialNumber; Trace.TraceInformation(tag + "Serial={0}", searchSerial); Gateway.GatewayEntities gatewayEntities = GetGatewayEntities(); if (gatewayEntities == null) { return(false); } var queryResult = from s in gatewayEntities.SerialNumbers where (s.TheSerialNumber == searchSerial) select s; Gateway.SerialNumber serialFound = null; Gateway.Tracker trackerFound = null; if (queryResult != null) { try { serialFound = queryResult.First(); } catch (InvalidOperationException) { serialFound = null; } if (serialFound != null) { var trackerQueryResult = from t in gatewayEntities.Trackers where (t.Id == serialFound.TrackerId) select t; if (trackerQueryResult != null) { try { trackerFound = trackerQueryResult.First(); } catch (InvalidOperationException) { Trace.TraceError(tag + "Tracker query returned 0 results."); trackerFound = null; } } } else { Trace.TraceInformation(tag + "Serial query returned 0 results."); } } else { Trace.TraceError(tag + "Serial query returned null."); } bool returnValue = false; if (serialFound == null || trackerFound == null) { MessageBox.Show(Local.Resources.NoRecordsFound, "", MessageBoxButton.OK, MessageBoxImage.Information); ViewModel.GatewayViewModel.SerialNumber = null; ViewModel.GatewayViewModel.Tracker = null; } else { ViewModel.GatewayViewModel.SerialNumber = serialFound; ViewModel.GatewayViewModel.Tracker = trackerFound; returnValue = true; } return(returnValue); }