Exemplo n.º 1
0
        public async Task <bool> RecordAsync <T>(T @event, string streamName)
        {
            string objAsJson = _serializer.Serialize(@event);

            byte[] objAsBytes = Encoding.UTF8.GetBytes(objAsJson + "\n");

            using (var ms = new MemoryStream(objAsBytes))
            {
                var record = new Record {
                    Data = ms
                };

                var request = new PutRecordRequest
                {
                    DeliveryStreamName = streamName,
                    Record             = record
                };

                PutRecordResponse response = await _client.PutRecordAsync(request);

                if (response.HttpStatusCode != HttpStatusCode.OK)
                {
                    throw new System.Exception($"Error sending message. HttpStatusCode: {response.HttpStatusCode}");
                }
            }

            return(true);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Sends sensor data to AWS Kinesis Firehose.
        /// </summary>
        /// <param name="temp">Temperature value</param>
        /// <param name="hum">Humidity value</param>
        /// <param name="bat">Battery level</param>
        private async void SendMessageToAWSFirehose(double temp, uint hum, uint bat)
        {
            // Check that the client is not null (might be if initialization failed)
            if (_client != null)
            {
                PutRecordRequest req = new PutRecordRequest();
                req.DeliveryStreamName = AWS_DELIVERY_STREAM;

                // Create the message
                // Message format: ISO timestamp, temperature, humidity, battery
                String data = String.Format("{0},{1},{2},{3}\n", DateTime.UtcNow.ToString("u"), temp, hum, bat);

                try
                {
                    var record = new Record
                    {
                        Data = new MemoryStream(UTF8Encoding.UTF8.GetBytes(data))
                    };

                    req.Record = record;

                    await _client.PutRecordAsync(req);
                }
                catch (Exception e)
                {
                    // Notify the user
                    await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                    {
                        statusText.Text = string.Format("Error sending data to cloud: {0}", e.ToString());
                    });
                }
            }
        }
Exemplo n.º 3
0
        private static Task <PutRecordResponse> PutRecordAsync(Guid id)
        {
            var data = "{\"id\": \"" + id + "\"}";

            // convert string to stream
            var byteArray = Encoding.UTF8.GetBytes(data);

            var putRecordRequest = new PutRecordRequest
            {
                DeliveryStreamName = "generic_event_stream", // AWS console -> Data FIrehose -> "Firehose delivery streams"
                Record             = new Record
                {
                    Data = new MemoryStream(byteArray)
                }
            };

            // Put record into the DeliveryStream
            Console.WriteLine($"PutRecordAsync: {data}");
            return(_firehoseClient.PutRecordAsync(putRecordRequest));
        }
Exemplo n.º 4
0
 private Amazon.KinesisFirehose.Model.PutRecordResponse CallAWSServiceOperation(IAmazonKinesisFirehose client, Amazon.KinesisFirehose.Model.PutRecordRequest request)
 {
     Utils.Common.WriteVerboseEndpointMessage(this, client.Config, "Amazon Kinesis Firehose", "PutRecord");
     try
     {
         #if DESKTOP
         return(client.PutRecord(request));
         #elif CORECLR
         return(client.PutRecordAsync(request).GetAwaiter().GetResult());
         #else
                 #error "Unknown build edition"
         #endif
     }
     catch (AmazonServiceException exc)
     {
         var webException = exc.InnerException as System.Net.WebException;
         if (webException != null)
         {
             throw new Exception(Utils.Common.FormatNameResolutionFailureMessage(client.Config, webException.Message), webException);
         }
         throw;
     }
 }