Exemplo n.º 1
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);
            }
        }