/// <summary> /// Saves an instance of <see cref="IDriverMomento" /> that represents drivers state asynchronously /// </summary> /// <param name="driverMomento"> /// An instance of <see cref="IDriverMomento" /> to save /// </param> /// <returns>An instance of System.Threading.Tasks.Task that represents current asynchronous operation</returns> public async Task SaveDriverMomentoAsync(IDriverMomento driverMomento) { this.ThrowIfDisposed(); await this.SaveContentAsync(driverMomento); await this._saveRootAsync(); }
private void SaveContent(IDriverMomento deviceMomento) { this.ThrowIfDisposed(); var type = deviceMomento.GetType(); var serializer = new DataContractSerializer(type, this.GetKnownTypesInternal()); using (var stream = new MemoryStream()) { serializer.WriteObject(stream, (object)deviceMomento); stream.Position = 0; XElement element; using (var reader = XmlReader.Create(stream)) { element = XElement.Load(reader); } var stateElement = this._root.Element(XELEMENT_NAME); if (stateElement == null) { this._root.Add(stateElement = new XElement(XELEMENT_NAME)); } stateElement.RemoveAttributes(); stateElement.RemoveAll(); // ReSharper disable AssignNullToNotNullAttribute stateElement.Add(new XAttribute("type", type.AssemblyQualifiedName)); // ReSharper restore AssignNullToNotNullAttribute stateElement.Add(element); } }
/// <summary> /// На основе id-ка достает и хранителя(Momento) создает новыё драйвер /// </summary> /// <param name="driverId"></param> /// <param name="driverMomento"></param> /// <returns></returns> private IConfigLogicalDriver RestoreDriverFromMomento(Guid driverId, IDriverMomento driverMomento) { if (driverMomento == null) { throw new ArgumentNullException("driverMomento"); } var driverType = driverMomento.State.DriverType; var driverFactory = _container.Resolve <ILogicalDriverFactory>(driverType); if (driverFactory == null) { throw new UnknownLogicalDeviceTypeException(driverType); } var logicalDriver = driverFactory.CreateConfigLogicalDriver(); if (logicalDriver == null) { throw new LogicalDriverCreationException(new LogicalDriverInformation()); } logicalDriver.SetMomento(driverMomento); ((IConfigLogicalDriver)logicalDriver).DriverId = driverId; return(logicalDriver); }
/// <summary> /// Sets a momento to current driver /// Сохраняет состояние драйвера /// </summary> /// <param name="momento"> /// An instance of <see cref="IDriverMomento" /> that represents current the state of current driver /// </param> public void SetMomento(IDriverMomento momento) { if (momento == null) { throw new ArgumentNullException("momento"); } var context = momento.State; if (context == null) { throw new ArgumentException(); } this._context = context; }
private async void InitializeDriver() { var driver = await _runtimeModeDriversService .GetDriverById(Guid.Parse(this.DeviceMomento.State.RelatedDriverId)); _applicationSettingsService.Load(); DriverMomento = driver.CreateMomento(); InitFormatters(); _tcpDeviceConnection = _applicationConnectionService.CreateTcpDeviceConnection( DriverMomento.State.GetTcpAddress(), DriverMomento.State.GetTcpPort(), _applicationSettingsService.QueryTimeoutPeriod * 1000); _tcpDeviceConnection.ConnectionRestoredAction += async() => { try { _isOnline = true; if (_isConnectionWasOnline) { _connectionLogger.ConnectionResroted(_logger); } _isConnectionWasOnline = true; } catch { } }; _tcpDeviceConnection.ConnectionLostAction += () => { if (_isOnline) { _isDeviceInitialized = false; _connectionLogger.ConnectionLost(_logger); _isOnline = false; } }; }
private Task SaveContentAsync(IDriverMomento deviceMomento) { return(Task.Factory.StartNew(() => SaveContent(deviceMomento), TaskCreationOptions.LongRunning)); }