public static void Dispose() { SerialComm.DeactivateSerialPort(); Server.Web.Stop(); Server.TCP.Stop(); Parser.ParserState = false; Environment.Exit(0); }
internal static string ToValues(this string context) { var method = Methods.Instance; IWeather weather = new OpenWeather(); if (context.Contains("%exit%") || context.Contains("%quit%")) { SerialComm.DeactivateSerialPort(); Parser.ParserState = false; context = context.Replace("%exit%", string.Empty) .Replace("%quit%", string.Empty); } if (context.Contains("%clear%") || context.Contains("%cls%")) { Console.Clear(); context = context.Replace("%clear%", string.Empty) .Replace("%cls%", string.Empty); } if (context.Contains("%mute%")) { Parser.Mute = true; context = context.Replace("%mute%", string.Empty); } if (context.Contains("%unmute%")) { Parser.Mute = false; context = context.Replace("%unmute%", string.Empty); } if (context.Contains("%inet%") || context.Contains("%inetcon%")) { String con = method.HasInternetConnection().ToString(); context = context.Replace("%inet%", con) .Replace("%inetcon%", con); } if (context.Contains("%gmailcount%") || context.Contains("%gcount%")) { context = context.Replace("%gmailcount%", new Net.Mail().GmailCheck(true)) .Replace("%gcount%", new Net.Mail().GmailCheck(true)); } if (context.Contains("%gmailreader%") || context.Contains("%gmailheaders%") || context.Contains("%greader%") || context.Contains("%gheaders%")) { context = context.Replace("%gmailreader%", new Net.Mail().GmailCheck(false)) .Replace("%gmailheaders%", new Net.Mail().GmailCheck(false)) .Replace("%greader%", new Net.Mail().GmailCheck(false)) .Replace("%gheaders%", new Net.Mail().GmailCheck(false)); } if (context.Contains("%pop3count%")) { context = context.Replace("%pop3count%", new Net.Mail().Pop3Check().ToString()); } if (context.Contains("%user%") || context.Contains("%whoami%")) { String whoami = method.WhoAmI(); context = context.Replace("%user%", whoami) .Replace("%whoami%", whoami); } if (context.Contains("%checkin%") || context.Contains("%usercheckin%")) { User.Status = true; context = context.Replace("%usercheckin%", string.Empty) .Replace("%checkin%", string.Empty); } if (context.Contains("%checkout%") || context.Contains("%usercheckout%")) { User.Status = false; context = context.Replace("%usercheckout%", string.Empty) .Replace("%checkout%", string.Empty); } if (context.Contains("%time%")) { context = context.Replace("%time%", method.GetTime()); } if (context.Contains("%time24%")) { context = context.Replace("%time24%", method.GetTime24()); } if (context.Contains("%hour%")) { context = context.Replace("%hour%", method.GetHour()); } if (context.Contains("%minute%")) { context = context.Replace("%minute%", method.GetMinute()); } if (context.Contains("%date%")) { context = context.Replace("%date%", method.GetDate()); } if (context.Contains("%calendardate%")) { context = context.Replace("%calendardate%", method.GetCalendarDate()); } if (context.Contains("%day%")) { context = context.Replace("%day%", method.GetDay()); } if (context.Contains("%calendarday%")) { context = context.Replace("%calendarday%", method.GetCalendarDay()); } if (context.Contains("%calendarmonth%")) { context = context.Replace("%calendarmonth%", method.GetCalendarMonth()); } if (context.Contains("%calendaryear%")) { context = context.Replace("%calendaryear%", method.GetCalendarYear()); } if (context.Contains("%salute%")) { context = context.Replace("%salute%", method.GetSalute()); } if (context.Contains("%daypart%") || context.Contains("%partofday%")) { String daypart = method.GetPartOfDay(false); context = context.Replace("%daypart%", daypart) .Replace("%partofday%", daypart); } if (context.Contains("%todayday%")) { context = context.Replace("%todayday%", weather.TodayDay); } if (context.Contains("%todayconditions%")) { context = context.Replace("%todayconditions%", weather.TodayConditions); } if (context.Contains("%todaylow%")) { context = context.Replace("%todaylow%", weather.TodayLow); } if (context.Contains("%todayhigh%")) { context = context.Replace("%todayhigh%", weather.TodayHigh); } if (context.Contains("%currenttemperature%") || context.Contains("%currenttemp%") || context.Contains("%todaytemp%") || context.Contains("%todaytemperature%")) { context = context.Replace("%currenttemperature%", weather.CurrentTemp) .Replace("%currenttemp%", weather.CurrentTemp) .Replace("%todaytemp%", weather.CurrentTemp) .Replace("%todaytemperature%", weather.CurrentTemp); } if (context.Contains("%currenthumidity%")) { context = context.Replace("%currenthumidity%", weather.CurrentHumidity); } if (context.Contains("%currentpressure%")) { context = context.Replace("%currentpressure%", weather.CurrentPressure); } if (context.Contains("%currentcity%")) { context = context.Replace("%currentcity%", weather.CurrentCity); } if (context.Contains("%weathericon%")) { context = context.Replace("%weathericon%", weather.WeatherIcon); } if (context.Contains("%tomorrowday%")) { context = context.Replace("%tomorrowday%", weather.TomorrowDay); } if (context.Contains("%tomorrowconditions%")) { context = context.Replace("%tomorrowconditions%", weather.TomorrowConditions); } if (context.Contains("%tomorrowlow%")) { context = context.Replace("%tomorrowlow%", weather.TomorrowLow); } if (context.Contains("%tomorrowhigh%")) { context = context.Replace("%tomorrowhigh%", weather.TomorrowHigh); } if (context.Contains("%whereami%") || context.Contains("%userstat%") || context.Contains("%userstatus%")) { if (User.Status) { context = context.Replace("%whereami%", "present") .Replace("%userstat%", "present") .Replace("%userstatus%", "present"); } else { context = context.Replace("%whereami%", "absent") .Replace("%userstat%", "absent") .Replace("%userstatus%", "absent"); } } if (context.Contains("%uptime%")) { context = context.Replace("%uptime%", Application.Uptime.getAll); } if (context.Contains("%updays%")) { context = context.Replace("%updays%", Application.Uptime.getDays.ToString()); } if (context.Contains("%uphours%")) { context = context.Replace("%uphours%", Application.Uptime.getHours.ToString()); } if (context.Contains("%upminutes%")) { context = context.Replace("%upminutes%", Application.Uptime.getMinutes.ToString()); } if (context.Contains("%upseconds%")) { context = context.Replace("%upseconds%", Application.Uptime.getSeconds.ToString()); } if (context.Contains("%about%") || context.Contains("%copyright%")) { context = context.Replace("%about%", method.GetCopyright()) .Replace("%copyright%", method.GetCopyright()); } // If Event if (context.Contains("%~>")) { method.GetEvent(context.Replace("%~>", string.Empty) .Replace("%", string.Empty)).Item(0).InnerText.Parse(); context = context.Replace(context, string.Empty); } return(context); }
internal static string JudoParser(string arg) { var appset = new ApplicationSettings(); var method = Methods.Instance; string output = string.Empty; string[] args; args = !arg.Contains("</lock>") ? ParsingTools.SplitArguments(arg.ToValues()) // Parse it normally : ParsingTools.SplitArguments(arg); // Leave it as is, code container switch (args[1]) { // TIMER case "timer": case "sleep": Thread.Sleep(Convert.ToInt32(args[2])); break; // SERIAL case "serial": SerialComm.SerialData = string.Empty; switch (args[2]) { case "open": if (args.Count() > 3) { SerialComm.ActivateSerialPort(args[3]); } else { SerialComm.ActivateSerialPort(string.Empty); } Thread.Sleep(50); output = "Serial port state: " + SerialComm.port.IsOpen; break; case "close": SerialComm.DeactivateSerialPort(); Thread.Sleep(50); output = "Serial port state: " + SerialComm.port.IsOpen; break; case "send": case "listen": case "monitor": try { lock (_serial_locker) { if (SerialComm.port.IsOpen) { if (args[2] == "send") { // Clear all buffers SerialComm.port.DiscardInBuffer(); SerialComm.port.DiscardOutBuffer(); SerialComm.SerialData = string.Empty; // Send a new argument SerialComm.port.WriteLine(args[3]); Thread.Sleep(220); } Action getSerialData = () => { while (output == string.Empty) { output = SerialComm.SerialData; Thread.Sleep(50); } }; if ((args[2] == "listen" || args[2] == "monitor") && args.Count() > 3) { Process.CallWithTimeout(getSerialData, Convert.ToInt32(args[3])); } else { Process.CallWithTimeout(getSerialData, 10000); } } else { output = "Serial port state: " + SerialComm.port.IsOpen; } } } catch { //Suppress //Logger.Instance.Append(string.Format("Serial Exception <JudoParser>: {0}", e.Message)); } break; case "set": case "setup": if (args.Count() >= 4) { output = method.AddToXML(new InstructionSet(args[3]), ApplicationSettings.ApplicationStructure.SystemCommRoot, ApplicationSettings.ApplicationStructure.ComPortElement); } if (args.Count() == 5) { output = method.AddToXML(new InstructionSet(args[4]), ApplicationSettings.ApplicationStructure.SystemCommRoot, ApplicationSettings.ApplicationStructure.ComBaudRateElement); } break; case "settings": output = string.Format("{0}\r\n{1}", appset.ComPort, appset.Baud); break; case "state": case "status": default: output = "Serial port state: " + SerialComm.port.IsOpen; break; } break; // HELP case "help": case "?": output = args.Count() > 2 ? output = method.getHelp(args[2]) : output = method.getHelp("all"); break; // INSTRUCTION SETS case "inset": switch (args[2]) { case "add": case "new": case "set": case "setup": if (args.Count() == 5) { output = method.AddToXML(new InstructionSet(args[3], // ID args[4]), // Action ApplicationSettings.ApplicationStructure.SystemInstructionsRoot, "InstructionSet"); } else { output = method.AddToXML(new InstructionSet(args[3], // ID args[4], // Action args[5].Replace("\"", string.Empty).Replace("'", string.Empty), // Category args[6].Replace("\"", string.Empty).Replace("'", string.Empty), // Header args[7].Replace("\"", string.Empty).Replace("'", string.Empty), // Short Description args[8].Replace("\"", string.Empty).Replace("'", string.Empty), // Description args[9].Replace("\"", string.Empty).Replace("'", string.Empty), // Thumbnail Url args[10].Replace("\"", string.Empty).Replace("'", string.Empty)), // Reference ApplicationSettings.ApplicationStructure.SystemInstructionsRoot, "InstructionSet"); } break; case "remove": case "rm": case "delete": case "del": case "kill": output = method.RemoveFromXML(args[3], ApplicationSettings.ApplicationStructure.SystemInstructionsRoot, "InstructionSet"); break; case "list": case "ls": default: XmlNodeList xList = method.GetXmlElementList(ApplicationSettings.ApplicationStructure.SystemInstructionsRoot, "InstructionSet"); foreach (XmlNode nodeItem in xList) { output += string.Format("{0}\r\n", nodeItem.OuterXml); } break; } break; // EVENTS case "event": switch (args[2]) { case "add": case "new": case "set": case "setup": output = method.AddToXML(new InstructionSet(args[3], args[4]), ApplicationSettings.ApplicationStructure.SystemEventsRoot, "event"); break; case "remove": case "rm": case "delete": case "del": case "kill": output = method.RemoveFromXML(args[3], ApplicationSettings.ApplicationStructure.SystemEventsRoot, "event"); break; case "list": case "ls": default: XmlNodeList xList = method.GetXmlElementList(ApplicationSettings.ApplicationStructure.SystemEventsRoot, "event"); foreach (XmlNode nodeItem in xList) { output += string.Format("{0}\r\n", nodeItem.OuterXml); } break; } break; // SOCKET LISTENING MODE case "socket": switch (args[2]) { case "on": case "enable": case "start": case "listen": case "open": Server.TCP.Start(); Thread.Sleep(50); output = "Socket state: " + Server.TCP.ServerState; break; case "off": case "disable": case "stop": case "close": Server.TCP.Stop(); Thread.Sleep(50); output = "Socket state: " + Server.TCP.ServerState; break; case "set": case "setup": if (args.Count() >= 4) { output = method.AddToXML(new InstructionSet(args[3]), ApplicationSettings.ApplicationStructure.SystemCommRoot, ApplicationSettings.ApplicationStructure.LocalHostElement); } if (args.Count() == 5) { output = method.AddToXML(new InstructionSet(args[4]), ApplicationSettings.ApplicationStructure.SystemCommRoot, ApplicationSettings.ApplicationStructure.LocalPortElement); } break; case "settings": output = string.Format("{0}\r\n{1}", appset.LocalHost, appset.LocalPort); break; case "state": case "status": default: output = "Socket state: " + Server.TCP.ServerState; break; } break; // WEB SERVER MODE case "server": switch (args[2]) { case "on": case "enable": case "start": case "listen": Server.Web.Start(); Thread.Sleep(50); output = "Web server state: " + Server.Web.httplistener.IsListening; break; case "off": case "disable": case "stop": Server.Web.Stop(); Thread.Sleep(50); output = "Web server state: " + Server.Web.httplistener.IsListening; break; case "login": case "cred": case "credentials": output = new Settings().SaveSettings(".htaccess", string.Format("{0}\r\n{1}", args[3], args[4])); break; case "set": case "setup": if (args.Count() >= 4) { output = method.AddToXML(new InstructionSet(args[3]), ApplicationSettings.ApplicationStructure.SystemCommRoot, ApplicationSettings.ApplicationStructure.HttpHostNameElement); } if (args.Count() >= 5) { output = method.AddToXML(new InstructionSet(args[4]), ApplicationSettings.ApplicationStructure.SystemCommRoot, ApplicationSettings.ApplicationStructure.HttpPortElement); } if (args.Count() == 6) { output = method.AddToXML(new InstructionSet(args[5]), ApplicationSettings.ApplicationStructure.SystemCommRoot, ApplicationSettings.ApplicationStructure.HttpAuthenticationElement); } break; case "settings": output = string.Format("{0}\r\n{1}\r\n{2}", appset.HostName, appset.HttpPort, appset.Authentication); break; case "state": case "status": default: output = "Web server state: " + Server.Web.httplistener.IsListening; break; } break; // SCHEDULER case "schedule": switch (args[2]) { case "add": case "new": case "set": case "setup": var s = arg.Replace("judo schedule add ", string.Empty).ToSchedule(); if (s.Date == Schedule.Period.Repeat || s.Date == Schedule.Period.Interval || s.Date == Schedule.Period.Timer) { output = Schedule.Add(s, Convert.ToInt32(s.Time)); } else { output = Schedule.Add(s); } break; case "enable": case "activate": case "start": case "on": output = Schedule.ChangeStatus(args[3], Schedule.State.Enable); break; case "enable-all": case "activate-all": case "start-all": case "on-all": output = Schedule.ChangeStatus(Schedule.State.EnableAll); break; case "disable": case "deactivate": case "stop": case "off": output = Schedule.ChangeStatus(args[3], Schedule.State.Disable); break; case "disable-all": case "deactivate-all": case "stop-all": case "off-all": output = Schedule.ChangeStatus(Schedule.State.DisableAll); break; case "remove": case "rm": case "delete": case "del": output = Schedule.ChangeStatus(args[3], Schedule.State.Remove); break; case "remove-all": case "delete-all": case "del-all": case "cleanup": case "clear": case "empty": output = Schedule.ChangeStatus(Schedule.State.RemoveAll); break; case "active": case "actives": case "active-list": case "active-ls": case "list-actives": case "ls-actives": foreach (Schedule schedule in Schedule.ScheduleList) { if (schedule.Status) { output += string.Format("{0}\r\n", schedule.Name); } } break; case "inactive": case "inactives": case "inactive-list": case "inactive-ls": case "list-inactives": case "ls-inactives": foreach (Schedule schedule in Schedule.ScheduleList) { if (!schedule.Status) { output += string.Format("{0}\r\n", schedule.Name); } } break; case "names": case "name-list": case "name-ls": case "list-names": case "ls-names": foreach (Schedule schedule in Schedule.ScheduleList) { output += string.Format("{0}\r\n", schedule.Name); } break; case "active-details": case "actives-details": case "active-list-details": case "active-ls-details": case "list-actives-details": case "ls-actives-details": foreach (Schedule schedule in Schedule.ScheduleList) { if (schedule.Status) { output += string.Format("{0} | {1} | {2} | {3} | {4}\r\n", schedule.Name, schedule.Date, schedule.Time, schedule.Action, schedule.Status.ToString().ToLower().Replace("true", "Active")); } } break; case "inactive-details": case "inactives-details": case "inactive-list-details": case "inactive-ls-details": case "list-inactives-details": case "ls-inactives-details": foreach (Schedule schedule in Schedule.ScheduleList) { if (!schedule.Status) { output += string.Format("{0} | {1} | {2} | {3} | {4}\r\n", schedule.Name, schedule.Date, schedule.Time, schedule.Action, schedule.Status.ToString().ToLower().Replace("false", "Inactive")); } } break; case "details": case "state": case "status": case "list": case "ls": default: foreach (Schedule schedule in Schedule.ScheduleList) { if (args.Count() > 3) { if (args[3] == schedule.Name) { output += string.Format("{0} | {1} | {2} | {3} | {4}\r\n", schedule.Name, schedule.Date, schedule.Time, schedule.Action, schedule.Status.ToString().ToLower().Replace("true", "Active") .Replace("false", "Inactive")); } } else { output += string.Format("{0} | {1} | {2} | {3} | {4}\r\n", schedule.Name, schedule.Date, schedule.Time, schedule.Action, schedule.Status.ToString().ToLower().Replace("true", "Active") .Replace("false", "Inactive")); } } break; } break; // SMTP case "smtp": switch (args[2]) { case "add": case "setup": case "set": output = new Settings().SaveSettings(".smtpsettings", string.Format("{0}\r\n{1}\r\n{2}\r\n{3}\r\n{4}", args[3], args[4], args[5], args[6], args[7])); break; case "settings": var smtpSettings = new Net.Mail.SmtpSettings(); output = string.Format("{0}\r\n{1}\r\n{2}\r\n{3}\r\n{4}", smtpSettings.Host, smtpSettings.Username, smtpSettings.Password, smtpSettings.Port, smtpSettings.SSL); break; } break; // POP3 case "pop3": switch (args[2]) { case "add": case "setup": case "set": output = new Settings().SaveSettings(".pop3settings", string.Format("{0}\r\n{1}\r\n{2}\r\n{3}\r\n{4}", args[3], args[4], args[5], args[6], args[7])); break; case "settings": var pop3Settings = new Net.Mail.Pop3Settings(); output = string.Format("{0}\r\n{1}\r\n{2}\r\n{3}\r\n{4}", pop3Settings.Host, pop3Settings.Username, pop3Settings.Password, pop3Settings.Port, pop3Settings.SSL); break; } break; // GMAIL case "gmail": switch (args[2]) { case "add": case "setup": case "set": output = new Settings().SaveSettings(".gmailsettings", string.Format("{0}\r\n{1}", args[3], args[4])); break; case "settings": var gmailSettings = new Net.Mail.GmailSettings(); output = string.Format("{0}\r\n{1}", gmailSettings.Username, gmailSettings.Password); break; } break; // MAIL case "mail": switch (args[2]) { case "send": output = new Net.Mail().Send(args[3], args[4], args[5], args[6]).ToString() .Replace("True", "Mail sent!").Replace("False", "Mail could not be sent"); break; } break; // SMS case "sms": switch (args[2]) { case "add": case "setup": case "set": output = new Settings().SaveSettings(".smssettings", string.Format("{0}\r\n{1}\r\n{2}", args[3], args[4], args[5])); break; case "settings": var smsSettings = new SMS.SmsSettings(); output = string.Format("{0}\r\n{1}\r\n{2}", smsSettings.SmsAPI, smsSettings.SmsUsername, smsSettings.SmsPassword); break; case "send": output = new SMS().Send(args[3], args[4]); break; } break; // WEB API case "json": switch (args[2]) { case "add": case "new": case "set": case "setup": output = method.AddToXML(new InstructionSet(args[3], // ID "judo json get " + Server.Web.SimpleUriEncode(args[4]) + " " + args[5]), //Action ApplicationSettings.ApplicationStructure.SystemInstructionsRoot, "InstructionSet"); break; case "get": case "response": case "consume": case "extract": output = new Helpers.Json().SelectSingleNode(Server.Web.SimpleUriDecode(args[3]), args[4]); break; } break; // WEB SERVICE case "xml": switch (args[2]) { case "add": case "new": case "set": case "setup": switch (args.Count()) { case 6: output = method.AddToXML(new InstructionSet(args[3], // ID "judo xml get " + Server.Web.SimpleUriEncode(args[4]) + " " + args[5]), //Action ApplicationSettings.ApplicationStructure.SystemInstructionsRoot, "InstructionSet"); break; case 7: output = method.AddToXML(new InstructionSet(args[3], // ID "judo xml get " + Server.Web.SimpleUriEncode(args[4]) + " " + args[5] + " " + args[6]), //Action ApplicationSettings.ApplicationStructure.SystemInstructionsRoot, "InstructionSet"); break; case 8: output = method.AddToXML(new InstructionSet(args[3], // ID "judo xml get " + Server.Web.SimpleUriEncode(args[4]) + " " + args[5] + " " + args[6] + " " + args[7]), //Action ApplicationSettings.ApplicationStructure.SystemInstructionsRoot, "InstructionSet"); break; } break; case "get": case "response": case "consume": case "extract": switch (args.Count()) { case 5: output = Helpers.Xml.SelectSingleNode(Server.Web.SimpleUriDecode(args[3]), args[4]); break; case 6: output = args[4].Contains("=") ? Helpers.Xml.SelectNodes(Server.Web.SimpleUriDecode(args[3]), args[4], args[5])[0] : Helpers.Xml.SelectSingleNode(Server.Web.SimpleUriDecode(args[3]), args[4], Convert.ToInt32(args[5])); break; case 7: output = Helpers.Xml.SelectNodes(Server.Web.SimpleUriDecode(args[3]), args[4], args[5])[Convert.ToInt32(args[6])]; break; } break; } break; // HTTP case "http": switch (args[2]) { case "get": output = Helpers.Http.Get(Server.Web.SimpleUriDecode(args[3])); break; } break; // WEATHER case "weather": switch (args[2]) { case "set": case "setup": output = method.AddToXML(new InstructionSet(args[3]), ApplicationSettings.ApplicationStructure.SystemOthersRoot, ApplicationSettings.ApplicationStructure.Weather); break; case "settings": output = appset.Weather; break; } break; // PINGER case "ping": output = args.Count() == 3 ? Net.SimplePing.Ping(args[2]).ToString() : Net.SimplePing.Ping(args[2], Convert.ToInt32(args[3])).ToString(); break; } return(output); }