public static int Command(this ITestableNetworkStream netStream, char commandkey) { try { if (netStream.CanWrite && Commands.ContainsKey(commandkey)) { System.Diagnostics.Debug.WriteLine("\t\tTransmit Command('{0}'): ASC({1} --> 0x{2})", commandkey, Commands[commandkey].Key, BitConverter.ToString(Noxon.IntToByteArray(Commands[commandkey].Key))); netStream.Write(Noxon.IntToByteArray(Commands[commandkey].Key), 0, sizeof(int)); return(0); } else { return(-1); } } catch (System.IO.IOException e) { System.Diagnostics.Debug.WriteLine("\t\tTransmit Command() failed ({0})", e.Message); Close(); Open(); if (netStream != null && netStream.CanWrite) { netStream.Write(Noxon.IntToByteArray(Commands[commandkey].Key), 0, sizeof(int)); } return(0); } catch { return(-1); } }
public static bool Get() { Noxon.ResetListMinMax(); Macro mf = new iRadio.Macro("Favorites.Get.F", new string[] { "F", "F" }); // select (F)avorites, 2 times to update Show.lastBrowsedLines while (mf.Step()) { ; } if (Noxon.GetListMinMax(out int min, out int max) && Show.lastBrowsedTitle == iRadioConsole.Properties.Resources.NoxonTitleFavorites) { favlist.Clear(); int entries = max - min + 1; for (int i = 0; i < Math.Min(Noxon.ListLines, entries); i++) // first 4 (or less) { if (Show.lastBrowsedLines[i] != "") { favlist.Add(new Fav(Show.lastBrowsedLines[i], Show.lastBrowsedFlags[i])); System.Diagnostics.Debug.WriteLine("Favorites.Get(): fav list[{0}] = {1}", favlist.Count, favlist[favlist.Count - 1]); } } if (entries > 4) { Macro md3 = new iRadio.Macro("Favorites.Get.D", new string[] { "D", "D", "D" }); // scroll 3 entries down to 4th while (md3.Step()) { ; } for (int i = Noxon.ListLines; i < entries; i++) { Macro md = new iRadio.Macro("Favorites.Get.D", new string[] { "D" }); while (md.Step()) { ; } favlist.Add(new Fav(Show.lastBrowsedLines[3], Show.lastBrowsedFlags[3])); System.Diagnostics.Debug.WriteLine("Favorites.Get(): fav list[{0}] = {1}", favlist.Count, favlist[favlist.Count - 1]); } } Macro mh = new iRadio.Macro("Favorites.Get.D", new string[] { "H" }); // home again while (mh.Step()) { ; } } return(true); }
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); } }
public static async Task <bool> OpenAsync() { if (Noxon.tcpClient != null) { Noxon.tcpClient.Close(); round--; } Noxon.tcpClient = new TcpClient(); IPAddress ip = Noxon.IP; try { await Noxon.tcpClient.ConnectAsync(ip, 10100); // connect to iRadio server port if (Noxon.netStream != null) { Noxon.netStream.Close(); } Noxon.netStream = new TestableNetworkStream(Noxon.tcpClient.GetStream()); round++; System.Diagnostics.Debug.WriteLine("OpenAsync(), round = {0}", round); return(true); } catch (SocketException se) { Console.WriteLine("Connect to NOXON iRadio failed ({0}, {1}), now try all IPs on gateway", se.SocketErrorCode, se.Message); } if (Noxon.PingHosts()) { ip = Noxon.IP; } await Noxon.tcpClient.ConnectAsync(ip, 10100); // connect to iRadio server port Noxon.netStream = new TestableNetworkStream(Noxon.tcpClient.GetStream()); Noxon.tcpClient.Client.LingerState = new LingerOption(false, 0); return(true); }
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); }