Exemplo n.º 1
0
 protected override void ConfigureRequestContainer(TinyIoCContainer container, NancyContext context)
 {
     base.ConfigureRequestContainer(container, context);
     var ctx = new SmartFlowContext();
     container.Register(ctx);
     container.Register<IUserMapper, SmartFlowAuthService>();
 }
Exemplo n.º 2
0
 public SnapshotQuery(SmartFlowContext context, DateTime fromDate, DateTime toDate, string floor)
 {
     Results = new List<SnapshotListResult>(context.Observations
         .AsNoTracking()
         .Include(x => x.Location.FloorLocations)
         .Where(x => x.SeenTime >= fromDate && x.SeenTime <= toDate)
         .Where(x => x.Location.FloorLocations.Any(y => y.Name == floor))
         .Select(x => new
         {
             x.ClientMac,
             x.Location.FloorLocations.FirstOrDefault(y => y.Name == floor).X,
             x.Location.FloorLocations.FirstOrDefault(y => y.Name == floor).Y,
             x.SeenTime,
             x.Location.Unc,
             x.Rssi,
             x.ApMac,
             x.RecordId
         })
         .ToList()
         .GroupBy(x => x.ClientMac)
         .Select(x => x.OrderByDescending(y => y.SeenTime).FirstOrDefault())
         .Select(x => new SnapshotListResult
         {
             ClientMac = x.ClientMac,
             X = x.X,
             Y = x.Y,
             SeenTime = x.SeenTime,
             Unc = x.Unc,
             Rssi = x.Rssi,
             ApMac = x.ApMac,
             RecordId = x.RecordId,
         })
         .OrderBy(x => x.SeenTime));
 }
Exemplo n.º 3
0
        public AuthModule(SmartFlowContext context)
            : base("/login")
        {
            Get["/"] = _ => View["Auth/Login", new LoginViewModel()];
            Post["/"] = _ =>
            {
                var model = this.Bind<LoginViewModel>();
                var result = this.Validate(model);
                if (result.IsValid)
                {
                    var user = context.Users.FirstOrDefault(x => x.UserName == model.Username);

                    if (user != null &&
                                        MachSecure.BusinessObjects.Encryption.ValidatePassword(model.Password, user.Password))
                        return this.LoginAndRedirect(user.RecordId);

                    this.AddValidationError("", "The username or password you entered are incorrect");
                    model.Errors = result.Errors;
                    return View["Auth/Login", model];
                }

                model.Errors = result.Errors;
                return View["Auth/Login", model];
            };
            Get["/logout"] = _ => this.LogoutAndRedirect("~/");
        }
Exemplo n.º 4
0
        public DashboardQuery(SmartFlowContext context, DateTime fromDate, DateTime toDate)
        {
            var aps = context.AccessPointConfigs.Where(x => x.IsActive).ToList();
            var config = context.DashboardConfigs.FirstOrDefault() ?? new DashboardConfig
            {
                MaximumUnc = 30,
                MinimumAccessPoints = 1,
                MinimumObservations = 5
            };

            var query = context.FloorLocations
                .Include(x => x.Location.Observation)
                .Where(z => z.Location.Observation.SeenTime >= fromDate && z.Location.Observation.SeenTime <= toDate)
                .Where(x => x.Location.Unc <= config.MaximumUnc)
                .AsNoTracking()
                .ToList()
                .GroupBy(x => x.Name)
                .Where(x => x.GroupBy(z => z.ApMac).Count() >= config.MinimumAccessPoints)
                .Select(x => new { Floor = x.Key, Clients = x.GroupBy(y => y.Location.Observation.ClientMac).Where(z => z.Count() >= config.MinimumObservations) })
                ;

            Floors = new List<ReportListResult>(query.Select(x => new ReportListResult
                {
                    Floor = x.Floor,
                    Observations = x.Clients.Sum(y => y.Count()),
                    TotalClients = x.Clients.Count(),
                    DwellTime = TimeSpan.FromTicks(Convert.ToInt64(x.Clients.Average(q => (q.Max(z => z.Location.Observation.SeenTime) - q.Min(z => z.Location.Observation.SeenTime)).Ticks))),
                    AccessPoints = aps.Where(y => y.Name == x.Floor).Select(y => new AccessPointDiagnosticResult
                    {
                        ApMac = y.ApMac,
                        DisplayName = y.DisplayName,
                        SeenTime = x.Clients.SelectMany(q => q).Any(z => z.ApMac == y.ApMac) ? x.Clients.SelectMany(q => q).Where(z => z.ApMac == y.ApMac).Max(z => z.Location.Observation.SeenTime) : DateTime.MinValue
                    }).OrderBy(y => y.DisplayName).ToList()
                }));
        }
Exemplo n.º 5
0
        public MerakiModule(TextFileLogger logger, SmartFlowContext context)
            : base("/meraki")
        {
            // Meraki will use this to validate we are the correct host
            Get["/"] = _ => Settings.Default.Validator;

            Post["/"] = _ =>
            {
                var req = this.Bind<MerakiDto>();

                // secret didnt match, return FORBIDDEN
                if (req.Secret != Settings.Default.Secret) return 403;

                // log to file (will log to database once its all working etc..)
                logger.Write(req.ToString(), Enums.LogLevel.Information);

                var floors = req.Data.ApFloors.Select(x => x.ToFloorEntity(req.Data.ApMac));

                foreach (var floor in floors)
                {
                    if (context.Floors.Any(x => x.ApMac == floor.ApMac && x.Name == floor.Name))
                    {
                        context.Floors.Attach(floor);
                    }
                    else
                    {
                        context.Floors.Add(floor);
                    }
                }

                var thisTime = DateTime.Now;
                var isDaylight = TimeZoneInfo.Local.IsDaylightSavingTime(thisTime);

                var observations = req.Data.Observations.Select(y => new Observation
                {
                    ApMac = req.Data.ApMac,
                    ClientMac = y.ClientMac,
                    Ipv4 = y.Ipv4,
                    Ipv6 = y.Ipv6,
                    Manufacturer = y.Manufacturer,
                    Os = y.Os,
                    Rssi = y.Rssi,
                    SeenTime = isDaylight ? y.SeenTime.AddHours(1) : y.SeenTime,
                    SeenEpoch = y.SeenEpoch,
                    Location = y.Location != null ? y.Location.ToLocationEntity(req.Data) : null
                });

                context.Observations.AddRange(observations);

                // return CREATED
                return 201;
            };
        }
Exemplo n.º 6
0
        public DashboardModule(SmartFlowContext context)
            : base(context)
        {
            JsonSettings.MaxJsonLength = Int32.MaxValue;
            Get["/dashboard"] = _ =>
            {
                var model = this.Bind<ReportListViewModel>();

                var fromDate = DateTime.Parse(model.FromDate);
                var toDate = DateTime.Parse(model.ToDate);
                var query = new DashboardQuery(context, fromDate, toDate);
                model.Floors.AddRange(query.Floors);

                return
                    Negotiate.WithModel(model)
                    ;
            };
        }
Exemplo n.º 7
0
 public SmartFlowAuthService(SmartFlowContext context)
 {
     _context = context;
 }
Exemplo n.º 8
0
        public SmartFlowModule(SmartFlowContext context)
            : base("/smartflow")
        {
            //this.RequiresAuthentication();
            JsonSettings.MaxJsonLength = Int32.MaxValue;

            Get["/admin"] = _ =>
            {
                return Negotiate.WithView("SmartFlow/Admin");
            };

            Get["/"] = _ =>
            {
                return View["SmartFlow/List"];
            };

            Get["/snapshot"] = _ =>
            {
                var model = this.Bind<SnapshotViewModel>();
                model.Floors.AddRange(context.Floors.Select(x => x.Name).Distinct());
                model.Floor = !string.IsNullOrEmpty(model.Floor) ? model.Floor : context.Floors.First().Name;
                var floorConfig = context.FloorConfigs.FirstOrDefault(x => x.Name == model.Floor);
                if (floorConfig != null)
                {
                    model.X = floorConfig.X;
                    model.Y = floorConfig.Y;
                }
                model.ImagePath = string.Format(@"/images/{0}.png", model.Floor);
                try
                {
                    var chartSize = ImageHelper.GetChartDimensions(model.Floor, model.ChartWidth, model.ChartHeight);
                    model.ChartWidth = chartSize.Width;
                    model.ChartHeight = chartSize.Height;
                }
                catch (FileNotFoundException ex)
                {
                    model.ImagePath = string.Empty;
                }

                var beginTime = DateTime.Parse(model.SelectedDate).AddMinutes(-1);
                var endTime = DateTime.Parse(model.SelectedDate).AddMinutes(1);
                var query = new SnapshotQuery(context, beginTime, endTime, model.Floor);
                model.Results.AddRange(query.Results);

                return Negotiate.WithModel(model);
            };

            Get["/{clientMac}/details"] = _ =>
            {
                var model = this.Bind<ReportDetailsViewModel>();
                //if (!string.IsNullOrEmpty(model.Floor))
                //{
                //    model.ImagePath = string.Format(@"/images/{0}.png", model.Floor);
                //    var floorConfig = context.FloorConfigs.FirstOrDefault(x => x.Name == model.Floor);
                //    if (floorConfig != null)
                //    {
                //        model.X = floorConfig.X;
                //        model.Y = floorConfig.Y;
                //    }
                //    model.AccessPoints = context.AccessPointConfigs.Where(x => x.Name == model.Floor).Where(x => x.IsActive).OrderBy(x => x.DisplayName).ToList();

                //    try
                //    {
                //        var chartSize = ImageHelper.GetChartDimensions(model.Floor, model.ChartWidth, model.ChartHeight);
                //        model.ChartWidth = chartSize.Width;
                //        model.ChartHeight = chartSize.Height;
                //    }
                //    catch (FileNotFoundException ex)
                //    {
                //        model.ImagePath = string.Empty;
                //    }

                //    var beginTime = DateTime.Parse(model.FromDate);
                //    var endTime = DateTime.Parse(model.ToDate);
                //    var results =
                //        context.Observations
                //            .AsNoTracking()
                //            .Include(x => x.Location.FloorLocations)
                //            .Where(x => x.ClientMac == model.ClientMac)
                //            .Where(x => x.Location.FloorLocations.Any(y => y.Name == model.Floor))
                //            .Where(x => x.SeenTime >= beginTime && x.SeenTime <= endTime)
                //            .Where(x => x.Rssi >= model.MinimumRssi)
                //            .Where(x => x.Location.Unc <= model.MaximumUnc)
                //            .Select(x => new ReportDetailsResult
                //            {
                //                X = x.Location.FloorLocations.FirstOrDefault(y => y.Name == model.Floor).X,
                //                Y = x.Location.FloorLocations.FirstOrDefault(y => y.Name == model.Floor).Y,
                //                SeenTime = x.SeenTime,
                //                Unc = x.Location.Unc,
                //                Rssi = x.Rssi,
                //                ApMac =
                //                    context.AccessPointConfigs.FirstOrDefault(
                //                        y => y.ApMac == x.ApMac && y.Name == model.Floor) != null
                //                        ? context.AccessPointConfigs.FirstOrDefault(
                //                            y => y.ApMac == x.ApMac && y.Name == model.Floor).DisplayName
                //                        : x.ApMac,
                //                RecordId = x.RecordId,
                //                Name = model.Floor
                //            })
                //            .OrderBy(y => y.SeenTime)
                //            .ThenBy(x => x.ApMac);

                //    model.Results.AddRange(results);
                //}

                return Negotiate.WithView("SmartFlow/Details").WithModel(model);
            };
        }