/// <summary> /// Constructor. Protected because we don't want anyone creating a base class instance. /// </summary> protected BaseCommand() { DG200FileLogger.Log("BaseCommand constructor.", 3); this._session = null; this._currentResult = null; this._serialConnection = null; }
/// <summary> /// Executes a command through the serial connection. /// </summary> public void Execute() { if (this._currentCommand != null) { DG200FileLogger.Log("DG200SerialConnection Execute method sending message.", 3); if (this.SendMessage(this._currentCommand.getCommandData())) { do { // Not sure if I should declare this on every iteration, but I guess it's ok. byte[] newReceivedData = new byte[128]; // Get data from the COM port. Int32 BytesRead = this._prt.Read(newReceivedData, 0, newReceivedData.Length); DG200FileLogger.Log("DG200SerialConnection Execute method read this many bytes: " + BytesRead + " from com. Passing to command.", 3); // Put in the buffer. this._currentCommand.addCommandResultData(newReceivedData, BytesRead); } while (this._currentCommand.continueReading()); } } else { // throw? DG200FileLogger.Log("DG200SerialConnection Execute method called without a command. Doing nothing instead.", 1); } }
/// <summary> /// Process our local buffer. /// </summary> protected override void processBuffer() { this.getCurrentBuffer().Position = BaseCommandResult.PAYLOAD_START; byte[] tmpArr = new byte[2]; this.getCurrentBuffer().Read(tmpArr, 0, tmpArr.Length); int sessionHeaderCount = bigEndianArrayToInt16(tmpArr); // If the session said no track headers remain, don't parse and set the start val to false. if (sessionHeaderCount != 0) { this._headerCount += sessionHeaderCount; DG200FileLogger.Log("GetDGTrackHeadersCommandResult found " + sessionHeaderCount + " track headers this iteration. Total now: " + this._headerCount, 3); this.getCurrentBuffer().Read(tmpArr, 0, tmpArr.Length); this._nextTrackId = bigEndianArrayToInt16(tmpArr); DG200FileLogger.Log("GetDGTrackHeadersCommandResult reading next track ID " + this._nextTrackId + ".", 3); this._additionalSession = true; for (int inx = 0; inx < sessionHeaderCount; inx++) { byte[] headerBuf = new byte[GetDGTrackHeadersCommandResult.HEADERSIZE]; this.getCurrentBuffer().Read(headerBuf, 0, headerBuf.Length); this._trackHeaders.Add(new DGTrackHeader(headerBuf)); } } else { //this._nextTrackId = 0; DG200FileLogger.Log("GetDGTrackHeadersCommandResult no additional headers found.", 3); this._additionalSession = false; } }
/// <summary> /// Constructor /// </summary> /// <param name="resultBuf">The buffer with the result of the command.</param> public GetDGTrackHeadersCommandResult() : base() { DG200FileLogger.Log("GetDGTrackHeadersCommandResult constructor.", 3); this._headerCount = 0; this._additionalSession = true; // We always do at least two. this._trackHeaders = new List <DGTrackHeader>(); }
/// <summary> /// Constructor. Requires the name of the port where the DG200 is connected. /// </summary> /// <param name="portName">Where to find the DG200.</param> public DG200SerialConnection(string portName) { DG200FileLogger.Log("DG200SerialConnection constructor. Port: " + portName, 3); _portName = portName; _status = ConnectionStatus.UNKNOWN; this._outputter = null; this._currentCommand = null; }
/// <summary> /// Run the command. /// </summary> public void execute() { DG200FileLogger.Log("BaseCommand execute method.", 3); do { this._serialConnection.Execute(); }while(this.doAnotherSession()); }
/// <summary> /// Constructor. Calls the parent to initialize. /// </summary> public GetDGTrackHeadersCommand() : base() { DG200FileLogger.Log("GetDGTrackHeadersCommand constructor.", 3); this._startingTrackIndex = 0; this._locResult = new GetDGTrackHeadersCommandResult(); this._session = new GetDGTrackHeadersSession(); this._session.setResult(this._locResult); this._currentResult = this._locResult; }
/// <summary> /// Generate a list of track header start times, formatted in UTC. /// </summary> /// <returns>A newline-delimited list of track header start times.</returns> public override string ToString() { string headers = ""; DG200FileLogger.Log("Writing to string. trackheader count is: " + this._trackHeaders.Count, 3); foreach (DGTrackHeader th in this._trackHeaders) { headers += th.ToString() + "\n"; } return(headers); }
public MainWindow() { InitializeComponent(); DataContext = this; DG200FileLogger.setLevel(3); this._ports = new PortScanner(); this.initializeObservers(); this.populatePortList(); }
/// <summary> /// Read the payload size data and store the result internally. /// </summary> protected void evaluatePayloadSizeData() { // Once we get enough data in the buffer, evaluate the payload size. if (!this._sizeDataEvaluated && this.getCurrentBuffer().Length > 3) { this._sizeDataEvaluated = true; byte[] sizeArr = new byte[2]; this.getCurrentBuffer().Position = 2; this.getCurrentBuffer().Read(sizeArr, 0, 2); this.getCurrentBuffer().Position = this.getCurrentBuffer().Length; this._expectedByteCount = this.calculateExpectedBytes(sizeArr); DG200FileLogger.Log("BaseSession calculated payload size: " + this._expectedByteCount, 3); this.overrideExpectedByteCount(); } }
/// <summary> /// Override the save-to-buffer method. Because we do sneaky math before passing to the result. /// </summary> /// <param name="bytes">The array of incoming bytes</param> /// <param name="byteCount">The number of useful bytes in the incoming array (not all of the array is always useful)</param> public override void Write(byte[] bytes, Int32 byteCount) { int bytesCopiedFromFirstSession = 0, newInputOffset = 0; // First, do we have enough bytes to start copying the second payload? // We only want to do this once. if (!this._movedSessionPointer && this.getCurrentBuffer().Length + byteCount >= GetDGTrackFileSession.SECOND_SESSION_PAYLOAD_START) { DG200FileLogger.Log("GetDGTrackFileSession evaluating data merge criteria.", 2); // If so, see if we need to copy any remaining data from the first session. if (this.getCurrentBuffer().Length < GetDGTrackFileSession.FIRST_PAYLOAD_END) { DG200FileLogger.Log("GetDGTrackFileSession: First payload not saved, doing that now.", 2); // Do the math and write with a custom offset. Casting, ugh. bytesCopiedFromFirstSession = unchecked ((int)(GetDGTrackFileSession.FIRST_PAYLOAD_END - this.getCurrentBuffer().Length)); this.getCurrentBuffer().Write(bytes, 0, bytesCopiedFromFirstSession); // The new starting offset for the second read. newInputOffset = bytesCopiedFromFirstSession + GetDGTrackFileSession.SPACE_BETWEEN_PAYLOADS; } else { DG200FileLogger.Log("GetDGTrackFileSession: First payload saved, changing buffer pointer and copying.", 2); // Calculate how many extraneous bytes were copied into the buffer on the last pass. bytesCopiedFromFirstSession = unchecked ((int)(this.getCurrentBuffer().Length - GetDGTrackFileSession.FIRST_PAYLOAD_END)); newInputOffset = GetDGTrackFileSession.SPACE_BETWEEN_PAYLOADS - bytesCopiedFromFirstSession; // Move the pointer to the end of the first buffer this.getCurrentBuffer().Position = GetDGTrackFileSession.FIRST_PAYLOAD_END; } DG200FileLogger.Log("GetDGTrackFileSession: Writing with modified buffer.", 2); this.getCurrentBuffer().Write(bytes, newInputOffset, byteCount - newInputOffset); this._movedSessionPointer = true; } else { DG200FileLogger.Log("GetDGTrackFileSession doing regular write.", 2); this.getCurrentBuffer().Write(bytes, 0, byteCount); } // Figure out what we have. this.evaluateData(); }
/// <summary> /// Generates the track points from the internal buffer. /// </summary> /// <returns>Yields track points that are stored.</returns> public IEnumerable <IDGTrackPoint> getTrackPoints() { DG200FileLogger.Log("DGTrackPointFactory: Starting processing.", 3); // Send back the first one. yield return(this._firstTrack); byte[] readArr = new byte[this._expectedBytes]; this._buf.Read(readArr, 0, readArr.Length); // Then move through the buffer and start building tracks. while (this._buf.Position != this._buf.Length && !this.endingBytes(readArr)) { yield return(this.getTrackPoint(readArr)); this._buf.Read(readArr, 0, readArr.Length); } DG200FileLogger.Log("DGTrackPointFactory: Completed processing.", 3); }
protected override void processDateTime() { // time is bytes 8-11, date is bytes 12-15. ArraySegment <byte> timeSeg = new ArraySegment <byte>(this._rawData, 8, 4); string t = this.intToSixDigitString(DG200Utils.bigEndianArrayToInt32(timeSeg)); ArraySegment <byte> dateSeg = new ArraySegment <byte>(this._rawData, 12, 4); string d = this.intToSixDigitString(DG200Utils.bigEndianArrayToInt32(dateSeg)); try { this._dt = new DateTime(this.getYear(d), Int32.Parse(d.Substring(2, 2)), Int32.Parse(d.Substring(0, 2)), Int32.Parse(t.Substring(0, 2)), Int32.Parse(t.Substring(2, 2)), Int32.Parse(t.Substring(4, 2)), DateTimeKind.Utc); } catch (Exception e) { DG200FileLogger.Log("DGPositionDateTimeTrackPoint: Unable to process date value (" + t + "). Setting to GPS epoch.", 2); base.processDateTime(); } }
public virtual bool continueReading() { // If we haven't even seen the payload size data, say yes. if (!this._sizeDataEvaluated) { DG200FileLogger.Log("BaseSession has not received enough bytes to evaluate size.", 3); return(true); } else { DG200FileLogger.Log("BaseSession comparing bytes read (" + this.getCurrentBuffer().Length + ") to expected (" + this._expectedByteCount + ").", 3); bool _continue = this.getCurrentBuffer().Length < this._expectedByteCount; if (!_continue) { DG200FileLogger.Log("BaseSession has read expected amount data and is starting the result processing.", 3); // Initialize the result and save the value on requesting an additional session. this.processResult(); } return(_continue); } }
public bool serialize() { string firstTrackHeaderTime = ""; ISet <GetDGTrackFileCommandResult> tfResults = new HashSet <GetDGTrackFileCommandResult>(); // open file XmlWriter wr = XmlWriter.Create(this._outputPath); // write processing instruction and required intro data wr.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-8'"); //<gpx version="1.1" creator="DG-200 ToolBox v1.1.20.237" xmlns="http://www.topografix.com/GPX/1/1" //xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" //xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd"> wr.WriteStartElement("gpx", "http://www.topografix.com/GPX/1/1"); wr.WriteAttributeString("version", "1.1"); wr.WriteAttributeString("creator", "DG-200"); wr.WriteAttributeString("xsi", "schemaLocation", "http://www.w3.org/2001/XMLSchema-instance", "http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd"); wr.WriteStartElement("trk"); wr.WriteElementString("name", firstTrackHeaderTime); wr.WriteElementString("src", "DG-200"); wr.WriteStartElement("trkseg"); DG200FileLogger.Log("Starting loop through track header entries.", 3); foreach (TrackHeaderEntry the in this._trackHeaderEntries) { DG200FileLogger.Log("Working on track header entry.", 3); if (firstTrackHeaderTime == "") { firstTrackHeaderTime = the.DateTimeString; } DG200FileLogger.Log("Going to do the retrieving. This many trackids expected: " + the.getTrackIds().Count, 3); foreach (int tfId in the.getTrackIds()) { DG200FileLogger.Log("Starting on track id: " + tfId, 3); GetDGTrackFileCommandResult res = this.getTrackFile(tfId); DG200FileLogger.Log("Got a track file. Going to write the entries.", 3); foreach (IDGTrackPoint tp in res.getTrackPoints()) { if (!tp.isWayPoint()) { this.writeTrackPoint(tp, wr, "trkpt"); } else { this._waypoints.Add(tp); } } DG200FileLogger.Log("Done writing entries. Moving to the next track file.", 3); } } /* * foreach(GetDGTrackFileCommandResult res in tfResults) * { * foreach(IDGTrackPoint tp in res.getTrackPoints()) * { * this.writeTrackPoint(tp, wr); * } * }*/ wr.WriteEndElement(); // trkseg wr.WriteEndElement(); // trk this.writeWayPoints(wr); wr.WriteEndElement(); // gpx // loop through entries // if a waypoint, copy that entry to the waypoints set // else, serialize // close trkseg // write waypoints // write closing tag // close file wr.Close(); return(true); }
/// <summary> /// We override the byte count since the DG200 sends two sessions in response to one command request. /// </summary> protected override void overrideExpectedByteCount() { // The track file command sends two sessions at once. this._expectedByteCount = GetDGTrackFileSession.ACTUAL_PAYLOAD_BYTE_COUNT; DG200FileLogger.Log("GetDGTrackFileSession overriding expected byte count: " + this._expectedByteCount, 2); }
/// <summary> /// Initializes the command. Since commands can be reused, this method resets the command for reuse. /// </summary> public void initialize() { DG200FileLogger.Log("BaseCommand initialize.", 3); }
protected BaseCommandResult() { DG200FileLogger.Log("BaseCommandResult constructor.", 3); this._buffers = new List <CommandBuffer>(); }
/// <summary> /// This command requires a manual override on the data size value b/c it comes back wrong. /// </summary> protected override void overrideExpectedByteCount() { this._expectedByteCount += 4; DG200FileLogger.Log("SetDGIDSession overriding expected byte count: " + this._expectedByteCount, 2); }