| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- 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<T>(eService service, string command) {
- return System.Net.ApiService.client.Get_Json<T>(GetUrl(service, command), DefaultHeader());
- }
- public static T GetJson<T>(eService service, string command, NameValueCollection query) {
- return System.Net.ApiService.client.Get_Json<T>(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<T>(eService service, string command) {
- return System.Net.ApiService.client.Get_Object<T>(GetUrl(service, command),DefaultHeader());
- }
- public static T GetObject<T>(eService service, string command, NameValueCollection query) {
- return System.Net.ApiService.client.Get_Object<T>(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<T>(eService service, string command, NameValueCollection post) {
- return System.Net.ApiService.client.Post_Json<T>(GetUrl(service, command), DefaultHeader(), post);
- }
- public static T PostJson<T>(eService service, string command, NameValueCollection query, NameValueCollection post) {
- return System.Net.ApiService.client.Post_Json<T>(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<T>(eService service, string command, NameValueCollection post) {
- return System.Net.ApiService.client.Post_Object<T>(GetUrl(service, command), DefaultHeader(), post);
- }
- public static T PostObject<T>(eService service, string command, NameValueCollection query, NameValueCollection post) {
- return System.Net.ApiService.client.Post_Object<T>(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();
- }
- }
- }
- }
|