コード例 #1
0
        public override void ProcessData(ZigBeeCoordinatorContext ctx, ZigBeeDevice device, byte[] data)
        {
            int?  userId = data[0];
            float weight = BitConverter.ToInt16(data, 1) / 10.0f;

            // user not specified
            if (userId == 0)
            {
                userId = FindUserBasedOnHistory(ctx, weight);
            }

            if (userId == null)
            {
                _log.Error("Could not find user based on weight '" + weight + "'");
                return;
            }

            _log.Info("Weight received for user [" + userId + "]: " + weight);

            WirelessScaleData wirelessScaleData = new WirelessScaleData
            {
                UserId           = userId.Value,
                Value            = weight,
                ZigBeeDevice     = device,
                ReceivedDateTime = DateTime.Now
            };

            ctx.DbCtx.WirelessScaleDatas.AddObject(wirelessScaleData);
            ctx.DbCtx.SaveChanges();
        }
コード例 #2
0
        public ActionResult ManualInput(DateTime date, string time, double weight)
        {
            int?userId = CoordinatorUser.Finder.GetCurrentUserId();

            if (userId == null)
            {
                throw new Exception("You must be logged in");
            }

            using (ZigBeeCoordinatorContext ctx = new ZigBeeCoordinatorContext())
            {
                DateTime t = DateTime.Parse(date.ToString("d") + " " + time);

                WirelessScaleData data = new WirelessScaleData
                {
                    UserId           = userId.Value,
                    ReceivedDateTime = t,
                    Value            = weight,
                    ZigBeeDevice     = null
                };
                ctx.DbCtx.WirelessScaleDatas.AddObject(data);
                ctx.DbCtx.SaveChanges();
            }

            return(RedirectToAction("Index", new { userId = userId }));
        }
コード例 #3
0
        private int?FindUserBasedOnHistory(ZigBeeCoordinatorContext ctx, float weight)
        {
            IEnumerable <WirelessScaleData> mostRecentHistory = FindUsersMostRecentData(ctx);
            WirelessScaleData closestData = mostRecentHistory.Smallest(d => Math.Abs(d.Value - weight));

            if (Math.Abs(closestData.Value - weight) > 20)
            {
                return(null);
            }
            return(closestData.UserId);
        }
コード例 #4
0
        public ActionResult DeleteValue(int wirelessScaleDataId)
        {
            int?userId = CoordinatorUser.Finder.GetCurrentUserId();

            if (userId == null)
            {
                throw new Exception("You must be logged in");
            }

            using (ZigBeeCoordinatorContext ctx = new ZigBeeCoordinatorContext())
            {
                WirelessScaleData data = ctx.DbCtx.WirelessScaleDatas
                                         .FirstOrDefault(d => d.WirelessScaleDataId == wirelessScaleDataId);
                ctx.DbCtx.WirelessScaleDatas.DeleteObject(data);
                ctx.DbCtx.SaveChanges();
            }

            return(RedirectToAction("Index", new { userId = userId }));
        }
コード例 #5
0
        public ActionResult HistoryChart(int userId, int?width, int?height, HistoryChartType?chartType)
        {
            using (ZigBeeCoordinatorContext ctx = new ZigBeeCoordinatorContext())
            {
                double minX = UserSetting.Finder.GetSettingAsDouble(ctx, userId, GoalWeightSettingKey, 100.0);

                string title = "History";
                IEnumerable <WirelessScaleData> datas = GetUserHistory(ctx, userId);
                if (chartType == null || chartType.Value == HistoryChartType.All)
                {
                    title = "History - All";
                }
                else if (chartType.Value == HistoryChartType.Month)
                {
                    title = "History - 30-days";
                    DateTime start = DateTime.Now.Subtract(new TimeSpan(30, 0, 0, 0));
                    datas = datas.Where(d => d.ReceivedDateTime > start);
                    WirelessScaleData smallest = datas.Smallest(d => d.Value);
                    if (smallest != null)
                    {
                        minX = smallest.Value;
                    }
                }
                else if (chartType.Value == HistoryChartType.Week)
                {
                    title = "History - 7-days";
                    DateTime start = DateTime.Now.Subtract(new TimeSpan(7, 0, 0, 0));
                    datas = datas.Where(d => d.ReceivedDateTime > start);
                    WirelessScaleData smallest = datas.Smallest(d => d.Value);
                    if (smallest != null)
                    {
                        minX = smallest.Value;
                    }
                }
                ViewBag.Chart = BuildHistoryChart(datas, minX, width ?? 400, height ?? 300, title);
                return(View());
            }
        }