Client to interrogate and control Logitech Harmony Hub.
Пример #1
0
        /// <summary>
        ///     Create a harmony client via myharmony.com authenification
        /// </summary>
        /// <param name="host">IP or hostname</param>
        /// <param name="username">myharmony.com username (email)</param>
        /// <param name="password">myharmony.com password</param>
        /// <param name="port">Port to connect to, default 5222</param>
        /// <returns>HarmonyClient</returns>
        public static async Task <HarmonyClient> Create(string host, string username, string password, int port = 5222)
        {
            string userAuthToken = await HarmonyAuthentication.GetUserAuthToken(username, password);

            if (string.IsNullOrEmpty(userAuthToken))
            {
                throw new Exception("Could not get token from Logitech server.");
            }

            // Make a guest connection only to exchange the session token via the user authentication token
            string sessionToken;

            using (var client = new HarmonyClient(host, "guest", port))
            {
                sessionToken = await client.SwapAuthToken(userAuthToken).ConfigureAwait(false);
            }

            if (string.IsNullOrEmpty(sessionToken))
            {
                throw new Exception("Could not swap token on Harmony Hub.");
            }

            // Create the client with the session token
            return(new HarmonyClient(host, sessionToken, port));
        }
Пример #2
0
        /// <summary>
        ///     Create a harmony client via myharmony.com authenification
        /// </summary>
        /// <param name="host">IP or hostname</param>
        /// <param name="port">Port to connect to, default 5222</param>
        /// <returns>HarmonyClient</returns>
        public static async Task <HarmonyClient> Create(string host, int port = 5222)
        {
            // Make a guest connection only to exchange the session token via the user authentication token
            string sessionToken;

            using (var client = new HarmonyClient(host, "guest", port))
            {
                sessionToken = await client.CreateToken().ConfigureAwait(false);
            }

            if (string.IsNullOrEmpty(sessionToken))
            {
                throw new Exception("Could not get token from Harmony Hub.");
            }

            // Create the client with the session token
            return(new HarmonyClient(host, sessionToken, port));
        }
Пример #3
0
        /// <summary>
        ///     Create a harmony client via myharmony.com authenification
        /// </summary>
        /// <param name="host">IP or hostname</param>
        /// <param name="port">Port to connect to, default 5222</param>
        /// <returns>HarmonyClient</returns>
        public static async Task<HarmonyClient> Create(string host, int port = 5222)
        {
            // Make a guest connection only to exchange the session token via the user authentication token
            string sessionToken;
            using (var client = new HarmonyClient(host, "guest", port))
            {
                sessionToken = await client.CreateToken().ConfigureAwait(false);
            }

            if (string.IsNullOrEmpty(sessionToken))
            {
                throw new Exception("Could not get token from Harmony Hub.");
            }

            // Create the client with the session token
            return new HarmonyClient(host, sessionToken, port);
        }
Пример #4
0
        /// <summary>
        ///     Create a harmony client via myharmony.com authenification
        /// </summary>
        /// <param name="host">IP or hostname</param>
        /// <param name="username">myharmony.com username (email)</param>
        /// <param name="password">myharmony.com password</param>
        /// <param name="port">Port to connect to, default 5222</param>
        /// <returns>HarmonyClient</returns>
        public static async Task<HarmonyClient> Create(string host, string username, string password, int port = 5222)
        {
            string userAuthToken = await HarmonyAuthentication.GetUserAuthToken(username, password);
            if (string.IsNullOrEmpty(userAuthToken))
            {
                throw new Exception("Could not get token from Logitech server.");
            }

            // Make a guest connection only to exchange the session token via the user authentication token
            string sessionToken;
            using (var client = new HarmonyClient(host, "guest", port))
            {
                sessionToken = await client.SwapAuthToken(userAuthToken).ConfigureAwait(false);
            }

            if (string.IsNullOrEmpty(sessionToken))
            {
                throw new Exception("Could not swap token on Harmony Hub.");
            }

            // Create the client with the session token
            return new HarmonyClient(host, sessionToken, port);
        }
Пример #5
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
        }
Пример #6
0
        private HarmonyClient GetHarmonyClient(User user)
        {
            return ScopingModule.Application.Ensure<HarmonyClient>(user.ID.ToString(), () =>
            {
                var auth = new HarmonyAuthenticationClient(user.Hostname, 5222);

                string sessionToken = auth.SwapAuthToken(user.HarmonyToken);
                if (string.IsNullOrEmpty(sessionToken))
                {
                    throw new Exception("Could not swap token on Harmony Hub.");
                }

                var r = new HarmonyClient(user.Hostname, 5222, sessionToken);

                r.GetConfig();

                return r;
            });
        }