예제 #1
0
        public async Task <ActionResult> GetCalibrationStatus(string accesstoken, string uuid)
        {
            if (!await this.checkUploadPermissions(accesstoken))
            {
                return(this.Error("GetStatus Denied"));
            }

            if (string.IsNullOrWhiteSpace(uuid))
            {
                return(this.Error("GetStatus Denied: invalid parameter uuid"));
            }

            LibreCalibrationModel cal;

            try
            {
                cal = await MongoConnection.GetCalibration(uuid);
            }
            catch (Exception ex)
            {
                return(Error("GetCalibrationStatus Failed: invalid uuid? " + ex.Message));
            }

            var requests = cal.requestids;

            if (requests.Count() < 4)
            {
                return(this.Error("GetCalibrationStatus Failed: calibrationrequest was malformed, aborting"));
            }
            var footerCRCS = cal.isValidForFooterWithReverseCRCs;

            LibreReadingModel reqb1;
            LibreReadingModel reqb2;

            LibreReadingModel reqf1;
            LibreReadingModel reqf2;

            try
            {
                reqb1 = await MongoConnection.GetRemoteReading(requests[0]);

                reqb2 = await MongoConnection.GetRemoteReading(requests[1]);

                reqf1 = await MongoConnection.GetRemoteReading(requests[2]);

                reqf2 = await MongoConnection.GetRemoteReading(requests[3]);
            }
            catch (Exception ex)
            {
                return(Error("GetCalibrationStatus Failed: could not get remote readings: " + ex.Message));
            }

            if (reqb1 == null || reqb2 == null || reqf1 == null || reqf2 == null)
            {
                return(Error("GetCalibrationStatus Failed: could not get remote readings"));
            }
            if (reqb1.status != "complete" || reqb2.status != "complete" || reqf1.status != "complete" || reqf2.status != "complete")
            {
                return(Success <CalibrationResult>(new CalibrationResult {
                    status = "not-ready", uuid = uuid
                }, "GetCalibrationStatus"));
            }



            var requestThresholds = cal.metadata;
            var responseb1        = LibreReadingUtil.ReadingResultToNumber(reqb1.result);
            var responseb2        = LibreReadingUtil.ReadingResultToNumber(reqb2.result);

            var responsef1 = LibreReadingUtil.ReadingResultToNumber(reqf1.result);
            var responsef2 = LibreReadingUtil.ReadingResultToNumber(reqf2.result);

            var slope1  = (responseb2 - responseb1) / (requestThresholds.GLUCOSE_UPPER_BOUND - requestThresholds.GLUCOSE_LOWER_BOUND);
            var offset1 = responseb2 - (requestThresholds.GLUCOSE_UPPER_BOUND * slope1);

            var slope2  = (responsef2 - responsef1) / (requestThresholds.GLUCOSE_UPPER_BOUND - requestThresholds.GLUCOSE_LOWER_BOUND);
            var offset2 = responsef2 - (requestThresholds.GLUCOSE_UPPER_BOUND * slope2);

            var slope_slope  = (slope1 - slope2) / (requestThresholds.RAW_TEMP1 - requestThresholds.RAW_TEMP2);
            var offset_slope = slope1 - (slope_slope * requestThresholds.RAW_TEMP1);

            var slope_offset  = (offset1 - offset2) / (requestThresholds.RAW_TEMP1 - requestThresholds.RAW_TEMP2);
            var offset_offset = offset2 - (slope_offset * requestThresholds.RAW_TEMP2);

            var result = new CalibrationResult
            {
                offset_offset = offset_offset,
                offset_slope  = offset_slope,
                slope_offset  = slope_offset,
                slope_slope   = slope_slope,

                status = "complete",
                uuid   = uuid,
                isValidForFooterWithReverseCRCs = footerCRCS
            };

            return(Success <CalibrationResult>(result, "GetCalibrationStatus"));

            //var content = $"accesstoken: {accesstoken}, uuid: {uuid}, reading: {reading}";
            //return Content("GetStatus IS NOT IMPLEMENTED YET:" + content);
        }