static void Main(string[] args) { string name = "TEST"; string message = String.Empty; string uri = "http://localhost:2525/alarm"; bool auth = false; string user = string.Empty; string pass = string.Empty; string apikey = string.Empty; bool help = false; OptionSet paramset = new OptionSet() { {"m|message=", "The {MESSAGE} of the Triggertest.", s => message = s}, {"n|name=", "The {NAME} of the Trigger to test.", s => name = s}, {"u|uri=", "The {URI} to send the trigger request to.", s => uri = s}, {"a|auth", "Authenticate at the server", s => auth = s != null}, {"c|user="******"Authenticate as {USER}", s => user = s}, {"k|key=", "Set API-Key to {KEY}", s => apikey = s}, {"p|pass="******"Authenticate with password {PASSWORD}", s => pass = s}, {"h|help", "Show help", v => help = v != null} }; paramset.Parse(args); if (name == String.Empty) { Console.WriteLine("AlarmSystem TriggerTest Program"); Console.WriteLine("By Merlin Steuer in 2015"); Console.WriteLine(); paramset.WriteOptionDescriptions(Console.Out); } else { TriggerRequest test = new TriggerRequest(name, (message == string.Empty), message, apikey); TriggerRequestSender sender = new TriggerRequestSender(uri); sender.Authenticate = auth; sender.Username = user; sender.Password = pass; sender.ApiKey = apikey; if (sender.Send(test)) { Console.WriteLine("Success!"); } else { Console.WriteLine("Error!"); } } }
public bool Send(TriggerRequest request) { using (HttpClient client = new HttpClient()) { XDocument doc = new XDocument(); XElement root; doc.Add(root = new XElement("Request")); root.Add(new XElement("ApiKey", ApiKey)); root.Add(new XElement("Type", "TriggerRequest")); root.Add(XElement.Parse(request.ToString())); HttpContent content = new StringContent(doc.ToString(), Encoding.UTF8); if (Authenticate) { string authString = String.Format("{0}:{1}", Username, Password); var bytes = Encoding.UTF8.GetBytes(authString); var header = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(bytes)); client.DefaultRequestHeaders.Authorization = header; } using (HttpResponseMessage resp = client.PostAsync(_uri, content).Result) { Console.WriteLine(resp.StatusCode); if (resp.IsSuccessStatusCode) { return true; } else { return false; } } } }
public void HandleTriggerRequest(object sender, TriggerRequest request) { Log.Add(LogLevel.Debug, "Trigger", "Received trigger request"); ExecuteTrigger(request.TriggerText, request.SendDefaultMessage, request.Message, request.Source); }