private bool ParseSecondLine(string secondLine) { try { if (secondLine.Length < 2) { SimpleFileWriter.WriteLineToEventFile(DirectoryName.Diag, "CeiloMsg61Parser: 2nd line is empty or contains only 1 character:" + secondLine); return(false); } int iDetectionStatus; cloudHeightDetectionStatus = secondLine[0].ToString(); var detectionStatusIsInteger = Validation.TryParseInt(cloudHeightDetectionStatus, out iDetectionStatus); alarmsAndWarningsInformation = secondLine[1].ToString(); char[] spaceSeparator = { ' ' }; var groups = secondLine.Substring(2).Split(spaceSeparator); var groupCount = groups.GetLength(0); if (detectionStatusIsInteger) { if (iDetectionStatus == 0 || iDetectionStatus == 5) { } else if (iDetectionStatus == 1 && groupCount > 1) { lowestCloudBase = groups[1]; } else if (iDetectionStatus == 2 && groupCount > 2) { lowestCloudBase = groups[1]; secondLowestCloudBase = groups[2]; } else if (iDetectionStatus == 3 && groupCount > 3) { lowestCloudBase = groups[1]; secondLowestCloudBase = groups[2]; highestCloudBase = groups[3]; } else if (iDetectionStatus == 4 && groupCount > 4) { verticalVisibility = groups[1]; highestSignal = groups[2]; } } if (groupCount > 4) { alarmsAndWarnings = groups[4]; } return(true); } catch (Exception ex) { var msg = "Exception parsing second line"; if (secondLine != null) { //var sb = new StringBuilder(); //for (var i = 0; i< secondLine.Length; i++) // msg. msg = msg + secondLine; } ExceptionHandler.HandleException(ex, msg); return(false); } }
/// <summary> /// Open a connection to the given socket /// </summary> /// <param name="ipAddress"></param> /// <param name="tcpPort"></param> /// <returns></returns> public async Task <bool> ConnectAsync(IPAddress ipAddress, int tcpPort) { try { InitializeConnection(ipAddress, tcpPort); await tcpClient.Client.ConnectAsync(IpAddr, Port); return(true); } catch (SocketException ex) { var msg = string.Format(CultureInfo.InvariantCulture, "Please check connection port parameters: {0} ", ex.Message); if (null != IpAddr) { msg += " Ip: " + IpAddr; } #if DEBUG SimpleFileWriter.WriteLineToEventFile(DirectoryName.Diag, msg); #endif Debug.Assert(true, msg); // keep this delay above 100 ms. If the connection is broken, the system will try again Thread.Sleep(250); ConnectionState = NamedPortState.Disconnected; // Debug.WriteLine(DateTimeEx.NowToString + msg); return(false); } catch (ArgumentOutOfRangeException ex) { // port is not between System.Net.IPEndPoint.MinPort and System.Net.IPEndPoint.MaxPort. Debug.Assert(false, " port is not between System.Net.IPEndPoint.MinPort and System.Net.IPEndPoint.MaxPort." + ex.Message); ExceptionRecorder.RecordException(" port is not between System.Net.IPEndPoint.MinPort and System.Net.IPEndPoint.MaxPort." + ex.Message); ConnectionState = NamedPortState.Disconnected; Debug.WriteLine(DateTimeEx.NowToString + " port is not between System.Net.IPEndPoint.MinPort and System.Net.IPEndPoint.MaxPort."); return(false); } catch (ObjectDisposedException ex) { ExceptionRecorder.RecordException(string.Format(CultureInfo.InvariantCulture, "Connect. - ObjectDisposedException: {0}", ex.Message)); Debug.Assert(false, " System.Net.Sockets.TcpClient is closed" + ex.Message); Debug.WriteLine(DateTimeEx.NowToString + " System.Net.Sockets.TcpClient is closed" + ex.Message); ConnectionState = NamedPortState.Disconnected; throw; } catch (ArgumentNullException ex) { ExceptionRecorder.RecordException("hostname is null " + ex.Message); ConnectionState = NamedPortState.Disconnected; throw; } catch (System.Security.SecurityException ex) { ExceptionRecorder.RecordException(string.Format(CultureInfo.InvariantCulture, "Connect. - SecurityException: {0}", ex.Message)); Debug.WriteLine(DateTimeEx.NowToString + " Connect. - SecurityException:" + ex.Message); ConnectionState = NamedPortState.Disconnected; throw; } catch (Exception ex) { ExceptionHandler.HandleException(ex); return(false); } } // Connect
private bool ParseDataItems(string line) { try { if (line.Length < 2) { SimpleFileWriter.WriteLineToEventFile(DirectoryName.Diag, "PwdMsg7Parser: line is empty or contains only 1 character:" + line); return(false); } //int iDetectionStatus; visibilityAlarm = line[0].ToString(); // bool detectionStatusIsInteger = Validation.TryParseInt(visibilityAlarm, out iDetectionStatus); alarmsAndWarningsInformation = line[1].ToString(); var remainingLine = line.Substring(2); while (remainingLine.Contains(" ")) { remainingLine = remainingLine.Replace(" ", " ").Trim(); } char[] spaceSeparator = { ' ' }; var dataItems = remainingLine.Split(spaceSeparator, StringSplitOptions.RemoveEmptyEntries); var itemCount = dataItems.GetLength(0); if (itemCount > 0) { visibility1MinAvg = dataItems[0]; } if (itemCount > 1) { visibility10MinAvg = dataItems[1]; } if (itemCount > 2) { instantPresentWeatherNwsCode = dataItems[2]; } if (itemCount > 3) { instantPresentWeather = dataItems[3]; } if (itemCount > 4) { presentWeather15m = dataItems[4]; } if (itemCount > 5) { presentWeather1h = dataItems[5]; } if (itemCount > 6) { precipitationIntensity = dataItems[6]; } if (itemCount > 7) { cumulativeWaterSum = dataItems[7]; } if (itemCount > 8) { cumulativeSnowSum = dataItems[8]; } if (itemCount > 9) { temperature = dataItems[9]; } if (itemCount > 10) { backgroundLuminance = dataItems[10]; } return(true); } catch (Exception ex) { var msg = "Exception parsing data items on first line"; if (line != null) { //var sb = new StringBuilder(); //for (var i = 0; i< secondLine.Length; i++) // msg. msg = msg + line; } ExceptionHandler.HandleException(ex, msg); return(false); } }
new internal bool Parse(string data, out MeasMsg measMsg) { var progress = 0; measMsg = new MeasMsg(); try { if (string.IsNullOrEmpty(data)) { SimpleFileWriter.WriteLineToEventFile(DirectoryName.Diag, "PwdMsg7Parser: null or empty input data."); return(false); } progress++; int indexOfSOH = data.IndexOf('\x01'); if (indexOfSOH > -1) { data = data.Substring(indexOfSOH); } char[] charsToTrim = { '\n', '\r' }; data = data.TrimStart(charsToTrim); char[] separator = { '\n' }; var lines = data.Split(separator); if (!CheckLines(lines)) { return(false); } if (lines.GetLength(0) < 3) { var error = GetErrorData(data); SimpleFileWriter.WriteLineToEventFile(DirectoryName.Diag, "PwdMsg7Parser: incomplete message. Lines:" + lines.GetLength(0) + data + " " + error); return(false); } progress++; string pw1 = lines[0].Substring(0, 5).Trim(invalidChars).Replace("\r", "").Replace("\n", "").Replace('\x01', ' ').Replace('\x02', ' ').Replace(" ", string.Empty); if (!Validation.IsValidFileName(pw1) || !Validation.IsValidDirectoryName(pw1)) { var error = GetErrorData(data); SimpleFileWriter.WriteLineToEventFile(DirectoryName.Diag, "PwdMsg7Parser: invalid station name. Lines:" + lines.GetLength(0) + data + " " + error); return(false); } measMsg.Station = pw1; measMsg.Time = DateTimeEx.Now; ResetVariables(); progress++; const char STX = '\x02'; var stxIndex = lines[0].IndexOf(STX); if (stxIndex < 0) { return(false); } var tmp = lines[0].Substring(stxIndex + 1).Trim(charsToTrim); if (!ParseDataItems(tmp)) { return(false); } progress++; measMsg.AddMeas(new Meas(GetVariableName("PWD_VISALARM"), measMsg.Time, visibilityAlarm, MeasStatus.c*K, measMsg.Station)); measMsg.AddMeas(new Meas(GetVariableName("PWD_WARNING"), measMsg.Time, alarmsAndWarningsInformation, MeasStatus.c*K, measMsg.Station)); measMsg.AddMeas(new Meas(GetVariableName("VISAVG1M"), measMsg.Time, visibility1MinAvg, MeasStatus.c*K, measMsg.Station)); measMsg.AddMeas(new Meas(GetVariableName("VISAVG10M"), measMsg.Time, visibility10MinAvg, MeasStatus.c*K, measMsg.Station)); measMsg.AddMeas(new Meas(GetVariableName("NWSCODE"), measMsg.Time, instantPresentWeatherNwsCode, MeasStatus.c*K, measMsg.Station)); measMsg.AddMeas(new Meas(GetVariableName("PW"), measMsg.Time, instantPresentWeather, MeasStatus.c*K, measMsg.Station)); measMsg.AddMeas(new Meas(GetVariableName("PW15M"), measMsg.Time, presentWeather15m, MeasStatus.c*K, measMsg.Station)); measMsg.AddMeas(new Meas(GetVariableName("PW1H"), measMsg.Time, presentWeather1h, MeasStatus.c*K, measMsg.Station)); measMsg.AddMeas(new Meas(GetVariableName("PRFAVG1M"), measMsg.Time, precipitationIntensity, MeasStatus.c*K, measMsg.Station)); measMsg.AddMeas(new Meas(GetVariableName("PRSUM24H"), measMsg.Time, cumulativeWaterSum, MeasStatus.c*K, measMsg.Station)); measMsg.AddMeas(new Meas(GetVariableName("SNOWSUM24H"), measMsg.Time, cumulativeSnowSum, MeasStatus.c*K, measMsg.Station)); measMsg.AddMeas(new Meas(GetVariableName("PWD_TA"), measMsg.Time, temperature, MeasStatus.c*K, measMsg.Station)); measMsg.AddMeas(new Meas(GetVariableName("LMAVG1M"), measMsg.Time, backgroundLuminance, MeasStatus.c*K, measMsg.Station)); progress++; if (lines.GetLength(0) > 1) { progress++; string line = (lines[1].Trim(charsToTrim)); progress++; measMsg.AddMeas(new Meas(GetVariableName("METARPWCODE"), measMsg.Time, line, MeasStatus.c*K, measMsg.Station)); } if (lines.GetLength(0) > 2) { progress++; string line = (lines[2].Trim(charsToTrim)); progress++; measMsg.AddMeas(new Meas(GetVariableName("METARRWCODE"), measMsg.Time, line, MeasStatus.c*K, measMsg.Station)); } return(true); } catch (Exception ex) { ExceptionRecorder.RecordException("Error in parsing pwd message. Progress:" + progress + " data:" + data, ex); return(false); } }
private void MarkClientToBeRemoved(INamedPort client) { clientsToBeRemoved.Add(client.RemoteSocketName); SimpleFileWriter.WriteLineToEventFile(DirectoryName.Diag, "clientsToBeRemoved.Count:" + clientsToBeRemoved.Count + "\tMarkClientToBeRemoved:" + client.RemoteSocketName); }
protected static void HandleError(string msg) { SimpleFileWriter.WriteLineToEventFile(DirectoryName.EventLog, msg); }