/// <summary> /// Query current status of FreeSwitch via event socket. /// </summary> /// <param name="state">return the state of freeswitch.</param> /// <returns>true is query succeed.</returns> public bool QueryServerState(out FSState state) { //FSState state; string cmd = ""; string resp = ""; cmd = "api status"; resp = SendCommand(cmd); return(FSState.TryParse(out state, resp)); }
/// <summary> /// Parse FSState struct by EventSocket responsed strings. /// </summary> /// <param name="status">parsed result</param> /// <param name="text">response string from EventSocket</param> /// <returns>parsing succeed or not?</returns> public static bool TryParse(out FSState status, string text) { bool ok = false; status = new FSState(); try { var match = Regex.Match(text, Pattern1); if (match.Success) { int days = int.Parse(match.Groups[1].Value) * 365 + int.Parse(match.Groups[2].Value); int hour = int.Parse(match.Groups[3].Value); int minute = int.Parse(match.Groups[4].Value); int second = int.Parse(match.Groups[5].Value); int msec = int.Parse(match.Groups[6].Value); status.UpTime = new TimeSpan(days, hour, minute, second, msec); } else { throw new FormatException("UP Time parsing failed."); } match = Regex.Match(text, Pattern2); if (match.Success) { status.PBXName = match.Groups[1].Value; status.Version = match.Groups[2].Value + " " + match.Groups[3].Value; } else { throw new FormatException("Version and SwitchName parsing failed."); } match = Regex.Match(text, Pattern3); if (match.Success) { status.MaxSession = int.Parse(match.Groups[1].Value); } else { throw new FormatException("Max Session parsing failed."); } match = Regex.Match(text, Pattern4); if (match.Success) { status.CpuMinIdle = float.Parse(match.Groups[1].Value); status.CpuLoading = 100.0f - float.Parse(match.Groups[2].Value); } else { throw new FormatException("Cpu min-idle threshold parsing failed."); } ok = true; } catch (Exception ex) { ok = false; } return(ok); }