public async Task <IActionResult> Get([FromQuery] DateTime start, [FromQuery] DateTime end, [FromQuery] string deviceName = null, [FromQuery] string deviceAddress = null)
        {
            IEnumerable <ShellTemp> shellTemps = await _shellTempRepository.GetShellTemperatureData(start, end, deviceName, deviceAddress);

            ShellTemp[] temps = shellTemps as ShellTemp[] ?? shellTemps.ToArray();
            temps = temps.OrderBy(date => date.RecordedDateTime).ToArray();
            return(Ok(temps));
        }
        public LiveWifiAndBluetoothShellDataViewModel(IBluetoothFinder bluetoothFinder,
                                                      IShellTemperatureRepository <ShellTemp> shellTemperatureRepository,
                                                      IShellTemperatureRepository <SdCardShellTemp> sdCardShellTemperatureRepository,
                                                      IDeviceRepository <DeviceInfo> deviceRepository,
                                                      IConfiguration configuration,
                                                      BluetoothConnectionSubject subject,
                                                      TemperatureSubject temperatureSubject,
                                                      ILogger <LiveBluetoothOnlyShellDataViewModel> logger,
                                                      OutlierDetector outlierDetector,
                                                      ClearList clear,
                                                      IRepository <ShellTemperatureComment> commentRepository,
                                                      IReadingCommentRepository <ReadingComment> readingCommentRepository,
                                                      IRepository <Positions> positionRepository,
                                                      IRepository <ShellTemperaturePosition> shellTempPositionRepository,
                                                      IRepository <SdCardShellTemperatureComment> sdCardCommentRepository)
            : base(bluetoothFinder, shellTemperatureRepository, sdCardShellTemperatureRepository, deviceRepository, configuration, subject,
                   temperatureSubject, logger, outlierDetector, clear, commentRepository, readingCommentRepository,
                   positionRepository, shellTempPositionRepository, sdCardCommentRepository)
        {
            _shellTemperatureRepository = shellTemperatureRepository;

            IList <DeviceInfo> potentialWifiDevices = FindPotentialWifiDevices();

            foreach (DeviceInfo device in potentialWifiDevices.ToList())
            {
                DateTime start = DateTime.Now.AddMinutes(-2);
                DateTime end   = DateTime.Now;

                WifiDevice wifiDevice = new WifiDevice(device.DeviceName, device.DeviceAddress, start);

                IEnumerable <ShellTemp> shellTemps = _shellTemperatureRepository.GetShellTemperatureData(start, end,
                                                                                                         device.DeviceName, device.DeviceAddress);
                ShellTemp[] dataReadings = shellTemps as ShellTemp[] ?? shellTemps.ToArray();

                if (dataReadings.Length == 0)
                {
                    potentialWifiDevices.Remove(device);
                }
                else
                {
                    SetWifiDeviceDataReadings(wifiDevice, dataReadings);
                    SetWifiDeviceDataPoints(wifiDevice);
                    InstantiateNewDevice(wifiDevice);

                    wifiDevice.State.Message = "Connected To - " + wifiDevice.DeviceName;
                }
            }

            SetSelectedDeviceWhenNull();
            if (Devices.Count == 1) // only one in devices so, must be selected
            {
                SetConnectionStatus(SelectedDevice, DeviceConnectionStatus.CONNECTED);
            }

            SetCanRemoveDevices();
        }
        public async Task <IEnumerable <ShellTemp> > GetShellTemperatureData(DateTime start, DateTime end, string deviceName = null, string deviceAddress = null)
        {
            IEnumerable <ShellTemp> temps = _shellTemperatureRepository.GetShellTemperatureData(start, end, deviceName, deviceAddress);

            return(await Task.FromResult(temps));
        }
        /// <summary>
        /// Get all the live and sd card shell temperatures along with
        /// the comments and positions
        /// </summary>
        /// <returns></returns>
        public ShellTemperatureRecord[] GetShellTemperatureRecords(DateTime start, DateTime end, DeviceInfo deviceInfo = null)
        {
            ShellTemp[]       tempData;
            SdCardShellTemp[] sdCardShellTemps;

            // Has device information, user selected device
            if (deviceInfo != null)
            {
                // Get live data and live data comments and positions
                tempData = _shellTemperatureRepository.GetShellTemperatureData(start, end,
                                                                               deviceInfo.DeviceName, deviceInfo.DeviceAddress).ToArray();

                // Get SD Card data and SD card data comments
                sdCardShellTemps = _sdCardShellTemperatureRepository.GetShellTemperatureData(start, end,
                                                                                             deviceInfo.DeviceName, deviceInfo.DeviceAddress).ToArray();
            }
            else // No device information, just use dates
            {
                // Get live data and live data comments and positions
                tempData = _shellTemperatureRepository.GetShellTemperatureData(start, end).ToArray();

                // Get SD Card data and SD card data comments
                sdCardShellTemps = _sdCardShellTemperatureRepository.GetShellTemperatureData(start, end).ToArray();
            }

            // Get the live comments
            ShellTemperatureComment[] liveDataComments = _commentRepository.GetAll()
                                                         .Where(x => x.ShellTemp.RecordedDateTime >= start &&
                                                                x.ShellTemp.RecordedDateTime <= end)
                                                         .ToArray();

            // Get the live positions
            ShellTemperaturePosition[] positions = _shellTemperaturePositionRepository.GetAll().ToArray();

            // Sd card comments
            SdCardShellTemperatureComment[] sdCardComments = _sdCardCommentRepository.GetAll().ToArray();


            // Create new temp list of records
            List <ShellTemperatureRecord> records = new List <ShellTemperatureRecord>();

            // For ever item in ShellTemps, find and match the comment that may have been made
            foreach (ShellTemp shellTemp in tempData)
            {
                ShellTemperatureRecord shellTemperatureRecord =
                    new ShellTemperatureRecord(shellTemp.Id, shellTemp.Temperature, shellTemp.RecordedDateTime,
                                               shellTemp.Latitude, shellTemp.Longitude, shellTemp.Device, false); // Not from SD

                // Find the comment for the shell temperature
                ShellTemperatureComment comment =
                    liveDataComments.FirstOrDefault(x => x.ShellTemp.Id == shellTemperatureRecord.Id);

                ShellTemperaturePosition position =
                    positions.FirstOrDefault(x => x.ShellTemp.Id == shellTemperatureRecord.Id);

                if (comment?.Comment != null)
                {
                    shellTemperatureRecord.Comment = comment.Comment.Comment;
                }
                if (position?.Position != null)
                {
                    shellTemperatureRecord.Position = position.Position.Position;
                }

                records.Add(shellTemperatureRecord);
            }

            // Find the sd card data shell temps
            foreach (SdCardShellTemp shellTemp in sdCardShellTemps)
            {
                if (!shellTemp.RecordedDateTime.HasValue) // Doesn't have DateTime, skip
                {
                    continue;
                }

                ShellTemperatureRecord shellTemperatureRecord =
                    new ShellTemperatureRecord(shellTemp.Id, shellTemp.Temperature, (DateTime)shellTemp.RecordedDateTime,
                                               shellTemp.Latitude, shellTemp.Longitude, shellTemp.Device, true); // This is from SD

                // Find the comment for the sd card shell temperature
                SdCardShellTemperatureComment temp =
                    sdCardComments.FirstOrDefault(x => x.SdCardShellTemp.Id == shellTemperatureRecord.Id);

                if (temp?.Comment != null)
                {
                    shellTemperatureRecord.Comment = temp.Comment.Comment;
                }

                records.Add(shellTemperatureRecord);
            }

            return(records.ToArray());
        }