Ejemplo n.º 1
0
        public async Task <int> InvokeAsync(InvocationContext context)
        {
            var console = context.Console.Out;

            var(client, schema) = await DeviceDetection.DetectDeviceAsync(console);

            if (client is null)
            {
                return(1);
            }

            using (client)
            {
                var channel   = context.ParseResult.ValueForOption <int>("channel");
                var keys      = context.ParseResult.ValueForOption <string>("keys");
                var targetKit = context.ParseResult.ValueForOption <int>("kit");
                if (targetKit < 1 || targetKit + 1 > schema.KitRoots.Count)
                {
                    console.WriteLine($"Kit {targetKit} is out of range for {schema.Identifier.Name} for this command.");
                    console.WriteLine("Note that one extra kit is required after the specified one.");
                    return(1);
                }

                // Detect the current kit
                // TODO: Stop assuming current kit is at address 0, although it is for TD-17, TD-50 and TD-27...
                var data = await client.RequestDataAsync(0, 1, new CancellationTokenSource(TimeSpan.FromSeconds(1)).Token);

                var currentKit = data[0] + 1;

                // Copy current kit to target kit and target kit + 1
                var kit = await KitUtilities.ReadKit(schema, client, currentKit, console);

                await KitUtilities.WriteKit(client, kit, targetKit, console);

                await KitUtilities.WriteKit(client, kit, targetKit + 1, console);

                SetCurrentKit(targetKit);
                var programChangeCommand = (byte)(0xc0 | (channel - 1));

                // Now listen for the foot switch...
                client.MessageReceived += (sender, message) =>
                {
                    if (message.Data.Length == 2 && message.Data[0] == programChangeCommand)
                    {
                        console.WriteLine("Turning the page...");
                        System.Windows.Forms.SendKeys.SendWait(keys);
                        SetCurrentKit(targetKit);
                    }
                };
                console.WriteLine("Listening for foot switch");
                await Task.Delay(TimeSpan.FromHours(1));
            }
            return(0);

            void SetCurrentKit(int newKitNumber) => client.SendData(0, new[] { (byte)(newKitNumber - 1) });
        }
Ejemplo n.º 2
0
        public async Task <int> InvokeAsync(InvocationContext context)
        {
            var console = context.Console.Out;
            var kit     = context.ParseResult.ValueForOption <int>("kit");
            var file    = context.ParseResult.ValueForOption <string>("file");

            var(client, schema) = await DeviceDetection.DetectDeviceAsync(console);

            if (client == null)
            {
                return(1);
            }

            using (client)
            {
                if (!schema.KitRoots.TryGetValue(kit, out _))
                {
                    console.WriteLine($"Kit {kit} out of range");
                    return(1);
                }
                ;

                // Allow up to 30 seconds in total, and 1 second per container.
                var overallToken = new CancellationTokenSource(TimeSpan.FromSeconds(30)).Token;
                try
                {
                    Stopwatch sw        = Stopwatch.StartNew();
                    var       kitToSave = await KitUtilities.ReadKit(schema, client, kit, console);

                    console.WriteLine($"Finished loading in {(int) sw.Elapsed.TotalSeconds} seconds");
                    using (var stream = File.Create(file))
                    {
                        kitToSave.Save(stream);
                    }
                    console.WriteLine($"Saved kit to {file}");
                }
                catch (OperationCanceledException)
                {
                    console.WriteLine("Data loading from device was cancelled");
                    return(1);
                }
                catch (Exception ex)
                {
                    console.WriteLine($"Error loading data from device: {ex}");
                    return(1);
                }
            }
            return(0);
        }