using System; using System.Net; using System.Fire.Ini; using System.Collections.Generic; using System.Collections.Specialized; using System.Text.Json; using System.Xml; namespace Leah { public class Client { public enum eService { DeviceEmotion, Intensi, Sambale, Sozoti, Speech, Keln, GregorCalendus } private static iniHandle ini; public static string GetString(eService service, string command) { return System.Net.ApiService.client.Get_String(GetUrl(service, command), DefaultHeader()); } public static string GetString(eService service, string command, NameValueCollection query) { return System.Net.ApiService.client.Get_String(GetUrl(service, command), DefaultHeader(), query); } public static JsonDocument GetJson(eService service, string command) { return System.Net.ApiService.client.Get_Json(GetUrl(service, command), DefaultHeader()); } public static JsonDocument GetJson(eService service, string command, NameValueCollection query) { return System.Net.ApiService.client.Get_Json(GetUrl(service, command), DefaultHeader(), query); } public static T GetJson(eService service, string command) { return System.Net.ApiService.client.Get_Json(GetUrl(service, command), DefaultHeader()); } public static T GetJson(eService service, string command, NameValueCollection query) { return System.Net.ApiService.client.Get_Json(GetUrl(service, command), DefaultHeader(), query); } public static XmlDocument GetXml(eService service, string command) { return System.Net.ApiService.client.Get_Xml(GetUrl(service, command), DefaultHeader()); } public static XmlDocument GetXml(eService service, string command, NameValueCollection query) { return System.Net.ApiService.client.Get_Xml(GetUrl(service, command), DefaultHeader(), query); } public static T GetObject(eService service, string command) { return System.Net.ApiService.client.Get_Object(GetUrl(service, command),DefaultHeader()); } public static T GetObject(eService service, string command, NameValueCollection query) { return System.Net.ApiService.client.Get_Object(GetUrl(service, command), DefaultHeader(), query); } public static string PostString(eService service, string command, NameValueCollection post) { return System.Net.ApiService.client.Post_String(GetUrl(service, command), DefaultHeader(), post); } public static string PostString(eService service, string command, NameValueCollection query, NameValueCollection post) { return System.Net.ApiService.client.Post_String(GetUrl(service, command), DefaultHeader(), query, post); } public static JsonDocument PostJson(eService service, string command, NameValueCollection post) { return System.Net.ApiService.client.Post_Json(GetUrl(service, command), DefaultHeader(), post); } public static JsonDocument PostJson(eService service, string command, NameValueCollection query, NameValueCollection post) { return System.Net.ApiService.client.Post_Json(GetUrl(service, command), DefaultHeader(), query, post); } public static T PostJson(eService service, string command, NameValueCollection post) { return System.Net.ApiService.client.Post_Json(GetUrl(service, command), DefaultHeader(), post); } public static T PostJson(eService service, string command, NameValueCollection query, NameValueCollection post) { return System.Net.ApiService.client.Post_Json(GetUrl(service, command), DefaultHeader(), query, post); } public static XmlDocument PostXml(eService service, string command, NameValueCollection post) { return System.Net.ApiService.client.Post_Xml(GetUrl(service, command), DefaultHeader(), post); } public static XmlDocument PostXml(eService service, string command, NameValueCollection query, NameValueCollection post) { return System.Net.ApiService.client.Post_Xml(GetUrl(service, command), DefaultHeader(), query, post); } public static T PostObject(eService service, string command, NameValueCollection post) { return System.Net.ApiService.client.Post_Object(GetUrl(service, command), DefaultHeader(), post); } public static T PostObject(eService service, string command, NameValueCollection query, NameValueCollection post) { return System.Net.ApiService.client.Post_Object(GetUrl(service, command), DefaultHeader(), query, post); } public static NameValueCollection DefaultHeader() { return new NameValueCollection() { { "DNT", "1" }, { "REFERER", "" } }; } public static string GetUrl(eService service, string Command) { OpenIni(); switch(service) { case eService.DeviceEmotion: return (ini["DeviceEmotion"]["Host"].Value==""?"localhost": ini["DeviceEmotion"]["Host"].Value) + (ini["DeviceEmotion"]["Port"].Value == "" ? "" : ":") + ini["DeviceEmotion"]["Port"].Value + (Command.StartsWith("/")?"":"/") + Command; case eService.Intensi: return (ini["Intensi"]["Host"].Value == "" ? "localhost" : ini["Intensi"]["Host"].Value) + (ini["Intensi"]["Port"].Value == "" ? "" : ":") + ini["Intensi"]["Port"].Value + (Command.StartsWith("/") ? "" : "/") + Command; case eService.Sambale: return (ini["Sambale"]["Host"].Value == "" ? "localhost" : ini["Sambale"]["Host"].Value) + (ini["Sambale"]["Port"].Value == "" ? "" : ":") + ini["Sambale"]["Port"].Value + (Command.StartsWith("/") ? "" : "/") + Command; case eService.Sozoti: return (ini["Sozoti"]["Host"].Value == "" ? "localhost" : ini["Sozoti"]["Host"].Value) + (ini["Sozoti"]["Port"].Value == "" ? "" : ":") + ini["Sozoti"]["Port"].Value + (Command.StartsWith("/") ? "" : "/") + Command; case eService.Speech: return (ini["Speech"]["Host"].Value == "" ? "localhost" : ini["Speech"]["Host"].Value) + (ini["Speech"]["Port"].Value == "" ? "" : ":") + ini["Speech"]["Port"].Value + (Command.StartsWith("/") ? "" : "/") + Command; case eService.Keln: return (ini["Keln"]["Host"].Value == "" ? "localhost" : ini["Keln"]["Host"].Value) + (ini["Keln"]["Port"].Value == "" ? "" : ":") + ini["Keln"]["Port"].Value + (Command.StartsWith("/") ? "" : "/") + Command; case eService.GregorCalendus: return (ini["GregorCalendus"]["Host"].Value == "" ? "localhost" : ini["GregorCalendus"]["Host"].Value) + (ini["GregorCalendus"]["Port"].Value == "" ? "" : ":") + ini["GregorCalendus"]["Port"].Value + (Command.StartsWith("/") ? "" : "/") + Command; } return ""; } public static string GetHostPort(eService service) { OpenIni(); switch (service) { case eService.DeviceEmotion: return (ini["DeviceEmotion"]["Host"].Value == "" ? "localhost" : ini["DeviceEmotion"]["Host"].Value) + (ini["DeviceEmotion"]["Port"].Value == "" ? "" : ":") + ini["DeviceEmotion"]["Port"].Value; case eService.Intensi: return (ini["Intensi"]["Host"].Value == "" ? "localhost" : ini["Intensi"]["Host"].Value) + (ini["Intensi"]["Port"].Value == "" ? "" : ":") + ini["Intensi"]["Port"].Value; case eService.Sambale: return (ini["Sambale"]["Host"].Value == "" ? "localhost" : ini["Sambale"]["Host"].Value) + (ini["Sambale"]["Port"].Value == "" ? "" : ":") + ini["Sambale"]["Port"].Value; case eService.Sozoti: return (ini["Sozoti"]["Host"].Value == "" ? "localhost" : ini["Sozoti"]["Host"].Value) + (ini["Sozoti"]["Port"].Value == "" ? "" : ":") + ini["Sozoti"]["Port"].Value; case eService.Speech: return (ini["Speech"]["Host"].Value == "" ? "localhost" : ini["Speech"]["Host"].Value) + (ini["Speech"]["Port"].Value == "" ? "" : ":") + ini["Speech"]["Port"].Value; case eService.Keln: return (ini["Keln"]["Host"].Value == "" ? "localhost" : ini["Keln"]["Host"].Value) + (ini["Keln"]["Port"].Value == "" ? "" : ":") + ini["Keln"]["Port"].Value; case eService.GregorCalendus: return (ini["GregorCalendus"]["Host"].Value == "" ? "localhost" : ini["GregorCalendus"]["Host"].Value) + (ini["GregorCalendus"]["Port"].Value == "" ? "" : ":") + ini["GregorCalendus"]["Port"].Value; } return ""; } private static void OpenIni() { if (ini != null) return; ini = new iniHandle("config/services.conf"); bool bHasChanges = false; if (!ini.HasCategorie("DeviceEmotion")) { ini.Categories.Add(new iniCategorie(ini, "DeviceEmotion")); ini["DeviceEmotion"]["Host"].Value = "localhost"; ini["DeviceEmotion"]["Port"].UInt16 = 8000; bHasChanges = true; } if (!ini.HasCategorie("Intensi")) { ini.Categories.Add(new iniCategorie(ini, "Intensi")); ini["Intensi"]["Host"].Value = "localhost"; ini["Intensi"]["Port"].UInt16 = 8001; bHasChanges = true; } if (!ini.HasCategorie("Sambale")) { ini.Categories.Add(new iniCategorie(ini, "Sambale")); ini["Sambale"]["Host"].Value = "localhost"; ini["Sambale"]["Port"].UInt16 = 8002; bHasChanges = true; } if (!ini.HasCategorie("Sozoti")) { ini.Categories.Add(new iniCategorie(ini, "Sozoti")); ini["Sozoti"]["Host"].Value = "localhost"; ini["Sozoti"]["Port"].UInt16 = 8003; bHasChanges = true; } if (!ini.HasCategorie("Speech")) { ini.Categories.Add(new iniCategorie(ini, "Speech")); ini["Speech"]["Host"].Value = "localhost"; ini["Speech"]["Port"].UInt16 = 8004; bHasChanges = true; } if (!ini.HasCategorie("GregorCalendus")) { ini.Categories.Add(new iniCategorie(ini, "GregorCalendus")); ini["GregorCalendus"]["Host"].Value = "localhost"; ini["GregorCalendus"]["Port"].UInt16 = 8005; bHasChanges = true; } if (!ini.HasCategorie("Keln")) { ini.Categories.Add(new iniCategorie(ini, "Keln")); ini["Keln"]["Host"].Value = "localhost"; ini["Keln"]["Port"].UInt16 = 8010; bHasChanges = true; } if (!System.IO.File.Exists("config/services.conf") || bHasChanges) { ini.Save(); } } } }