コード例 #1
0
        public CommandSet ReadCommandsFromFile(string filepath)
        {
            try
            {
                if (!File.Exists(filepath))
                {
                    return(null);
                }
                SettingsCollection settings = null;
                try
                {
                    settings = (new SettingsFileLoader()).ReadSettingsFile(filepath);
                }
                catch (Exception exc)
                {
                    log.WriteError("Exception reading commands from file '{0}': {1}", filepath, exc);
                    //return new CommandSet(new List<Command>(), new DateTime());
                    return(null);
                }

                string fileVersion = SettingHelpers.GetSingleStringValue(settings, "FileVersion");
                if (!fileVersion.StartsWith(MASTER_FILE_VERSION_COMPAT))
                {
                    log.WriteError(
                        "Incompatible command file, version: {0}",
                        fileVersion);
                    return(null);
                }
                DateTime timestamp = settings.GetValue("Timestamp").GetUtcDateParam("TimeUtc");
                if (DateTime.UtcNow - timestamp > TimeSpan.FromHours(1))
                {
                    log.WriteInfo(
                        "Ignoring command file older than one hour, timestamp={0:O}",
                        timestamp);
                    return(null);
                }
                DateTime       acknowledgement = SettingHelpers.GetSingleDateTimeValue(settings, "AcknowledgementUtc");
                int            ncmds           = SettingHelpers.GetSingleIntValue(settings, "CommandCount");
                List <Command> cmdlist         = new List <Command>(ncmds);
                for (int i = 0; i < ncmds; ++i)
                {
                    string   key       = string.Format("Command{0}", i + 1);
                    var      cmdData   = settings.GetValue(key);
                    DateTime time      = cmdData.GetDateParam("TimeStampUtc");
                    string   cmdString = cmdData.GetStringParam("CommandString");
                    var      cmd       = new Command(time, cmdString);
                    cmdlist.Add(cmd);
                }

                CommandSet cmdset = new CommandSet(cmdlist, acknowledgement);
                return(cmdset);
            }
            catch (Exception exc)
            {
                log.WriteError("Exception reading command file: {0}", exc);
                return(null);
            }
        }