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(); } } }
private static void InterLoop() { try { float[] lastVal = new float[51]; while (!_shouldStop) { for (int i = 0; i < 51; ++i) { foreach (PbCmdArg pbCmdArg in ctrlVals.pbCmdArgs[i]) { float scale = pbCmdArg.max - pbCmdArg.min; float end = pbCmdArg.min + (ccValues[i] * scale); if (lastVal[i] != ccValues[i]) { if (!initialized[i]) { pbCmdArg.val = end; pbCmdArg.step = 0; initialized[i] = true; Application.Current.Dispatcher.Invoke(new Action(() => { PbCtrl.ExecuteCmdArg(pbCmdArg, pbCmdArg.val); })); } else { float diff = end - pbCmdArg.val; float step = pbCmdArg.interp; if (step <= 0) { step = 1; } pbCmdArg.step = diff / step; } } if (pbCmdArg.step < 0 && pbCmdArg.val > end || pbCmdArg.step > 0 && pbCmdArg.val < end) { pbCmdArg.val += pbCmdArg.step; float min = Math.Min(pbCmdArg.min, pbCmdArg.max); float max = Math.Max(pbCmdArg.min, pbCmdArg.max); if (pbCmdArg.val < min) { pbCmdArg.val = min; } else if (pbCmdArg.val > max) { pbCmdArg.val = max; } Application.Current.Dispatcher.Invoke(new Action(() => { PbCtrl.ExecuteCmdArg(pbCmdArg, pbCmdArg.val); })); } } if (lastVal[i] != ccValues[i]) { lastVal[i] = ccValues[i]; if (ccValues[i] <= 0) { foreach (PbCmd pbCmdOff in ctrlVals.pbCmdOff[i]) { Application.Current.Dispatcher.Invoke(new Action(() => { PbCtrl.ExecuteCmd(pbCmdOff); })); } foreach (string ccCmdOff in ctrlVals.ccCmdOff[i]) { ExecuteCcCmd(ccCmdOff); } } else if (ccValues[i] >= 1) { foreach (PbCmd pbCmdOn in ctrlVals.pbCmdOn[i]) { Application.Current.Dispatcher.Invoke(new Action(() => { PbCtrl.ExecuteCmd(pbCmdOn); })); } foreach (string ccCmdOn in ctrlVals.ccCmdOn[i]) { ExecuteCcCmd(ccCmdOn); } } } } Thread.Sleep(40); } } catch { LogCtrl.ThreadSafeError("MIDI Controller thread crashed!"); } }