Ejemplo n.º 1
0
        public async Task FunctionHandler(IotButtonPayload @event, ILambdaContext context)
        {
            context.Logger.LogLine($"Received event: {JsonConvert.SerializeObject(@event)}");

            var publishRequest = new PublishRequest
            {
                TopicArn = TOPIC_ARN,
                Subject  = "Greetings from IoT Button",
                Message  = $"Pressed: {@event.ClickType}",
            };

            string topicArn = Environment.GetEnvironmentVariable("TopicArn");

            context.Logger.LogLine($"TopicArn: {topicArn.Substring(0, 30)}...");
            string accessKey = Environment.GetEnvironmentVariable("AccessKey");

            context.Logger.LogLine($"AccessKey: {accessKey.Substring(0, 5)}...");
            string secretKey = Environment.GetEnvironmentVariable("SecretKey");

            context.Logger.LogLine($"SecretKey: {secretKey.Substring(0, 5)}...");

            var client = new AmazonSimpleNotificationServiceClient(accessKey, secretKey, RegionEndpoint.EUWest1);
            await client.PublishAsync(publishRequest).ConfigureAwait(false);

            context.Logger.LogLine("Event published successfully");
        }
Ejemplo n.º 2
0
        public async Task <HttpStatusCode> FunctionHandler(IotButtonPayload @event, ILambdaContext context)
        {
            context.Logger.LogLine($"Received event: {JsonConvert.SerializeObject(@event)}");

            var dataToPost = new
            {
                value1 = "Hello IoT world",
                value2 = $"Greetings from IFTTT Maker triggered by {@event.SerialNumber} IoT Button",
                value3 = @event.ClickType,
            };
            string json = JsonConvert.SerializeObject(dataToPost);

            string makerEvent = Environment.GetEnvironmentVariable("MakerEvent");

            context.Logger.LogLine($"MakerEvent: {makerEvent}");
            string makerKey = Environment.GetEnvironmentVariable("MakerKey");

            context.Logger.LogLine($"MakerKey: {makerKey.Substring(0, 5)}...");

            var postUrl  = new Uri($"https://maker.ifttt.com/trigger/{makerEvent}/with/key/{makerKey}");
            var client   = new HttpClient();
            var response = await client.PostAsync(postUrl, new StringContent(json, Encoding.UTF8, "application/json"))
                           .ConfigureAwait(false);

            context.Logger.LogLine($"STATUS CODE: {response.StatusCode}");

            var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

            context.Logger.LogLine($"Data received: {responseContent}");

            return(response.StatusCode);
        }
Ejemplo n.º 3
0
        public async Task FunctionHandler(IotButtonPayload @event, ILambdaContext context)
        {
            context.Logger.LogLine($"Received event: {JsonConvert.SerializeObject(@event)}");

            string weatherUrl = Environment.GetEnvironmentVariable("WeatherUrl");

            context.Logger.LogLine($"WeatherUrl: {weatherUrl}");
            var weatherData = await GetWeatherAsync(weatherUrl).ConfigureAwait(false);

            context.Logger.LogLine("Weather captured successfully");

            using (var smtpClient = new SmtpClient())
            {
                var configuration = GetConfiguration();
                context.Logger.LogLine($"SMTP configuration: Host:{configuration.Host}, Port:{configuration.Port}, " +
                                       $"EmailTo:{configuration.EmailTo}, EmailFrom:{configuration.EmailFrom}, Login:{configuration.Login}");

                smtpClient.Host                  = configuration.Host;
                smtpClient.Port                  = configuration.Port ?? 0;
                smtpClient.EnableSsl             = true;
                smtpClient.UseDefaultCredentials = false;
                smtpClient.Credentials           = new NetworkCredential(configuration.Login, configuration.Password);

                var mailMessage = new MailMessage
                {
                    Subject    = "Weather from IoT Button",
                    Body       = $"Weather attached.<br><br>Button: <b>{@event.SerialNumber}</b><br>Clicked: <b>{@event.ClickType}</b>",
                    IsBodyHtml = true
                };

                mailMessage.From = new MailAddress(configuration.EmailFrom, "IoT Button");
                mailMessage.To.Add(configuration.EmailTo);
                mailMessage.Attachments.Add(new Attachment(weatherData, "weather.png", "image/png"));

                context.Logger.LogLine("All set, going to send mail");
                await smtpClient.SendMailAsync(mailMessage).ConfigureAwait(false);

                context.Logger.LogLine("Email sent successfully");
            }
        }