public void ShouldAllowPartKeysToHaveBooleanValuesToEnableThemWithoutSpecifyingConfig()
            {
                const string configString = @"
                {
                'Parts': {
                'Part1': true,
                'Part2': false
                }
                }";

                // Arrange
                var configFile = new TestTextFile(configString);

                // Act
                var config = new JsonRobotConfiguration(configFile);

                // Assert
                var part1 = config.Parts.First();
                Assert.Equal("Part1", part1.Name);
                Assert.True(part1.IsEnabled);

                var part2 = config.Parts.Last();
                Assert.Equal("Part2", part2.Name);
                Assert.False(part2.IsEnabled);
            }
            public void ShouldInitializeKeyValuePairsFromLoader()
            {
                const string configString = @"
                {
                'Settings': {
                'Global1': 42,
                'Global2': 'Foo'
                }
                }";

                // Arrange
                var configFile = new TestTextFile(configString);

                // Act
                var config = new JsonRobotConfiguration(configFile);

                // Assert
                Assert.Equal(42, config.GetSetting("Global1", Int32.Parse));
                Assert.Equal("Foo", config.GetSetting("Global2"));
            }
            public void ShouldInitializePartConfigurations()
            {
                const string configString = @"
                {
                'Parts': {
                'Part1': {
                'Enabled': true,
                'Settings': {
                'Local1': 42
                }
                },
                'Part2': {
                'Enabled': false,
                'Settings': {
                'Local2': 'Foo'
                }
                }
                }
                }";

                // Arrange
                var configFile = new TestTextFile(configString);

                // Act
                var config = new JsonRobotConfiguration(configFile);

                // Assert
                var part1 = config.Parts.First();
                Assert.Equal("Part1", part1.Name);
                Assert.True(part1.IsEnabled);
                Assert.Equal(42, part1.GetSetting("Local1", Int32.Parse));

                var part2 = config.Parts.Last();
                Assert.Equal("Part2", part2.Name);
                Assert.False(part2.IsEnabled);
                Assert.Equal("Foo", part2.GetSetting("Local2"));
            }
Exemple #4
0
        static async Task AsyncMain(string[] args)
        {
            TryLaunchDebugger(ref args);

            // Look for the configuration file
            string configPath = null;
            if (args.Length > 0)
            {
                configPath = args[0];
            } 
            else 
            {
                var userConfig = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "NuBot", ConfigFileName);
                var appConfig = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "NuBot", ConfigFileName);
                var localConfig = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ConfigFileName);
                if (File.Exists(userConfig))
                {
                    configPath = userConfig;
                }
                else if (File.Exists(appConfig))
                {
                    configPath = appConfig;
                }
                else
                {
                    configPath = localConfig;
                }
            }

            // Set up logging
            var logConfig = new DefaultLogConfiguration();

            // Load config file
            var configFile = new PhysicalTextFile(configPath);
            var config = new JsonRobotConfiguration(configFile);

            // Set up the assembler
            var log = logConfig.CreateLogger("Program");
            log.Info("Assembling NuBot from Config File: {0}", configPath);
            var assembler = new RobotAssembler(config, logConfig,
                partAssemblies: new[] {
                    typeof(Program).Assembly
                },
                partDirectories: new[] {
                    Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Parts")
                });

            // Make a NuBot!
            var robot = assembler.CreateRobot();

            // Start the NuBot and wait for shutdown
            try
            {
                robot.Start();
                log.Info("Press Ctrl-C to shut down the robot");
                await WaitForControlC();
                log.Info("Shutdown Request Recieved");
                robot.Stop();
            }
            catch (Exception ex)
            {
                while (ex != null)
                {
                    log.Error("{0} - {1}", ex.GetType().FullName, ex.Message);
                    ex = ex.InnerException;
                }
            }
            log.Info("Robot shut down.");
        }