Beispiel #1
0
        /// <summary>
        /// Writes a kit to a device.
        /// </summary>
        internal static async Task WriteKit(RolandMidiClient client, Kit kit, int kitNumber, IStandardStreamWriter console)
        {
            var targetKitRoot = kit.Schema.KitRoots[kitNumber];
            var clonedData    = kit.KitRoot.Context.CloneData(kit.Data, targetKitRoot.Context.Address);
            var segments      = clonedData.GetSegments();

            console.WriteLine($"Writing {segments.Count} containers to device {kit.Schema.Identifier.Name} kit {kitNumber}");

            foreach (var segment in segments)
            {
                client.SendData(segment.Start.Value, segment.CopyData());
                await Task.Delay(40);
            }
        }
        internal async void CopySegmentsToDeviceAsync(List <DataSegment> segments)
        {
            // Slightly ugly to have this here rather than in XAML, but it's at least simple.
            Title = "Copying data to module";
            Stopwatch sw      = Stopwatch.StartNew();
            int       written = 0;

            try
            {
                logger.LogInformation($"Writing {segments.Count} segments to the device.");
                progress.Maximum = segments.Count;
                foreach (var segment in segments)
                {
                    cancellationTokenSource.Token.ThrowIfCancellationRequested();
                    client.SendData(segment.Start.Value, segment.CopyData());
                    await Task.Delay(40);

                    written++;
                    progress.Value = written;
                }
                logger.LogInformation($"Finished writing segments to the device {(int) sw.Elapsed.TotalSeconds} seconds.");
                DialogResult = true;
            }
            catch (OperationCanceledException)
            {
                logger.LogWarning($"Data copying to device was cancelled. Warning: module may now have inconsistent data.");
                DialogResult = false;
            }
            catch (Exception e)
            {
                logger.LogError("Failed while writing data to the device.");
                logger.LogInformation($"Segments successfully written: {written}");
                logger.LogError($"Error: {e}");
                DialogResult = false;
            }
        }
 // TODO: Put these somewhere common.
 private async Task SetCurrentKit(int newKitNumber)
 {
     device.SendData(0, new[] { (byte)(newKitNumber - 1) });
     await Task.Delay(40);
 }