Example #1
0
        private bool AddStation(station_form a_form)
        {
            wf_station station = new wf_station(api_key);

            if (station.GetStationMeta(a_form.station_id.ToString()) || (a_form.station_id == 0))
            {
                a_form.air_id    = (a_form.air_id == 0) ? station.Air : a_form.air_id;
                a_form.sky_id    = (a_form.sky_id == 0) ? station.Sky : a_form.sky_id;
                a_form.air_sn    = (a_form.air_sn == "") ? station.AirSN : a_form.air_sn;
                a_form.sky_sn    = (a_form.sky_sn == "") ? station.SkySN : a_form.sky_sn;
                a_form.elevation = (a_form.elevation == 0) ? station.Elevation : a_form.elevation;
                WeatherFlowNS.NS.AddStation(a_form.station_id, a_form.elevation, a_form.air_id, a_form.sky_id, a_form.remote, station.AirSN, station.SkySN, a_form.rapid);
                return(true);
            }
            else
            {
                WFLogging.Error("Failed to find station " + a_form.station_id.ToString());
                cfg_file_status = "Station " + a_form.station_id.ToString() + " lookup failed.";
            }
            return(false);
        }
Example #2
0
        private station_form GetStationInfo(string resp)
        {
            string[]     list;
            station_form sf = new station_form();

            sf.remote = false;
            sf.rapid  = false;

            list = resp.Split('&');
            foreach (string p in list)
            {
                string[] pr = p.Split('=');
                switch (pr[0])
                {
                case "station_id":
                    sf.station_id = 0;
                    int.TryParse(pr[1], out sf.station_id);
                    break;

                case "air_id": int.TryParse(pr[1], out sf.air_id); break;

                case "sky_id": int.TryParse(pr[1], out sf.sky_id); break;

                case "air_sn": sf.air_sn = pr[1]; break;

                case "sky_sn": sf.sky_sn = pr[1]; break;

                case "elevation": double.TryParse(pr[1], out sf.elevation); break;

                case "remote": sf.remote = true; break;

                case "rapid": sf.rapid = true; break;

                case "stationcfg": sf.action = pr[1]; break;
                }
            }
            return(sf);
        }
Example #3
0
        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();
        }