public Task <StreamDownloadResponse> GetWtmLocalDataAsync() { try { using (var db = new LiteDatabase(Constants.DataBase)) { var collection = db.GetCollection <HistoricalData>(Constants.LightDB_WtmCacheCollection); var cache = collection.FindAll().Last(); if (cache != null) { MemoryStream stream = NetHelper.SerializeToStream <HistoricalData>(cache); return(Task.FromResult(new StreamDownloadResponse { Stream = stream })); } return(Task.FromResult(new StreamDownloadResponse { Stream = null, Error = "Cache is empty." })); } } catch (Exception ex) { throw new FaultException(ex.Message); } }
public Task <StreamDownloadResponse> DownloadStream(StreamDownloadRequest request) { MemoryStream stream = NetHelper.SerializeToStream <Workers>(ViewModel.Instance.Workers); return(Task.FromResult(new StreamDownloadResponse { Stream = stream })); }
private async Task <int> UpdateWtmSettings(Computer pc, CancellationToken token, int errorCount, int iteration, int jobCount, DateTime wtmSettingsDate) { pc.SwitchStatus = Computer.OperationStatus.OperationInProgress; var address = ViewModel.BuildServerAddress(pc.Name, Constants.StreamServer); var channel = Service.NewStreamChannel(address, TimeSpan.FromSeconds(60)); MemoryStream stream = NetHelper.SerializeToStream <WtmSettingsObject>(ViewModel.Instance.WtmSettings); var uploadRequest = new StreamUploadRequest { Stream = stream }; try { if (ViewModel.Instance.WtmSettings.ProxyPassword != null && ViewModel.Instance.WtmSettings.ProxyPassword.Length != 0) { uploadRequest.ProxyPassword = ViewModel.Instance.WtmSettings.ProxyPassword.ToCharArray(); } var response = await channel.UpdateWtmSettings(uploadRequest).WithCancellation(token).ConfigureAwait(false); if (response.ResponseFlag) { pc.SwitchStatus = Computer.OperationStatus.Success; pc.WtmSettingsDate = response.Date; if (pc.WtmSettingsDate >= wtmSettingsDate) { pc.UpdateStatus = Computer.OperationStatus.NotNecessary; } } } catch (Exception ex) { errorCount++; pc.SwitchStatus = Computer.OperationStatus.Failure; Application.Current.Dispatcher.Invoke(() => { var p = new Paragraph(); p.Inlines.Add(new Run($"{pc.Name}: ").FontWeight(FontWeights.Bold).Color(Colors.Salmon)); p.Inlines.Add(new Run($"Failed to update.\r\n")); p.Inlines.Add(new Run(ex.Message + "\r\n")); NewParagraph = p; }); } finally { if (uploadRequest.ProxyPassword.Length != 0) { Array.Clear(uploadRequest.ProxyPassword, 0, uploadRequest.ProxyPassword.Length); } NetHelper.CloseChannel(channel); stream.Dispose(); ReportTitle = $"Progress: {iteration + 1} of {jobCount}."; } return(errorCount); }
public async Task <StreamDownloadResponse> GetPriceHistoryAsync(StreamDownloadRequest request) { try { var task = Task.Run(async() => { var vm = ViewModel.Instance; await vm.UpdatePriceHistory(); return(ViewModel.ReadHistoricalData(request.Period)); }); var history = await task; MemoryStream stream = NetHelper.SerializeToStream <List <HistoricalData> >(history); return(new StreamDownloadResponse { Stream = stream }); } catch (Exception ex) { throw new FaultException(ex.Message); } }