static void Main(string[] args) { try { string currentPath = Assembly.GetExecutingAssembly().Location; currentPath = Path.GetDirectoryName(currentPath); WriteLog("Starting"); //check that the output folder exists if (!Directory.Exists(Properties.Settings.Default.OutputFilePath)) Directory.CreateDirectory(Properties.Settings.Default.OutputFilePath); if (!String.IsNullOrWhiteSpace(Properties.Settings.Default.CosmApiId) && !String.IsNullOrWhiteSpace(Properties.Settings.Default.CosmFeed)) account = new Cosm.Account(Properties.Settings.Default.CosmApiId, Properties.Settings.Default.CosmFeed); //load the sensors xml string settingPath = Path.Combine(currentPath, "Sensors.xml"); WriteLog(String.Format("Loading settings from {0}", settingPath)); sensors = Sensors.FromFile(settingPath); WriteLog("Settings loaded"); if (System.Diagnostics.Debugger.IsAttached) { Charting c = new Charting(); c.GetGraph(null, DateTime.Parse("2013-04-04 20:00")); //Cosm c = new Cosm(account); //c.Post("Ambient", DateTime.Now, 12.2); } TempReader tr = new TempReader(); tr.Start(); WriteLog("Reader started"); HTTPComs coms = new HTTPComs(); coms.Listen(new string[] { "http://+:8086/" }); //Console.ReadLine(); WriteLog("Stopping"); tr.End(); } catch (Exception ex) { WriteLog(ex.Message); } }
public void Listen(string[] prefixes) { if (!HttpListener.IsSupported) { Console.WriteLine("Windows XP SP2 or Server 2003 is required to use the HttpListener class."); return; } // URI prefixes are required, // for example "http://contoso.com:8080/index/". if (prefixes == null || prefixes.Length == 0) throw new ArgumentException("prefixes"); // Create a listener. HttpListener listener = new HttpListener(); // Add the prefixes. foreach (string s in prefixes) { listener.Prefixes.Add(s); } listener.Start(); Program.WriteLog("Web service listening"); while (true) { // Note: The GetContext method blocks while waiting for a request. HttpListenerContext context = listener.GetContext(); HttpListenerRequest request = context.Request; // Obtain a response object. HttpListenerResponse response = context.Response; try { if (request.Url.LocalPath == "/stop") { // Construct a response. string responseString = "<HTML><BODY>Stopped</BODY></HTML>"; byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString); // Get a response stream and write the response to it. response.ContentLength64 = buffer.Length; response.OutputStream.Write(buffer, 0, buffer.Length); listener.Stop(); return; } else if (request.Url.LocalPath == "/image") { Charting c = new Charting(); c.GetGraph(response.OutputStream, DateTime.Now.AddDays(-1)); } else { //construct the page // Construct a response. string page = "<HTML><BODY>{0}<br><br><img src='/image' width='800px' height='600px' style='border:0' /><br><a href='/stop'>Stop</a></BODY></HTML>"; StringBuilder sb = new StringBuilder(); foreach (SensorConfig c in Program.sensors.sensors) { sb.AppendFormat("{0} : {1:F1}C ({2:F1}F) ", c.Name,c.CurrentTemp, Program.ToF(c.CurrentTemp)); } string responseString = String.Format(page, sb.ToString()); byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString); // Get a response stream and write the response to it. response.ContentLength64 = buffer.Length; response.OutputStream.Write(buffer, 0, buffer.Length); } } finally { response.OutputStream.Close(); } } }