Пример #1
0
        public async Task <ActionResult> CreateRequestAsync(string accesstoken, string contents, string tag, string deletepolicy)
        {
            //var permissions= await NightscoutPermissions.CheckUploadPermissions(accesstoken);
            //var permissions = await NightscoutPermissions.CheckProcessPermissions(accesstoken);
            if (!await this.checkUploadPermissions(accesstoken))
            {
                return(this.Error("CreateRequestAsync Denied"));
            }



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



            var g       = Guid.NewGuid().ToString();
            var reading = new RBS_Blob
            {
                CreatedOn    = DateTime.Now,
                ModifiedOn   = DateTime.Now,
                contents     = contents,
                uuid         = g,
                purpose      = "RBS",
                tag          = string.IsNullOrWhiteSpace(tag) ? null : tag.Substring(0, Math.Min(tag.Length, 10)),
                deletePolicy = deletepolicy == "autodelete" ? "auto" : null
            };

            //return this.Error("synthax ok");
            try{
                await MongoConnection.AsyncInsertReading(reading);
            } catch (System.TimeoutException) {
                return(Error("Timeout, database down?"));
            }

            return(Success <RBS_Blob>(reading, "CreateRequestAsync"));

            //var content = $"accesstoken: {accesstoken}, b64contents: {b64contents}, guid: {g}";
            //return Content("CreateRequestAsync IS NOT IMPLEMENTED YET:" + content);
        }
Пример #2
0
        public async Task <ActionResult> CreateRequestAsync(string accesstoken, string b64contents, string oldState, string sensorStartTimestamp, string sensorScanTimestamp, string currentUtcOffset)
        {
            //var permissions= await NightscoutPermissions.CheckUploadPermissions(accesstoken);
            //var permissions = await NightscoutPermissions.CheckProcessPermissions(accesstoken);
            if (!await this.checkUploadPermissions(accesstoken))
            {
                return(this.Error("CreateRequestAsync Denied"));
            }
            long?longSensorStartTimestamp = null;
            long?longSensorScanTimestamp  = null;
            long?longCurrentUtcOffset     = null;


            if (string.IsNullOrWhiteSpace(b64contents))
            {
                return(this.Error("CreateRequestAsync Denied: invalid parameter b64contents"));
            }
            try{
                // Database expects a base64, which we already have
                // this is just to verify that the contents is valid as base64
                System.Convert.FromBase64String(b64contents);
            } catch (FormatException) {
                return(this.Error("CreateRequestAsync Denied: invalid parameter b64contents: (not a b64string)"));
            }

            if (string.IsNullOrWhiteSpace(oldState + sensorStartTimestamp + sensorScanTimestamp + currentUtcOffset))
            {
                //these advanced parameters are optional, and this is ok
            }
            else if (oldState?.Length > 0 && sensorStartTimestamp?.Length > 0 && sensorScanTimestamp?.Length > 0 && currentUtcOffset?.Length > 0)
            {
                try
                {
                    //database expects a base64, which we already have
                    // this is just to verify that the contents is valid as base64
                    System.Convert.FromBase64String(oldState);
                }
                catch (FormatException)
                {
                    return(this.Error("CreateRequestAsync Denied: invalid parameter oldState: (not a b64string)"));
                }
                longSensorStartTimestamp = Int64.TryParse(sensorStartTimestamp, out var t1) ? t1 : (long?)null;
                longSensorScanTimestamp  = Int64.TryParse(sensorScanTimestamp, out var t2) ? t2 : (long?)null;
                longCurrentUtcOffset     = Int64.TryParse(currentUtcOffset, out var t3) ? t3 : (long?)null;

                if (longSensorStartTimestamp == null)
                {
                    return(this.Error("CreateRequestAsync Denied: invalid parameter sensorStartTimestamp: (not a long)"));
                }

                if (longSensorScanTimestamp == null)
                {
                    return(this.Error("CreateRequestAsync Denied: invalid parameter sensorScanTimestamp: (not a long)"));
                }

                if (longCurrentUtcOffset == null)
                {
                    return(this.Error("CreateRequestAsync Denied: invalid parameter currentUtcOffset: (not a long)"));
                }
            }
            else
            {
                return(this.Error("CreateRequestAsync Denied: when speciying advanced parameters, all advanced parameters should be specified (oldState, sensorStartTimestamp, sensorScanTimestamp, currentUtcOffset)"));
            }

            var g       = Guid.NewGuid().ToString();
            var reading = new LibreReadingModel
            {
                CreatedOn            = DateTime.Now,
                ModifiedOn           = DateTime.Now,
                status               = "pending",
                b64contents          = b64contents,
                uuid                 = g,
                oldState             = (oldState == null || oldState == "") ? null : oldState,
                currentUtcOffset     = longCurrentUtcOffset,
                sensorStartTimestamp = longSensorStartTimestamp,
                sensorScanTimestamp  = longSensorScanTimestamp,
                newState             = null //gets updated by the algo
            };

            //return this.Error("synthax ok");
            try{
                await MongoConnection.AsyncInsertReading(reading);
            } catch (System.TimeoutException) {
                return(Error("Timeout, database down?"));
            }

            return(Success <LibreReadingModel>(reading, "CreateRequestAsync"));

            //var content = $"accesstoken: {accesstoken}, b64contents: {b64contents}, guid: {g}";
            //return Content("CreateRequestAsync IS NOT IMPLEMENTED YET:" + content);
        }
Пример #3
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"));
        }