public async Task CheckForCamera() { try { if (!MainForm.m.finishedLoading || !AsyncCamCom.sock.Connected) { isCamera = false; isActive = false; return; } if (ConfigControl.forceCamera.boolVal) { isCamera = true; SettingsPage.UpdateCamType(); } else { OtherCamCom.CamConfig cfg = await OtherCamCom.CheckConfiguration(); if (cfg != OtherCamCom.CamConfig.Null) { isCamera = true; } MainForm.m.setPage.UpdateCamConfig(cfg); } } catch (Exception e) { MessageBox.Show(e.ToString()); } }
static async Task DoPingAndReconnect() { if (!await OtherCamCom.PingAdr(ConfigControl.savedIP.stringVal) || !sock.Connected) { OtherCamCom.LabelDisplay(false); TryConnect(); } }
public static async Task <bool> TryConnect(bool showErrors = false, IPEndPoint customep = null) //Return true or false if Connect worked { bool result = true; //needs to be on true try { if (connectingAlready) { return(false); } connectingAlready = true; if (ConfigControl.savedIP.stringVal.Length == 0 || ConfigControl.savedPort.stringVal.Length == 0) { result = false; } if (customep == null) { customep = new IPEndPoint(IPAddress.Parse(ConfigControl.savedIP.stringVal), int.Parse(ConfigControl.savedPort.stringVal)); } if (result) { result = Connect(customep, showErrors); } } catch (Exception e) { if (showErrors) { Tools.ShowPopup("Failed to initialize connection to camera!\nShow more?", "Connection Attempt Failed!" , e.ToString()); } Console.WriteLine(e.ToString()); result = false; } if (result && ConfigControl.legacyLayout.stringVal.ToUpper() == "LEGACY") { ConfigControl.savedIP.UpdateValue(MainForm.m.tB_Legacy_IP.Text); ConfigControl.savedPort.UpdateValue(MainForm.m.tB_Legacy_Port.Text); } connectingAlready = false; OtherCamCom.LabelDisplay(result); VideoSettings.UpdateControlFields(); return(result); }
public static void Disconnect(bool showErrors = true) { try { if (sock == null || !MainForm.m.finishedLoading) { return; } string oldAdr = null; try { if (sock.RemoteEndPoint != null) { oldAdr = sock.RemoteEndPoint.ToString(); } } catch (Exception e) { } OtherCamCom.LabelDisplay(false); if (!ConfigControl.forceCamera.boolVal) { InfoPanel.i.isCamera = false; } if (!sock.Connected) { return; } sock.Shutdown(SocketShutdown.Both); if (oldAdr != null) { MainForm.m.WriteToResponses("Disconnected from: " + oldAdr, true); } Console.WriteLine("disconnected"); } catch (Exception e) { if (showErrors) { Tools.ShowPopup("Disconnect error occurred!\nShow more?", "Error Occurred!", e.ToString()); } } }
public Uri ConfirmAdr(bool showErrors, string fullAdr, bool doPing) { try { Uri newUri = null; string errorMsg = ""; try { newUri = new Uri(fullAdr); } catch { errorMsg = "Address was invalid!\n"; } if (newUri != null && !ConfigControl.ignoreAddress.boolVal && settings.isMainPlayer && doPing) { if (!OtherCamCom.PingAdr(newUri.Host).Result) { errorMsg += "Address had no RTSP stream attached!\n"; } } if (errorMsg != "") { if (showErrors) { MessageBox.Show(errorMsg); } return(null); } return(newUri); } catch (Exception e) { if (showErrors) { Tools.ShowPopup("Failed to parse address!\nShow more?", "Error Occurred!", e.ToString()); } return(null); } }
async Task CheckConfig() { UpdateCamConfig(await OtherCamCom.CheckConfiguration()); }
public async Task UpdateNext() { try { if (!isActive) { return; } if (commandPos >= 4) { commandPos = 0; return; } tickCount = 0; int id = 0; switch (commandPos) { case 0: id = panID; break; case 1: id = tiltID; break; case 2: id = fovID; break; case 3: id = tFovID; break; } string result = await AsyncCamCom.QueueRepeatingCommand(id); if (result == OtherCamCom.defaultResult) { UpdateNext(); return; } string commandType = result.Substring(9, 2); switch (commandType) { case "59": //pan pan = OtherCamCom.CalculatePan(result); break; case "5B": //tilt tilt = OtherCamCom.CalculateTilt(result); break; case "5D": //fov if (InfoPanel.i.commandPos == 2) { fov = OtherCamCom.ReturnedHexValToFloat(result); Console.WriteLine("BBB " + result); } else { Console.WriteLine("AAA " + result); tfov = OtherCamCom.ReturnedHexValToFloat(result); } break; default: return; } Invoke((MethodInvoker) delegate { l_Tilt.Text = "TILT: " + tilt.ToString() + " °"; l_FOV.Text = "DAYLIGHT FOV: " + fov.ToString() + " °"; l_TFOV.Text = "THERMAL FOV: " + tfov.ToString() + " °"; l_Pan.Text = "PAN: " + pan.ToString() + " °"; }); commandPos++; UpdateNext(); } catch (Exception e) { MessageBox.Show(e.ToString()); } }
static async Task WaitForCommandResponse(Command com) { try { SendTimer.Stop(); if (com.isInfo) { com.done = false; } int i = 0; bool repeated = false; while (i < commandRetries) { repeated = i > 0; AsyncCamCom.SendCurrent(repeated); if (!com.spammable && !com.isInfo) { break; } if (com.done) { break; } else { await Task.Delay(commandRate); } i++; } if (repeated) { MainForm.m.WriteToResponses("Sent command " + i + " times!", true, com.isInfo); } if (!com.done) { MainForm.m.WriteToResponses(GetNameString() + "No response!", true, com.isInfo); } else { string msg = com.myReturn.msg; if (com.name.Contains("query")) { msg = OtherCamCom.ConvertQueryResult(com.name, com.myReturn.msg); } MainForm.m.WriteToResponses(GetNameString() + "Received: " + msg, false, com.isInfo); } com.done = true; if (queueList.Contains(com)) { queueList.Remove(com); } SendTimer.Start(); } catch (Exception e) { Tools.ShowPopup("Failed to process message return!\nShow more?", "Response Failed!", e.ToString()); } }
private static bool Connect(IPEndPoint ep, bool showErrors) //Connects to the socket { try { if (sock == null || ep == null || ep.Address == null || ep.Port == 0 || ep.Address.ToString().Length == 0) { return(false); } else if (sock.Connected && ep.ToString() == sock.RemoteEndPoint.ToString()) { return(true); } else if (sock.Connected && ep.ToString() != sock.RemoteEndPoint.ToString()) { Console.WriteLine("trying to disconnect " + sock.RemoteEndPoint.ToString() + "\n" + ep.ToString()); Disconnect(true); } Console.WriteLine("trying to connect to " + ep.Address.ToString() + ":" + ep.Port.ToString()); if (!ConfigControl.forceCamera.boolVal) { InfoPanel.i.isCamera = false; } sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); bool parsedIP = IPAddress.TryParse(ep.Address.ToString(), out IPAddress ip); bool parsedPort = int.TryParse(ep.Port.ToString(), out int port); string errorMessage = "IP (" + ep.Address.ToString() + ") valid: " + parsedIP.ToString() + "\nPort (" + ep.Port.ToString() + ") valid: " + parsedPort.ToString(); if (!parsedIP || !parsedPort) { if (showErrors) { Tools.ShowPopup("Failed to parse endpoint!\nAddress provided is likely invalid!\nShow more?", "Failed to connect!", errorMessage); } return(false); } if (!OtherCamCom.PingAdr(ep.Address.ToString()).Result) { if (showErrors) { Tools.ShowPopup("Failed to ping IP address!\nAddress provided is likely invalid!\nShow more?", "Failed to connect!", errorMessage + "\nPing: Failed"); } return(false); } IPEndPoint end = new IPEndPoint(ip, port); if (end == null) { return(false); } OtherCamCom.CheckIsSameSubnet(ep.Address.ToString()); connecting = sock.BeginConnect(end, ConnectCallback, null); MainForm.m.WriteToResponses("Successfully connected to: " + end.Address.ToString() + ":" + end.Port.ToString(), true); InitReconnectTimer(); return(true); } catch (SocketException ex) { MainForm.m.WriteToResponses(ex.Message, false); } catch (ObjectDisposedException ex) { MainForm.m.WriteToResponses(ex.Message, false); } catch (Exception e) { if (showErrors) { MessageBox.Show("An error occured whilst connecting to camera!\n" + e.ToString()); } Console.WriteLine("CONNECT\n" + e.ToString()); } return(false); }