internal static void LogPage(HttpListenerContext context) { byte[] page; // Process changes if (context.Request.ContentLength64 > 0) { int len = (int)context.Request.ContentLength64; byte[] post = new byte[1024]; context.Request.InputStream.Read(post, 0, len); string resp = Encoding.Default.GetString(post); resp = resp.Substring(0, len); foreach (string item in resp.Split('&')) { string[] pair = item.Split('='); switch (pair[0]) { case "sLogLevel": int l = 0; int.TryParse(pair[1], out l); if (l != WF_Config.LogLevel) { WF_Config.LogLevel = l; WFLogging.Level = (LOG_LEVELS)l; WeatherFlowNS.SaveConfiguration(); } break; case "Save": // Look up value of filename and save log to that file string fname = element(resp, "filename"); try { using (StreamWriter sw = new StreamWriter(fname)) { sw.Write(WFLogging.ToString()); } } catch (Exception ex) { WFLogging.Error(ex.Message); } break; case "Clear": WFLogging.Clear(); break; } } } page = ASCIIEncoding.ASCII.GetBytes(MakeLog()); context.Response.ContentType = "text/html"; context.Response.ContentLength64 = page.Length; context.Response.AddHeader("Date", DateTime.Now.ToString("r")); context.Response.StatusCode = (int)HttpStatusCode.OK; context.Response.OutputStream.Write(page, 0, page.Length); context.Response.OutputStream.Flush(); context.Response.OutputStream.Close(); context.Response.Close(); }
private void ConfigPage(HttpListenerContext context) { string cfg_page; byte[] page; byte[] post = new byte[1024]; Thread do_something; //Console.WriteLine("content length = " + context.Request.ContentLength64.ToString()); if (context.Request.ContentLength64 > 0) { string[] list; string[] pair; int len = (int)context.Request.ContentLength64; bool initISY = false; bool saveCfg = false; context.Request.InputStream.Read(post, 0, len); string resp = Encoding.Default.GetString(post); resp = resp.Substring(0, len); //Console.WriteLine("Response = " + resp); cfg_file_status = ""; if (resp.Contains("stationcfg")) { station_form a_form = GetStationInfo(resp); switch (a_form.action) { case "Delete": WFLogging.Info("Delete station " + a_form.station_id.ToString()); WeatherFlowNS.NS.DeleteStation(a_form.station_id); saveCfg = true; break; case "++Add++": bool exists = false; WFLogging.Info("Add station " + a_form.station_id.ToString()); // Check if station already exists foreach (StationInfo s in WF_Config.WFStationInfo) { if (s.station_id == a_form.station_id) { cfg_file_status = "Station " + a_form.station_id.ToString() + " already exists."; exists = true; break; } } if (!exists) { saveCfg = AddStation(a_form); } break; case "Update": WFLogging.Info("Update station " + a_form.station_id.ToString()); saveCfg = AddStation(a_form); break; default: WFLogging.Warning("Unknown action [" + a_form.action + "]"); break; } } else if (resp.Contains("upload")) { // Upload profile files TODO: Should this be done in a thread? do_something = new Thread(WeatherFlowNS.NS.UpdateProfileFiles); do_something.IsBackground = true; do_something.Start(); } else { list = resp.Split('&'); foreach (string item in list) { int v = 0; pair = item.Split('='); pair[1] = HttpUtility.UrlDecode(pair[1]); switch (pair[0]) { case "sAddress": if (pair[1] != WF_Config.ISY) { WF_Config.ISY = pair[1]; initISY = true; saveCfg = true; } break; case "sUsername": if (pair[1] != WF_Config.Username) { WF_Config.Username = pair[1]; initISY = true; saveCfg = true; } break; case "sPassword": if (pair[1] != WF_Config.Password) { WF_Config.Password = pair[1]; initISY = true; saveCfg = true; } break; case "sProfile": int.TryParse(pair[1], out v); if (v != WF_Config.Profile) { WF_Config.Profile = v; initISY = true; saveCfg = true; } break; case "webPort": int.TryParse(pair[1], out v); if (v != WF_Config.Port) { WF_Config.Port = v; saveCfg = true; } break; case "sSI": bool imperial = (pair[1] == "1"); if (imperial != WF_Config.SI) { WF_Config.SI = (pair[1] == "1"); saveCfg = true; } break; case "sHub": bool hub = (pair[1] == "1"); if (hub != WF_Config.Hub) { WF_Config.Hub = (pair[1] == "1"); saveCfg = true; } break; case "sDevice": bool device = (pair[1] == "1"); if (device != WF_Config.Device) { WF_Config.Device = (pair[1] == "1"); saveCfg = true; } break; case "sUnits": int.TryParse(pair[1], out v); if (v != WF_Config.Units) { WF_Config.Units = v; saveCfg = true; } break; case "sLogLevel": int.TryParse(pair[1], out v); if (v != WF_Config.LogLevel) { WF_Config.LogLevel = v; WFLogging.Level = (LOG_LEVELS)v; saveCfg = true; } break; case "serverctl": if (pair[1].Contains("Restart")) { WeatherFlowNS.NS.udp_client.Start(); WeatherFlowNS.NS.heartbeat.Start(); cfg_file_status = "Server Started"; Thread.Sleep(400); } else if (pair[1].Contains("Pause")) { WeatherFlowNS.NS.heartbeat.Stop(); WeatherFlowNS.NS.udp_client.Stop(); cfg_file_status = "Server Paused"; } break; case "websocket": if (pair[1].Contains("Start")) { try { WeatherFlowNS.NS.StartWebSocket(); } catch (Exception ex) { WFLogging.Error("Starting websocket client failed: " + ex.Message); } cfg_file_status = "Websocket Client Started"; Thread.Sleep(400); } else if (pair[1].Contains("Stop")) { WeatherFlowNS.NS.wsi.Stop(); cfg_file_status = "Websocket Client Stopped"; Thread.Sleep(800); } break; default: break; } } } if (saveCfg) { cfg_file_status += WeatherFlowNS.SaveConfiguration(); } if (initISY) { WeatherFlowNS.NS.InitializeISY(); } } try { cfg_page = MakeConfigPage(); } catch (Exception ex) { WFLogging.Error("Failed to make configuration web page."); WFLogging.Error(ex.Message); context.Response.Close(); return; } // How can we substitute values into the page? May need to dynamically // generate the page instead of storing it as a resource. That would // be a bit of a pain. page = ASCIIEncoding.ASCII.GetBytes(cfg_page); context.Response.ContentType = "text/html"; context.Response.ContentLength64 = page.Length; context.Response.AddHeader("Date", DateTime.Now.ToString("r")); context.Response.StatusCode = (int)HttpStatusCode.OK; context.Response.OutputStream.Write(page, 0, page.Length); context.Response.OutputStream.Flush(); context.Response.OutputStream.Close(); context.Response.Close(); }