Beispiel #1
0
        public static async Task MainAsync(string[] args)
        {
            var options = new Options();
            if (!Parser.Default.ParseArguments(args, options))
            {
                return;
            }

            HarmonyClient client;
            if (File.Exists("SessionToken"))
            {
                var sessionToken = File.ReadAllText("SessionToken");
                Console.WriteLine("Reusing token: {0}", sessionToken);
                client = HarmonyClient.Create(options.IpAddress, sessionToken);
            }
            else
            {
                client = await HarmonyClient.Create(options.IpAddress);
                File.WriteAllText("SessionToken", client.Token);
            }

            using (client)
            {
                string deviceId = options.DeviceId;
                string activityId = options.ActivityId;
                // do we need to grab the config first?
                Config harmonyConfig = null;
                if (!string.IsNullOrEmpty(deviceId) || options.GetActivity || !string.IsNullOrEmpty(options.ListType))
                {
                    harmonyConfig = await client.GetConfigAsync();
                }

                // Monitor activity changes
                client.OnActivityChanged += (sender, activity) =>
                {
                    Console.WriteLine("The current activity is now: " + (harmonyConfig?.ActivityNameFromId(activity) ?? activity));
                };

                if (!string.IsNullOrEmpty(deviceId) && !string.IsNullOrEmpty(options.Command))
                {
                    await client.SendKeyPressAsync(deviceId, options.Command);
                }

                if (null != harmonyConfig && !string.IsNullOrEmpty(deviceId) && string.IsNullOrEmpty(options.Command))
                {
                    // just list device control options
                    foreach (var device in harmonyConfig.Devices.Where(device => device.Id == deviceId))
                    {
                        foreach (var controlGroup in device.ControlGroups)
                        {
                            foreach (var function in controlGroup.Functions)
                            {
                                Console.WriteLine(function.ToString());
                            }
                        }
                    }
                }

                if (!string.IsNullOrEmpty(activityId))
                {
                    await client.StartActivityAsync(activityId);
                }

                if (null != harmonyConfig && options.GetActivity)
                {
                    var currentActivity = await client.GetCurrentActivityAsync();
                    Console.WriteLine("Current Activity: {0}", harmonyConfig.ActivityNameFromId(currentActivity));
                }

                if (options.TurnOff)
                {
                    await client.TurnOffAsync();
                }

                if (null != harmonyConfig && !string.IsNullOrEmpty(options.ListType))
                {
                    if (!options.ListType.Equals("d") && !options.ListType.Equals("a")) return;

                    if (options.ListType.Equals("a"))
                    {
                        Console.WriteLine("Activities:");
                        foreach (var activity in harmonyConfig.Activities.OrderBy(x => x.ActivityOrder))
                        {
                            Console.WriteLine(" {0}:{1}", activity.Id, activity.Label);
                        }
                    }

                    if (options.ListType.Equals("d"))
                    {
                        Console.WriteLine("Devices:");
                        foreach (var device in harmonyConfig.Devices.OrderBy(x => x.Label))
                        {
                            Console.WriteLine(device.ToString());
                        }
                    }
                }
                Console.WriteLine("Press enter to disconnect");
                await Task.Run(() => Console.ReadLine()).ConfigureAwait(false);
            }
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            const int harmonyPort = 5222;

            var options = new Options();
            if (Parser.Default.ParseArguments(args, options))
            {
                Console.WriteLine();

                string ipAddress = options.IpAddress;
                string username = options.Username;
                string password = options.Password;

                string deviceId = options.DeviceId;
                string activityId = options.ActivityId;

                Dns.GetHostEntry(ipAddress);

                string sessionToken;

                if (File.Exists("SessionToken"))
                {
                    sessionToken = File.ReadAllText("SessionToken");
                    Console.WriteLine("Reusing token: {0}", sessionToken);
                }
                else
                {
                    sessionToken = LoginToLogitech(username, password, ipAddress, harmonyPort);
                }

                // do we need to grab the config first?
                HarmonyConfigResult harmonyConfig = null;

                HarmonyClient client = null;

                if (!string.IsNullOrEmpty(deviceId) || options.GetActivity || !string.IsNullOrEmpty(options.ListType))
                {
                    client = new HarmonyClient(ipAddress, harmonyPort, sessionToken);
                    client.GetConfig();

                    while (string.IsNullOrEmpty(client.Config)) { }
                    File.WriteAllText("HubConfig", client.Config);
                    harmonyConfig = new JavaScriptSerializer().Deserialize<HarmonyConfigResult>(client.Config);

                }

                if (!string.IsNullOrEmpty(deviceId) && !string.IsNullOrEmpty(options.Command))
                {
                    if (null == client) client = new HarmonyClient(ipAddress, harmonyPort, sessionToken);
                    //activityClient.PressButton("14766260", "Mute");
                    client.PressButton(deviceId, options.Command);
                }

                if (null != harmonyConfig && !string.IsNullOrEmpty(deviceId) && string.IsNullOrEmpty(options.Command))
                {
                    // just list device control options
                    foreach (var device in harmonyConfig.device.Where(device => device.id == deviceId))
                    {
                        foreach (Dictionary<string, object> controlGroup in device.controlGroup)
                        {
                            foreach (var o in controlGroup.Where(o => o.Key == "name"))
                            {
                                Console.WriteLine("{0}:{1}", o.Key, o.Value);
                            }
                        }
                    }
                }

                if (!string.IsNullOrEmpty(activityId))
                {
                    if (null == client) client = new HarmonyClient(ipAddress, harmonyPort, sessionToken);
                    client.StartActivity(activityId);
                }

                if (null != harmonyConfig && options.GetActivity)
                {
                    client.GetCurrentActivity();
                    // now wait for it to be populated
                    while (string.IsNullOrEmpty(client.CurrentActivity)) { }
                    Console.WriteLine("Current Activity: {0}", harmonyConfig.ActivityNameFromId(client.CurrentActivity));
                }

                if (options.TurnOff)
                {
                    if (null == client) client = new HarmonyClient(ipAddress, harmonyPort, sessionToken);
                    client.TurnOff();
                }

                if (null != harmonyConfig && !string.IsNullOrEmpty(options.ListType))
                {
                    if (!options.ListType.Equals("d") && !options.ListType.Equals("a")) return;

                    if (options.ListType.Equals("a"))
                    {
                        Console.WriteLine("Activities:");
                        harmonyConfig.activity.Sort();
                        foreach (var activity in harmonyConfig.activity)
                        {
                            Console.WriteLine(" {0}:{1}", activity.id, activity.label);
                        }
                    }

                    if (options.ListType.Equals("d"))
                    {
                        Console.WriteLine("Devices:");
                        harmonyConfig.device.Sort();
                        foreach (var device in harmonyConfig.device)
                        {
                            Console.WriteLine(" {0}:{1}", device.id, device.label);
                        }
                    }
                }

            } // option parsing
        }