public Registrations GetByModel(string model)
        {
            Registrations devices = new Registrations();

            try
            {
                var query = "SELECT * FROM DeviceModel d WHERE d.model='" + model + "'";
                var deviceList = this._persist.SelectByQuery<Registration>(query);

                if (deviceList != null)
                {
                    foreach(var d in deviceList)
                    {
                        devices.list.Add(d);
                    }
                }
                else
                {
                    throw new Exception(Errors.ERR_DEVICEM_PROFILE_NOT_FOUND);
                }
            }
            catch (Exception err)
            {
                throw new Exception(Errors.ERR_DEVICEM_BADREQUEST, err);
            }

            return devices;
        }
        public Registrations GetAll()
        {
            Registrations devices = new Registrations();

            try
            {
                List<Registration> deviceList = this._persist.SelectAll<Registration>();
                foreach (Registration d in deviceList)
                {
                    devices.list.Add(d);
                }
                this._persist.InsertCache(devices);
            }
            catch (Exception err)
            {
                throw new Exception(Errors.ERR_DEVICEM_BAGREQUEST, err);
            }

            return devices;
        }
        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);
            }
        }