示例#1
0
        public ActionResult Index(string sessionId, string minutes, string maxCount)
        {
            //the minutes parameter is often 1440 (24 hours), telling you how long back you should do the search
            //In nightscout context it is mostly redundant as the maxCount will search as long back as needed.
            //we ignore that parameter

            sessionId = sessionId ?? "";
            int count = int.TryParse(maxCount, out count) ? count : 3;

            if (!this.CheckAuth(sessionId))
            {
                return(Json("Some error validating sessionid!!", JsonRequestBehavior.AllowGet));
            }

            if (Config.EnableMockedGlucoseMode)
            {
                var glucose = mockupGlucoseValues();
                return(Content(this.encodeGlucose(glucose), "application/json"));
            }



            NightscoutPebble nsglucose = null;
            Exception        lasterror = null;
            var i = 0;

            do
            {
                try
                {
                    nsglucose = this.fetchNightscoutPebbleData(Config.NsHost, count);
                    lasterror = null;
                }
                catch (Exception err)
                {
                    lasterror = err;
                }
            } while (i++ < 2);

            if (lasterror != null)
            {
                throw lasterror;
            }
            var shareglucose = new List <ShareGlucose>();

            foreach (var entry in nsglucose.bgs)
            {
                var     glucosedate = DateTimeOffset.FromUnixTimeMilliseconds(entry.datetime).DateTime;
                decimal val;
                Decimal.TryParse(entry.sgv, out val);

                var trend = SlopeConvert.NsDirectionToShareTrend(entry.direction);

                shareglucose.Add(
                    new ShareGlucose
                {
                    DT    = glucosedate,
                    ST    = glucosedate,
                    WT    = glucosedate.ToUniversalTime(),
                    Value = (int)val,
                    Trend = trend
                }

                    );
            }

            return(Content(this.encodeGlucose(shareglucose), "application/json"));
        }
        public ActionResult Get([FromHybrid] GlucoseModel model)
        {
            //return Content($"got GET with sessionId: {sessionId}, minutes: {minutes}, maxCount:{maxCount}");
            //the minutes parameter is often 1440 (24 hours), telling you how long back you should do the search
            //In nightscout context it is mostly redundant as the maxCount will search as long back as needed.
            //we ignore that parameter
            //Logger.LogInfo("Accesing Glucose Index");

            int count = int.TryParse(model.maxCount ?? "3", out count) ? count : 3;

            if (!this.CheckAuth(model.sessionId ?? ""))
            {
                //Logger.LogInfo($"Error in checking sessionId");
                return(Json("Some error validating sessionid!!"));
            }

            if (Config.Instance.EnableMockedGlucoseMode)
            {
                //Logger.LogInfo($"Mocking up glucose value");
                var glucose = mockupGlucoseValues();
                return(Content(this.encodeGlucose(glucose), "application/json"));
            }



            //NightscoutPebble nsglucose = null;
            List <NightscoutApiEntries> nsglucose = null;
            Exception lasterror = null;
            var       i         = 0;

            do
            {
                var n = i + 1;
                try
                {
                    //Logger.LogInfo($"Attempt {n} to fetch glucose from {Config.NsHost}");
                    nsglucose = this.fetchNightscoutGlucoseEntries(Config.Instance.NsHost, count);
                    //Logger.LogInfo($"Got {nsglucose.Count} entries from nightscout");
                    lasterror = null;
                }
                catch (Exception err)
                {
                    lasterror = err;
                }
            } while (i++ < 2 && lasterror != null);

            if (lasterror != null)
            {
                throw lasterror;
            }
            var shareglucose = new List <ShareGlucose>();

            foreach (var entry in nsglucose)
            {
                var     glucosedate = DateTimeOffset.FromUnixTimeMilliseconds(entry.date).DateTime;
                decimal val;
                Decimal.TryParse(entry.sgv, out val);

                var trend = SlopeConvert.NsDirectionToShareTrend(entry.direction);

                shareglucose.Add(
                    new ShareGlucose
                {
                    DT    = glucosedate,
                    ST    = glucosedate,
                    WT    = glucosedate.ToUniversalTime(),
                    Value = (int)val,
                    Trend = trend
                }

                    );
            }

            return(Content(this.encodeGlucose(shareglucose), "application/json"));
        }