Пример #1
0
 static async Task LoadConfigAsync(string file)
 {
     using (var sr = new StreamReader(file, Encoding.UTF8))
         using (var strreader = new StringReader(await sr.ReadToEndAsync()))
         {
             var d = new Deserializer();
             Config = d.Deserialize <HueConfigFile>(strreader);
         }
 }
Пример #2
0
        static async Task Main(string[] o_args)
        {
            Console.OutputEncoding = Encoding.UTF8;
            Console.InputEncoding  = Encoding.UTF8;

            string[] args;
            bool     regist  = false;
            bool     help    = false;
            int      timeout = 5;

            OptionSet o = new OptionSet()
            {
                { "timeout=", "Set time out", (int v) => timeout = v },
                { "regist", "Regist Hue Bridge", v => regist = v != null },
                { "h|help", "Get help", v => help = v != null },
            };

            try
            {
                args = o.Parse(o_args).ToArray();
            }
            catch (OptionException ex)
            {
                Console.WriteLine(ex.Message);
                Environment.Exit(1);
                return;
            }

            if (help)
            {
                WriteHelp();
                return;
            }

            string appConfigPath = Path.Combine(ApplicationDirectory, "hueconfig.yaml");

            if (!regist && !File.Exists(appConfigPath))
            {
                Console.WriteLine("Not configurated");
                Console.WriteLine("use \"--regist\" option to regist hue.");
                Environment.Exit(2);
                return;
            }

            HueController.TimeOut = TimeSpan.FromSeconds(timeout);

            try
            {
                var h = new HueController();
                await h.GetClientAsync();

                if (regist)
                {
                    string key = await h.RegistAsync();

                    Config = new HueConfigFile();
                    Config.ApplicationKey = key;
                    await SaveConfigAsync(appConfigPath);

                    Console.WriteLine("Registed");
                    Environment.Exit(0);
                }
                else
                {
                    await LoadConfigAsync(appConfigPath);

                    h.Connect();
                }

                if (args.Length < 1)
                {
                    Console.WriteLine("No args");
                    Console.WriteLine("use \"--help\" option to check usage.");
                    Environment.Exit(7);
                }

                if (args[0] == "list")
                {
                    await h.GetLightsListAsync();

                    return;
                }

                string lid = args[0];

                var cmd = ArgsParser.ParseAsync(args.Skip(1).ToArray());

                if (cmd != null)
                {
                    await h.ControlLight(lid, cmd);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Unknown Error");
                Console.WriteLine(ex.Message);
                Environment.Exit(1);
            }
        }