示例#1
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
        }
 public void Reset()
 {
     _config = null;
     File.Delete("SessionToken");
     Connect();
 }