public async Task ProcessEventsAsync(PartitionContext context, IEnumerable <EventData> messages)
        {
            Trace.TraceInformation("\n");
            Trace.TraceInformation("........ProcessEventsAsync........");
            foreach (EventData eventData in messages)
            {
                try
                {
                    string jsonString = Encoding.UTF8.GetString(eventData.GetBytes());

                    Trace.TraceInformation(string.Format("Message received at '{0}'. Partition: '{1}'",
                                                         eventData.EnqueuedTimeUtc.ToLocalTime(), this.partitionContext.Lease.PartitionId));

                    Trace.TraceInformation(string.Format("-->Raw Data: '{0}'", jsonString));

                    SensorEvent newSensorEvent = this.DeserializeEventData(jsonString);

                    Trace.TraceInformation(string.Format("-->Serialized Data: '{0}', '{1}', '{2}', '{3}', '{4}'",
                                                         newSensorEvent.timestart, newSensorEvent.dsplalert, newSensorEvent.alerttype, newSensorEvent.message, newSensorEvent.targetalarmdevice));

                    // Issuing alarm to device.
                    string commandParameterNew = "{\"Name\":\"AlarmThreshold\",\"Parameters\":{\"SensorId\":\"" + newSensorEvent.dsplalert + "\"}}";
                    Trace.TraceInformation("Issuing alarm to device: '{0}', from sensor: '{1}'", newSensorEvent.targetalarmdevice, newSensorEvent.dsplalert);
                    Trace.TraceInformation("New Command Parameter: '{0}'", commandParameterNew);
                    await WorkerRole.iotHubServiceClient.SendAsync(newSensorEvent.targetalarmdevice, new Microsoft.Azure.Devices.Message(Encoding.UTF8.GetBytes(commandParameterNew)));
                }
                catch (Exception ex)
                {
                    Trace.TraceInformation("Error in ProssEventsAsync -- {0}\n", ex.Message);
                }
            }

            await context.CheckpointAsync();
        }
예제 #2
0
        public async Task ProcessEventsAsync(PartitionContext context, IEnumerable <EventData> messages)
        {
            Trace.TraceInformation("\n");
            Trace.TraceInformation("........ProcessEventsAsync........");
            foreach (EventData eventData in messages)
            {
                try
                {
                    string jsonString = Encoding.UTF8.GetString(eventData.GetBytes());

                    Trace.TraceInformation(string.Format("Message received at '{0}'. Partition: '{1}'",
                                                         eventData.EnqueuedTimeUtc.ToLocalTime(), this.partitionContext.Lease.PartitionId));

                    Trace.TraceInformation(string.Format("-->Raw Data: '{0}'", jsonString));

                    SensorEvent newSensorEvent = this.DeserializeEventData(jsonString);

                    Trace.TraceInformation(string.Format("-->Serialized Data: '{0}', '{1}', '{2}', '{3}'",
                                                         newSensorEvent.deviceid, newSensorEvent.currenttemp, newSensorEvent.gassense, newSensorEvent.flamesense));
                    /* Send command to IoT device.*/

                    // Turn off heater device.
                    string commandParameterNew = "{\"Name\":\"TurnHeaterOff\",\"Parameters\":{\"reason\":" + 1 + "}}";
                    Trace.TraceInformation("Turning heater off: '{0}'", newSensorEvent.deviceid);
                    Trace.TraceInformation("New Command Parameter: '{0}'", commandParameterNew);
                    await WorkerRole.iotHubServiceClient.SendAsync(newSensorEvent.deviceid, new Microsoft.Azure.Devices.Message(Encoding.UTF8.GetBytes(commandParameterNew)));


                    if (DateTime.Now - lastTweet > new TimeSpan(0, 5, 0))  // One tweet per 5 min
                    {
                        // Tweeting here
                        string consumerKey    = ConfigurationManager.AppSettings["Twitter.ConsumerKey"];
                        string consumerSecret = ConfigurationManager.AppSettings["Twitter.ConsumerSecret"];
                        string accessToken    = ConfigurationManager.AppSettings["Twitter.AccessToken"];
                        string accessSecret   = ConfigurationManager.AppSettings["Twitter.AccessSecret"];

                        // Obtain keys by registering your app on https://dev.twitter.com/apps or https://apps.twitter.com/
                        var service = new TwitterService(consumerKey, consumerSecret);
                        service.AuthenticateWith(accessToken, accessSecret);
                        SendTweetOptions x = new SendTweetOptions();
                        x.Status = string.Format("Gas leak detected from Smart heater! (Auto-generated messsage from Azure Stream Analytics)");
                        service.SendTweet(x);

                        lastTweet = DateTime.Now;
                    }
                }
                catch (Exception ex)
                {
                    Trace.TraceInformation("Error in ProssEventsAsync -- {0}\n", ex.Message);
                }
            }

            await context.CheckpointAsync();
        }
        public async Task ProcessEventsAsync(PartitionContext context, IEnumerable <EventData> messages)
        {
            Trace.TraceInformation("\n");
            Trace.TraceInformation("........ProcessEventsAsync........");
            foreach (EventData eventData in messages)
            {
                try
                {
                    string jsonString = Encoding.UTF8.GetString(eventData.GetBytes());

                    Trace.TraceInformation(string.Format("Message received at '{0}'. Partition: '{1}'",
                                                         eventData.EnqueuedTimeUtc.ToLocalTime(), this.partitionContext.Lease.PartitionId));

                    Trace.TraceInformation(string.Format("-->Raw Data: '{0}'", jsonString));
                    String receiverid;
                    string datatosend;
                    //TODO It can be made better by mataining runtime registry of devices and controllers.
                    if (jsonString.Contains("\"deviceid\"") || jsonString.Contains("\"DeviceId\""))  // Telemetry
                    {
                        receiverid = "UWA1";
                        SensorEvent newSensorEvent = this.DeserializeEventData(jsonString);


                        Trace.TraceInformation(string.Format("-->Serialized Data: '{0}', '{1}', '{2}', '{3}'",
                                                             newSensorEvent.deviceid, newSensorEvent.currenttemp, newSensorEvent.gassense, newSensorEvent.flamesense));

                        datatosend = jsonString;
                    }
                    else   // command
                    {
                        receiverid = "Heater1";
                        HeaterCommand heatercommand = JsonConvert.DeserializeObject <HeaterCommand>(jsonString);
                        datatosend = "{\"Name\":\"" + heatercommand.command + "\",\"Parameters\":{";
                        if (heatercommand.parameterName != null)
                        {
                            datatosend += "\"" + heatercommand.parameterName + "\":" + heatercommand.parameter + "";
                        }
                        datatosend += "}}";
                    }
                    /* Send command to IoT device. */


                    Trace.TraceInformation("Sending jason to: '{0}'", receiverid);
                    Trace.TraceInformation("Data: '{0}'", datatosend);
                    await WorkerRole.iotHubServiceClient.SendAsync(receiverid, new Microsoft.Azure.Devices.Message(Encoding.UTF8.GetBytes(datatosend)));
                }
                catch (Exception ex)
                {
                    Trace.TraceInformation("Error in ProssEventsAsync -- {0}\n", ex.Message);
                }
            }

            await context.CheckpointAsync();
        }
예제 #4
0
        public async Task ProcessEventsAsync(PartitionContext context, IEnumerable <EventData> messages)
        {
            Trace.TraceInformation("\n");
            Trace.TraceInformation("........ProcessEventsAsync........");
            foreach (EventData eventData in messages)
            {
                try
                {
                    string jsonString = Encoding.UTF8.GetString(eventData.GetBytes());

                    Trace.TraceInformation(string.Format("Message received at '{0}'. Partition: '{1}'",
                                                         eventData.EnqueuedTimeUtc.ToLocalTime(), this.partitionContext.Lease.PartitionId));

                    Trace.TraceInformation(string.Format("-->Raw Data: '{0}'", jsonString));

                    SensorEvent newSensorEvent = this.DeserializeEventData(jsonString);

                    Trace.TraceInformation(string.Format("-->Serialized Data: '{0}', '{1}', '{2}', '{3}', '{4}'",
                                                         newSensorEvent.timestart, newSensorEvent.dsplalert, newSensorEvent.alerttype, newSensorEvent.message, newSensorEvent.targetalarmdevice));

                    // Issuing alarm to device.
                    string commandParameterNew = "{\"Name\":\"AlarmThreshold\",\"Parameters\":{\"SensorId\":\"" + newSensorEvent.dsplalert + "\"}}";
                    Trace.TraceInformation("Issuing alarm to device: '{0}', from sensor: '{1}'", newSensorEvent.targetalarmdevice, newSensorEvent.dsplalert);
                    Trace.TraceInformation("New Command Parameter: '{0}'", commandParameterNew);
                    await WorkerRole.iotHubServiceClient.SendAsync(newSensorEvent.targetalarmdevice, new Microsoft.Azure.Devices.Message(Encoding.UTF8.GetBytes(commandParameterNew)));

                    // Tweeting here
                    string consumerKey    = ConfigurationManager.AppSettings["Twitter.ConsumerKey"];
                    string consumerSecret = ConfigurationManager.AppSettings["Twitter.ConsumerSecret"];
                    string accessToken    = ConfigurationManager.AppSettings["Twitter.AccessToken"];
                    string accessSecret   = ConfigurationManager.AppSettings["Twitter.AccessSecret"];

                    // Obtain keys by registering your app on https://dev.twitter.com/apps or https://apps.twitter.com/
                    var service = new TwitterService(consumerKey, consumerSecret);
                    service.AuthenticateWith(accessToken, accessSecret);
                    SendTweetOptions x = new SendTweetOptions();
                    x.Status = string.Format("Temprature Anomaly detected fom FRDM-K64F! (Auto-generated messsage from Azure Stream Analytics)");
                    service.SendTweet(x);
                }
                catch (Exception ex)
                {
                    Trace.TraceInformation("Error in ProssEventsAsync -- {0}\n", ex.Message);
                }
            }

            await context.CheckpointAsync();
        }
예제 #5
0
        public async Task ProcessEventsAsync(PartitionContext context, IEnumerable <EventData> messages)
        {
            Trace.TraceInformation("\n");
            Trace.TraceInformation("........ProcessEventsAsync........");
            foreach (EventData eventData in messages)
            {
                try
                {
                    string jsonString = Encoding.UTF8.GetString(eventData.GetBytes());

                    Trace.TraceInformation(string.Format("Message received at '{0}'. Partition: '{1}'",
                                                         eventData.EnqueuedTimeUtc.ToLocalTime(), this.partitionContext.Lease.PartitionId));

                    Trace.TraceInformation(string.Format("-->Raw Data: '{0}'", jsonString));

                    SensorEvent newSensorEvent = this.DeserializeEventData(jsonString);

                    Trace.TraceInformation(string.Format("-->Serialized Data: '{0}', '{1}', '{2}', '{3}'",
                                                         newSensorEvent.deviceid, newSensorEvent.x, newSensorEvent.y, newSensorEvent.z));

                    // Issuing alarm to device.
                    string commandParameterNew = "{\"Name\":\"vibrationdetected\",\"Parameters\":{\"deviceid\":\"" + newSensorEvent.deviceid + "\"}}";
                    Trace.TraceInformation("Issuing alarm to device: '{0}'", newSensorEvent.deviceid);
                    Trace.TraceInformation("New Command Parameter: '{0}'", commandParameterNew);
                    await WorkerRole.iotHubServiceClient.SendAsync(newSensorEvent.deviceid, new Microsoft.Azure.Devices.Message(Encoding.UTF8.GetBytes(commandParameterNew)));

                    // Tweeting here
                    string consumerKey    = ConfigurationManager.AppSettings["Twitter.ConsumerKey"];
                    string consumerSecret = ConfigurationManager.AppSettings["Twitter.ConsumerSecret"];
                    string accessToken    = ConfigurationManager.AppSettings["Twitter.AccessToken"];
                    string accessSecret   = ConfigurationManager.AppSettings["Twitter.AccessSecret"];

                    // Obtain keys by registering your app on https://dev.twitter.com/apps or https://apps.twitter.com/
                    var service = new TwitterService(consumerKey, consumerSecret);
                    service.AuthenticateWith(accessToken, accessSecret);
                    SendTweetOptions x = new SendTweetOptions();
                    x.Status = string.Format("{0} was flipped on {1} UTC, position data: x={2}, y={3}, z={4}", newSensorEvent.deviceid, eventData.EnqueuedTimeUtc.ToString(), newSensorEvent.x, newSensorEvent.y, newSensorEvent.z);
                    service.SendTweet(x);
                }
                catch (Exception ex)
                {
                    Trace.TraceInformation("Error in ProssEventsAsync -- {0}\n", ex.Message);
                }
            }

            await context.CheckpointAsync();
        }