/// <summary> /// Initializes the settings file with default data /// </summary> /// <param name="stream">Settings file opened stream</param> /// <remarks>This method is used as WinAppStock initializer</remarks> private void InitializeSettingsFile(FileStream stream) { SettingsTab.ConnectionSettingsStruct connectionData = new SettingsTab.ConnectionSettingsStruct() { host = "127.0.0.1", port = 27960, period = 5, username = "******", password = "******" }; RVClient.StreamDataV1[] streamData = new RVClient.StreamDataV1[1]; streamData[0].streamName = "my_local_stream_name"; streamData[0].port = 554; streamData[0].proxiedName = "my_stream_name"; SerializedSettings settings = new SerializedSettings(); settings.connectionData = connectionData; settings.streamData = streamData; XmlSerializer serializer = new XmlSerializer(typeof(SerializedSettings)); serializer.Serialize(stream, settings); }
/// <summary> /// Load settings from a file /// </summary> /// <param name="view">Main Window reference</param> /// <param name="connectionData"></param> /// <param name="streamData"></param> /// <returns>Whether loading data succeeded and out parameters can be read.</returns> /// <remarks>If file is corrupted and loading fails this method tries to reinitialize the file.</remarks> public bool LoadSettings(MainWindow view, out SettingsTab.ConnectionSettingsStruct connectionData, out RVClient.StreamDataV1[] streamData) { FileStream stream = this.GetSettingsFileStream(view, false); bool defaultSettings = false; if (stream != null) { XmlSerializer serializer = new XmlSerializer(typeof(SerializedSettings)); SerializedSettings settings; try { settings = (SerializedSettings)serializer.Deserialize(stream); } catch { view.WriteLog("Error: Could not load previous settings"); view.WriteLog("Falling back to default settings"); defaultSettings = true; stream.SetLength(0); InitializeSettingsFile(stream); stream.Flush(); stream.Seek(0, SeekOrigin.Begin); try { settings = (SerializedSettings)serializer.Deserialize(stream); } catch (Exception e2) { // Something must be really screwed up now... view.WriteLog("Error: Could not reset to default settings"); view.WriteLog("Additional error info: " + e2.Message); stream.Close(); connectionData = new SettingsTab.ConnectionSettingsStruct(); streamData = null; return false; } } connectionData = settings.connectionData; streamData = settings.streamData; stream.Close(); if (!defaultSettings) { view.WriteLog("Previous settings loaded successfully"); } return true; } else { connectionData = new SettingsTab.ConnectionSettingsStruct(); streamData = null; return false; } }
/// <summary> /// Handles Start button action /// </summary> /// <param name="view">Main Window reference</param> /// <remarks>Starts a timer to connect to RV server periodically</remarks> public void Start(MainWindow view) { //only one timer per class allowed if (this.timer != null) { view.WriteLog("Error: Job is already in progress."); return; } view.Status = MainWindow.JobStatus.Working; this.connectionData = view.ConnectionData; this.streamData = view.StreamData; ConnectOnce(view, this.connectionData, this.streamData); view.WriteLog("Scheduling new connection in " + this.connectionData.period + " minutes."); this.timer = new System.Timers.Timer(this.connectionData.period * 1000 * 60); this.timer.Elapsed += delegate(Object source, ElapsedEventArgs e) { ConnectOnce(view, this.connectionData, this.streamData); view.WriteLog("Scheduling new connection in " + this.connectionData.period + " minutes."); }; this.timer.SynchronizingObject = view; this.timer.AutoReset = true; this.timer.Enabled = true; }