Пример #1
0
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddLogging(builder =>
            {
                builder.AddConsole(o =>
                {
                    o.TimestampFormat = o.TimestampFormat = "[HH:mm:ss.fff] ";
                });
                builder.AddDebug();
            });

            services.AddSingleton(Configuration);
            services.AddSingleton(new Keyboard(Configuration));
            services.AddSingleton(new Mouse(Configuration));
            services.AddSingleton(new Joystick(Configuration));

            var senseHatFramebufferDevPath = LedMatrix.GetFrameBufferDevicePath();

            if (!string.IsNullOrWhiteSpace(senseHatFramebufferDevPath))
            {
                services.AddSingleton(new LedMatrix(senseHatFramebufferDevPath));
            }

            services.AddSingleton <IBeholderStalk, BeholderStalk>();
        }
Пример #2
0
 public void SendLedMatrix(Color[] pixels)
 {
     if (LedMatrix != null)
     {
         _logger.LogInformation($"Sending Led Matrix {pixels.Length} to {LedMatrix.DevPath}...");
         LedMatrix.SetPixels(pixels);
     }
     else
     {
         _logger.LogInformation($"Led Matrix recieved a message, but was not present...");
     }
 }
Пример #3
0
        public BeholderStalk(IConfigurationRoot configuration, ILogger <BeholderStalk> logger, Keyboard keyboard, Mouse mouse, Joystick joystick, LedMatrix ledMatrix = null)
        {
            _logger   = logger;
            Keyboard  = keyboard;
            Mouse     = mouse;
            Joystick  = joystick;
            LedMatrix = ledMatrix;

            Keyboard.KeyboardLedsChanged += Keyboard_KeyboardLedsChanged;
            Mouse.MouseResolutionChanged += Mouse_MouseResolutionChanged;

            // Connect to the Nexus
            _clientId = configuration["beholder_stalk_clientid"];
            _nexusUrl = configuration["beholder_nexus_url"];
            var nexusUsername = configuration["beholder_nexus_username"];
            var nexusPassword = configuration["beholder_nexus_password"];

            // Create a new MQTT client.
            var factory    = new MqttFactory();
            var mqttClient = factory.CreateMqttClient();

            _mqttClientOptions = new MqttClientOptionsBuilder()
                                 .WithClientId(_clientId)
                                 .WithWebSocketServer(_nexusUrl)
                                 .WithCredentials(nexusUsername, nexusPassword)
                                 .WithKeepAlivePeriod(TimeSpan.FromSeconds(10))
                                 .WithCommunicationTimeout(TimeSpan.FromSeconds(30))
                                 .WithWillDelayInterval(60 * 1000)
                                 .WithWillMessage(new MqttApplicationMessage()
            {
                PayloadFormatIndicator = MQTTnet.Protocol.MqttPayloadFormatIndicator.CharacterData,
                ContentType            = "text/plain",
                Topic   = StalkTopic.Status_BeholderStalk,
                Payload = Encoding.UTF8.GetBytes("Disconnected"),
                Retain  = true
            })
                                 .WithCleanSession()
                                 .Build();

            mqttClient.UseConnectedHandler(OnConnected);
            mqttClient.UseDisconnectedHandler(OnDisconnected);
            mqttClient.UseApplicationMessageReceivedHandler(OnApplicationMessageReceived);

            MqttClient = mqttClient;
        }