/// <summary> /// Method to determine if the incoming APRS packet has the comment we're searching for /// </summary> /// <param name="oPacket"></param> /// <returns></returns> private bool PacketHasSearchComment(AprsPacket oPacket) { bool bReturn = true; if (!string.IsNullOrWhiteSpace(sCommentSearch)) { bReturn = oPacket.Comment.Contains(sCommentSearch); } return(bReturn); }
/// <summary> /// Sets up a dictionary of data to be passed when updating the position of the spotter. /// </summary> /// <param name="oPacket"></param> /// <returns></returns> private Dictionary <string, object> SetupDataForUpdate(AprsPacket oPacket) { return(new Dictionary <string, object> { { Constants.PositionDetailFields.ID, sApplicationID }, { Constants.PositionDetailFields.REPORT_AT, oPacket.TimeStamp.TimeStamp.Value.ToString(Constants.Utility.DATE_TIME_FORMAT) }, { Constants.PositionDetailFields.LAT, oPacket.Position.CoordinateSet.Latitude.Value }, { Constants.PositionDetailFields.LON, oPacket.Position.CoordinateSet.Longitude.Value }, { Constants.PositionDetailFields.DIR, oPacket.Position.Course }, { Constants.PositionDetailFields.ACTIVE, 1 }, { Constants.PositionDetailFields.GPS, 1 } }); }
/// <summary> /// Method to update the main textbox with information from the latest APRS packet received /// </summary> /// <param name="oPacket"></param> private void UpdateTextBox(AprsPacket oPacket) { txtDebug.Text = $"Callsign: {oPacket.SourceCallsign.StationCallsign}{Environment.NewLine}"; txtDebug.Text += $"Time: {oPacket.TimeStamp.TimeStamp}{Environment.NewLine}"; txtDebug.Text += $"Latitude {oPacket.Position.CoordinateSet.Latitude.Value}{Environment.NewLine}"; txtDebug.Text += $"Longitude: {oPacket.Position.CoordinateSet.Longitude.Value}{Environment.NewLine}"; txtDebug.Text += $"Elevation: {oPacket.Position.Altitude}{Environment.NewLine}"; txtDebug.Text += $"Speed: {oPacket.Position.Speed}{Environment.NewLine}"; txtDebug.Text += $"Dir: {oPacket.Position.Course}{Environment.NewLine}"; //txtDebug.Text += $"Status: {oPacket.InformationField}{Environment.NewLine}"; txtDebug.Text += $"Type: {oPacket.DataType}{Environment.NewLine}"; txtDebug.Text += $"Message: {oPacket.MessageData.MsgText}{Environment.NewLine}"; txtDebug.Text += $"Comment: {oPacket.Comment}"; }
public void LogAPRSPacket(AprsPacket oPacket) { lock (oLock) { List <string> lstsLines = new List <string>(); lstsLines.Add($"{DateTime.UtcNow.ToString(Constants.Utility.DATE_TIME_FORMAT + ".ffffff")}: Received Packet "); lstsLines.Add($"Callsign: {oPacket.SourceCallsign.StationCallsign}"); lstsLines.Add($"Time: {oPacket.TimeStamp.TimeStamp}"); lstsLines.Add($"Latitude {oPacket.Position.CoordinateSet.Latitude.Value}"); lstsLines.Add($"Longitude: {oPacket.Position.CoordinateSet.Longitude.Value}"); lstsLines.Add($"Elevation: {oPacket.Position.Altitude}"); lstsLines.Add($"Speed: {oPacket.Position.Speed}"); lstsLines.Add($"Dir: {oPacket.Position.Course}"); //lstsLines.Add($"Status: {oPacket.InformationField}"); lstsLines.Add($"Type: {oPacket.DataType}"); lstsLines.Add($"Message: {oPacket.MessageData.MsgText}"); lstsLines.Add($"Comment: {oPacket.Comment}"); lstsLines.Add("***"); File.AppendAllLines($"{Directory.GetCurrentDirectory()}\\Logs\\{sLogFileName}", lstsLines); } }
/// <summary> /// Method run by a thread to read information from APRS-IS and update SpotterNetwork /// </summary> private void ReadAPRS() { NetworkStream nsStream = tcpClient.GetStream(); AprsPacket oPacket; while (bRunUpdate) { byte[] yarData = new byte[256]; if (!nsStream.CanRead || !nsStream.DataAvailable) { continue; } int iByteCount = nsStream.Read(yarData, 0, yarData.Length); if (iByteCount > 0) { oPacket = new AprsPacket(); string sRead = Encoding.ASCII.GetString(yarData, 0, iByteCount); try { if (oPacket.Parse(sRead) && (oPacket.DataType.Equals(PacketDataType.Position) || oPacket.DataType.Equals(PacketDataType.PositionMsg) || oPacket.DataType.Equals(PacketDataType.PositionTime) || oPacket.DataType.Equals(PacketDataType.PositionTimeMsg) || oPacket.DataType.Equals(PacketDataType.MicE)) && PacketHasSearchComment(oPacket)) { if (oPacket.DataType.Equals(PacketDataType.PositionTime) || oPacket.DataType.Equals(PacketDataType.PositionTimeMsg)) { DateTime dtLastReported = new DateTime(); HttpResponseMessage oSNPositionResponse = SendPost(SetupDataForRequest(), Constants.URLS.GET_POSITIONS); if (oSNPositionResponse.IsSuccessStatusCode) { string oResult = oSNPositionResponse.Content.ReadAsStringAsync().Result; Dictionary <string, Dictionary <string, string>[]> dictResponses = JsonConvert.DeserializeObject <Dictionary <string, Dictionary <string, string>[]> >(oResult); dtLastReported = DateTime.Parse(dictResponses[Constants.PositionDetailFields.POSITIONS][0][Constants.PositionDetailFields.REPORT_AT]); if (chkLog.Checked && bLogAll) { oLogging.LogHTTPResponse(oSNPositionResponse); } } else { if (chkLog.Checked) { oLogging.LogHTTPResponse(oSNPositionResponse); } } if (!dtLastReported.Equals(DateTime.MinValue) && !oPacket.TimeStamp.IsLocalTime && oPacket.TimeStamp.TimeStamp < dtLastReported) { continue; } } if (oPacket.TimeStamp.TimeStamp == null || oPacket.TimeStamp.TimeStamp.Equals(DateTime.MinValue)) { oPacket.TimeStamp.TimeStamp = DateTime.UtcNow; } if (chkLog.Checked && bLogAll) { oLogging.LogAPRSPacket(oPacket); } txtDebug.Invoke(UpdateTextboxDelegate, oPacket); HttpResponseMessage oPositionUpdateReponse = SendPost(SetupDataForUpdate(oPacket), Constants.URLS.UPDATE_POSITIONS); if ((!oPositionUpdateReponse.IsSuccessStatusCode || bLogAll) && chkLog.Checked) { oLogging.LogHTTPResponse(oPositionUpdateReponse); } } } catch (Exception ex) { if (chkLog.Checked) { oLogging.LogException(ex); } } } } nsStream.Close(); if (chkLog.Checked) { oLogging.LogInformation("Thread terminated."); } }