using System; using System.Collections.Generic; using System.Text; using System.Data.SqlClient; using System.Data.SqlTypes; namespace BrazilTimestampReconstruction { class AnchorReader { SqlConnection conn; Dictionary fit; Queue segment; // Stores the starting GPS segments public AnchorReader(SqlConnection conn, Dictionary fit, Queue segment) { this.conn = conn; this.fit = fit; this.segment = segment; } #region Read Anchor Information public void ReadAnchorInformation() { ReadAllSegments(); ReadRelativeAnchors(); InitializeGPSNodes(); } public void ReadAllSegments() { try { conn.Open(); SqlCommand cmd = new SqlCommand(Constants.GET_ALL_SEGMENTS, conn); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { InsertNewSegment(reader.GetInt16(0), reader.GetInt16(1)); } } catch (SqlException ex) { // Do something. } finally { conn.Close(); } } public void ReadRelativeAnchors() { try { // Get all the anchors conn.Open(); SqlCommand cmd = new SqlCommand(Constants.GET_ANCHORS_QUERY, conn); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { AnchorPoint ap = new AnchorPoint (reader.GetInt16(0), reader.GetInt64(1), reader.GetInt16(2), reader.GetInt16(3), reader.GetInt64(4), reader.GetInt16(5)); InsertNewAnchorPoint(ap); } } catch (SqlException ex) { // Do something with the error } finally { conn.Close(); } } public void InitializeGPSNodes() { try { conn.Open(); SqlCommand cmd = new SqlCommand(Constants.INIT_GPS_NODE, conn); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { string key = GenerateNodeSegmentKey(reader.GetInt16(0), reader.GetInt16(1)); segment.Enqueue(key); } } catch (SqlException ex) { // Do something. } finally { conn.Close(); } } #endregion #region Create the SegmentTree and Load the anchors public void InsertNewSegment(Int16 node, Int16 rebootNum) { string key = GenerateNodeSegmentKey(node, rebootNum); if (!fit.ContainsKey(key)) { fit.Add(key, new ClockFit(key)); } } private void InsertNewAnchorPoint(AnchorPoint ap) { string keyParent = "", keyNode = ""; double x, y; // Parent node is always x. Child node is y // The data is stored in only one of the nodes (not both) // even though both directions are added // data is stored in the smaller of the two nodeis if (ap.Node1 <= ap.Node2) { keyParent = GenerateNodeSegmentKey(ap.Node1, ap.Rc1); keyNode = GenerateNodeSegmentKey(ap.Node2, ap.Rc2); x = ap.Lc1; y = ap.Lc2; } else { keyNode = GenerateNodeSegmentKey(ap.Node1, ap.Rc1); keyParent = GenerateNodeSegmentKey(ap.Node2, ap.Rc2); x = ap.Lc2; y = ap.Lc1; } // Add the data part ClockFit cf = fit[keyParent]; RelativeFit rf = cf.InsertLocalAnchors(keyNode, x, y); // Add the other link too without the data // If the pointer to the mirror anchors does not exist // create it ClockFit otherDirCF = fit[keyNode]; otherDirCF.InsertLinkWithoutAnchors(keyParent, rf); } #endregion #region Key Management public string GenerateNodeSegmentKey(short node, short rebootNum) { return node.ToString() + Constants.KEY_SYMB + rebootNum.ToString(); } #endregion } }