public static void Main(string[] args) { House thisHouse = new House(); ConsoleOutputLogger.verbose = true; ConsoleOutputLogger.writeLogfile = true; Console.WriteLine("ELV MAX! Debug Tool version 1 (C) Daniel Kirstenpfad 2012"); Console.WriteLine(); // not enough paramteres given, display help if (args.Length < 2) { Console.WriteLine("Syntax:"); Console.WriteLine(); Console.WriteLine("\tmaxdebug <hostname/ip> <port (e.g. 62910)> [commands]"); Console.WriteLine(); return; } ConsoleOutputLogger.LogToFile("--------------------------------------"); // we obviously have enough paramteres, go on and try to connect TcpClient client = new TcpClient(); client.Connect(args[0], Convert.ToInt32(args[1])); NetworkStream stream = client.GetStream(); // the read buffer (chosen quite big) byte[] myReadBuffer = new byte[4096 * 8]; List <String> Messages = new List <string>(); // to build the complete message StringBuilder myCompleteMessage = new StringBuilder(); int numberOfBytesRead = 0; MAXEncodeDecode DecoderEncoder = new MAXEncodeDecode(); // Incoming message may be larger than the buffer size. do { myCompleteMessage = new StringBuilder(); stream.ReadTimeout = 1000; try { numberOfBytesRead = stream.Read(myReadBuffer, 0, myReadBuffer.Length); myCompleteMessage.AppendFormat("{0}", Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead)); Messages.Add(myCompleteMessage.ToString()); } catch (Exception) { //Console.WriteLine("Exception: "+e.Message); keepRunning = false; } }while(keepRunning); List <String> PreProcessedMessages = new List <string>(); // preprocess foreach (String _Message in Messages) { if (_Message.Remove(_Message.Length - 2).Contains("\r\n")) { String[] PMessages = _Message.Remove(_Message.Length - 2).Split(new char[1] { '\n' }, StringSplitOptions.RemoveEmptyEntries); foreach (String pmessage in PMessages) { PreProcessedMessages.Add(pmessage.Replace("\r", "") + "\r\n"); } } else { PreProcessedMessages.Add(_Message); } } // Analyze and Output Messages foreach (String _Message in PreProcessedMessages) { IMAXMessage Message = DecoderEncoder.ProcessMessage(_Message.ToString(), thisHouse); if (Message != null) { //ConsoleOutputLogger.WriteLine(_Message.ToString()); ConsoleOutputLogger.WriteLine(Message.ToString()); //ConsoleOutputLogger.WriteLine(""); } } // some writing if (args.Length > 2) { System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding(); byte[] args_data_buffer = enc.GetBytes(args[2] + "\r\n"); ConsoleOutputLogger.WriteLine("Sending Command: " + args[2]); stream.Write(args_data_buffer, 0, args_data_buffer.Length); keepRunning = true; Messages = new List <string>(); do { myCompleteMessage = new StringBuilder(); stream.ReadTimeout = 1000; try { numberOfBytesRead = stream.Read(myReadBuffer, 0, myReadBuffer.Length); myCompleteMessage.AppendFormat("{0}", Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead)); Messages.Add(myCompleteMessage.ToString()); } catch (Exception) { //jConsole.WriteLine("Exception: "+e.Message); keepRunning = false; } }while(keepRunning); PreProcessedMessages = new List <string>(); // preprocess foreach (String _Message in Messages) { if (_Message.Remove(_Message.Length - 2).Contains("\r\n")) { String[] PMessages = _Message.Remove(_Message.Length - 2).Split(new char[1] { '\n' }, StringSplitOptions.RemoveEmptyEntries); foreach (String pmessage in PMessages) { PreProcessedMessages.Add(pmessage.Replace("\r", "") + "\r\n"); } } else { PreProcessedMessages.Add(_Message); } } foreach (String _Message in PreProcessedMessages) { IMAXMessage Message = DecoderEncoder.ProcessMessage(_Message, thisHouse); if (Message != null) { ConsoleOutputLogger.WriteLine(Message.ToString()); ConsoleOutputLogger.LogToFile(""); } } } stream.Close(); client.Close(); }
// this is the ELV MAX! Cube monitoring script public void Run() { while (running) { #region Update House try { if (theHouse != null) { previousHouse = theHouse.GetAllDevicesInADictionary(); } theHouse = new House(); // we obviously have enough paramteres, go on and try to connect TcpClient client = new TcpClient(); client.Connect(Hostname, Port); NetworkStream stream = client.GetStream(); // the read buffer (chosen quite big) byte[] myReadBuffer = new byte[4096 * 8]; List <String> Messages = new List <string>(); // to build the complete message StringBuilder myCompleteMessage = new StringBuilder(); int numberOfBytesRead = 0; MAXEncodeDecode DecoderEncoder = new MAXEncodeDecode(); keepRunning = true; // Incoming message may be larger than the buffer size. do { myCompleteMessage = new StringBuilder(); stream.ReadTimeout = 1000; try { numberOfBytesRead = stream.Read(myReadBuffer, 0, myReadBuffer.Length); myCompleteMessage.AppendFormat("{0}", Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead)); Messages.Add(myCompleteMessage.ToString()); } catch (Exception) { keepRunning = false; } }while(keepRunning); List <String> PreProcessedMessages = new List <string>(); // preprocess foreach (String _Message in Messages) { if (_Message.Remove(_Message.Length - 2).Contains("\r\n")) { String[] PMessages = _Message.Remove(_Message.Length - 2).Split(new char[1] { '\n' }, StringSplitOptions.RemoveEmptyEntries); foreach (String pmessage in PMessages) { PreProcessedMessages.Add(pmessage.Replace("\r", "") + "\r\n"); } } else { PreProcessedMessages.Add(_Message); } } // Analyze and Output Messages foreach (String _Message in PreProcessedMessages) { IMAXMessage Message = DecoderEncoder.ProcessMessage(_Message.ToString(), theHouse); /* if (Message != null) * { * ConsoleOutputLogger.WriteLine(_Message.ToString()); * ConsoleOutputLogger.WriteLine(Message.ToString()); * ConsoleOutputLogger.WriteLine(""); * }*/ } stream.Close(); client.Close(); } catch (Exception) { } #endregion #region Diff the house if (previousHouse != null) { // only if we already got two houses in here... List <IDeviceDiffSet> differences = DiffHouse.CalculateDifferences(previousHouse, theHouse.GetAllDevicesInADictionary()); if (differences.Count != 0) { foreach (IDeviceDiffSet _difference in differences) { StringBuilder sb = new StringBuilder(); sb.Append("S\t" + _difference.DeviceName + "\t" + _difference.DeviceType); if (_difference.DeviceType == DeviceTypes.HeatingThermostat) { HeatingThermostatDiff _heating = (HeatingThermostatDiff)_difference; ConsoleOutputLogger.WriteLine(_heating.ToString()); } if (_difference.DeviceType == DeviceTypes.ShutterContact) { ShutterContactDiff _shutter = (ShutterContactDiff)_difference; ConsoleOutputLogger.WriteLine(_shutter.ToString()); } } } } #endregion Thread.Sleep(MAXUpdateTime); } }
private static void StartClient(String[] args) { ConsoleOutputLogger.verbose = true; ConsoleOutputLogger.writeLogfile = true; Console.WriteLine("ELV MAX! Debug Tool version 1 (C) Daniel Kirstenpfad 2012"); Console.WriteLine(); // not enough paramteres given, display help if (args.Length < 2) { Console.WriteLine("Syntax:"); Console.WriteLine(); Console.WriteLine("\tmaxdebug <hostname/ip> <port (e.g. 62910)> [commands]"); Console.WriteLine(); return; } ConsoleOutputLogger.LogToFile("--------------------------------------"); // Connect to a remote device. try { // Establish the remote endpoint for the socket. // The name of the // remote device is "host.contoso.com". IPHostEntry ipHostInfo = Dns.Resolve(args[0]); IPAddress ipAddress = ipHostInfo.AddressList[0]; IPEndPoint remoteEP = new IPEndPoint(ipAddress, Convert.ToInt32(args[1])); // Create a TCP/IP socket. Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // Connect to the remote endpoint. client.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), client); connectDone.WaitOne(); client.ReceiveTimeout = 1000; // Receive the response from the remote device. Receive(client); receiveDone.WaitOne(); #region execute command from commandline if (args.Length > 2) { // Send test data to the remote device. Send(client, args[2] + "\r\n"); sendDone.WaitOne(); // Receive the response from the remote device. Receive(client); receiveDone.WaitOne(); } #endregion // Release the socket. client.Shutdown(SocketShutdown.Both); client.Close(); // take the response and split it into it's lines String[] SplittedResponse = response.Replace("\r", "").Split(new char[1] { '\n' }, StringSplitOptions.RemoveEmptyEntries); MAXEncodeDecode DecoderEncoder = new MAXEncodeDecode(); // Analyze and Output Messages foreach (String _Message in SplittedResponse) { IMAXMessage Message = DecoderEncoder.ProcessMessage(_Message.ToString()); if (Message != null) { ConsoleOutputLogger.WriteLine(Message.ToString()); ConsoleOutputLogger.WriteLine(""); } } } catch (Exception e) { Console.WriteLine(e.ToString()); } }