Пример #1
0
        public async Task <ActionResult> CreateCalibrationRequestAsync(string accesstoken, string b64contents)
        {
            byte[] decoded    = null;
            double?footerCRCS = null;

            if (!await this.checkUploadPermissions(accesstoken))
            {
                return(this.Error("CreateCalibrationRequestAsync"));
            }
            //testing only
            //b64contents = Convert.ToBase64String(LibreUtils.TestPatchAlwaysReturning63);
            //2018-10-07 patch
            //b64contents = "hNtgFwMAAAAAAAAAAAAAAAAAAAAAAAAA+SIECI4EyPRXAKIEyOxXALUEyORXAMgEyNxXADEEyJBYADEEyIBYADEEyGxYADIEyFxYADUEyFBYADoEyEBYAEEEyDRYAEgEyChYAFIEyCBYAF4EyBRYAG4EyAxYAH4EyABYANwEyIhZAEUEyHRZAJoDyExZAEwDyFBZAEsDyMRZAKUDyOwZgLADyAhaADMEyFxYACgFyKRZAC0FyExaAN0EyCQbgKMEyLxZAAQEyPgagK4DyChaAKwDyMRZAAkEyAhZACAEyGRZAOsDyGBZACQEyJhZAPQDyPwagP0DyPBZABMEyLwbgBoEyDgbgK8DyPBbAIkDiOYbgP4DiHJdgL0EyNwbgCIGyNQagLIHyDwagFYHyORZAGoGyMBZACoFyKhZACUGAACWLgABYwddURQHloBaAO2mEHoayATReG4=";
            //2018-08-14 patch
            //b64contents = "p/g4FQMAAAAAAAAAAAAAAAAAAAAAAAAA+4MAAH4DyHRXAn4DyIiXAoMDyJyXAosDyKSXApADyKRXApEDyKyXApkDyLCXAp8DyLiXAqMDyMCXArADyMRXAqwDyMxXAq0DyNSXArEDyLhXAs4DyJyXAtQDyLyXAtsDyMyXAhwEyIzVAvkDyHCWAvADyFAVAwsEyKhWAngDyARXAl0DyBiYAjEDyCyYAh8DyPCWAgwDyHiWAuYCyJiWAuUCyGiWAicDyEjVAisDyFjVAhgDyDjVAhMDyNQUA/QCyHQUA/ACyBjVAtACyCDVAgsDyBQVA8sCyPCWAh8DyKTZATwDyGzaAXEDyCTaAZADyBTaAZ0DyPRXAs4DyNhWAlEDyByXAjwDyFzVAlADyNSVAnADyPhWAkUDyMCWAn4DyHBXAtEwAADISAABBgcYURQHloBaAO2mAG4ayASz2GI=";


            try
            {
                //database expects a base64, which we already have
                // this is just to verify that the contents is valid as base64
                decoded = Convert.FromBase64String(b64contents);
            }
            catch (Exception)
            {
                return(this.Error("CreateCalibrationRequestAsync Denied: invalid parameter b64contents: (not a b64string)"));
            }



            if (decoded == null || decoded.Count() != LibreUtils.TestPatchAlwaysReturning63.Count())
            {
                return(this.Error("CreateCalibrationRequestAsync Denied: Invalid length"));
            }



            if (!LibreUtils.verify(decoded))
            {
                return(this.Error("CreateCalibrationRequestAsync Denied: Invalid checksums"));
            }

            try
            {
                footerCRCS = LibreUtils.CalculateFooterCRCs(decoded);
            }
            catch
            {
                return(this.Error("CreateCalibrationRequestAsync Denied: could not extract footer crcs "));
            }
            //modify sensorstatusbyte to a "ready" state
            decoded[4] = 0x03;

            var uuids = new List <String>();

            uuids.Add("b1-cal-" + Guid.NewGuid());
            uuids.Add("b2-cal-" + Guid.NewGuid());
            uuids.Add("f1-cal-" + Guid.NewGuid());
            uuids.Add("f2-cal-" + Guid.NewGuid());

            try
            {
                //these calls must not changebody crcs in any way

                var b1 = Convert.ToBase64String(LibreUtils.CreateFakePatch(patch: decoded, raw_glucose: DefaultAlgorithmThresholds.GLUCOSE_LOWER_BOUND, raw_temp: DefaultAlgorithmThresholds.RAW_TEMP1));

                var b2 = Convert.ToBase64String(LibreUtils.CreateFakePatch(patch: decoded, raw_glucose: DefaultAlgorithmThresholds.GLUCOSE_UPPER_BOUND, raw_temp: DefaultAlgorithmThresholds.RAW_TEMP1));

                var f1 = Convert.ToBase64String(LibreUtils.CreateFakePatch(patch: decoded, raw_glucose: DefaultAlgorithmThresholds.GLUCOSE_LOWER_BOUND, raw_temp: DefaultAlgorithmThresholds.RAW_TEMP2));
                var f2 = Convert.ToBase64String(LibreUtils.CreateFakePatch(patch: decoded, raw_glucose: DefaultAlgorithmThresholds.GLUCOSE_UPPER_BOUND, raw_temp: DefaultAlgorithmThresholds.RAW_TEMP2));


                await MongoConnection.AsyncInsertReading(new LibreReadingModel { CreatedOn = DateTime.Now, ModifiedOn = DateTime.Now, status = "pending", b64contents = b1, uuid = uuids[0], oldState = null, currentUtcOffset = null, sensorStartTimestamp = null, sensorScanTimestamp = null, newState = null });

                await MongoConnection.AsyncInsertReading(new LibreReadingModel { CreatedOn = DateTime.Now, ModifiedOn = DateTime.Now, status = "pending", b64contents = b2, uuid = uuids[1], oldState = null, currentUtcOffset = null, sensorStartTimestamp = null, sensorScanTimestamp = null, newState = null });

                await MongoConnection.AsyncInsertReading(new LibreReadingModel { CreatedOn = DateTime.Now, ModifiedOn = DateTime.Now, status = "pending", b64contents = f1, uuid = uuids[2], oldState = null, currentUtcOffset = null, sensorStartTimestamp = null, sensorScanTimestamp = null, newState = null });

                await MongoConnection.AsyncInsertReading(new LibreReadingModel { CreatedOn = DateTime.Now, ModifiedOn = DateTime.Now, status = "pending", b64contents = f2, uuid = uuids[3], oldState = null, currentUtcOffset = null, sensorStartTimestamp = null, sensorScanTimestamp = null, newState = null });
            }
            catch (System.TimeoutException)
            {
                return(this.Error("CreateCalibrationRequestAsync Failed: Timeout inserting patches to database"));
            }
            catch (Exception) {
                return(this.Error("CreateCalibrationRequestAsync Failed: Unknown reason"));
            }

            LibreCalibrationModel cal;

            try
            {
                var metadata = new AlgorithmMetadata {
                    GLUCOSE_LOWER_BOUND = DefaultAlgorithmThresholds.GLUCOSE_LOWER_BOUND,
                    GLUCOSE_UPPER_BOUND = DefaultAlgorithmThresholds.GLUCOSE_UPPER_BOUND,
                    RAW_TEMP1           = DefaultAlgorithmThresholds.RAW_TEMP1,
                    RAW_TEMP2           = DefaultAlgorithmThresholds.RAW_TEMP2
                };


                cal = new LibreCalibrationModel
                {
                    CreatedOn  = DateTime.Now,
                    ModifiedOn = DateTime.Now,
                    uuid       = "calibrationmetadata-" + Guid.NewGuid(),
                    metadata   = metadata,
                    requestids = uuids,
                    isValidForFooterWithReverseCRCs = LibreUtils.CalculateFooterCRCs(decoded)
                };
                await MongoConnection.AsyncInsertCalibrationReading(cal);
            }
            catch (System.TimeoutException)
            {
                return(this.Error("CreateCalibrationRequestAsync Failed: Timeout inserting metadata to database"));
            }
            catch (Exception)
            {
                return(this.Error("CreateCalibrationRequestAsync Failed: Unknown reason"));
            }



            return(Success <LibreCalibrationModel>(cal, "CreateCalibrationRequestAsync"));
        }