private async Task ConnectAsync() { toolStripStatusLabelConnection.Text = "Connecting... "; //First create our client and login if (File.Exists("SessionToken")) { var sessionToken = File.ReadAllText("SessionToken"); Console.WriteLine("Reusing token: {0}", sessionToken); toolStripStatusLabelConnection.Text += $"Reusing token: {sessionToken}"; Program.Client = HarmonyClient.Create(textBoxHarmonyHubAddress.Text, sessionToken); } else { if (string.IsNullOrEmpty(textBoxPassword.Text)) { toolStripStatusLabelConnection.Text = "Credentials missing!"; return; } toolStripStatusLabelConnection.Text += "authenticating with Logitech servers..."; Program.Client = await HarmonyClient.Create(textBoxHarmonyHubAddress.Text, textBoxUserName.Text, textBoxPassword.Text); File.WriteAllText("SessionToken", Program.Client.Token); } toolStripStatusLabelConnection.Text = "Fetching Harmony Hub configuration..."; //Fetch our config var harmonyConfig = await Program.Client.GetConfigAsync(); PopulateTreeViewConfig(harmonyConfig); toolStripStatusLabelConnection.Text = "Ready"; }
private async Task HarmonyConnectAsync(bool shouldUpdate = true) { var currentActivityID = ""; formMain.BeginInvoke(new Action(() => { formMain.labelHarmonyStatus.Text = "Disconnected"; formMain.labelHarmonyStatus.ForeColor = System.Drawing.Color.Maroon; Logging.writeLog("Harmony: Connecting to Hub"); })); try { Program.Client = await HarmonyClient.Create(Properties.Settings.Default.harmonyHubIP); await doDelay(2000); currentActivityID = await Program.Client.GetCurrentActivityAsync(); lastHarmonyActivityID = currentActivityID.ToString(); formMain.BeginInvoke(new Action(() => { formMain.labelHarmonyStatus.Text = "Connected"; formMain.labelHarmonyStatus.ForeColor = System.Drawing.Color.ForestGreen; Logging.writeLog("Harmony: Connected to Hub, current activity ID is '" + currentActivityID + "'"); })); if (currentActivityID != "-1") { formMain.BeginInvoke(new Action(() => { if (formMain.labelRoomOccupancy.Text != "Occupied") { formMain.insteonDoMotion(false); } })); } } catch { formMain.BeginInvoke(new Action(() => { Logging.writeLog("Harmony: Cannot connect to Harmony Hub"); })); return; } await doDelay(3000); if (Program.Client != null) { Program.Client.OnActivityChanged += HarmonyClient_OnActivityChanged; } if (shouldUpdate) { formMain.BeginInvoke(new Action(() => { formMain.HarmonyUpdateActivities(currentActivityID); })); } }
private void treeViewConfig_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) { var tag = e.Node.Tag as Function; if (tag != null && e.Node.Parent.Parent.Tag is Device) { Function f = tag; Device d = (Device)e.Node.Parent.Parent.Tag; tspLabelCmdDesc.Text = $"Device: {d.Id} Cmd: {f.Action.Command}"; richTextBoxCmd.Text = HarmonyClient.GetPressCmd(d.Id, f.Action.Command); } }
private void Connect() { const int harmonyPort = 5222; if (File.Exists("SessionToken")) { sessionToken = File.ReadAllText("SessionToken"); Console.WriteLine("Reusing token: {0}", sessionToken); } else { sessionToken = LoginToLogitech(username, password, ipAddress, harmonyPort); } client = new HarmonyClient(ipAddress, harmonyPort, sessionToken); IsConnected = true; }
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; })); }
public SearchChainsEntries(HarmonyClient harmony) { Harmony = harmony; }
public GetChainsFirstEntry(HarmonyClient harmony) { Harmony = harmony; }
public ReplaceIdentityKey(HarmonyClient harmony) { Harmony = harmony; }
public APIInfo(HarmonyClient harmony) { Harmony = harmony; }
public GetChainInfo(HarmonyClient harmony) { Harmony = harmony; }
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, options.Username, options.Password); 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); } }
public CreateAnEntry(HarmonyClient harmony) { Harmony = harmony; }
public GetChainsEntries(HarmonyClient harmony) { Harmony = harmony; }
public GetIdentityChainInfo(HarmonyClient harmony) { Harmony = harmony; }
public GetKeyInfo(HarmonyClient harmony) { Harmony = harmony; }
public CreateAChain(HarmonyClient harmony) { Harmony = harmony; }
public GetAllChains(HarmonyClient harmony) { Harmony = harmony; }
private bool Command(User user, string name, Dictionary <string, string> values, out string speech) { bool success = false; speech = null; lock (_lock) { var client = ScopingModule.Application.Ensure <HarmonyClient>(user.ID.ToString(), () => { string sessionToken = LoginToLogitech(user.HarmonyToken, user.Hostname, 5222); var r = new HarmonyClient(user.Hostname, 5222, sessionToken); r.GetConfig(); return(r); }); var config = new JavaScriptSerializer().Deserialize <HarmonyConfigResult>(client.Config); string s; // Shortcuts switch (name) { case "PlayIntent": name = "ButtonIntent"; values["Button"] = "play"; speech = "Playing"; break; case "PauseIntent": name = "ButtonIntent"; values["Button"] = "pause"; speech = "Pausing"; break; } // Commands switch (name) { case "ButtonIntent": { string buttonName = (values.TryGetValue("Button", out s) ? s : "Pause").ToLower(); if (config != null && config.activity != null) { client.GetCurrentActivity(); if (client.CurrentActivity != null) { var activity = config.activity .FirstOrDefault(o => o.id == client.CurrentActivity); if (activity != null) { var buttons = activity.controlGroup .Where(o => o.function != null) .SelectMany(o => o.function) .Where(o => o.action != null) .ToList(); var button = buttons .OrderBy(o => { var label = (o.label ?? "") .ToLower() .Replace("direction ", ""); return(Distance(buttonName, label)); }) .FirstOrDefault(); if (button != null) { var action = new JavaScriptSerializer().Deserialize <HarmonyAction>(button.action); if (action != null) { client.PressButton(action.deviceId, action.command); speech = speech ?? "Pressing the " + button.label + " button"; success = true; } } } } } } break; case "ActivityIntent": { string activityName = (values.TryGetValue("Activity", out s) ? s : "TV").ToLower(); if (config != null && config.activity != null) { var activity = config.activity .Where(o => o.id != "-1") .OrderBy(o => Distance(activityName, (o.label ?? "").ToLower())) .FirstOrDefault(); if (activity != null) { client.StartActivity(activity.id); speech = "Starting the " + activity.label + " activity"; success = true; } } } break; case "OffIntent": client.TurnOff(); speech = "Powering off"; break; } } return(success); }
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 CreateIdentityChain(HarmonyClient harmony) { Harmony = harmony; }
public GetEntryInfo(HarmonyClient harmony) { Harmony = harmony; }
public GetIdentityChainsKeys(HarmonyClient harmony) { Harmony = harmony; }