private async Task <IRuntimeDeviceViewModel> RestoreDeviceFromMomentoAsync(Guid deviceId, IDeviceMomento deviceMomento) { if (deviceMomento == null) { throw new ArgumentNullException("deviceMomento"); } var deviceType = deviceMomento.State.DeviceType; var deviceFactory = _container.Resolve <ILogicalDeviceViewModelFactory>(deviceType); if (deviceFactory == null) { throw new UnknownLogicalDeviceTypeException(deviceType); } var runtimeDeviceViewModel = deviceFactory.CreateRuntimeLogicalDeviceViewModel(deviceMomento, deviceId); if (runtimeDeviceViewModel == null) { throw new LogicalDeviceException(); } return(runtimeDeviceViewModel); }
//—охран¤ет состоние(моменто) устройства в xml private void SaveMomentoInternal(IDeviceMomento 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(STATE_ELEMENT_NAME); if (stateElement == null) { this._root.Add(stateElement = new XElement(STATE_ELEMENT_NAME)); } stateElement.RemoveAttributes(); stateElement.RemoveAll(); // ReSharper disable AssignNullToNotNullAttribute stateElement.Add(new XAttribute("type", type.AssemblyQualifiedName)); // ReSharper restore AssignNullToNotNullAttribute stateElement.Add(element); } }
/// <summary> /// Saves deviceViewModel momento asynchronously /// ћетод сохран¤ет дынные (о) устройство в xml /// </summary> /// <param name="deviceMomento"> /// An instance of <see cref="IDeviceMomento" /> that represents deviceViewModel momento /// </param> /// <returns>An instance of System.Threading.Tasks.Task that represents current synchronous operation</returns> public async void SaveDeviceMomentoAsync(IDeviceMomento deviceMomento) { this.ThrowIfDisposed(); await Task.Factory.StartNew(() => this.SaveMomentoInternal(deviceMomento), TaskCreationOptions.LongRunning); await this._saveRootAsync(); }
/// <summary> /// Создает устройство на основе данных из xml по Id-ку и инициализирует его состояние данных /// </summary> /// <param name="deviceId">ID-шник устройства</param> /// <param name="deviceMomento">Состояние(данные) устройство (Можешь почитать про шаблон Momento)</param> /// <returns></returns> private async Task <IConfigLogicalDevice> RestoreDeviceFromMomentoAsync(Guid deviceId, IDeviceMomento deviceMomento) { if (deviceMomento == null) { throw new ArgumentNullException("deviceMomento"); } var deviceType = deviceMomento.State.DeviceType; var deviceFactory = _container.Resolve <ILogicalDeviceViewModelFactory>(deviceType); if (deviceFactory == null) { throw new UnknownLogicalDeviceTypeException(deviceType); } var logicalDevice = deviceFactory.CreateConfigLogicalDevice(); if (logicalDevice == null) { throw new LogicalDeviceException(); } await logicalDevice.SetMomentoAsync(deviceMomento); ((IConfigLogicalDevice)logicalDevice).DeviceId = deviceId; return(logicalDevice); }
/// <summary> /// Creates an instance of <see cref="IRuntimeDeviceViewModel" /> /// </summary> /// <param name="deviceMomento"></param> /// <returns> /// Created instance of <see cref="IRuntimeDeviceViewModel" /> /// </returns> public IRuntimeDeviceViewModel CreateRuntimeLogicalDeviceViewModel(IDeviceMomento deviceMomento, Guid id) { RuntimeDeviceViewModel runtimeDeviceViewModel = this._container.Resolve <RuntimeDeviceViewModel>(); runtimeDeviceViewModel.Model = _runo3DeviceFactory.CreateLogicalDevice(deviceMomento); runtimeDeviceViewModel.Model.DeviceId = id; return(runtimeDeviceViewModel); }
public ILogicalDevice CreateLogicalDevice(IDeviceMomento deviceMomento) { IRuno3Device runo3Device = _container.Resolve <IRuno3Device>(); runo3Device.DeviceMomento = deviceMomento; if (deviceMomento.State.AnalogMeterType != DeviceStringKeys.DeviceAnalogMetersTagKeys.NO) { runo3Device.AnalogMeter = _analogMeterFactory.CreateAnalogMeter(deviceMomento.State.AnalogMeterType); } return(runo3Device); }
/// <summary> /// Sets an instance of <see cref="IDeviceMomento" /> that represents current deviceViewModel state momento /// Сохраняет состояние устройства /// </summary> /// <param name="momento"> /// An instance of <see cref="IDeviceMomento" /> to restore current deviceViewModel state from /// </param> private void SetMomento(IDeviceMomento momento) { if (momento == null) { throw new ArgumentNullException("momento"); } var context = momento.State; if (context == null) { throw new ArgumentException(); } this._context = context; }
/// <summary> /// Sets an instance of <see cref="IDeviceMomento" /> that represents current deviceViewModel state momento asynchronously /// Сохраняет состояние устройства /// </summary> /// <param name="momento"> /// An instance of <see cref="IDeviceMomento" /> to restore current deviceViewModel state from /// </param> /// <returns>An instance of System.Threading.Tasks.Task that represents cyrrent asynchronous operation</returns> public Task SetMomentoAsync(IDeviceMomento momento) { return(Task.Factory.StartNew(() => this.SetMomento(momento))); }