private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            sasClient = new EventHubSasClient(string.Empty, serviceNamespace, hubName, "");

            EventListener informationListener = new StorageFileEventListener("MyListenerInformation");

            informationListener.EnableEvents(MetroEventSource.Log, EventLevel.Informational);

            MetroEventSource.Log.Info("Tracking Started");

            DeviceController.DoWork(MetroEventSource.Log, sasClient, x => { LabelMessage.Text = x + LabelMessage.Text; });
        }
예제 #2
0
        public static async void DoWork(MetroEventSource logger, EventHubSasClient eventHub, Action <string> action)
        {
            Random MyRandom = new Random();

            List <Task <HttpResponseMessage> > tasks = new List <Task <HttpResponseMessage> >();

            //Création d'une variable liste pour stocker mes objets
            List <Device> MyDevices = new List <Device>();

            List <KeyValuePair <string, string> > listKeys = new List <KeyValuePair <string, string> >();

            listKeys.Add(new KeyValuePair <string, string>("device1", "SharedAccessSignature sr=https%3a%2f%2feumariothub.servicebus.windows.net%2fdevicemonitoring%2fpublishers%2f1%2fmessages&sig=Insert your key&se=1460220109&skn=Default"));
            listKeys.Add(new KeyValuePair <string, string>("device2", "SharedAccessSignature sr=https%3a%2f%2feumariothub.servicebus.windows.net%2fdevicemonitoring%2fpublishers%2f2%2fmessages&sig=Insert your key&se=1465172326&skn=Default"));
            listKeys.Add(new KeyValuePair <string, string>("device3", "SharedAccessSignature sr=https%3a%2f%2feumariothub.servicebus.windows.net%2fdevicemonitoring%2fpublishers%2f3%2fmessages&sig=Insert your key&se=1465172448&skn=Default"));
            listKeys.Add(new KeyValuePair <string, string>("device4", "SharedAccessSignature sr=https%3a%2f%2feumariothub.servicebus.windows.net%2fdevicemonitoring%2fpublishers%2f4%2fmessages&sig=Insert your key&se=1465172404&skn=Default"));
            listKeys.Add(new KeyValuePair <string, string>("device5", "SharedAccessSignature sr=https%3a%2f%2feumariothub.servicebus.windows.net%2fdevicemonitoring%2fpublishers%2f5%2fmessages&sig=Insert your key&se=1465172478&skn=Default"));

            int count = 0;

            while (count < 50000)
            {
                try
                {
                    count++;

                    MyDevices.Clear();

                    MyDevices.Add(new Device(1, MyRandom.Next(0, 25), MyRandom.Next(0, 600), MyRandom.Next(0, 10), MyRandom.Next(0, 35), MyRandom.Next(0, 359), MyRandom.Next(0, 359), MyRandom.Next(0, 500), MyRandom.Next(0, 2000), MyRandom.Next(0, 2000), MyRandom.Next(-180, 180), MyRandom.Next(-90, 90), "Tunis"));
                    //MyDevices.Add(new Device(2, MyRandom.Next(0, 25), MyRandom.Next(0, 600), MyRandom.Next(0, 10), MyRandom.Next(0, 35), MyRandom.Next(0, 359), MyRandom.Next(0, 359), MyRandom.Next(0, 500), MyRandom.Next(0, 2000), MyRandom.Next(0, 2000), MyRandom.Next(-180, 180), MyRandom.Next(-90, 90), "France"));
                    //MyDevices.Add(new Device(3, MyRandom.Next(0, 25), MyRandom.Next(0, 600), MyRandom.Next(0, 10), MyRandom.Next(0, 35), MyRandom.Next(0, 359), MyRandom.Next(0, 359), MyRandom.Next(0, 500), MyRandom.Next(0, 2000), MyRandom.Next(0, 2000), MyRandom.Next(-180, 180), MyRandom.Next(-90, 90), "Algerie"));
                    //MyDevices.Add(new Device(4, MyRandom.Next(0, 25), MyRandom.Next(0, 600), MyRandom.Next(0, 10), MyRandom.Next(0, 35), MyRandom.Next(0, 359), MyRandom.Next(0, 359), MyRandom.Next(0, 500), MyRandom.Next(0, 2000), MyRandom.Next(0, 2000), MyRandom.Next(-180, 180), MyRandom.Next(-90, 90), "Sousse"));
                    //MyDevices.Add(new Device(5, MyRandom.Next(0, 25), MyRandom.Next(0, 600), MyRandom.Next(0, 10), MyRandom.Next(0, 35), MyRandom.Next(0, 359), MyRandom.Next(0, 359), MyRandom.Next(0, 500), MyRandom.Next(0, 2000), MyRandom.Next(0, 2000), MyRandom.Next(-180, 180), MyRandom.Next(-90, 90), "Allemagne"));

                    for (int j = 0; j < 1; j++)
                    {
                        var Device = MyDevices[j];
                        // Serialize to JSON
                        var serializedString = JsonConvert.SerializeObject(Device);
                        var key = listKeys[j].Value;

                        Device.Time                    = DateTime.Now;
                        eventHub.DeviceName            = Device.DeviceID.ToString();
                        eventHub.SharedAccessSignature = key;

                        Task <HttpResponseMessage> lastTask = eventHub.SendMessageAsync(serializedString);

                        await lastTask;
                        tasks.Add(lastTask);

                        logger.Info(serializedString);

                        while (!lastTask.IsCompleted)
                        {
                            System.Threading.Tasks.Task.Delay(0).Wait();
                        }
                        try
                        {
                            HttpResponseMessage response = lastTask.Result;

                            string result = await response.Content.ReadAsStringAsync();

                            logger.Info(result);

                            action(response.StatusCode.ToString() + "/n" + DateTime.Now.ToString("hh:mm:ss mmm"));
                        }
                        catch (Exception ex)
                        {
                            logger.Error(ex.Message);
                        }
                        //client.SendAsync(data);
                    }
                }
                catch (Exception ex) {
                    try
                    {
                        System.Diagnostics.Debug.Write(ex.Message);
                        logger.Error(ex.Message);
                    }
                    catch
                    {
                    }
                }
            }
        }