private static void ExecuteMatrixCmd(MatrixCmd matrixCmd) { using (TcpClient client = new TcpClient()) { IAsyncResult ar = client.BeginConnect(ip, 10001, null, null); WaitHandle wh = ar.AsyncWaitHandle; try { if (!ar.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(5), false)) { LogCtrl.ThreadSafeError("Matrix: Can't connect"); return; } Stream stm = client.GetStream(); byte[] bb = new byte[9]; stm.Write(matrixCmd.message, 0, matrixCmd.message.Length); stm.Read(bb, 0, 9); if (Encoding.UTF8.GetString(bb).Trim() == matrixCmd.response) { LogCtrl.ThreadSafeSuccess(matrixCmd.successText); } else { LogCtrl.ThreadSafeError(matrixCmd.failText); } client.EndConnect(ar); } catch (Exception e) { LogCtrl.ThreadSafeError("Matrix: Error connecting."); LogCtrl.ThreadSafeError(e.Message); } finally { wh.Close(); } } }
private void ShutterMethod(bool open) { using (TcpClient client = new TcpClient()) { IAsyncResult ar = client.BeginConnect(ip, 4352, null, null); WaitHandle wh = ar.AsyncWaitHandle; try { if (!ar.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(5), false)) { LogCtrl.ThreadSafeError("Beamer " + id + ": Timed out. (" + ip + ")"); return; } byte[] bb = new byte[9]; Stream stm = client.GetStream(); stm.Read(bb, 0, 9); if (open) { stm.Write(openCmd, 0, openCmd.Length); } else { stm.Write(closeCmd, 0, closeCmd.Length); } stm.Read(bb, 0, 9); if (Encoding.UTF8.GetString(bb) == "%1AVMT=OK") { if (open) { LogCtrl.ThreadSafeSuccess("Beamer " + id + ": Opened (" + ip + ")"); } else { LogCtrl.ThreadSafeSuccess("Beamer " + id + ": Closed (" + ip + ")"); } } else { if (open) { LogCtrl.ThreadSafeError("Beamer " + id + ": Error opening. (" + ip + ")"); } else { LogCtrl.ThreadSafeError("Beamer " + id + ": Error closing. (" + ip + ")"); } } client.EndConnect(ar); } catch (Exception e) { LogCtrl.ThreadSafeError("Beamer " + id + ": Error connecting. (" + ip + ")"); LogCtrl.ThreadSafeError(e.Message); } finally { wh.Close(); } } }