/// <summary> /// Serializes the <see cref="SmartEyeSetting"/> into the given file in a xml structure. /// </summary> /// <param name="settings">The <see cref="SmartEyeSetting"/> object to serialize.</param> /// <param name="filePath">Full file path to the xml settings file.</param> public void SerializeSettings(SmartEyeSetting settings, string filePath) { // Create an instance of the XmlSerializer class; // specify the type of object to serialize var serializer = new XmlSerializer(typeof(SmartEyeSetting)); // Serialize the SmartEyeSetting, and close the TextWriter. try { TextWriter writer = new StreamWriter(filePath, false); serializer.Serialize(writer, settings); writer.Close(); } catch (Exception ex) { string message = "Serialization of SmartEyeSettings failed with the following message: " + Environment.NewLine + ex.Message; if (this.smartEyeSettings.SilentMode) { ExceptionMethods.HandleExceptionSilent(ex); } else { ExceptionMethods.HandleException(ex); } } }
/////////////////////////////////////////////////////////////////////////////// // Construction and Initializing methods // /////////////////////////////////////////////////////////////////////////////// #region CONSTRUCTION /// <summary> /// Initializes a new instance of the <see cref="SmartEyeCalibrationRunner"/> class. /// </summary> /// <param name="client">The <see cref="SmartEyeClient"/></param> /// <param name="settings">The <see cref="SmartEyeSetting"/></param> public SmartEyeCalibrationRunner(SmartEyeClient client, SmartEyeSetting settings) { this.client = client; this.client.UdpSocket.PacketReceived += this.BaseClientOnPacketReceived; this.client.PropertyChanged += this.SmartEyeClientPropertyChanged; this.settings = settings; this.smartEyeCalibrationForm = new SmartEyeCalibrationForm(); this.smartEyeCalibrationForm.Load += this.SmartEyeCalibrationFormLoaded; this.smartEyeCalibrationForm.KeyDown += this.SmartEyeCalibrationFormKeyDown; this.initTimer = new Timer() { Interval = 50 }; this.initTimer.Tick += this.CheckStartCalibration; this.collectSamplesTimer = new Timer() { Interval = 50 }; this.collectSamplesTimer.Tick += this.CheckCollectSamples; this.HasShownMessage = false; }
/// <summary> /// Sets up calibration procedure and the tracking client /// and wires the events. Reads settings from file. /// </summary> protected override sealed void Initialize() { // Load Smart Eye tracker settings. if (File.Exists(this.SettingsFile)) { this.smartEyeSettings = this.DeserializeSettings(this.SettingsFile); } else { this.smartEyeSettings = new SmartEyeSetting(); this.SerializeSettings(this.smartEyeSettings, this.SettingsFile); } base.Initialize(); }
/////////////////////////////////////////////////////////////////////////////// // Inherited methods // /////////////////////////////////////////////////////////////////////////////// #region OVERRIDES /// <summary> /// An implementation of this method should show a hardware /// system specific dialog to change its settings like /// sampling rate or connection properties. It should also /// provide a xml serialization possibility of the settings, /// so that the user can store and backup system settings in /// a separate file. These settings should be implemented in /// a separate class and are stored in a special place of /// Ogama's directory structure. /// </summary> /// <remarks>Please have a look at the existing implementation /// of the Smart Eye system in the namespace SmartEye.</remarks> public override void ChangeSettings() { var dlg = new SmartEyeSettingsDialog { SmartEyeSettings = this.smartEyeSettings }; switch (dlg.ShowDialog()) { case DialogResult.OK: this.smartEyeSettings = dlg.SmartEyeSettings; dlg.UpdateSmartEyeSettings(); this.SerializeSettings(this.smartEyeSettings, this.SettingsFile); break; } }
/// <summary> /// Deserializes the <see cref="SmartEyeSetting"/> from the given xml file. /// </summary> /// <param name="filePath">Full file path to the xml settings file.</param> /// <returns>A <see cref="SmartEyeSetting"/> object.</returns> private SmartEyeSetting DeserializeSettings(string filePath) { var settings = new SmartEyeSetting(); // Create an instance of the XmlSerializer class; // specify the type of object to be deserialized var serializer = new XmlSerializer(typeof(SmartEyeSetting)); // * If the XML document has been altered with unknown // nodes or attributes, handle them with the // UnknownNode and UnknownAttribute events.*/ serializer.UnknownNode += this.SerializerUnknownNode; serializer.UnknownAttribute += this.SerializerUnknownAttribute; try { // A FileStream is needed to read the XML document. var fs = new FileStream(filePath, FileMode.Open); // Use the Deserialize method to restore the object's state with // data from the XML document. settings = (SmartEyeSetting)serializer.Deserialize(fs); fs.Close(); } catch (Exception ex) { if (this.smartEyeSettings.SilentMode) { ExceptionMethods.HandleExceptionSilent(ex); } else { ExceptionMethods.HandleException(ex); } } this.ValidateAddresses(settings); return(settings); }
/////////////////////////////////////////////////////////////////////////////// // Construction and Initializing methods // /////////////////////////////////////////////////////////////////////////////// #region CONSTRUCTION /// <summary> /// Initializes a new instance of the <see cref="SmartEyeClient"/> class. /// </summary> /// <param name="setting">The settings</param> public SmartEyeClient(SmartEyeSetting setting) { this.smartEyeSettings = setting; this.IsClosingDown = false; this.RpcIsConnected = false; this.dataIds = new List <TrackerDataId>(); this.KillRunningEyeTrackerProcess(); Process smartEyeTrackingProcess; var smartEyeVersion = Registry.GetValue(@"HKEY_CURRENT_USER\Software\Smart Eye AB\Shared", "EyeTrackerCoreVersion", null); if (smartEyeVersion != null) { var smartEyeTrackingPath = Registry.GetValue( string.Format(@"HKEY_CURRENT_USER\Software\Smart Eye AB\Eye tracker core {0}\DefaultPaths", smartEyeVersion.ToString()), "ProgramDirectory", null); if (smartEyeTrackingPath != null) { smartEyeTrackingProcess = new Process { StartInfo = { WorkingDirectory = smartEyeTrackingPath.ToString(), FileName = smartEyeTrackingPath + "\\eye_tracker_core.exe", UseShellExecute = false, RedirectStandardError = true } }; } else { ConnectionFailedDialog dlg = new ConnectionFailedDialog(); dlg.ErrorMessage = "Cannot start Smart Eye Tracker Core Process, registry parameters not correct."; dlg.ShowDialog(); return; } } else { ConnectionFailedDialog dlg = new ConnectionFailedDialog(); dlg.ErrorMessage = "Cannot start Smart Eye Tracker Core Process, registry parameters not correct."; dlg.ShowDialog(); return; } try { if (!smartEyeTrackingProcess.Start()) { ConnectionFailedDialog dlg = new ConnectionFailedDialog(); dlg.ErrorMessage = "Cannot start Smart Eye Tracker Core Process." + Environment.NewLine + Environment.NewLine + "If this error is recurring, please make sure the hardware is connected and set up correctly, and try to reconnect."; dlg.ShowDialog(); return; } } catch (Exception ex) { ConnectionFailedDialog dlg = new ConnectionFailedDialog(); dlg.ErrorMessage = "Cannot start Smart Eye Tracker Core Process, failed with the following message: " + Environment.NewLine + ex.Message + Environment.NewLine + Environment.NewLine + "If this error is recurring, please make sure the hardware is connected and set up correctly, and try to reconnect."; dlg.ShowDialog(); ExceptionMethods.HandleExceptionSilent(ex); return; } this.CreateRPC(); this.CreateUDP(this.smartEyeSettings.SmartEyeServerAddress, this.smartEyeSettings.OgamaPort); }
/// <summary> /// Validate IP and port addresses of the deserialized settings. /// </summary> /// <param name="settings">Deserialized settings.</param> /// <returns>True if all addresses are valid</returns> private bool ValidateAddresses(SmartEyeSetting settings) { return this.ValidateIpAddress(settings.SmartEyeServerAddress) && this.ValidatePortAddress(settings.SmartEyeRPCPort) && this.ValidatePortAddress(settings.OgamaPort); }
/// <summary> /// Deserializes the <see cref="SmartEyeSetting"/> from the given xml file. /// </summary> /// <param name="filePath">Full file path to the xml settings file.</param> /// <returns>A <see cref="SmartEyeSetting"/> object.</returns> private SmartEyeSetting DeserializeSettings(string filePath) { var settings = new SmartEyeSetting(); // Create an instance of the XmlSerializer class; // specify the type of object to be deserialized var serializer = new XmlSerializer(typeof(SmartEyeSetting)); // * If the XML document has been altered with unknown // nodes or attributes, handle them with the // UnknownNode and UnknownAttribute events.*/ serializer.UnknownNode += this.SerializerUnknownNode; serializer.UnknownAttribute += this.SerializerUnknownAttribute; try { // A FileStream is needed to read the XML document. var fs = new FileStream(filePath, FileMode.Open); // Use the Deserialize method to restore the object's state with // data from the XML document. settings = (SmartEyeSetting)serializer.Deserialize(fs); fs.Close(); } catch (Exception ex) { if (this.smartEyeSettings.SilentMode) { ExceptionMethods.HandleExceptionSilent(ex); } else { ExceptionMethods.HandleException(ex); } } this.ValidateAddresses(settings); return settings; }
/////////////////////////////////////////////////////////////////////////////// // Construction and Initializing methods // /////////////////////////////////////////////////////////////////////////////// #region CONSTRUCTION /// <summary> /// Initializes a new instance of the <see cref="SmartEyeClient"/> class. /// </summary> /// <param name="setting">The settings</param> public SmartEyeClient(SmartEyeSetting setting) { this.smartEyeSettings = setting; this.IsClosingDown = false; this.RpcIsConnected = false; this.dataIds = new List<TrackerDataId>(); this.KillRunningEyeTrackerProcess(); Process smartEyeTrackingProcess; var smartEyeVersion = Registry.GetValue(@"HKEY_CURRENT_USER\Software\Smart Eye AB\Shared", "EyeTrackerCoreVersion", null); if (smartEyeVersion != null) { var smartEyeTrackingPath = Registry.GetValue( string.Format(@"HKEY_CURRENT_USER\Software\Smart Eye AB\Eye tracker core {0}\DefaultPaths", smartEyeVersion.ToString()), "ProgramDirectory", null); if (smartEyeTrackingPath != null) { smartEyeTrackingProcess = new Process { StartInfo = { WorkingDirectory = smartEyeTrackingPath.ToString(), FileName = smartEyeTrackingPath + "\\eye_tracker_core.exe", UseShellExecute = false, RedirectStandardError = true } }; } else { ConnectionFailedDialog dlg = new ConnectionFailedDialog(); dlg.ErrorMessage = "Cannot start Smart Eye Tracker Core Process, registry parameters not correct."; dlg.ShowDialog(); return; } } else { ConnectionFailedDialog dlg = new ConnectionFailedDialog(); dlg.ErrorMessage = "Cannot start Smart Eye Tracker Core Process, registry parameters not correct."; dlg.ShowDialog(); return; } try { if (!smartEyeTrackingProcess.Start()) { ConnectionFailedDialog dlg = new ConnectionFailedDialog(); dlg.ErrorMessage = "Cannot start Smart Eye Tracker Core Process." + Environment.NewLine + Environment.NewLine + "If this error is recurring, please make sure the hardware is connected and set up correctly, and try to reconnect."; dlg.ShowDialog(); return; } } catch (Exception ex) { ConnectionFailedDialog dlg = new ConnectionFailedDialog(); dlg.ErrorMessage = "Cannot start Smart Eye Tracker Core Process, failed with the following message: " + Environment.NewLine + ex.Message + Environment.NewLine + Environment.NewLine + "If this error is recurring, please make sure the hardware is connected and set up correctly, and try to reconnect."; dlg.ShowDialog(); ExceptionMethods.HandleExceptionSilent(ex); return; } this.CreateRPC(); this.CreateUDP(this.smartEyeSettings.SmartEyeServerAddress, this.smartEyeSettings.OgamaPort); }
/// <summary> /// Updates the forms UI with the new settings from the /// <see cref="SmartEyeSetting"/> member. /// </summary> /// <param name="setting">A <see cref="SmartEyeSetting"/> with the settings /// to apply.</param> private void SetupUIWithNewSettings(SmartEyeSetting setting) { switch (this.SmartEyeSettings.CalibPointSize) { case 44: rdbSmartEyeSizeLarge.Checked = true; break; case 22: rdbSmartEyeSizeMedium.Checked = true; break; case 11: rdbSmartEyeSizeSmall.Checked = true; break; default: rdbSmartEyeSizeMedium.Checked = true; break; } switch (this.SmartEyeSettings.CalibPointSpeed) { case 3: rdbSmartEyeSpeedFast.Checked = true; break; case 2: rdbSmartEyeSpeedMedium.Checked = true; break; case 1: rdbSmartEyeSpeedSlow.Checked = true; break; default: rdbSmartEyeSpeedMedium.Checked = true; break; } switch (this.SmartEyeSettings.NumCalibPoints) { case 3: rdbSmartEye3PtsCalib.Checked = true; break; case 5: rdbSmartEye5PtsCalib.Checked = true; break; case 9: rdbSmartEye9PtsCalib.Checked = true; break; default: rdbSmartEye5PtsCalib.Checked = true; break; } txbSmartEyeAddress.Text = setting.SmartEyeServerAddress; txbSmartEyePort.Text = setting.SmartEyeRPCPort.ToString(); txbOGAMAPort.Text = setting.OgamaPort.ToString(); clbSmartEyeBackColor.CurrentColor = this.SmartEyeSettings.CalibBackgroundColor; clbSmartEyePointColor.CurrentColor = this.SmartEyeSettings.CalibPointColor; chbRandomizePointOrder.Checked = this.SmartEyeSettings.RandomizeCalibPointOrder; }
/// <summary> /// Validate IP and port addresses of the deserialized settings. /// </summary> /// <param name="settings">Deserialized settings.</param> /// <returns>True if all addresses are valid</returns> private bool ValidateAddresses(SmartEyeSetting settings) { return(this.ValidateIpAddress(settings.SmartEyeServerAddress) && this.ValidatePortAddress(settings.SmartEyeRPCPort) && this.ValidatePortAddress(settings.OgamaPort)); }