internal LogSettings(Load program) { InitializeComponent(); this.program = program; timeConverter = new Converters.TimeConverter(); // fill with current values textBoxPeriod.Text = (string)(timeConverter.Convert(program.LoggingPeriod, typeof(string), program.LoggingTimeUnit, System.Globalization.CultureInfo.CurrentCulture)); comboBoxUnit.Items.Clear(); for (byte i = 0; i < (Enum.GetValues(typeof(TimeUnits))).Length; i++) { comboBoxUnit.Items.Add((TimeUnits)i); if ((byte)(program.LoggingTimeUnit) == i) { comboBoxUnit.SelectedIndex = i; } } }
// creates Xml file with program items, loop info, local/remote info and period settings public void SaveItems(BindingList <ProgramItem> programItems, bool loopIsEnabled, int loopCount, bool isRemote, double logPeriod, TimeUnits logTimeUnit, LEDBrightnesses LEDBrightness, byte LEDRule, FanRules FanRule, MeasurementFilters MeasurementFilter, bool AutorangingCurrent, bool AutorangingVoltage) { XmlNode declaration = document.CreateXmlDeclaration("1.0", "UTF-8", null); document.AppendChild(declaration); XmlNode comment = document.CreateComment("MightyWatt R3 experiment settings"); document.AppendChild(comment); XmlElement main = document.CreateElement("settings"); document.AppendChild(main); if (loopIsEnabled == true) { // loop settings XmlNode loop = document.CreateElement("loop"); // duration XmlAttribute count = document.CreateAttribute("count"); if (loopCount < 0) { count.Value = "infinite"; } else { count.Value = loopCount.ToString(); } loop.Attributes.Append(count); main.AppendChild(loop); } // local/remote settings XmlNode remote = document.CreateElement("voltageSenseMode"); main.AppendChild(remote); // is remote XmlAttribute isrem = document.CreateAttribute("isRemote"); isrem.Value = isRemote.ToString(); remote.Attributes.Append(isrem); // LED settings XmlNode LED = document.CreateElement("LED"); main.AppendChild(LED); // brightness XmlAttribute brightness = document.CreateAttribute("brightness"); brightness.Value = Enum.GetName(typeof(LEDBrightnesses), LEDBrightness); LED.Attributes.Append(brightness); // rules XmlAttribute LEDrule = document.CreateAttribute("rules"); LEDrule.Value = "0x" + LEDRule.ToString("X2"); LED.Attributes.Append(LEDrule); // Fan XmlNode FanRules = document.CreateElement("fan"); main.AppendChild(FanRules); // rules XmlAttribute AFanRule = document.CreateAttribute("rules"); AFanRule.Value = Enum.GetName(typeof(FanRules), FanRule); FanRules.Attributes.Append(AFanRule); // Measurement filter XmlNode MeasurementFilters = document.CreateElement("measurement"); main.AppendChild(MeasurementFilters); // filter XmlAttribute AMeasurementFilter = document.CreateAttribute("filter"); AMeasurementFilter.Value = Enum.GetName(typeof(MeasurementFilters), MeasurementFilter); MeasurementFilters.Attributes.Append(AMeasurementFilter); // Autoranging XmlNode Autoranging = document.CreateElement("autoranging"); main.AppendChild(Autoranging); // current XmlAttribute CurrentAutoranging = document.CreateAttribute("current"); CurrentAutoranging.Value = AutorangingCurrent.ToString(); Autoranging.Attributes.Append(CurrentAutoranging); // voltage XmlAttribute VoltageAutoranging = document.CreateAttribute("voltage"); VoltageAutoranging.Value = AutorangingVoltage.ToString(); Autoranging.Attributes.Append(VoltageAutoranging); // logging settings XmlNode loggingPeriod = document.CreateElement("logging"); main.AppendChild(loggingPeriod); XmlAttribute periodValue = document.CreateAttribute("period"); timeConverter = new Converters.TimeConverter(); // convert seconds to representation in time-unit periodValue.Value = (string)(timeConverter.Convert(logPeriod, typeof(string), logTimeUnit, System.Globalization.CultureInfo.CurrentCulture)); loggingPeriod.Attributes.Append(periodValue); XmlAttribute periodTimeUnit = document.CreateAttribute("timeUnit"); periodTimeUnit.Value = logTimeUnit.ToString(); loggingPeriod.Attributes.Append(periodTimeUnit); // items XmlNode items = document.CreateElement("items"); main.AppendChild(items); foreach (ProgramItem programItem in programItems) { XmlNode item = document.CreateElement("item"); XmlAttribute programMode = document.CreateAttribute("programMode"); programMode.Value = programItem.ProgramMode.ToString(); item.Attributes.Append(programMode); switch (programItem.ProgramMode) { case ProgramModes.Constant: { // parameters XmlAttribute mode = document.CreateAttribute("mode"); mode.Value = programItem.Mode.ToString(); item.Attributes.Append(mode); XmlAttribute value = document.CreateAttribute("value"); if (programItem.Value == null) { value.Value = "previous"; } else { value.Value = programItem.Value.ToString(); } item.Attributes.Append(value); XmlAttribute duration = document.CreateAttribute("duration"); duration.Value = programItem.DurationString; item.Attributes.Append(duration); XmlAttribute timeUnit = document.CreateAttribute("timeUnit"); timeUnit.Value = programItem.TimeUnit.ToString(); item.Attributes.Append(timeUnit); break; } case ProgramModes.Ramp: { // parameters XmlAttribute mode = document.CreateAttribute("mode"); mode.Value = programItem.Mode.ToString(); item.Attributes.Append(mode); XmlAttribute startingValue = document.CreateAttribute("startingValue"); if (programItem.StartingValue == null) { startingValue.Value = "previous"; } else { startingValue.Value = programItem.StartingValue.ToString(); } item.Attributes.Append(startingValue); XmlAttribute finalValue = document.CreateAttribute("finalValue"); finalValue.Value = programItem.FinalValue.ToString(); item.Attributes.Append(finalValue); XmlAttribute duration = document.CreateAttribute("duration"); duration.Value = programItem.DurationString; item.Attributes.Append(duration); XmlAttribute timeUnit = document.CreateAttribute("timeUnit"); timeUnit.Value = programItem.TimeUnit.ToString(); item.Attributes.Append(timeUnit); break; } case ProgramModes.Pin: { XmlAttribute pinNumber = document.CreateAttribute("pinLogicalNumber"); pinNumber.Value = programItem.Pin < Load.Pins.Length ? programItem.Pin.ToString() : "all"; item.Attributes.Append(pinNumber); XmlAttribute action = document.CreateAttribute("action"); action.Value = programItem.SetUserPin ? "set" : "reset"; item.Attributes.Append(action); break; } } // skip attributes if (programItem.SkipEnabled) { XmlNode skip = document.CreateElement("skip"); XmlAttribute skipMode = document.CreateAttribute("mode"); skipMode.Value = programItem.SkipMode.ToString(); skip.Attributes.Append(skipMode); XmlAttribute skipComparator = document.CreateAttribute("comparator"); skipComparator.Value = programItem.SkipComparator.ToString(); skip.Attributes.Append(skipComparator); XmlAttribute skipValue = document.CreateAttribute("value"); skipValue.Value = programItem.SkipValue.ToString(); skip.Attributes.Append(skipValue); item.AppendChild(skip); } items.AppendChild(item); } document.Save(path); }
// creates Xml file with program items, loop info, local/remote info and period settings public void SaveItems(BindingList <ProgramItem> programItems, bool loopIsEnabled, int loopCount, bool isRemote, double logPeriod, TimeUnits logTimeUnit) { XmlNode declaration = document.CreateXmlDeclaration("1.0", "UTF-8", null); document.AppendChild(declaration); XmlNode comment = document.CreateComment("MightyWatt experiment settings"); document.AppendChild(comment); XmlElement main = document.CreateElement("settings"); document.AppendChild(main); if (loopIsEnabled == true) { // loop settings XmlNode loop = document.CreateElement("loop"); // duration XmlAttribute count = document.CreateAttribute("count"); if (loopCount < 0) { count.Value = "infinite"; } else { count.Value = loopCount.ToString(); } loop.Attributes.Append(count); main.AppendChild(loop); } // local/remote settings XmlNode remote = document.CreateElement("voltageSenseMode"); main.AppendChild(remote); // is remote XmlAttribute isrem = document.CreateAttribute("isRemote"); isrem.Value = isRemote.ToString(); remote.Attributes.Append(isrem); // logging settings XmlNode loggingPeriod = document.CreateElement("logging"); main.AppendChild(loggingPeriod); XmlAttribute periodValue = document.CreateAttribute("period"); timeConverter = new Converters.TimeConverter(); // convert seconds to representation in time-unit periodValue.Value = (string)(timeConverter.Convert(logPeriod, typeof(string), logTimeUnit, System.Globalization.CultureInfo.CurrentCulture)); loggingPeriod.Attributes.Append(periodValue); XmlAttribute periodTimeUnit = document.CreateAttribute("timeUnit"); periodTimeUnit.Value = logTimeUnit.ToString(); loggingPeriod.Attributes.Append(periodTimeUnit); // items XmlNode items = document.CreateElement("items"); main.AppendChild(items); foreach (ProgramItem programItem in programItems) { XmlNode item = document.CreateElement("item"); XmlAttribute programMode = document.CreateAttribute("programMode"); programMode.Value = programItem.ProgramMode.ToString(); item.Attributes.Append(programMode); // parameters XmlAttribute mode = document.CreateAttribute("mode"); mode.Value = programItem.Mode.ToString(); item.Attributes.Append(mode); switch (programItem.ProgramMode) { case ProgramModes.Constant: { XmlAttribute value = document.CreateAttribute("value"); if (programItem.Value == null) { value.Value = "previous"; } else { value.Value = programItem.Value.ToString(); } item.Attributes.Append(value); break; } case ProgramModes.Ramp: { XmlAttribute startingValue = document.CreateAttribute("startingValue"); if (programItem.StartingValue == null) { startingValue.Value = "previous"; } else { startingValue.Value = programItem.StartingValue.ToString(); } item.Attributes.Append(startingValue); XmlAttribute finalValue = document.CreateAttribute("finalValue"); finalValue.Value = programItem.FinalValue.ToString(); item.Attributes.Append(finalValue); break; } } XmlAttribute duration = document.CreateAttribute("duration"); duration.Value = programItem.DurationString; item.Attributes.Append(duration); XmlAttribute timeUnit = document.CreateAttribute("timeUnit"); timeUnit.Value = programItem.TimeUnit.ToString(); item.Attributes.Append(timeUnit); // skip attributes if (programItem.SkipEnabled) { XmlNode skip = document.CreateElement("skip"); XmlAttribute skipMode = document.CreateAttribute("mode"); skipMode.Value = programItem.SkipMode.ToString(); skip.Attributes.Append(skipMode); XmlAttribute skipComparator = document.CreateAttribute("comparator"); skipComparator.Value = programItem.SkipComparator.ToString(); skip.Attributes.Append(skipComparator); XmlAttribute skipValue = document.CreateAttribute("value"); skipValue.Value = programItem.SkipValue.ToString(); skip.Attributes.Append(skipValue); item.AppendChild(skip); } items.AppendChild(item); } document.Save(path); }