static void Main(string[] args)
        {
            var count = 0;

            var configM = new ConfigM { ApiUrl = ConfigurationManager.AppSettings["ConfigM"] };

            var profileManifest = configM.GetByName("ProfileM");
            var refManifest = configM.GetByName("RefM");

            var profileM = new ProfileM {ApiUrl = profileManifest.lineitems[LineitemsKey.PublicAPI]};
            var participants = profileM.GetAllByType(UserType.Participant);

            var refM = new RefM {ApiUrl = refManifest.lineitems[LineitemsKey.PublicAPI]};
            var products = refM.GetAllByDomain("Products");
            var models = refM.GetAllByDomain("Models");

            // create  a device registration for each particpant
            foreach (var p in participants)
            {
                var r = new Registration();

                foreach (var m in models)
                {
                    if (m.code == "BIOMAX-HOME")
                    {
                        r.key = string.Empty;
                        r.productline = products[0].code;
                        r.model = m.code;
                        r.version = m.attributes["version"];
                        r.firmwareversion = m.attributes["firmware"];
                    }
                }

                r.participantid = p.id;

                var json = ModelManager.ModelToJson<Registration>(r);
                var filename = AppDomain.CurrentDomain.BaseDirectory + @"data\deviceregistration" + count + ".json";
                System.IO.File.WriteAllText(filename, json);

                Console.WriteLine("Device Registration " + count + " generated");
                count++;
            }
        }
        static void Main(string[] args)
        {
            Console.WriteLine("************************************************************");
            Console.WriteLine("*  B I O M A X  S E N S O R  E V E N T  G E N E R A T O R  *");
            Console.WriteLine("*                                                          *");
            Console.WriteLine("*               I O T  H U B  E D I T I O N                *");
            Console.WriteLine("************************************************************");
            Console.WriteLine();
            Console.WriteLine("Press Enter to start the generator.");
            Console.WriteLine("Press Ctrl-C to stop the generator.");
            Console.WriteLine();
            Console.ReadLine();
            Console.Write("Working....");

            // initialize the ConfigM microservice client sdk
            _configM = new ConfigM {ApiUrl = ConfigurationManager.AppSettings["ConfigM"]};

            // lookup the manifests for the devie registry and user profile microservices
            var deviceManifest = _configM.GetByName("DeviceM");
            var profileManifest = _configM.GetByName("ProfileM");

            // initialize the DeviceM microservice client sdk
            _registryM = new DeviceM {ApiUrl = deviceManifest.lineitems[LineitemsKey.AdminAPI]};

            // initialize the ProfileM microservice client sdk
            _profilesM = new ProfileM {ApiUrl = profileManifest.lineitems[LineitemsKey.PublicAPI]};

            // get the device registry from the device microservice
            _devices = _registryM.GetAll();

            // get all the participants in the study
            _profiles = _profilesM.GetAllByType("Participant");

            // send simluated messages from the device collection
            SendDeviceToCloudMessagesAsync();

            Console.ReadLine();
        }
        static void Main(string[] args)
        {
            // initialize the configM microservice client sdk
            _configM = new ConfigM {ApiUrl = ConfigurationManager.AppSettings["ConfigM"]};

            // lookup the manifest for the device microservice
            var deviceManifest = _configM.GetByName("DeviceM");

            // initialize the device registery microservice client SDK
            _registryM = new DeviceM {ApiUrl = deviceManifest.lineitems[LineitemsKey.AdminAPI]};

            // get a list of all devices from the registry
            _devices = _registryM.GetAll();

            // initialize the IoT Hub registration manager
            _registryManager = RegistryManager.CreateFromConnectionString(ConfigurationManager.AppSettings["IoTHubConnStr"]);

            // register each device with IoT Hub
            foreach (var device in _devices.list)
            {
                AddDeviceAsync(device).Wait();
            }
        }
        static void Main(string[] args)
        {
            Console.WriteLine("************************************************************");
            Console.WriteLine("*  B I O M A X  S E N S O R  E V E N T  G E N E R A T O R  *");
            Console.WriteLine("************************************************************");
            Console.WriteLine();
            Console.WriteLine("Press Enter to start the generator.");
            Console.WriteLine("Press Ctrl-C to stop the generator.");
            Console.WriteLine();
            Console.ReadLine();
            Console.Write("Working....");

            _configM = new ConfigM();
            _registryM = new DeviceM();
            _profilesM = new ProfileM();

            // the endpoiont for ConfigM is defined in the app config
            _configM.ApiUrl = ConfigurationManager.AppSettings["ConfigM"];

            var deviceManifest = _configM.GetByName("DeviceM");
            var profileManifest = _configM.GetByName("ProfileM");

            _registryM.ApiUrl = deviceManifest.lineitems[LineitemsKey.AdminAPI];
            _profilesM.ApiUrl = profileManifest.lineitems[LineitemsKey.PublicAPI];

            // get the device registry from the device microservice
            _devices = _registryM.GetAll();

            // get all the participants in the study
            _profiles = _profilesM.GetAllByType("Participant");

            var random = new Random();
            var spin = new ConsoleSpiner();

            // connect to Event Hub
            var servicebus = ConfigurationManager.AppSettings["Microsoft.ServiceBus.ConnectionString"];
            var eventhub = ConfigurationManager.AppSettings["EventHub"];
            var eventHubClient = EventHubClient.CreateFromConnectionString(servicebus, eventhub);

            while (true)
            {
                spin.Turn();

                try
                {
                    var deviceReading = new DeviceMessage();
                    var index = random.Next(0, _devices.list.Count - 1);
                    
                    // randomly select a device from the registry
                    var device = _devices.list[index];

                    // lookup the participant
                    var participant = _profiles.Find(p => p.id == device.participantid);

                    // beging to create the simulated device message
                    deviceReading.deviceid = device.id;
                    deviceReading.participantid = participant.id;
                    deviceReading.location.latitude = participant.location.latitude;
                    deviceReading.location.longitude = participant.location.longitude;

                    // generate simulated sensor reaings
                    var glucose = new SensorReading
                    {
                        type = SensorType.Glucose,
                        value = random.Next(70, 210)
                    };

                    var heartrate = new SensorReading
                    {
                        type = SensorType.Heartrate,
                        value = random.Next(60, 180)
                    };

                    var temperature = new SensorReading
                    {
                        type = SensorType.Temperature,
                        value = random.Next(98, 105) + (.1 * random.Next(0, 9))
                    };

                    var bloodoxygen = new SensorReading
                    {
                        type = SensorType.Bloodoxygen,
                        value = random.Next(80, 100)
                    };

                    deviceReading.sensors.Add(glucose);
                    deviceReading.sensors.Add(heartrate);
                    deviceReading.sensors.Add(temperature);
                    deviceReading.sensors.Add(bloodoxygen);

                    deviceReading.reading = DateTime.Now;

                    // serialize the message to JSON
                    var json = ModelManager.ModelToJson<DeviceMessage>(deviceReading);
                    
                    // use these lines to gen JSON files for SA test input
                    //var filename = AppDomain.CurrentDomain.BaseDirectory + @"\data\device-" + DateTime.Now.Ticks + ".json";
                    //System.IO.File.WriteAllText(filename, json);

                    // send the message to EventHub
                    eventHubClient.SendAsync(new EventData(Encoding.UTF8.GetBytes(json)));
                }
                catch (Exception exception)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("{0} > Exception: {1}", DateTime.Now, exception.Message);
                    Console.ResetColor();
                }

                Thread.Sleep(200);
            }
        }
        public Task OpenAsync(PartitionContext context)
        {
            // get the manifests for the profile and biometrics services
            _config = new ConfigM { ApiUrl = RoleEnvironment.GetConfigurationSettingValue("ConfigM") };

            var profileManifest = _config.GetByName("ProfileM");
            _profile = new ProfileM { ApiUrl = profileManifest.lineitems["PublicAPI"] };

            var biometricsManifest = _config.GetByName("BiometricsAPI");
            _biometricsApi = biometricsManifest.lineitems["PublicAPI"] + "/alarm";

            // connect to notification hub
            _hub = NotificationHubClient.CreateClientFromConnectionString(
                RoleEnvironment.GetConfigurationSettingValue("NotificationHubConnectionString"),
                RoleEnvironment.GetConfigurationSettingValue("NotificationHubName"));

            return Task.FromResult<object>(null);
        }