Created
July 6, 2016 18:39
-
-
Save jsiegmund/e3ddc1d1783423d6f1787a4b0575ee4c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#r "D:\Program Files (x86)\SiteExtensions\Functions\0.3.10261\bin\Microsoft.Azure.WebJobs.Extensions.NotificationHubs.dll" | |
using System; | |
using Newtonsoft.Json; | |
using Newtonsoft.Json.Linq; | |
using Microsoft.Azure.NotificationHubs; | |
using Microsoft.Azure.WebJobs; | |
using Microsoft.Azure.WebJobs.Extensions; | |
public static void Run(string inputEventMessage, string inputBlob, IBinder binder, out string outputBlob, TraceWriter log) | |
{ | |
if (String.IsNullOrEmpty(inputEventMessage)) | |
{ | |
log.Info($"The inputEventMessage array was null or didn't contain exactly one item."); | |
//notification = null; | |
outputBlob = inputBlob; | |
return; | |
} | |
log.Info($"C# Event Hub trigger function processed a message: {inputEventMessage}"); | |
if (String.IsNullOrEmpty(inputBlob)) | |
inputBlob = DateTime.MinValue.ToString(); | |
DateTime lastEvent = DateTime.Parse(inputBlob); | |
TimeSpan duration = DateTime.Now - lastEvent; | |
string notificationHubConnectionString = System.Configuration.ConfigurationManager.AppSettings["repsaj-neptune-notifications_NOTIFICATIONHUB"]; | |
string notificationHubName = System.Configuration.ConfigurationManager.AppSettings["repsaj-neptune-notifications_NOTIFICATIONHUBNAME"]; | |
log.Info($"Configured notification hub: {notificationHubConnectionString}, {notificationHubName}"); | |
var attribute = new NotificationHubAttribute | |
{ | |
ConnectionStringSetting = notificationHubConnectionString, | |
HubName = notificationHubName | |
}; | |
IAsyncCollector<Notification> notifications = binder.Bind<IAsyncCollector<Notification>>(attribute); | |
if (duration.TotalMinutes >= 0) { | |
GetGcmMessage(inputEventMessage, notifications, log); | |
//notification = GetTemplateNotification(myEventHubMessage); | |
outputBlob = DateTime.Now.ToString(); | |
} | |
else { | |
log.Info($"Not sending notification message because of timer ({(int)duration.TotalMinutes} minutes ago)."); | |
//notification = null; | |
outputBlob = inputBlob; | |
} | |
} | |
private static void GetGcmMessage(string input, IAsyncCollector<Notification> notifications, TraceWriter log) | |
{ | |
string message; | |
dynamic eventData = ((JArray)JsonConvert.DeserializeObject(input))[0]; | |
if (eventData.readingtype == "leakage") | |
message = String.Format("Leakage detected! Sensor {0} has detected a possible leak.", eventData.reading); | |
else if (eventData.readingtype == "EventsMissing") | |
message = String.Format("Device {0} hasn't sent any data since {1}. Connection problems?", eventData.deviceid, eventData.reading); | |
else | |
message = String.Format("Sensor {0} is reading {1:0.0}, threshold is {2:0.0}.", eventData.readingtype, eventData.reading, eventData.threshold); | |
message = "{\"data\":{\"message\":\""+message+"\"}}"; | |
var notification = new GcmNotification(message); | |
notifications.AddAsync(notification); | |
log.Info($"Added notification message to collection: {message}"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment