public override Task <DeviceDataArray> GetBulkDeviceData(DeviceDataRequest request, ServerCallContext context)
        {
            var response = new DeviceDataArray();

            response.Items.AddRange(_dataService.GetReadings(request.DataIterations));
            return(Task.FromResult(response));
        }
        public override Task <DeviceDataArray> GetProgramResults(ProgramPageRequest request, ServerCallContext context)
        {
            List <DeviceDataModel> modelList = new List <DeviceDataModel>();

            var    response = new DeviceDataArray();
            string filename = request.RunAccelerometer ? AccelerometerOnly.FileName : GyroscopeOnly.FileName;

            using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
            {
                _logger.LogInformation($"Request: {request}");
                long startIndex = request.DataSetStart * request.SegmentSize;
                long endIndex   = startIndex + (request.Rows * request.SegmentSize);
                long rows       = request.Rows;

                _logger.LogInformation($"FileStream size: {fs.Length}. Start Index: {startIndex}. End Index: {endIndex}");
                if (startIndex > fs.Length)
                {
                    throw new IndexOutOfRangeException("Requested starting index is larger than the file's size.");
                }

                if (endIndex > fs.Length)
                {
                    long bytesLeft = fs.Length - startIndex;
                    rows = (bytesLeft / request.SegmentSize);
                }

                fs.Seek(startIndex, SeekOrigin.Begin);

                for (long i = 0; i < rows; i++)
                {
                    byte[] bytes = new byte[request.SegmentSize];

                    fs.Read(bytes, 0, request.SegmentSize);

                    DeviceDataModel model = new DeviceDataModel();

                    if (request.RunAccelerometer)
                    {
                        int[] accelData = System.Runtime.InteropServices.MemoryMarshal.Cast <byte, int>(bytes).ToArray();

                        model.AccelData.Add(accelData);
                    }
                    else if (request.RunGyroscope)
                    {
                        int[] gyroData = System.Runtime.InteropServices.MemoryMarshal.Cast <byte, int>(bytes).ToArray();

                        model.GyroData.Add(gyroData);
                    }

                    modelList.Add(model);
                }
            }

            response.Items.AddRange(modelList);

            return(Task.FromResult(response));
        }
        public override Task <DeviceDataArray> GetFullSystemResults(ProgramPageRequest request, ServerCallContext context)
        {
            List <DeviceDataModel> modelList = new List <DeviceDataModel>();

            var    response = new DeviceDataArray();
            string filename = request.RunAccelerometer ? FullSystemSharedRTC.TestAccelFile : FullSystemSharedRTC.TestGyroFile;

            using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
            {
                long startIndex = request.DataSetStart * request.SegmentSize;
                long endIndex   = startIndex + (request.Rows * request.SegmentSize);
                long rows       = request.Rows;


                if (startIndex > fs.Length)
                {
                    _logger.LogInformation($"Request: {request}");
                    _logger.LogInformation($"FileStream size: {fs.Length}. Start Index: {startIndex}. End Index: {endIndex}");
                    throw new IndexOutOfRangeException("Requested starting index is larger than the file's size.");
                }

                if (endIndex > fs.Length)
                {
                    long bytesLeft = fs.Length - startIndex;
                    rows = (bytesLeft / request.SegmentSize);
                }

                fs.Seek(startIndex, SeekOrigin.Begin);

                for (long i = 0; i < rows; i++)
                {
                    byte[] bytes = new byte[request.SegmentSize];

                    fs.Read(bytes, 0, request.SegmentSize);

                    DeviceDataModel model      = new DeviceDataModel();
                    const int       accelBytes = 12;
                    const int       gyroBytes  = 20;
                    const int       rtcBytes   = 8;
                    const int       cpuBytes   = 8;

                    if (request.RunAccelerometer)
                    {
                        Span <byte> data         = bytes;
                        Span <byte> accelSegment = data.Slice(0, accelBytes);
                        Span <byte> rtcSegment   = data.Slice(accelBytes, rtcBytes);
                        Span <byte> cpuSegment   = data.Slice(accelBytes + rtcBytes, cpuBytes);
                        model.AccelData.Add(System.Runtime.InteropServices.MemoryMarshal.Cast <byte, int>(accelSegment).ToArray());
                        model.TransactionTime = BitConverter.ToInt64(rtcSegment);
                        model.CpuTemp         = BitConverter.ToDouble(cpuSegment);
                    }
                    else if (request.RunGyroscope)
                    {
                        Span <byte> data        = bytes;
                        Span <byte> gyroSegment = data.Slice(0, gyroBytes);
                        Span <byte> rtcSegment  = data.Slice(gyroBytes, rtcBytes);
                        Span <byte> cpuSegment  = data.Slice(gyroBytes + rtcBytes, cpuBytes);
                        model.GyroData.Add(System.Runtime.InteropServices.MemoryMarshal.Cast <byte, int>(gyroSegment).ToArray());
                        model.TransactionTime = BitConverter.ToInt64(rtcSegment);
                        model.CpuTemp         = BitConverter.ToDouble(cpuSegment);
                    }

                    modelList.Add(model);
                }
            }

            response.Items.AddRange(modelList);

            return(Task.FromResult(response));
        }