public IHttpActionResult AddAMSDataSet([FromBody]DataPoint[] dataSet)
        {
            var db = new AirUDBCOE();

            db.DataPoints.AddRange(dataSet);

            db.SaveChanges();

            return Ok(dataSet);
        }
        public IHttpActionResult UpdateAMSDeviceState([FromBody]DeviceState[] states)
        {
            var db = new AirUDBCOE();
            string deviceID = states[0].DeviceID;
            Device device = db.Devices.SingleOrDefault(x => x.DeviceID == deviceID);

            if (device == null)
            {
                // Failed to add DeviceState.
                return Ok("Failed to add device state with Device with ID = " + states[0].DeviceID + " not found.");                
            }

            db.DeviceStates.AddRange(states);

            db.SaveChanges();

            // Success.
            return Ok(states);
        }
        public IHttpActionResult Delete(string id)
        {
            AirUDBCOE db = new AirUDBCOE();

            // Validate Device from given DeviceId exists.
            Device registeredDevice = db.Devices.SingleOrDefault(x => x.DeviceID == id);

            if (registeredDevice != null)
            {
                Device toDelete = (from dev in db.Devices
                                   where dev.DeviceID == id
                                   select dev).Single();

                db.Devices.Remove(toDelete);
                db.SaveChanges();

                return Ok("Delete Successful");
            }
            else
            {
                // Device with DeviceID: <deviceID> does not exist.
                return NotFound();
            }
        }
        public IHttpActionResult UpdateUserDeviceState([FromBody]SwaggerDeviceState state)
        {
            var db = new AirUDBCOE();
            
            // Validate Device from given DeviceId exists.
            Device registeredDevice = db.Devices.SingleOrDefault(x => x.DeviceID == state.Id);

            if (registeredDevice != null)
            {
                // Request previous state from database based on state.DeviceID
                DeviceState previousState = (
                                    from device in db.DeviceStates
                                    where device.DeviceID == state.Id
                                    && device.StateTime <= DateTime.Now // **May be a future source of contention - REVIEW**
                                    group device by device.DeviceID into deviceIDGroup
                                    select new
                                    {
                                        DeviceID = deviceIDGroup.Key,
                                        MaxMeasurementTime = deviceIDGroup.Max(device => device.StateTime)
                                    } into MaxStates
                                    join coordinates in db.DeviceStates
                                                            on MaxStates.MaxMeasurementTime equals coordinates.StateTime into latestStateGroup
                                    select latestStateGroup.FirstOrDefault()).Single();

                // Inherit lat and long from previous state

                DeviceState newDeviceState = new DeviceState();
                newDeviceState.Device = previousState.Device;
                newDeviceState.DeviceID = state.Id;
                newDeviceState.InOrOut = state.Indoor;
                newDeviceState.StatePrivacy = state.Privacy;
                newDeviceState.Lat = previousState.Lat;
                newDeviceState.Long = previousState.Long;
                newDeviceState.StateTime = DateTime.Now;
                db.DeviceStates.Add(newDeviceState);
                db.SaveChanges();

                registeredDevice.Name = state.Name;
                registeredDevice.Purpose = state.Purpose;

                //db.Devices.Add(registeredDevice);
                db.SaveChanges();

                // Send user newly updated state back to user
                return Ok(state);
            }
            else
            {
                // Device with DeviceID: <deviceID> does not exist.
                return NotFound();
            }
        }
        public IHttpActionResult RegisterUserDevice([FromBody]SwaggerDeviceState newDeviceState)
        {
            var db = new AirUDBCOE();

            Device existingDevice = db.Devices.SingleOrDefault(x => x.DeviceID == newDeviceState.Id);
            if (existingDevice == null)
            {
                // Add device success.
                Device device = new Device();
                device.Name = newDeviceState.Name;
                device.DeviceID = newDeviceState.Id;
                device.Email = "*****@*****.**"; // newDeviceAndState.Email;
                device.DevicePrivacy = newDeviceState.Privacy;
                device.Purpose = newDeviceState.Purpose;
                db.Devices.Add(device);
                db.SaveChanges();

                DeviceState state = new DeviceState();
                state.Device = device;
                state.DeviceID = newDeviceState.Id;
                state.InOrOut = newDeviceState.Indoor;
                state.StatePrivacy = newDeviceState.Privacy;
                state.StateTime = new DateTime(1900, 1, 1);
                state.Long = 0.0m;
                state.Lat = 90.0m;
                db.DeviceStates.Add(state);
                db.SaveChanges();

                return Ok(newDeviceState);
            }
            else
            {
                // Add device fail.
                return BadRequest("Existing Device");
            }
        }
        public IHttpActionResult UserRegistration([FromBody]SwaggerUser user)
        {
            var db = new AirUDBCOE();

            User existingUser = db.Users.SingleOrDefault(x => x.Email == user.email);

            if (existingUser == null)
            {
                // Perform queries to insert new user into database.
                User newUser = new User();
                newUser.Email = user.email;
                newUser.Pass = user.pass;

                db.Users.Add(newUser);
                db.SaveChanges();

                // Account register success.
                return Ok("Account registration successful! Welcome, " + user.email);
            }
            else
            {
                // Account register failed. Account with email address: '<user.Email>' already exists. Please try a different email address.
                return BadRequest("Account registration failed! Account with email address: " + 
                                                                             user.email + 
                                                                             " already exists. Please try a different email address.");
            }
        }