public async static Task <int> CommandAsync(this NetworkStream netStream, char commandkey) { try { if (netStream.CanWrite && Noxon.Commands.ContainsKey(commandkey)) { System.Diagnostics.Debug.WriteLine("\t\tTransmit CommandAsync('{0}'): ASC({1} --> 0x{2})", commandkey, Noxon.Commands[commandkey].Key, BitConverter.ToString(Noxon.IntToByteArray(Noxon.Commands[commandkey].Key))); if (Properties.Settings.Default.LogCommands) { Program.FormShow.Log(Form1.ParsedElementsWriter, Form1.StdOut, new XElement("CommandAsync", commandkey)); } await netStream.WriteAsync(Noxon.IntToByteArray(Noxon.Commands[commandkey].Key), 0, sizeof(int)); return(0); } else { return(-1); } } catch (System.IO.IOException e) { System.Diagnostics.Debug.WriteLine("\t\tTransmit CommandAsync() failed ({0})", e.Message); Noxon.Close(); Task <bool> isOPen = NoxonAsync.OpenAsync(); await isOPen; if (netStream != null && netStream.CanWrite) { await netStream.WriteAsync(Noxon.IntToByteArray(Noxon.Commands[commandkey].Key), 0, sizeof(int)); } return(0); } catch { return(-1); } }
static void Main(string[] args) { Noxon.Testmode = false; if (args.Length > 0) { Console.WriteLine("args not yet implemented."); } FileStream ostrm1, ostrm2; // pepare to re-direct Console.WriteLine StreamWriter nonParsedElementsWriter, parsedElementsWriter; TextWriter stdOut = Console.Out; Show.columnBrowse = Console.BufferWidth / 2 + 2; Show.columnHeader = Console.BufferWidth / 2 - 5; unShowKeyPressedTimer = new System.Timers.Timer(2000); // reset key display after a second or two unShowKeyPressedTimer.Elapsed += ResetShowKeyPressed; keyPressedTimer = new System.Timers.Timer(100); // loop console for key press keyPressedTimer.Elapsed += ProcessKeyPressed; keyPressedTimer.Start(); try { ostrm1 = new FileStream("./iRadio-non-parsed-elements.txt", FileMode.Create, FileAccess.Write); nonParsedElementsWriter = new StreamWriter(ostrm1); ostrm2 = new FileStream("./iRadio-logging.txt", FileMode.Create, FileAccess.Write); parsedElementsWriter = new StreamWriter(ostrm2); } catch (Exception e) { Console.WriteLine("Cannot open iRadio .txt files for writing"); Console.WriteLine(e.Message); return; } if (Noxon.Testmode) { Console.SetOut(nonParsedElementsWriter); // re-direct to file Console.WriteLine("iRadio play data:"); string markup = @" <update id=""play"" > <value id =""timep"" min=""0"" max=""65535"" > 1698 </value > </update > <update id=""play"" > <value id =""timep"" min=""0"" max=""65535"" > 1699 </value > </update > "; // if missing: unexpected end of file. Elements not closed: Root. IEnumerable <string> playData = from el in StreamiRadioDoc(new StringReader(markup)) where (string)el.Attribute("id") == "play" select(string) el.Element("value"); foreach (string str in playData) { Console.WriteLine(str); } } if (Noxon.Testmode) { Console.WriteLine("iRadio Telnet.xml:"); Console.SetOut(stdOut); // stop re-direct // use Console cursor control from now on ConsoleShow.Header(); StreamReader TelnetFile = new StreamReader("Telnet.xml"); IEnumerable <XElement> iRadioData = from el in StreamiRadioDoc(TelnetFile) select el; Noxon.Parse(iRadioData, null, nonParsedElementsWriter, stdOut, ConsoleShow); // don't log parsed elements } if (Noxon.Testmode) { CloseStreams(ostrm1, ostrm2, nonParsedElementsWriter, parsedElementsWriter); Environment.Exit(1); } // https://docs.microsoft.com/de-de/dotnet/csharp/programming-guide/concepts/linq/how-to-stream-xml-fragments-from-an-xmlreader ConsoleShow.Header(); while (true) { Noxon.Open(); IEnumerable <XElement> iRadioNetData = from el in StreamiRadioNet(Noxon.netStream) select el; Noxon.Parse(iRadioNetData, parsedElementsWriter, nonParsedElementsWriter, stdOut, ConsoleShow); // new Thread(delegate () {Noxon.Parse(Noxon.netStream, iRadioNetData, parsedElementsWriter, nonParsedElementsWriter, stdOut); }).Start(); Noxon.Close(); } // CloseStreams(ostrm1, ostrm2, nonParsedElementsWriter, parsedElementsWriter); }