/// <summary> /// parse data to populate fields and start listening to device /// </summary> /// <param name="e"></param> protected override void OnNavigatedTo(NavigationEventArgs e) { hubData = e.Parameter as IoTAccountData; if (hubData != null) { username.Content = hubData.Name; msgManager = new MessageManager(this); foreach (string partition in hubData.EventHubInfo.PartitionIds) { ReceiveLocation(partition); } } }
/// <summary> /// sign in silently first. If that fails, then bring up gui /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void On_Loaded(object sender, RoutedEventArgs e) { List <string> partitionIds = new List <string>(); partitionIds.Add("0"); partitionIds.Add("1"); string entity = "..."; //should start with "iothub-ehub-<hub name>-<random numbers>" string port = "sb://.../"; //should start with "sb://" and end with "sevicebus.windows.net/" string name = "..."; //iothub name string deviceName = "..."; //name of iot device you want to listen to string primaryKey = "..."; EventHubData eventhub = new EventHubData(partitionIds, entity, port, name); IoTAccountData a = new IoTAccountData(null, null, null, null, null, deviceName, "iothubowner", primaryKey, eventhub); //NavigateToMap(a); }
/// <summary> /// Given a token from the logged in user, retrieve the list of all devices registered to that user under any IoT Hub /// </summary> /// <param name="tkn"></param> /// <returns></returns> public static async Task <ICollection <IoTAccountData> > GetAllDevices(string tkn) { ICollection <string> tenants = await GetTenants(tkn); List <IoTAccountData> devices = new List <IoTAccountData>(); string name = AccountManager.GetUserName(); string policy = "iothubowner"; foreach (string ten in tenants) { ICollection <string> subscriptions = await GetSubscription(ten); foreach (string sub in subscriptions) { ICollection <string> resources = await GetResourceGroups(sub); ICollection <IoTConnector.Models.EventHubData> hubs = await GetIoTHubs(sub); foreach (string group in resources) { foreach (EventHubData hub in hubs) { ICollection <string> devs = await GetIoTDevices(sub, group, hub.HubName, policy); foreach (string dev in devs) { string key = await GetPrimaryKey(sub, group, hub.HubName, policy); IoTAccountData account = new IoTAccountData(ten, name, sub, group, hub.HubName, dev, policy, key, hub); devices.Add(account); } } } } } return(devices); }
/// <summary> /// load an instance of MapPage.xaml /// </summary> /// <param name="name"></param> public void NavigateToMap(IoTAccountData test) { this.Frame.Navigate(typeof(MapPage), test); }
/// <summary> /// Receive messages from specified azure iothub on specified partition. The MessageManager parses the received message and displays it accordingly /// </summary> /// <param name="partition"></param> /// <param name="offset"></param> /// <param name="msgman"></param> /// <param name="hubData"></param> /// <returns></returns> public static async Task ReceiveMessages(string partition, DateTime offset, MessageManager msgman, IoTAccountData hubData) { string port = hubData.EventHubInfo.EventHubPort.Replace("sb://", ""); port = port.Replace("/", ""); Address address = new Address(port, 5671, hubData.SharedAccessPolicy, hubData.PrimaryKey, "/", "amqps"); Connection connection = await Connection.Factory.CreateAsync(address); Session session = new Session(connection); string totalMilliseconds = ((long)(offset - new DateTime(StartOfEpoch, DateTimeKind.Utc)).TotalMilliseconds).ToString(); Map filters = new Map(); filters.Add(new Amqp.Types.Symbol("apache.org:selector-filter:string"), new DescribedValue( new Amqp.Types.Symbol("apache.org:selector-filter:string"), "amqp.annotation.x-opt-enqueuedtimeutc > " + totalMilliseconds + "")); ReceiverLink receiver = new ReceiverLink(session, "my-receiver", new global::Amqp.Framing.Source() { Address = hubData.EventHubInfo.EventHubEntity + "/ConsumerGroups/$Default/Partitions/" + partition, FilterSet = filters }, null); Amqp.Types.Symbol deviceIdKey = new Amqp.Types.Symbol("iothub-connection-device-id"); string deviceId = hubData.DeviceName; while (true) { Amqp.Message m = await receiver.ReceiveAsync(10000); if (m != null) { var id = m.MessageAnnotations.Map[deviceIdKey].ToString(); if (id == deviceId) { Data data = (Data)m.BodySection; string msg = System.Text.Encoding.UTF8.GetString(data.Binary, 0, data.Binary.Length); bool isValid = msgman.parseMessage(msg); if (isValid) { receiver.Accept(m); } else { receiver.Release(m); } } } } }
/// <summary> /// Navigate to map page with selected device /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ItemSelected(object sender, ItemClickEventArgs e) { IoTAccountData device = e.ClickedItem as IoTAccountData; this.Frame.Navigate(typeof(MapPage), device); }