Client.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. using System;
  2. using System.Net;
  3. using System.Fire.Ini;
  4. using System.Collections.Generic;
  5. using System.Collections.Specialized;
  6. using System.Text.Json;
  7. using System.Xml;
  8. namespace Leah {
  9. public class Client {
  10. public enum eService {
  11. DeviceEmotion,
  12. Intensi,
  13. Sambale,
  14. Sozoti,
  15. Speech,
  16. Keln,
  17. GregorCalendus
  18. }
  19. private static iniHandle ini;
  20. public static string GetString(eService service, string command) {
  21. return System.Net.ApiService.client.Get_String(GetUrl(service, command), DefaultHeader());
  22. }
  23. public static string GetString(eService service, string command, NameValueCollection query) {
  24. return System.Net.ApiService.client.Get_String(GetUrl(service, command), DefaultHeader(), query);
  25. }
  26. public static JsonDocument GetJson(eService service, string command) {
  27. return System.Net.ApiService.client.Get_Json(GetUrl(service, command), DefaultHeader());
  28. }
  29. public static JsonDocument GetJson(eService service, string command, NameValueCollection query) {
  30. return System.Net.ApiService.client.Get_Json(GetUrl(service, command), DefaultHeader(), query);
  31. }
  32. public static T GetJson<T>(eService service, string command) {
  33. return System.Net.ApiService.client.Get_Json<T>(GetUrl(service, command), DefaultHeader());
  34. }
  35. public static T GetJson<T>(eService service, string command, NameValueCollection query) {
  36. return System.Net.ApiService.client.Get_Json<T>(GetUrl(service, command), DefaultHeader(), query);
  37. }
  38. public static XmlDocument GetXml(eService service, string command) {
  39. return System.Net.ApiService.client.Get_Xml(GetUrl(service, command), DefaultHeader());
  40. }
  41. public static XmlDocument GetXml(eService service, string command, NameValueCollection query) {
  42. return System.Net.ApiService.client.Get_Xml(GetUrl(service, command), DefaultHeader(), query);
  43. }
  44. public static T GetObject<T>(eService service, string command) {
  45. return System.Net.ApiService.client.Get_Object<T>(GetUrl(service, command),DefaultHeader());
  46. }
  47. public static T GetObject<T>(eService service, string command, NameValueCollection query) {
  48. return System.Net.ApiService.client.Get_Object<T>(GetUrl(service, command), DefaultHeader(), query);
  49. }
  50. public static string PostString(eService service, string command, NameValueCollection post) {
  51. return System.Net.ApiService.client.Post_String(GetUrl(service, command), DefaultHeader(), post);
  52. }
  53. public static string PostString(eService service, string command, NameValueCollection query, NameValueCollection post) {
  54. return System.Net.ApiService.client.Post_String(GetUrl(service, command), DefaultHeader(), query, post);
  55. }
  56. public static JsonDocument PostJson(eService service, string command, NameValueCollection post) {
  57. return System.Net.ApiService.client.Post_Json(GetUrl(service, command), DefaultHeader(), post);
  58. }
  59. public static JsonDocument PostJson(eService service, string command, NameValueCollection query, NameValueCollection post) {
  60. return System.Net.ApiService.client.Post_Json(GetUrl(service, command), DefaultHeader(), query, post);
  61. }
  62. public static T PostJson<T>(eService service, string command, NameValueCollection post) {
  63. return System.Net.ApiService.client.Post_Json<T>(GetUrl(service, command), DefaultHeader(), post);
  64. }
  65. public static T PostJson<T>(eService service, string command, NameValueCollection query, NameValueCollection post) {
  66. return System.Net.ApiService.client.Post_Json<T>(GetUrl(service, command), DefaultHeader(), query, post);
  67. }
  68. public static XmlDocument PostXml(eService service, string command, NameValueCollection post) {
  69. return System.Net.ApiService.client.Post_Xml(GetUrl(service, command), DefaultHeader(), post);
  70. }
  71. public static XmlDocument PostXml(eService service, string command, NameValueCollection query, NameValueCollection post) {
  72. return System.Net.ApiService.client.Post_Xml(GetUrl(service, command), DefaultHeader(), query, post);
  73. }
  74. public static T PostObject<T>(eService service, string command, NameValueCollection post) {
  75. return System.Net.ApiService.client.Post_Object<T>(GetUrl(service, command), DefaultHeader(), post);
  76. }
  77. public static T PostObject<T>(eService service, string command, NameValueCollection query, NameValueCollection post) {
  78. return System.Net.ApiService.client.Post_Object<T>(GetUrl(service, command), DefaultHeader(), query, post);
  79. }
  80. public static NameValueCollection DefaultHeader() {
  81. return new NameValueCollection() {
  82. { "DNT", "1" },
  83. { "REFERER", "" }
  84. };
  85. }
  86. public static string GetUrl(eService service, string Command) {
  87. OpenIni();
  88. switch(service) {
  89. case eService.DeviceEmotion:
  90. return (ini["DeviceEmotion"]["Host"].Value==""?"localhost": ini["DeviceEmotion"]["Host"].Value) + (ini["DeviceEmotion"]["Port"].Value == "" ? "" : ":") + ini["DeviceEmotion"]["Port"].Value + (Command.StartsWith("/")?"":"/") + Command;
  91. case eService.Intensi:
  92. return (ini["Intensi"]["Host"].Value == "" ? "localhost" : ini["Intensi"]["Host"].Value) + (ini["Intensi"]["Port"].Value == "" ? "" : ":") + ini["Intensi"]["Port"].Value + (Command.StartsWith("/") ? "" : "/") + Command;
  93. case eService.Sambale:
  94. return (ini["Sambale"]["Host"].Value == "" ? "localhost" : ini["Sambale"]["Host"].Value) + (ini["Sambale"]["Port"].Value == "" ? "" : ":") + ini["Sambale"]["Port"].Value + (Command.StartsWith("/") ? "" : "/") + Command;
  95. case eService.Sozoti:
  96. return (ini["Sozoti"]["Host"].Value == "" ? "localhost" : ini["Sozoti"]["Host"].Value) + (ini["Sozoti"]["Port"].Value == "" ? "" : ":") + ini["Sozoti"]["Port"].Value + (Command.StartsWith("/") ? "" : "/") + Command;
  97. case eService.Speech:
  98. return (ini["Speech"]["Host"].Value == "" ? "localhost" : ini["Speech"]["Host"].Value) + (ini["Speech"]["Port"].Value == "" ? "" : ":") + ini["Speech"]["Port"].Value + (Command.StartsWith("/") ? "" : "/") + Command;
  99. case eService.Keln:
  100. return (ini["Keln"]["Host"].Value == "" ? "localhost" : ini["Keln"]["Host"].Value) + (ini["Keln"]["Port"].Value == "" ? "" : ":") + ini["Keln"]["Port"].Value + (Command.StartsWith("/") ? "" : "/") + Command;
  101. case eService.GregorCalendus:
  102. return (ini["GregorCalendus"]["Host"].Value == "" ? "localhost" : ini["GregorCalendus"]["Host"].Value) + (ini["GregorCalendus"]["Port"].Value == "" ? "" : ":") + ini["GregorCalendus"]["Port"].Value + (Command.StartsWith("/") ? "" : "/") + Command;
  103. }
  104. return "";
  105. }
  106. public static string GetHostPort(eService service) {
  107. OpenIni();
  108. switch (service) {
  109. case eService.DeviceEmotion:
  110. return (ini["DeviceEmotion"]["Host"].Value == "" ? "localhost" : ini["DeviceEmotion"]["Host"].Value) + (ini["DeviceEmotion"]["Port"].Value == "" ? "" : ":") + ini["DeviceEmotion"]["Port"].Value;
  111. case eService.Intensi:
  112. return (ini["Intensi"]["Host"].Value == "" ? "localhost" : ini["Intensi"]["Host"].Value) + (ini["Intensi"]["Port"].Value == "" ? "" : ":") + ini["Intensi"]["Port"].Value;
  113. case eService.Sambale:
  114. return (ini["Sambale"]["Host"].Value == "" ? "localhost" : ini["Sambale"]["Host"].Value) + (ini["Sambale"]["Port"].Value == "" ? "" : ":") + ini["Sambale"]["Port"].Value;
  115. case eService.Sozoti:
  116. return (ini["Sozoti"]["Host"].Value == "" ? "localhost" : ini["Sozoti"]["Host"].Value) + (ini["Sozoti"]["Port"].Value == "" ? "" : ":") + ini["Sozoti"]["Port"].Value;
  117. case eService.Speech:
  118. return (ini["Speech"]["Host"].Value == "" ? "localhost" : ini["Speech"]["Host"].Value) + (ini["Speech"]["Port"].Value == "" ? "" : ":") + ini["Speech"]["Port"].Value;
  119. case eService.Keln:
  120. return (ini["Keln"]["Host"].Value == "" ? "localhost" : ini["Keln"]["Host"].Value) + (ini["Keln"]["Port"].Value == "" ? "" : ":") + ini["Keln"]["Port"].Value;
  121. case eService.GregorCalendus:
  122. return (ini["GregorCalendus"]["Host"].Value == "" ? "localhost" : ini["GregorCalendus"]["Host"].Value) + (ini["GregorCalendus"]["Port"].Value == "" ? "" : ":") + ini["GregorCalendus"]["Port"].Value;
  123. }
  124. return "";
  125. }
  126. private static void OpenIni() {
  127. if (ini != null)
  128. return;
  129. ini = new iniHandle("config/services.conf");
  130. bool bHasChanges = false;
  131. if (!ini.HasCategorie("DeviceEmotion")) {
  132. ini.Categories.Add(new iniCategorie(ini, "DeviceEmotion"));
  133. ini["DeviceEmotion"]["Host"].Value = "localhost";
  134. ini["DeviceEmotion"]["Port"].UInt16 = 8000;
  135. bHasChanges = true;
  136. }
  137. if (!ini.HasCategorie("Intensi")) {
  138. ini.Categories.Add(new iniCategorie(ini, "Intensi"));
  139. ini["Intensi"]["Host"].Value = "localhost";
  140. ini["Intensi"]["Port"].UInt16 = 8001;
  141. bHasChanges = true;
  142. }
  143. if (!ini.HasCategorie("Sambale")) {
  144. ini.Categories.Add(new iniCategorie(ini, "Sambale"));
  145. ini["Sambale"]["Host"].Value = "localhost";
  146. ini["Sambale"]["Port"].UInt16 = 8002;
  147. bHasChanges = true;
  148. }
  149. if (!ini.HasCategorie("Sozoti")) {
  150. ini.Categories.Add(new iniCategorie(ini, "Sozoti"));
  151. ini["Sozoti"]["Host"].Value = "localhost";
  152. ini["Sozoti"]["Port"].UInt16 = 8003;
  153. bHasChanges = true;
  154. }
  155. if (!ini.HasCategorie("Speech")) {
  156. ini.Categories.Add(new iniCategorie(ini, "Speech"));
  157. ini["Speech"]["Host"].Value = "localhost";
  158. ini["Speech"]["Port"].UInt16 = 8004;
  159. bHasChanges = true;
  160. }
  161. if (!ini.HasCategorie("GregorCalendus")) {
  162. ini.Categories.Add(new iniCategorie(ini, "GregorCalendus"));
  163. ini["GregorCalendus"]["Host"].Value = "localhost";
  164. ini["GregorCalendus"]["Port"].UInt16 = 8005;
  165. bHasChanges = true;
  166. }
  167. if (!ini.HasCategorie("Keln")) {
  168. ini.Categories.Add(new iniCategorie(ini, "Keln"));
  169. ini["Keln"]["Host"].Value = "localhost";
  170. ini["Keln"]["Port"].UInt16 = 8010;
  171. bHasChanges = true;
  172. }
  173. if (!System.IO.File.Exists("config/services.conf") || bHasChanges) {
  174. ini.Save();
  175. }
  176. }
  177. }
  178. }