private PairingService CreatePairingService()
        {
            var userID  = Guid.Parse(User.Identity.GetUserId());
            var service = new PairingService(userID);

            return(service);
        }
示例#2
0
        public async Task GeneratePairings()
        {
            using (var context = new ApplicationDbContext(Options))
            {
                var            pairingService = new PairingService(context);
                List <Pairing> pairings       = await pairingService.GeneratePairings(1);

                List <int> santaIds = new List <int>();

                foreach (Pairing pairing in pairings)
                {
                    if (!santaIds.Contains(pairing.SantaId))
                    {
                        santaIds.Add(pairing.SantaId);
                    }
                }

                foreach (var pairing in pairings)
                {
                    Assert.AreNotEqual <int>(pairing.SantaId, pairing.RecipientId);
                }

                Assert.IsNotNull(pairings);
                Assert.IsTrue(pairings.Count == 5);
                Assert.AreEqual(pairings.Count, santaIds.Count);
            }
        }
        public ActionResult Index()
        {
            var userID  = Guid.Parse(User.Identity.GetUserId());
            var service = new PairingService(userID);
            var model   = service.GetPairings();

            return(View(model));
        }
示例#4
0
 public ResultsViewModel()
 {
     serv             = PairingService.Instance;
     LastGeneration   = serv.LastGeneration;
     Results          = serv.PairedDrawers;
     Drawers          = serv.Drawers;
     Pastebin         = serv.Paste;
     SelectiveResults = new List <DrawingPair>();
 }
        public async Task GeneratePairings_CreatesPairings()
        {
            using (var context = new ApplicationDbContext(Options))
            {
                var            pairingService = new PairingService(context);
                List <Pairing> pairings       = await pairingService.GeneratePairings(1);

                Assert.IsNotNull(pairings);
                Assert.IsTrue(pairings.Count == 4);
            }
        }
        public async Task GeneratePairings_NoRecipientIsTheirOwnSanta()
        {
            using (var context = new ApplicationDbContext(Options))
            {
                var            pairingService = new PairingService(context);
                List <Pairing> pairings       = await pairingService.GeneratePairings(1);

                foreach (var pairing in pairings)
                {
                    Assert.AreNotEqual <int>(pairing.SantaId, pairing.RecipientId);
                }
            }
        }
示例#7
0
        public async Task <IActionResult> GeneratePairings(int groupId)
        {
            if (groupId < 1)
            {
                return(BadRequest("A group id must be specified"));
            }

            List <Pairing> result = await PairingService.GeneratePairings(groupId);

            if (result.Count == 0)
            {
                return(BadRequest());
            }

            return(Created($"/pairing/{groupId}", result.Select(p => Mapper.Map <PairingViewModel>(p)).ToList()));
        }
        public async Task PairingServiceWithNullUrl()
        {
            var restConnector = new RestConnectorBuilder()
                                .WithResult(string.Empty)
                                .Build();
            var ioptions = new OptionBuilder()
                           .BuildNullUrlAppSettings();

            var pairingService = new PairingService(restConnector, ioptions);

            var selectOptions = await pairingService.GetCurrenciesAsync();

            selectOptions.Should().BeEmpty("the result should be empty if there is no configuration available.");
            var pairings = await pairingService.GetPairingAsync(1, 1);

            pairings.Should().BeEmpty("the result should be empty if there is no configuration available.");
        }
示例#9
0
        public async Task <IActionResult> GeneratePairings(int groupId)
        {
            if (groupId < 1)
            {
                return(BadRequest("No ID given."));
            }


            List <Pairing> pairings = await PairingService.GeneratePairings(groupId);

            if (pairings.Count < 1 || pairings == null)
            {
                return(BadRequest("Issue generating pairings."));
            }

            return(Created($"/Pairing/{groupId}", pairings.Select(p => Mapper.Map <PairingViewModel>(p)).ToList()));
        }
示例#10
0
        public async Task GetCurrencies(string result, int length)
        {
            var restConnector = new RestConnectorBuilder()
                                .WithResult(result)
                                .Build();
            var ioptions = new OptionBuilder()
                           .WithDefaultCurrenciesUrl()
                           .Build();

            var pairingService = new PairingService(restConnector, ioptions);

            var selectOptions = await pairingService.GetCurrenciesAsync();

            selectOptions.Should().BeAssignableTo <List <SelectOption> >("the method always returns a list of pairings.");
            var selectOptionList = (List <SelectOption>)selectOptions;

            selectOptionList.Count.Should().Be(length, "the data returned is such.");
        }
示例#11
0
        public async Task GetPairingAsync(string result, int length)
        {
            var restConnector = new RestConnectorBuilder()
                                .WithResult(result)
                                .Build();

            var ioptions = new OptionBuilder()
                           .WithDefaultPairingsUrl()
                           .Build();

            var pairingService = new PairingService(restConnector, ioptions);

            var pairings = await pairingService.GetPairingAsync(1, 1);

            pairings.Should().BeAssignableTo <List <Pairing> >("the method always returns a list of pairings.");
            var pairingList = (List <Pairing>)pairings;

            pairingList.Count.Should().Be(length, "the data returned is such.");
        }
        public async Task GeneratePairings_SantasAreUnique()
        {
            using (var context = new ApplicationDbContext(Options))
            {
                var            pairingService = new PairingService(context);
                List <Pairing> pairings       = await pairingService.GeneratePairings(1);

                List <int> santaIds = new List <int>(); // List of uniquely found santaIds
                foreach (Pairing pairing in pairings)
                {
                    if (!santaIds.Contains(pairing.SantaId))
                    {
                        santaIds.Add(pairing.SantaId);
                    }
                }

                Assert.AreEqual(pairings.Count, santaIds.Count);
            }
        }
示例#13
0
        public void Start()
        {
            if (!Directory.Exists(SourcesPath))
            {
                Log.Fatal("{@SourcesPath} doesn't exist; service has nothing to do without sources", SourcesPath);
                Stop();
                return;
            }

            if (!Directory.Exists(SinksPath))
            {
                Log.Warning("{@SinksPath} doesn't exist; service has nothing to do without sinks", SinksPath);
            }

            _childDevices.CollectionChanged += (sender, args) =>
            {
                switch (args.Action)
                {
                case NotifyCollectionChangedAction.Add:
                    foreach (IDualShockDevice item in args.NewItems)
                    {
                        Log.Information("Device {Device} got attached via {ConnectionType}", item,
                                        item.ConnectionType);
                        foreach (var plugin in SinkPlugins.Select(p => p.Value))
                        {
                            plugin.DeviceArrived(item);
                        }
                    }

                    break;

                case NotifyCollectionChangedAction.Remove:
                    foreach (IDualShockDevice item in args.OldItems)
                    {
                        Log.Information("Device {Device} got removed via {ConnectionType}", item,
                                        item.ConnectionType);
                        foreach (var plugin in SinkPlugins.Select(p => p.Value))
                        {
                            plugin.DeviceRemoved(item);
                        }
                    }

                    break;
                }
            };

            #region MEF

            //Creating an instance of aggregate catalog. It aggregates other catalogs
            var aggregateCatalog = new AggregateCatalog();

            //Load parts from the current assembly if available
            var asmCatalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());

            //Add to the aggregate catalog
            aggregateCatalog.Catalogs.Add(new DirectoryCatalog(SourcesPath, "*.dll"));
            aggregateCatalog.Catalogs.Add(new DirectoryCatalog(SinksPath, "*.dll"));
            aggregateCatalog.Catalogs.Add(asmCatalog);

            //Crete the composition container
            var container = new CompositionContainer(aggregateCatalog);

            // Composable parts are created here i.e.
            // the Import and Export components assembles here
            container.ComposeParts(this);

            #endregion

            // Log loaded sink plugins
            foreach (var plugin in SinkPlugins)
            {
                Log.Information("Loaded sink plugin {Plugin}", plugin.Metadata["Name"]);

                plugin.Value.RumbleRequestReceived += (sender, args) =>
                                                      _childDevices[(IDualShockDevice)sender].Rumble(args.LargeMotor, args.SmallMotor);
            }

            // Log and enable sources
            foreach (var emulator in BusEmulators)
            {
                Log.Information("Loaded bus emulator {Emulator}", emulator.Metadata["Name"]);

                emulator.Value.ChildDeviceAttached += (sender, args) => _childDevices.Add(args.Device);
                emulator.Value.ChildDeviceRemoved  += (sender, args) => _childDevices.Remove(args.Device);
                emulator.Value.InputReportReceived += EmulatorOnInputReportReceived;

                try
                {
                    Log.Information("Starting bus emulator {Emulator}", emulator.Metadata["Name"]);
                    emulator.Value.Start();
                    Log.Information("Bus emulator {Emulator} started successfully", emulator.Metadata["Name"]);
                }
                catch (Exception ex)
                {
                    Log.Error("Failed to start {@emulator}: {@ex}", emulator.Metadata["Name"], ex);
                }
            }

            #region IPC

            var services = new DelegateServiceFactory();
            services.Register <IPairingService>(() =>
            {
                var service = new PairingService();

                service.DeviceListRequested += (sender, args) => _childDevices
                                               .Where(d => d.ConnectionType.Equals(DualShockConnectionType.USB))
                                               .Select(d => new DualShockDeviceDescriptor
                {
                    ClientAddress  = new UniqueAddress(d.ClientAddress),
                    ConnectionType = d.ConnectionType,
                    DeviceType     = d.DeviceType,
                    HostAddress    = new UniqueAddress(d.HostAddress)
                }).ToList();

                service.DevicePairingRequested += (device, args) =>
                                                  _childDevices[device.ClientAddress].PairTo(new PhysicalAddress(args.HostAddress.AddressBytes));

                return(service);
            });

            _ipcServer = new HalibutRuntime(services, Configuration.ServerCertificate);
            _ipcServer.Listen(Configuration.ServerEndpoint);
            _ipcServer.Trust(Configuration.ClientCertificate.Thumbprint);

            #endregion
        }
示例#14
0
        public void Start()
        {
            if (!Directory.Exists(SourcesPath))
            {
                Log.Fatal("{@SourcesPath} doesn't exist; service has nothing to do without sources", SourcesPath);
                Stop();
                return;
            }

            if (!Directory.Exists(SinksPath))
            {
                Log.Warning("{@SinksPath} doesn't exist; service has nothing to do without sinks", SinksPath);
            }

            _childDevices.CollectionChanged += (sender, args) =>
            {
                switch (args.Action)
                {
                case NotifyCollectionChangedAction.Add:
                    foreach (IDualShockDevice item in args.NewItems)
                    {
                        Log.Information("Device {Device} got attached via {ConnectionType}", item,
                                        item.ConnectionType);

                        // Crude auto-pairing mechanism in case BthPS3 is present
                        if (item.ConnectionType.Equals(DualShockConnectionType.USB) &&
                            BusEmulators.Select(be => be.Value).Any(b => b.Name == "BthPS3BusEmulator") &&
                            BluetoothAdapter.GetDefault() != null)
                        {
                            // Check if newly connected USB device is already connected via Bluetooth
                            if (item.ConnectionType.Equals(DualShockConnectionType.USB))
                            {
                                var emulator = BusEmulators.Select(be => be.Value).Where(b => b.Name == "BthPS3BusEmulator").First();

                                foreach (var otherItem in _childDevices)
                                {
                                    if (otherItem.ConnectionType.Equals(DualShockConnectionType.Bluetooth))
                                    {
                                        if (otherItem.ClientAddress.Equals(item.ClientAddress))     // If found remove bluetooth device
                                        {
                                            emulator.RemoveDevice(otherItem);
                                            break;
                                        }
                                    }
                                }
                            }

                            // Get address of detected primary radio
                            var hostAddress = new PhysicalAddress(BitConverter
                                                                  .GetBytes(BluetoothAdapter.GetDefault().BluetoothAddress).Take(6).Reverse()
                                                                  .ToArray());

                            if (!item.HostAddress.Equals(hostAddress))
                            {
                                Log.Information("Auto-pairing device {Device} to {HostAddress}",
                                                item, hostAddress.AsFriendlyName());

                                // Pair USB device
                                item.PairTo(hostAddress);
                            }
                            else
                            {
                                Log.Information("Device {Device} already paired to this radio {HostAddress}.", item, hostAddress.AsFriendlyName());
                            }
                        }
                        else
                        {
                            Log.Warning("Auto-pairing not supported as BthPS3 and/or Bluetooth Host Radio not found");
                        }

                        foreach (var plugin in SinkPlugins.Where(p => p.Value.IsEnabled).Select(p => p.Value))
                        {
                            plugin.DeviceArrived(item);
                        }
                    }

                    break;

                case NotifyCollectionChangedAction.Remove:
                    foreach (IDualShockDevice item in args.OldItems)
                    {
                        Log.Information("Device {Device} got removed via {ConnectionType}", item,
                                        item.ConnectionType);
                        foreach (var plugin in SinkPlugins.Where(p => p.Value.IsEnabled).Select(p => p.Value))
                        {
                            plugin.DeviceRemoved(item);
                        }
                    }

                    break;
                }

                UpdateLEDS();
            };

            #region MEF

            //Creating an instance of aggregate catalog. It aggregates other catalogs
            var aggregateCatalog = new AggregateCatalog();

            //Load parts from the current assembly if available
            var asmCatalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());

            //Add to the aggregate catalog
            aggregateCatalog.Catalogs.Add(new DirectoryCatalog(SourcesPath, "*.dll"));
            aggregateCatalog.Catalogs.Add(new DirectoryCatalog(SinksPath, "*.dll"));
            aggregateCatalog.Catalogs.Add(asmCatalog);

            //Crete the composition container
            var container = new CompositionContainer(aggregateCatalog);

            // Composable parts are created here i.e.
            // the Import and Export components assembles here
            container.ComposeParts(this);

            #endregion

            // Log loaded sink plugins
            foreach (var plugin in SinkPlugins.Where(p => p.Value.IsEnabled))
            {
                Log.Information("Loaded sink plugin {Plugin}", plugin.Metadata["Name"]);

                plugin.Value.RumbleRequestReceived += (sender, args) =>
                                                      _childDevices[(IDualShockDevice)sender].Rumble(args.LargeMotor, args.SmallMotor);
            }

            // Log and enable sources
            foreach (var emulator in BusEmulators.Where(p => p.Value.IsEnabled))
            {
                Log.Information("Loaded bus emulator {Emulator}", emulator.Metadata["Name"]);

                emulator.Value.ChildDeviceAttached += (sender, args) => _childDevices.Add(args.Device);
                emulator.Value.ChildDeviceRemoved  += (sender, args) => _childDevices.Remove(args.Device);
                emulator.Value.InputReportReceived += EmulatorOnInputReportReceived;

                try
                {
                    Log.Information("Starting bus emulator {Emulator}", emulator.Metadata["Name"]);
                    emulator.Value.Start();
                    Log.Information("Bus emulator {Emulator} started successfully", emulator.Metadata["Name"]);
                }
                catch (Exception ex)
                {
                    Log.Error("Failed to start {@emulator}: {@ex}", emulator.Metadata["Name"], ex);
                }
            }

            #region IPC

            if (Config.Global.Core.Halibut.IsEnabled)
            {
                var services = new DelegateServiceFactory();
                services.Register <IPairingService>(() =>
                {
                    var service = new PairingService();

                    service.DeviceListRequested += (sender, args) => _childDevices
                                                   .Where(d => d.ConnectionType.Equals(DualShockConnectionType.USB))
                                                   .Select(d => new DualShockDeviceDescriptor
                    {
                        ClientAddress  = new UniqueAddress(d.ClientAddress),
                        ConnectionType = d.ConnectionType,
                        DeviceType     = d.DeviceType,
                        HostAddress    = new UniqueAddress(d.HostAddress)
                    }).ToList();

                    service.DevicePairingRequested += (device, args) =>
                                                      _childDevices[device.ClientAddress].PairTo(new PhysicalAddress(args.HostAddress.AddressBytes));

                    return(service);
                });

                _ipcServer = new HalibutRuntime(services, Configuration.ServerCertificate);
                _ipcServer.Listen(Configuration.ServerEndpoint);
                _ipcServer.Trust(Configuration.ClientCertificate.Thumbprint);
            }

            #endregion

            Task.Factory.StartNew(PollBatteryLevel, _inputCancellationTokenSource.Token);
        }