internal override EventData PrepareEventData(IDictionary bag)
        {
            foreach (SensorType type in bag.Keys)
            {
                if (type == SensorType.Temperature)
                {
                    float temperature = (float)bag[type];

                    this.sensor.measurename = Enum.GetName(typeof(SensorType), SensorType.Temperature);
                    this.sensor.timecreated = DateTime.UtcNow.ToString("o");
                    this.sensor.unitofmeasure = "C";
                    this.sensor.value = temperature;

                    Debug.WriteLine("temp: " + temperature);
                }
                else if (type == SensorType.Humidity)
                {
                    // nothing
                }
                else if (type == SensorType.Accelerometer)
                {
                    // nothing
                }
            }

            EventData data = new EventData(Encoding.UTF8.GetBytes(this.sensor.ToJson()));

            return data;
        }
Ejemplo n.º 2
0
        internal override EventData PrepareEventData(IDictionary bag)
        {
            EventData data = new EventData();

            foreach (SensorType type in bag.Keys)
            {
                if (type == SensorType.Temperature)
                {
                    float temperature = (float)bag[type];
                    data.Properties["time"] = DateTime.UtcNow;
                    data.Properties["temp"] = temperature;
                    Debug.WriteLine("temp: " + temperature);
                }
                else if (type == SensorType.Humidity)
                {
                    // nothing
                }
                else if (type == SensorType.Accelerometer)
                {
                    // nothing
                }
            }

            data.PartitionKey = this.DeviceId;

            return data;
        }
Ejemplo n.º 3
0
        public void SendMessage(string message)
        {
            EventData data = new EventData(Encoding.UTF8.GetBytes(message));
            data.Properties["time"] = DateTime.UtcNow;

            _client.Send(data);
        }
Ejemplo n.º 4
0
        internal override EventData PrepareEventData(IDictionary bag)
        {
            EventData data = new EventData();

            foreach (SensorType type in bag.Keys)
            {
                if (type == SensorType.Temperature)
                {
                    double temperature = (double)bag[type];
                    data.Properties["time"] = DateTime.UtcNow;
                    data.Properties["temp"] = temperature;
                    Debug.Print("temp: " + temperature);
                }
                else if (type == SensorType.Humidity)
                {
                    double humidity = (double)bag[type];
                    data.Properties["time"] = DateTime.UtcNow;
                    data.Properties["hmdt"] = humidity;
                    Debug.Print("hmdt: " + humidity);
                }
                else if (type == SensorType.Accelerometer)
                {
                    double[] acceleration = (double[])bag[type];
                    data.Properties["time"] = DateTime.UtcNow;
                    data.Properties["accx"] = acceleration[0];
                    data.Properties["accy"] = acceleration[1];
                    data.Properties["accz"] = acceleration[2];
                    Debug.Print("acceleration: " + acceleration[0] + "," + acceleration[1] + "," + acceleration[2]);
                }
            }

            data.PartitionKey = this.DeviceId;

            return data;
        }
Ejemplo n.º 5
0
        public SendResult Send(string json)
        {
            try
            {
                string connectionString = ServiceBusConnectionStringBuilder.CreateUsingSharedAccessSignature(
                    this.ConnectionParameters.ServiceBusNamespaceUri,
                    this.ConnectionParameters.EventHubName,
                    this.ConnectionParameters.PublisherName,
                    this.ConnectionParameters.SharedAccessSignature);

                MessagingFactory factory = MessagingFactory.CreateFromConnectionString(connectionString);

                EventHubClient client = factory.CreateEventHubClient(this.ConnectionParameters.EventHubName);

                EventHubSender sender = client.CreateSender(this.ConnectionParameters.PublisherName);

                EventData data = new EventData(Encoding.UTF8.GetBytes("Body"));
                data.Properties["time"] = DateTime.UtcNow;

                sender.Send(data);

                sender.Close();
                client.Close();
                factory.Close();

                return SendResult.Success;
            }
            catch
            {
                return SendResult.Failure;
            }
        }
Ejemplo n.º 6
0
        public async Task TransmitImageSavedAsync(SentimentResult result)
        {
            var serialized = JsonConvert.SerializeObject(result);
            var eventData = new EventData(Encoding.UTF8.GetBytes(serialized));

            _exceptionHandler.Run(() =>  _eventHubClient.Send(eventData));

            await Task.FromResult<object>(null);
        }
Ejemplo n.º 7
0
        public void Scenario1_EventHubSend(string eventHubEntity)
        {
            ServiceBusConnectionStringBuilder builder = new ServiceBusConnectionStringBuilder(this.ConnectionString);
            builder.TransportType = TransportType.Amqp;

            MessagingFactory factory = MessagingFactory.CreateFromConnectionString(this.ConnectionString);

            EventHubClient client = factory.CreateEventHubClient(eventHubEntity);

            EventData data = new EventData(Encoding.UTF8.GetBytes("Body"));
            data.Properties["time"] = DateTime.UtcNow;

            client.Send(data);

            client.Close();
            factory.Close();
        }
Ejemplo n.º 8
0
        internal override EventData PrepareEventData(IDictionary bag)
        {
            EventData data = new EventData();

            // Create hashtable for data
            Hashtable hashtable = new Hashtable();
            hashtable.Add("Subject", "wthr");
            hashtable.Add("time", DateTime.UtcNow);
            hashtable.Add("from", this.DeviceId);
            hashtable.Add("dspl", this.DeviceName);

            foreach (SensorType type in bag.Keys)
            {
                if (type == SensorType.Temperature)
                {
                    double temperature = (double)bag[type];
                    data.Properties["temp"] = temperature;
                    Debug.Print("temp: " + temperature);
                }
                else if (type == SensorType.Humidity)
                {
                    double humidity = (double)bag[type];
                    data.Properties["hmdt"] = humidity;
                    Debug.Print("hmdt: " + humidity);
                }
                else if (type == SensorType.Accelerometer)
                {
                    double[] acceleration = (double[])bag[type];
                    data.Properties["accx"] = acceleration[0];
                    data.Properties["accy"] = acceleration[1];
                    data.Properties["accz"] = acceleration[2];
                    Debug.Print("acceleration: " + acceleration[0] + "," + acceleration[1] + "," + acceleration[2]);
                }
            }

            // Serialize hashtable into JSON
            JsonSerializer serializer = new JsonSerializer(DateTimeFormat.Default);
            string payload = serializer.Serialize(hashtable);

            data.PartitionKey = this.DeviceId;

            return data;
        }
Ejemplo n.º 9
0
        internal override EventData PrepareEventData(IDictionary bag)
        {
            EventData data = new EventData();

            foreach (SensorType type in bag.Keys)
            {
                if (type == SensorType.HearRate)
                {
                    byte bpm = (byte)bag[type];
                    data.Properties["time"] = DateTime.UtcNow;
                    data.Properties["bpm"] = bpm;
                    Debug.Print("bpm: " + bpm);
                }
            }

            data.PartitionKey = this.DeviceId;

            return data;
        }
Ejemplo n.º 10
0
        private void btnSend_Click(object sender, RoutedEventArgs e)
        {
            //ServiceBusConnectionStringBuilder builder = new ServiceBusConnectionStringBuilder(ConnectionString)
            //{
            //    TransportType = TransportType.Amqp
            //};

            MessagingFactory factory = MessagingFactory.CreateFromConnectionString(ConnectionString);

            EventHubClient client = factory.CreateEventHubClient(EventHubName);

            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof (CustomData));

            CustomData data = new CustomData
            {
                Date = DateTime.UtcNow,
                Message = "Test message"
            };

            string body;
            using (MemoryStream ms = new MemoryStream())
            using (StreamReader sr = new StreamReader(ms))
            {
                serializer.WriteObject(ms, data);
                ms.Seek(0, SeekOrigin.Begin);
                body = sr.ReadToEnd();
            }

            EventData message = new EventData(Encoding.UTF8.GetBytes(body));

            /*  Alternative serialization

            EventData message = new EventData(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(data)));

            */
            message.Properties["time"] = DateTime.UtcNow;

            client.Send(message);

            client.Close();
            factory.Close();
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Enqueue EventData for sending
        /// </summary>
        /// <param name="data">EventData to enqueue</param>
        private void Enqueue(EventData data)
        {
            // enqueue event data
            lock (this.queue)
            {
                this.queue.Enqueue(data);
            }

            this.queueEvent.Set();
        }
Ejemplo n.º 12
0
 /// <summary>
 /// Send data to the event hub
 /// </summary>
 /// <param name="data">EventData instance to send</param>
 public void Send(EventData data)
 {
     this.CreateSender();
     this.Sender.SendEventData(data);
 }
Ejemplo n.º 13
0
        public void Scenario4_EventHubSendToPublisherWithToken(string sbnamespace, string eventHubEntity, string publisher, string sharedAccessKeyName, string sharedAccessKey)
        {
            string token = SharedAccessSignatureTokenProvider.GetPublisherSharedAccessSignature(
                new Uri(sbnamespace),
                eventHubEntity,
                publisher,
                sharedAccessKeyName,
                sharedAccessKey,
                new TimeSpan(0, 20, 0));

            string connectionString = ServiceBusConnectionStringBuilder.CreateUsingSharedAccessSignature(
                new Uri(sbnamespace),
                eventHubEntity,
                publisher,
                token);

            MessagingFactory factory = MessagingFactory.CreateFromConnectionString(connectionString);

            EventHubClient client = factory.CreateEventHubClient(eventHubEntity);

            EventHubSender sender = client.CreateSender(publisher);

            EventData data = new EventData(Encoding.UTF8.GetBytes("Body"));
            data.Properties["time"] = DateTime.UtcNow;

            sender.Send(data);

            sender.Close();
            client.Close();
            factory.Close();
        }