/// <summary> /// Deserializes the <see cref="MirametrixSetting"/> from the given xml file. /// </summary> /// <param name="filePath">Full file path to the xml settings file.</param> /// <returns>A <see cref="MirametrixSetting"/> object.</returns> private MirametrixSetting DeserializeSettings(string filePath) { MirametrixSetting settings = new MirametrixSetting(); // Create an instance of the XmlSerializer class; // specify the type of object to be deserialized XmlSerializer serializer = new XmlSerializer(typeof(MirametrixSetting)); // If the XML document has been altered with unknown // nodes or attributes, handle them with the // UnknownNode and UnknownAttribute events. serializer.UnknownNode += new XmlNodeEventHandler(this.SerializerUnknownNode); serializer.UnknownAttribute += new XmlAttributeEventHandler(this.SerializerUnknownAttribute); try { // A FileStream is needed to read the XML document. FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read); // Use the Deserialize method to restore the object's state with // data from the XML document. settings = (MirametrixSetting)serializer.Deserialize(fs); fs.Close(); } catch (Exception ex) { InformationDialog.Show( "Error occured", "Deserialization ofMirametrixSettings failed with the following message: " + Environment.NewLine + ex.Message, false, MessageBoxIcon.Error); } return(settings); }
/// <summary> /// Serializes the <see cref="MirametrixSetting"/> into the given file in a xml structure. /// </summary> /// <param name="settings">The <see cref="MirametrixSetting"/> object to serialize.</param> /// <param name="filePath">Full file path to the xml settings file.</param> private void SerializeSettings(MirametrixSetting settings, string filePath) { // Create an instance of the XmlSerializer class; // specify the type of object to serialize XmlSerializer serializer = new XmlSerializer(typeof(MirametrixSetting)); // Serialize the MirametrixSetting, and close the TextWriter. try { TextWriter writer = new StreamWriter(filePath, false); serializer.Serialize(writer, settings); writer.Close(); } catch (Exception ex) { InformationDialog.Show( "Error occured", "Serialization of MirametrixSettings failed with the following message: " + Environment.NewLine + ex.Message, false, MessageBoxIcon.Error); } }
/// <summary> /// Sets up calibration procedure and the tracking client /// and wires the events. Reads settings from file. /// </summary> protected override void Initialize() { // Load Mirametrix tracker settings. if (File.Exists(this.SettingsFile)) { this.memSettings = this.DeserializeSettings(this.SettingsFile); } else { this.memSettings = new MirametrixSetting(); this.SerializeSettings(this.memSettings, this.SettingsFile); } // We just need one connection for now. Maybe for subsequent development, we will be able to connect to many trackers this.memNetworkManager = new ClientNetworkManager(this.memSettings.ServerAddress, this.memSettings.ServerPort, out this.memConnectionsIds); this.memNetworkManager.MessageReceived += this.ProcessReceivedMessage; this.memIsCalibrating = false; this.memIsRecording = false; //this.memXmlDocument = new XmlDocument(); //this.memTimeOfRecordingStart = new Stopwatch(); base.Initialize(); }
/// <summary> /// Raises MirametrixSettingDialog to change the settings /// for this interface. /// </summary> public override void ChangeSettings() { var dlg = new MirametrixSettingsDialog { MirametrixSetting = this.memSettings }; string prevAdd = this.memSettings.ServerAddress; int prevPort = this.memSettings.ServerPort; if (dlg.ShowDialog() == DialogResult.OK) { // If settings has changed, and we where connected, we need to disconnect if (!prevAdd.Equals(this.memSettings.ServerAddress) || !prevPort.Equals(this.memSettings.ServerPort)) { this.CleanUp(); this.memNetworkManager.ChangePort(this.memConnectionsIds[0], dlg.MirametrixSetting.ServerPort); this.memNetworkManager.ChangeAddress(this.memConnectionsIds[0], dlg.MirametrixSetting.ServerAddress); } this.memSettings = dlg.MirametrixSetting; this.SerializeSettings(this.Settings, this.SettingsFile); } }
/// <summary> /// Deserializes the <see cref="MirametrixSetting"/> from the given xml file. /// </summary> /// <param name="filePath">Full file path to the xml settings file.</param> /// <returns>A <see cref="MirametrixSetting"/> object.</returns> private MirametrixSetting DeserializeSettings(string filePath) { MirametrixSetting settings = new MirametrixSetting(); // Create an instance of the XmlSerializer class; // specify the type of object to be deserialized XmlSerializer serializer = new XmlSerializer(typeof(MirametrixSetting)); // If the XML document has been altered with unknown // nodes or attributes, handle them with the // UnknownNode and UnknownAttribute events. serializer.UnknownNode += new XmlNodeEventHandler(this.SerializerUnknownNode); serializer.UnknownAttribute += new XmlAttributeEventHandler(this.SerializerUnknownAttribute); try { // A FileStream is needed to read the XML document. FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read); // Use the Deserialize method to restore the object's state with // data from the XML document. settings = (MirametrixSetting)serializer.Deserialize(fs); fs.Close(); } catch (Exception ex) { InformationDialog.Show( "Error occured", "Deserialization ofMirametrixSettings failed with the following message: " + Environment.NewLine + ex.Message, false, MessageBoxIcon.Error); } return settings; }