Skip to content

Instantly share code, notes, and snippets.

@SamVanhoutte
Last active August 29, 2015 14:13
Show Gist options
  • Save SamVanhoutte/640a5cb649cb705eb993 to your computer and use it in GitHub Desktop.
Save SamVanhoutte/640a5cb649cb705eb993 to your computer and use it in GitHub Desktop.
Azure Stream Analytics - blog post sample
async void Main()
{
var mf = MessagingFactory.CreateFromConnectionString("your cnx string here");
var eventHubClient = mf.CreateEventHubClient("ehname");
Random rnd = new Random();
Parallel.For(1, 11, async (deviceId) =>
{
for(int cnt = 0; cnt < 50; cnt++)
{
Console.WriteLine ("Sending event nr {0} for device {1}", cnt, deviceId);
var reading = new TelemetryInfo
{
DeviceId = "Device" + deviceId.ToString(),
ErrorReading = false,
MainValue = 30 * rnd.NextDouble(),
Timestamp = DateTime.UtcNow,
Type = TelemetryType.Temperature
};
EventData msg = new EventData(Encoding.UTF8.GetBytes(reading.ToJson()));
await eventHubClient.SendAsync(msg);
await Task.Delay(TimeSpan.FromSeconds(5));
//Thread.Sleep(TimeSpan.FromSeconds(5));
}
}
);
}
public class TelemetryInfo
{
public bool ErrorReading { get; set; }
public DateTime Timestamp { get; set; }
public string DeviceId { get; set; }
public TelemetryType Type { get; set; }
public Dictionary<string, object> ExtendedData { get; set; }
public object MainValue { get; set; }
public string ToJson()
{
dynamic eventMessage = new ExpandoObject();
eventMessage.DeviceId = DeviceId;
eventMessage.ErrorReading = ErrorReading.ToString();
eventMessage.MainValue = MainValue;
eventMessage.Timestamp = Timestamp;
eventMessage.Type = (int)Type;
return JsonConvert.SerializeObject(eventMessage);
}
}
public enum TelemetryType
{
Temperature = 0,
SwitchStatus = 1,
LightStatus = 2,
CurtainStatus = 3,
Weight = 4,
Distance = 5,
Daylight = 6,
WaterLevel = 7
}
SELECT * FROM TelemetryStream
SELECT SYSTEM.TIMESTAMP as TimeValue, DEVICEID, Type, AVG(MainValue) as MainValue
FROM TelemetryStream
GROUP BY DeviceId, Type, TUMBLINGWINDOW(minute, 1)
WHERE Type = 1
SELECT SYSTEM.TIMESTAMP as TimeValue, ts.DEVICEID, ts.Type, sl.Name as SensorName, AVG(ts.MainValue) as MainValue
FROM TelemetryStream ts INNER JOIN SensorList sl on sl.SensorId = ts.DeviceId
WHERE ts.Type = 0
GROUP BY ts.DeviceId, ts.Type, sl.Name, TUMBLINGWINDOW(minute, 1)
CREATE TABLE TelemetryStream (
DeviceId nvarchar(max),
Type bigint,
MainValue float,
Timestamp datetime
);
SELECT SYSTEM.TIMESTAMP as TimeValue, ts.DEVICEID, ts.Type, sl.Name as SensorName, AVG(ts.MainValue) as MainValue
FROM TelemetryStream ts INNER JOIN SensorList sl on sl.SensorId = ts.DeviceId
WHERE ts.Type = 0
GROUP BY ts.DeviceId, ts.Type, sl.Name, TUMBLINGWINDOW(minute, 1)
We can make this file beautiful and searchable if this error is corrected: No commas found in this CSV file in line 0.
SensorId;SensorTypeId;Name;Location
Device1;0;Outside;Home
Device2;0;Bedroom;Home
Device3;0;Kitchen;Home
Device4;0;Living;Home
Device5;0;Lounge;Home
Device6;0;Office;Home
Device7;1;Bathroom;Home
Device8;0;Cellar;Home
Device9;0;Hallway;Home
Device10;1;Attic;Home
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment