public async Task ConnectToSignalR(string signalRHubUrl)
        {
            var token = await _restClient.GetToken();

            _connection = BuildHubConnection(signalRHubUrl, token);

            _connection.Closed += async(error) =>
            {
                await _logger.SendMessage("Lighting System SignalR Disconnected");

                var connectionState = false;
                while (!connectionState)
                {
                    await Task.Delay(new Random().Next(0, 5) * 1000);

                    try
                    {
                        var token = await _restClient.GetToken();

                        _connection = BuildHubConnection(signalRHubUrl, token);
                        SignalRMethodsRegistration();
                        await _connection.StartAsync();

                        connectionState = true;
                    }
                    catch (Exception ex)
                    {
                        await _lokiLogger.SendMessage($"Lighting System {ex}", LogLevel.Error);

                        connectionState = false;
                        Console.WriteLine(ex);
                    }
                }
            };

            SignalRMethodsRegistration();

            try
            {
                await _connection.StartAsync();
            }
            catch (Exception ex)
            {
                await _lokiLogger.SendMessage($"Lighting System {ex}", LogLevel.Error);

                //Here can be retry
                Console.WriteLine(ex);
            }
        }
Exemplo n.º 2
0
        public async Task HandleClientDisconnectedAsync(MqttServerClientDisconnectedEventArgs eventArgs)
        {
            Debug.WriteLine($"Client disconnected {eventArgs.ClientId}");
            await _mqttServer.ClearRetainedApplicationMessagesAsync();

            try
            {
                await _restClient.DisableLightPoint(Guid.Parse(eventArgs.ClientId));
            }
            catch (Exception ex)
            {
                await _lokiLogger.SendMessage($"Lighting System {ex}", LogLevel.Error);

                Console.WriteLine(ex);
            }
        }
Exemplo n.º 3
0
        private async Task UserConnected(string payload)
        {
            //TODO try catch serializer can crash
            LightPoint lightPointSwitch = JsonConvert.DeserializeObject <LightPoint>(payload);

            try
            {
                var lightPoint = await _restClient.GetLightPoints(Guid.Parse(lightPointSwitch.Id));

                if (lightPoint.CustomName == null)
                {
                    var lightBulb = new List <LightBulbDto>();
                    foreach (var id in lightPointSwitch.BulbsId)
                    {
                        lightBulb.Add(new LightBulbDto()
                        {
                            Id = Guid.Parse(id)
                        });
                    }

                    var lightPointDto = new LightPointDto()
                    {
                        CustomName = lightPointSwitch.CustomName,
                        Id         = Guid.Parse(lightPointSwitch.Id),
                        LightBulbs = lightBulb
                    };

                    //TODO auto generation of guid and saving to memeory of rPi
                    await _restClient.AddLightPoint(Guid.Parse(_homeAutomationLocalLightSystemId), lightPointDto);
                }
                else
                {
                    await _restClient.EnableLightPoint(Guid.Parse(lightPointSwitch.Id));
                }
            }
            catch (Exception ex)
            {
                await _logger.SendMessage($"Lighting System {ex}", LogLevel.Error);

                await _lokiLogger.SendMessage($"Lighting System {ex}", LogLevel.Error);

                Console.WriteLine(ex);
            }
        }
Exemplo n.º 4
0
        public async Task AddLightPoint(Guid homeLightSystemId, LightPointDto lightPointDto)
        {
            try
            {
                var token = await GetToken();

                await $"{_homeAutomationLightSystemApi}/{lightPointUrl}/{homeLightSystemId}"
                .SetQueryParams(new {
                    access_token = token
                })
                .PostJsonAsync(lightPointDto);
            }
            catch (Exception ex)
            {
                await _lokiLogger.SendMessage($"Lighting System {ex}", LogLevel.Error);

                Console.Write(ex);
            }
        }
 private void checkConnect()
 {
     try
     {
         if (_sock == null)
         {
             _sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
             {
                 ReceiveTimeout = 1000
             }
         }
         ;
         if (!_sock.Connected && DeviceIp != null)
         {
             _sock.Connect(DeviceIp, DevicePort);
         }
     }
     catch (Exception ex)
     {
         _lokiLogger.SendMessage($"Sound System {ex}", LogLevel.Error);
         _logger.SendMessage($"Sound System {ex}", LogLevel.Error);
     }
 }
        public async Task ConnectToSignalR(string token, string signalRHubUrl)
        {
            _connection = new HubConnectionBuilder()
                          .WithUrl(signalRHubUrl)
                          //,
                          //options =>
                          //{
                          //    options.AccessTokenProvider = () => Task.FromResult(token);
                          //})
                          .WithAutomaticReconnect()
                          .Build();

            _connection.Closed += async(error) =>
            {
                var connectionState = false;
                await _logger.SendMessage("Sound System SignalR Disconnected");

                while (!connectionState)
                {
                    await Task.Delay(new Random().Next(0, 5) * 1000);

                    try
                    {
                        await _connection.StartAsync();

                        connectionState = true;
                    }
                    catch (Exception ex)
                    {
                        await _lokiLogger.SendMessage($"Sound System {ex}", LogLevel.Error);

                        connectionState = false;
                        Console.WriteLine(ex);
                    }
                }
            };

            _connection.On <string, string>("ReceiveMessage", async(user, message) =>
            {
                Debug.WriteLine($"Message from {user} revived. {message}");

                // _logger.Log($"Message from {user} recived. {message}", typeof(SignalRClient).Namespace, "");
            });

            _connection.On("MasterVolumeUp", async() =>
            {
                await _lokiLogger.SendMessage($"Sound System MasterVolumeUp");
                await _onkyoApi.MasterVolumeUp();
            });

            _connection.On("MasterVolumeDown", async() =>
            {
                await _lokiLogger.SendMessage($"Sound System MasterVolumeDown");
                _onkyoApi.MasterVolumeDown();
            });

            _connection.On("PowerOff", async() =>
            {
                await _lokiLogger.SendMessage($"Sound System PowerOff");
                _onkyoApi.PowerOff();
            });

            _connection.On("PowerOn", async() =>
            {
                await _lokiLogger.SendMessage($"Sound System PowerOn");
                _onkyoApi.PowerOn();
            });

            try
            {
                await _connection.StartAsync();

                _onkyoApi.OnPacketRecieved += OnSoundSystemChanged;
                _onkyoApi.MasterVolumeStatus();
            }
            catch (Exception ex)
            {
                await _lokiLogger.SendMessage($"Sound System {ex}", LogLevel.Error);

                Console.WriteLine(ex);
            }
        }
Exemplo n.º 7
0
 public async Task HandleClientConnectedAsync(MqttServerClientConnectedEventArgs eventArgs)
 {
     Debug.WriteLine($"Mqqt Client connected {eventArgs.ClientId}");
     await _lokiLogger.SendMessage($"Lighting System Mqqt Client connected {eventArgs.ClientId}");
 }