Example #1
0
    public YAMLObject GetYAMLObject(string path)
    {
        if (File.Exists(path))
        {
            if (cache.ContainsKey(path))
            {
                return(cache[path]);
            }
            else
            {
                var input = new StreamReader(path, Encoding.UTF8);
                var yaml  = new YamlStream();
                yaml.Load(input);
                YamlMappingNode rootNode = (YamlMappingNode)yaml.Documents[0].RootNode;

                YAMLObject yamlObject = new YAMLObject();

                {
                    var nodes = rootNode.Children[new YamlScalarNode("bool")];
                    var map   = nodes as YamlMappingNode;
                    if (map != null)
                    {
                        foreach (var node in map)
                        {
                            yamlObject.AddData(node.Key.ToString(), bool.Parse(node.Value.ToString()));
                        }
                    }
                }

                {
                    var nodes = rootNode.Children[new YamlScalarNode("float")];
                    var map   = nodes as YamlMappingNode;
                    if (map != null)
                    {
                        foreach (var node in map)
                        {
                            yamlObject.AddData(node.Key.ToString(), float.Parse(node.Value.ToString()));
                        }
                    }
                }

                {
                    var nodes = rootNode.Children[new YamlScalarNode("int")];
                    var map   = nodes as YamlMappingNode;
                    if (map != null)
                    {
                        foreach (var node in map)
                        {
                            yamlObject.AddData(node.Key.ToString(), int.Parse(node.Value.ToString()));
                        }
                    }
                }

                {
                    var nodes = rootNode.Children[new YamlScalarNode("string")];
                    var map   = nodes as YamlMappingNode;
                    if (map != null)
                    {
                        foreach (var node in map)
                        {
                            yamlObject.AddData(node.Key.ToString(), node.Value.ToString());
                        }
                    }
                }

                input.Close();

                return(yamlObject);
            }
        }
        else
        {
            return(null);
        }
    }
        private void loadSettings()
        {
            if (data_path == null)
            {
                //only use hard drive once per run:
                string data_path_specifier_file_fullname        = Path.Combine(@"C:\ProgramData\IntegratorEduTeacherNotify", data_path_specifier_file_name);
                string locally_ignored_categories_file_fullname = Path.Combine(@"C:\ProgramData\IntegratorEduTeacherNotify", locally_ignored_categories_file_name);
                try {
                    if (File.Exists(data_path_specifier_file_fullname))
                    {
                        StreamReader streamIn = new StreamReader(data_path_specifier_file_fullname);
                        string       line;
                        string       line_strip;
                        while ((line = streamIn.ReadLine()) != null)
                        {
                            line_strip = line.Trim();
                            if (line_strip.Length > 0)
                            {
                                data_path = line_strip;
                            }
                        }
                        streamIn.Close();
                    }
                    if (File.Exists(locally_ignored_categories_file_fullname))
                    {
                        StreamReader streamIn = new StreamReader(locally_ignored_categories_file_fullname);
                        string       line;
                        string       line_strip;
                        while ((line = streamIn.ReadLine()) != null)
                        {
                            line_strip = line.Trim();
                            if (line_strip.Length > 0)
                            {
                                if (locally_ignored_categories_list == null)
                                {
                                    locally_ignored_categories_list = new ArrayList();
                                }
                                locally_ignored_categories_list.Add(line_strip);
                            }
                        }
                        streamIn.Close();
                    }
                    if (string.IsNullOrEmpty(data_path) || !Directory.Exists(data_path))
                    {
                        //data_path=@"..\IntegratorEduTeacherNotify_FCA_data"
                        Console.Error.WriteLine("Missing schedule data");
                    }
                }
                catch (Exception exn) {
                    Console.Error.WriteLine("loadSettings could not finish: " + exn.ToString());
                }
            }
            if (data_path == null)
            {
                data_path = @"\\FCAFILES\main\Operations\BellSettings";
            }
            ;
            sounds_path = Path.Combine(data_path, "sounds");
            organization_settings_file_path = Path.Combine(data_path, "settings.yml");
            YAMLObject settingsYO = new YAMLObject();

            is_verbose            = true; //debug only
            YAMLObject.is_verbose = is_verbose;
            if (is_verbose)
            {
                Console.Error.WriteLine();
            }
            if (is_verbose)
            {
                Console.Error.WriteLine(organization_settings_file_path + "...");
            }

            settingsYO.load(organization_settings_file_path);
            ArrayList error_strings = settingsYO.deq_errors_in_yaml_syntax();

            if (error_strings != null && error_strings.Count > 0)
            {
                Console.Error.WriteLine(error_strings.Count.ToString() + " syntax error(s) in " + organization_settings_file_path + ":");
                foreach (string error_string in error_strings)
                {
                    Console.Error.WriteLine(error_string);
                }
            }

            organization_current_calendar_name = settingsYO.get_sub_value("current_calendar");
            organization_current_calendar_path = Path.Combine(data_path, organization_current_calendar_name);
            Console.Error.WriteLine("organization_current_calendar_path:" + organization_current_calendar_path);
            organization_date_ranges_file_path = Path.Combine(organization_current_calendar_path, "date ranges.yml");
            daterangesYO = new YAMLObject();
            if (is_verbose)
            {
                Console.Error.WriteLine();
            }
            if (is_verbose)
            {
                Console.Error.WriteLine(organization_date_ranges_file_path + "...");
            }

            daterangesYO.load(organization_date_ranges_file_path);
            error_strings = daterangesYO.deq_errors_in_yaml_syntax();
            if (error_strings != null && error_strings.Count > 0)
            {
                Console.Error.WriteLine(error_strings.Count.ToString() + " syntax error(s) in " + organization_date_ranges_file_path + ":");
                foreach (string error_string in error_strings)
                {
                    Console.Error.WriteLine(error_string);
                }
            }

            if (is_verbose)
            {
                Console.Error.WriteLine("organization_date_ranges.dump_to_stderr():");
                daterangesYO.dump_to_stderr();
                daterangesYO.save("output.verbose.dateranges.yml");
            }
            int  index     = 0;
            bool is_active = false;

            schedule_path         = null;   //the current schedule where today is in its range
            current_schedule_name = null;
            while (!is_active)
            {
                YAMLObject daterangeYO = daterangesYO.get_array_value(index);
                if (daterangeYO != null)
                {
                    current_schedule_name = daterangeYO.get_sub_value("schedule");
                    string daterange_first = daterangeYO.get_sub_value("first");
                    string daterange_last  = daterangeYO.get_sub_value("last");
                    Console.Error.WriteLine("[" + index.ToString() + "]:");
                    if (current_schedule_name != null)
                    {
                        Console.Error.WriteLine("  schedule:" + current_schedule_name);
                        if (daterange_first != null)
                        {
                            Console.Error.WriteLine("  first:" + daterange_first);
                            if (daterange_last != null)
                            {
                                Console.Error.WriteLine("  last:" + daterange_last);
                                DateTime start_dt = DateTime.Parse(daterange_first + "/" + DateTime.Now.Year.ToString() + " 0:00");
                                DateTime end_dt   = DateTime.Parse(daterange_last + "/" + DateTime.Now.Year.ToString() + " 23:59");
                                if (DateTime.Now >= start_dt && DateTime.Now <= end_dt)
                                {
                                    Console.Error.WriteLine("Current schedule:" + current_schedule_name);
                                    schedule_path = Path.Combine(organization_current_calendar_path, current_schedule_name);
                                }
                                else
                                {
                                    Console.Error.WriteLine(DateTime.Now.ToString("yyy-MM-dd HH:mm:ss")
                                                            + " is not in date range "
                                                            + start_dt.ToString("yyy-MM-dd HH:mm:ss")
                                                            + " to "
                                                            + end_dt.ToString("yyy-MM-dd HH:mm:ss")
                                                            );
                                }
                            }
                            else
                            {
                                Console.Error.WriteLine("  last:null");
                            }
                        }
                        else
                        {
                            Console.Error.WriteLine("  first:null");
                        }
                    }
                    else
                    {
                        Console.Error.WriteLine("  schedule:null");
                    }
                }
                else
                {
                    break;
                }
                index++;
            }
            recurring_event_categories.Clear();
            if (schedule_path != null)
            {
                events_file_path = Path.Combine(schedule_path, "Events, Daily Disposable.csv");
                daily_disposable_events_table = new FastTable();
                Console.Error.WriteLine("Setting schedule to '" + events_file_path + "' since today is in it's daterange.");
                daily_disposable_events_table.Load(events_file_path, true);
                //TODO: (show an error if there is a problem) if (daily_disposable_events_table.
            }
            else
            {
                daily_disposable_events_table = null;
            }
            categories_path = Path.Combine(data_path, "RecurringEventCategories");
            try {
                DirectoryInfo recDI = new DirectoryInfo(categories_path);
                foreach (FileInfo thisFI in recDI.GetFiles())
                {
                    if (!thisFI.Name.StartsWith(".") && thisFI.Name.ToLower().EndsWith(".yml"))
                    {
                        IEduEventCategory thisEC = new IEduEventCategory();
                        thisEC.name = thisFI.Name.Substring(0, thisFI.Name.Length - 4);
                        YAMLObject thisYO = new YAMLObject();
                        if (is_verbose)
                        {
                            Console.Error.WriteLine();
                        }
                        if (is_verbose)
                        {
                            Console.Error.WriteLine(thisFI.FullName + "...");
                        }
                        thisYO.load(thisFI.FullName);
                        ArrayList cat_error_strings = thisYO.deq_errors_in_yaml_syntax();
                        if (cat_error_strings != null && cat_error_strings.Count > 0)
                        {
                            Console.Error.WriteLine(cat_error_strings.Count.ToString() + " syntax error(s) in " + thisFI.FullName + ":");
                            foreach (string error_string in cat_error_strings)
                            {
                                Console.Error.WriteLine(error_string);
                            }
                        }
                        thisEC.sound_file_path = thisYO.get_sub_value("sound");
                        thisEC.message         = thisYO.get_sub_value("message");
                        thisEC.message_heading = thisYO.get_sub_value("message_heading");
                        recurring_event_categories.Add(thisEC);
                        //Console.Error.WriteLine("sound:"+((thisEC.sound_file_path!=null)?thisEC.sound_file_path:"null"));
                    }
                }
            }
            catch (Exception exn) {
                last_error_string = exn.ToString();
            }

            settings_load_datetime = DateTime.Now;
            last_daily_alert_not_including_before_loadtime_alerts_time = settings_load_datetime;             //prevents loading backlogged events
            updateStatus();
        }