Ejemplo n.º 1
0
        /// <summary>
        /// Routes the message specified within the event arguments, invoking any associated delegates where possible.
        /// </summary>
        /// <param name="client">The Stream Deck client.</param>
        /// <param name="e">The <see cref="WebSocketMessageEventArgs" /> instance containing the event data.</param>
        public Task RouteAsync(StreamDeckClient client, WebSocketMessageEventArgs e)
        {
            // determine if there is an event specified, and a delegate handler
            var jArgs = JObject.Parse(e.Message);

            if (!jArgs.TryGetString(nameof(StreamDeckEventArgs.Event), out var @event) ||
                !EventMethodCache.Value.TryGetValue(@event, out var @delegate))
            {
                this.Logger?.LogWarning("Unrecognised event received from Elgato Stream Deck: {0}", @event);
                return(Task.CompletedTask);
            }

            // determine the owner of the delegate, based on the event args
            var owner = this.TryGetActionInfo(jArgs, out var actionInfo)
                ? this.GetActionOrClient(jArgs, actionInfo, client)
                : client;

            try
            {
                return((Task)@delegate.Invoke(owner, new[] { jArgs.ToObject(@delegate.GetParameters()[0].ParameterType) }));
            }
            catch (Exception ex)
            {
                throw new ActionInvokeException(actionInfo.context, ex);
            }
        }
Ejemplo n.º 2
0
        private async Task OnExecuteAsync()
        {
            var cancellationTokenSource = new CancellationTokenSource();
            var streamDeck = new StreamDeckClient(Port, PluginUuid, RegisterEvent, Info);

            streamDeck.KeyDownEventAsync += UpdateCounter;
            await streamDeck.RunAsync(cancellationTokenSource.Token);
        }
        internal static async Task <(StreamDeckSoftwareEmulator emulator, StreamDeckClient client)> ConnectAsync(int port, string pluginUUID)
        {
            var emulator = new StreamDeckSoftwareEmulator(port, pluginUUID);

            string[] args = emulator.Start();
            var      sut  = new StreamDeckClient(Assembly.GetExecutingAssembly(), args);

            sut.RunAsync();
            await emulator.AwaitConnectionAsync();

            return(emulator, sut);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Defines the entry point of the application.
        /// </summary>
        /// <param name="args">The arguments.</param>
        public static void Main(string[] args)
        {
#if DEBUG
            Debugger.Launch();
#endif

            using (var client = new StreamDeckClient(args))
            {
                // register our custom action, and start the client
                client.RegisterAction("com.sharpdeck.testplugin.counter", () => new CounterAction(500));
                client.Start();
            }
        }
Ejemplo n.º 5
0
 public static async Task Main(string[] args)
 {
     var client = new StreamDeckClient(args);
     await client.RunAsync();
 }
Ejemplo n.º 6
0
        /// <summary>
        /// Processes the parameters passed by StreamDeck and starts the registration process.
        /// </summary>
        /// <param name="args">The arguments as passed to the main() method.</param>
        public void Run(string[] args)
        {
            var portString    = GetArgument(args, StreamDeckConsts.kESDSDKPortParameter);
            var pluginUUID    = GetArgument(args, StreamDeckConsts.kESDSDKPluginUUIDParameter);
            var registerEvent = GetArgument(args, StreamDeckConsts.kESDSDKRegisterEventParameter);
            var infoString    = GetArgument(args, StreamDeckConsts.kESDSDKInfoParameter);

            if (portString == null)
            {
                throw new StreamDeckMissingParameter($"{StreamDeckConsts.kESDSDKPortParameter} argument missing");
            }
            if (pluginUUID == null)
            {
                throw new StreamDeckMissingParameter($"{StreamDeckConsts.kESDSDKPluginUUIDParameter} argument missing");
            }
            if (registerEvent == null)
            {
                throw new StreamDeckMissingParameter($"{StreamDeckConsts.kESDSDKRegisterEventParameter} argument missing");
            }
            if (infoString == null)
            {
                throw new StreamDeckMissingParameter($"{StreamDeckConsts.kESDSDKInfoParameter} argument missing");
            }

            if (!int.TryParse(portString, out var port) || port < 0 || port > 65535)
            {
                throw new StreamDeckInvalidParameter("-port argument must be an integer between 0 and 65536");
            }

            var info = JsonConvert.DeserializeObject <StreamDeckInfo>(infoString);


            var cancellationTokenSource = new CancellationTokenSource();

            Console.CancelKeyPress += (sender, eventArgs) =>
            {
                eventArgs.Cancel = true;

                Console.WriteLine("Closing connection...");
                cancellationTokenSource.Cancel();
            };


            var loggerInstance = logger ??
                                 (Debugger.IsAttached
                    ? (IStreamDeckLogger) new DebugLogger()
                    : new DevNullLogger());

            using (var worker = new StreamDeckClient(loggerInstance, new StreamDeckEventHandlerFactory(this), port, pluginUUID, registerEvent, info))
            {
                // ReSharper disable MethodSupportsCancellation - handled by StreamDeckWorker
                // ReSharper disable once AccessToDisposedClosure - not an issue, task is awaited
                var workerTask = Task.Run(async() => { await worker.Run(cancellationTokenSource.Token); });

                try
                {
                    workerTask.Wait();
                }
                catch (AggregateException e)
                {
                    if (e.InnerExceptions.Count != 1 || !(e.InnerExceptions[0] is TaskCanceledException))
                    {
                        throw;
                    }
                }
                catch (TaskCanceledException)
                {
                    // This space intentionally left blank
                }
                // ReSharper restore MethodSupportsCancellation
            }
        }
Ejemplo n.º 7
0
 private static async Task UpdateCounter(StreamDeckClient client, StreamDeckEventPayload args)
 {
     _counter++;
     await client.SetTitle(args.Context, _counter.ToString());
 }