private void toolStripButton1_Click(object sender, EventArgs e) { string mac = "00:00:00:00:00:00"; if (toolStripComboBox_NedDevices.SelectedIndex <= _netDevices.Count) { mac = _netDevices[toolStripComboBox_NedDevices.SelectedIndex].GetMacAddress().ToString(); } GooseControl gc = new GooseControl("Goose " + gooseItems++.ToString() + ":", mac, _netDevices[toolStripComboBox_NedDevices.SelectedIndex]); gc.Dock = DockStyle.Top; if (toolStripButton_Start.Enabled == true) { gc.StopAndLockSending(); } panel1.Controls.Add(gc); panel1.Controls.SetChildIndex(gc, 0); }
public void Import(ref int gooseItems, Control.ControlCollection cc, PacketDevice netDev) { using (OpenFileDialog importOpenFileDialog = new OpenFileDialog()) { importOpenFileDialog.Title = "Select XML File"; importOpenFileDialog.Filter = "Xml File(*.xml)|*.xml"; if (DialogResult.OK == importOpenFileDialog.ShowDialog()) { XDocument xmlDoc = XDocument.Load(importOpenFileDialog.FileName); XElement gooses = xmlDoc.Element("Gooses"); if (gooses != null && xmlDoc.Elements("Gooses").Count() == 1) { if (gooses.Elements().Count() > 0) { foreach (XElement goose in gooses.Elements()) { GooseControl gc = new GooseControl("Goose " + gooseItems++.ToString() + ":", "00:00:00:00:00:00", netDev); XElement parameters = goose.Element("Parameters"); if (parameters != null && goose.Elements("Parameters").Count() == 1) { if (parameters.Elements().Count() == 17) { foreach (XElement parameter in parameters.Elements()) { if (parameter.Attribute("Value") != null) { gc.gooseParameters[parameter.Name.ToString()] = parameter.Attribute("Value").Value.ToString(); } else { MessageBox.Show("Invalid XML file !", "Import from Xml", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } gc.updateComponents(); } else { MessageBox.Show("Invalid XML file !", "Import from Xml", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } XElement dataset = goose.Element("DataSet"); if (dataset != null && goose.Elements("DataSet").Count() == 1) { if (dataset.Elements("Data").Count() > 0) { foreach (XElement data in dataset.Elements("Data")) { recursiveCreateDataList(gc.dataList, data); } } } else { MessageBox.Show("Invalid XML file !", "Import from Xml", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } XElement seqdata = goose.Element("SeqData"); if (seqdata != null && goose.Elements("SeqData").Count() == 1) { if (seqdata.Elements("Data").Count() > 0) { NodeGVL gvl = new NodeGVL("Data"); GooseDataEdit gde = new GooseDataEdit(); int i = 0; foreach (GOOSE_ASN1_Model.Data dataListCn in gc.dataList) { i = gde.recursiveReadData(null, dataListCn, gvl, i, DateTime.Now); } gc.seqData.Clear(); foreach (XElement seqd in seqdata.Elements("Data")) { if (seqd.Attribute("Name") != null) { if (gvl.GetChildNodes().Length > 0) { foreach (NodeGData ngdcn in gvl.GetChildNodes()) { NodeGData fngd; if ((fngd = recursiveFindNodeGData(ngdcn, seqd.Attribute("Name").Value)) != null) { if (seqd.Attribute("Duration") != null && seqd.Attribute("Value") != null) { gc.seqData.Add(new SeqData(fngd, seqd.Attribute("Value").Value, Convert.ToInt32(seqd.Attribute("Duration").Value))); } else { MessageBox.Show("Invalid XML file !", "Import from Xml", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } break; } } } } else { MessageBox.Show("Invalid XML file !", "Import from Xml", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } } else { MessageBox.Show("Invalid XML file !", "Import from Xml", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } } else { MessageBox.Show("Invalid XML file !", "Import from Xml", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } gc.Dock = DockStyle.Top; cc.Add(gc); cc.SetChildIndex(gc, 0); } } else { MessageBox.Show("Invalid XML file !", "Import from Xml", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } else { MessageBox.Show("Invalid XML file !", "Import from Xml", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } } }
public void Export(Control.ControlCollection cc) { try { XDocument xmlDoc = new XDocument(); if (cc.Count > 0) { XElement gooses = new XElement("Gooses"); xmlDoc.Add(gooses); for (int i = 0; i < cc.Count; i++) { GooseControl gc = cc[cc.Count - i - 1] as GooseControl; if ((gc.gooseParameters.Count == 17) && (gc.dataList.Count > 0)) { XElement goose = new XElement(gc.Name.Replace(" ", "_").Replace(":", "")); gooses.Add(goose); XElement parameters = new XElement("Parameters"); goose.Add(parameters); foreach (DictionaryEntry param in gc.gooseParameters) { parameters.Add(new XElement(param.Key.ToString(), new XAttribute("Value", param.Value.ToString()))); } XElement dataset = new XElement("DataSet"); goose.Add(dataset); foreach (Data d in gc.dataList) { recursiveReadData(d, dataset); } if (gc.seqData.Count > 0) { XElement seqdata = new XElement("SeqData"); goose.Add(seqdata); foreach (SeqData seqd in gc.seqData) { seqdata.Add(new XElement("Data", new XAttribute("Name", seqd.refdata.CommAddress.Domain.ToString() + ((seqd.refdata.CommAddress.Variable.ToString() != "") ? ("/" + seqd.refdata.CommAddress.Variable.ToString().Replace("$", "/")) : "")), new XAttribute("Value", seqd.data), new XAttribute("Duration", seqd.duration.ToString()))); } } } else { MessageBox.Show("Goose not exported. No data to export !", "Export to Xml", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } } using (SaveFileDialog exportSaveFileDialog = new SaveFileDialog()) { exportSaveFileDialog.Title = "Select XML File"; exportSaveFileDialog.Filter = "Xml File(*.xml)|*.xml"; if (DialogResult.OK == exportSaveFileDialog.ShowDialog()) { string fullFileName = exportSaveFileDialog.FileName; xmlDoc.Save(fullFileName); MessageBox.Show("Gosses exported successfully", "Export to Xml", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } else { MessageBox.Show("No Gooses to Export !", "Export to Xml", MessageBoxButtons.OK, MessageBoxIcon.Information); } } catch (Exception ex) { Logger.getLogger().LogError("Goose export Exception: " + ex.Message); } }