/// <summary> /// Send message to HarmonyHub with UserAuthToken, wait for SessionToken /// </summary> /// <param name="userAuthToken"></param> /// <returns></returns> public string SwapAuthToken(string userAuthToken) { EnsureConnection(); var iqToSend = new IQ { Type = IqType.get, Namespace = "", From = "1", To = "guest" }; iqToSend.AddChild(HarmonyDocuments.LogitechPairDocument(userAuthToken)); iqToSend.GenerateId(); var iqGrabber = new IqGrabber(Xmpp); var iq = iqGrabber.SendIq(iqToSend, 5000); if (iq != null) { var match = IdentityRegex.Match(iq.InnerXml); if (match.Success) { return(match.Groups[1].ToString()); } } return(null); }
/// <summary> /// Send a command to the given device through your Harmony Hub. /// The returned task will complete once we receive acknowledgment from the Hub. /// </summary> /// <param name="deviceId">string with the ID of the device</param> /// <param name="command">string with the command for the device</param> /// <param name="press">true for press, false for release</param> /// <param name="timestamp">Timestamp for the command, e.g. send a press with 0 and a release with 100</param> public async Task SendCommandAsync(string deviceId, string command, bool press = true, int?timestamp = null) { Trace.WriteLine("Harmony-logs: SendCommandAsync"); var document = HarmonyDocuments.IrCommandDocument(deviceId, command, press, timestamp); await SendDocumentAsync(document, TaskType.SendCommmand).ConfigureAwait(false); }
/// <summary> /// Send message to HarmonyHub to request Configuration. /// Result is parsed by OnIq based on ClientCommandType /// </summary> public void GetConfig() { EnsureConnection(); var iqToSend = new IQ { Type = IqType.get, Namespace = "", From = "1", To = "guest" }; iqToSend.AddChild(HarmonyDocuments.ConfigDocument()); iqToSend.GenerateId(); var iqGrabber = new IqGrabber(Xmpp); var iq = iqGrabber.SendIq(iqToSend, 10000); if (iq != null) { var match = IdentityRegex.Match(iq.InnerXml); if (match.Success) { RawConfig = match.Groups[1].ToString(); Config = null; try { Config = new JavaScriptSerializer().Deserialize <HarmonyConfigResult>(RawConfig); } catch { } } } }
/// <summary> /// Get cmd string /// </summary> /// <param name="deviceId"></param> /// <param name="command"></param> /// <param name="timespan"></param> /// <returns></returns> public static string GetPressCmd(string deviceId, string command, int timespan = 100) { var now = (int)DateTime.Now.ToUniversalTime().Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds; var press = HarmonyDocuments.IrCommandDocument(deviceId, command, true, now - timespan); return(press.ToString()); }
/// <summary> /// Send a message that a button was pressed /// Result is parsed by OnIq based on ClientCommandType /// </summary> /// <param name="deviceId">string with the ID of the device</param> /// <param name="command">string with the command for the device</param> /// <param name="timespan">The time between the press and release, default 100ms</param> public async Task SendKeyPressAsync(string deviceId, string command, int timespan = 100) { var now = (int)DateTime.Now.ToUniversalTime().Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds; var press = HarmonyDocuments.IrCommandDocument(deviceId, command, true, now - timespan); await FireAndForgetAsync(press).ConfigureAwait(false); var release = HarmonyDocuments.IrCommandDocument(deviceId, command, false, timespan); await FireAndForgetAsync(release).ConfigureAwait(false); }
/// <summary> /// Request the configuration from the hub /// </summary> /// <returns>HarmonyConfig</returns> public async Task <Config> GetConfigAsync() { var iq = await RequestResponseAsync(HarmonyDocuments.ConfigDocument()).ConfigureAwait(false); var config = GetData(iq); if (config != null) { return(Serializer.FromJson <Config>(config)); } throw new Exception("No data found"); }
/// <summary> /// Send message to HarmonyHub to request current activity /// Result is parsed by OnIq based on ClientCommandType /// </summary> /// <returns>string with the current activity</returns> public async Task <string> GetCurrentActivityAsync() { var iq = await RequestResponseAsync(HarmonyDocuments.GetCurrentActivityDocument()).ConfigureAwait(false); var currentActivityData = GetData(iq); if (currentActivityData != null) { return(currentActivityData.Split('=')[1]); } throw new Exception("No data found in IQ"); }
/// <summary> /// Send a message that a button was pressed /// Result is parsed by OnIq based on ClientCommandType /// </summary> /// <param name="deviceId">string with the ID of the device</param> /// <param name="command">string with the command for the device</param> /// <param name="timespan">The time between the press and release, default 100ms</param> public async Task SendKeyPressAsync(string deviceId, string command, int timespan = 100) { Trace.WriteLine("Harmony: SendKeyPressAsync"); var now = (int)DateTime.Now.ToUniversalTime().Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds; var press = HarmonyDocuments.IrCommandDocument(deviceId, command, true, now - timespan); await SendDocumentAsync(press, TaskType.SendCommmand).ConfigureAwait(false); var release = HarmonyDocuments.IrCommandDocument(deviceId, command, false, now); await SendDocumentAsync(release, TaskType.SendCommmand).ConfigureAwait(false); }
/// <summary> /// Send message to HarmonyHub to request current activity /// Result is parsed by OnIq based on ClientCommandType /// </summary> /// <returns>string with the current activity</returns> public async Task <string> GetCurrentActivityAsync() { Trace.WriteLine("Harmony: GetCurrentActivityAsync"); var iq = await SendDocumentAsync(HarmonyDocuments.GetCurrentActivityDocument()).ConfigureAwait(false); var currentActivityData = GetData(iq.ResultIQ); if (currentActivityData != null) { return(currentActivityData.Split('=')[1]); } throw new Exception("No data found in IQ"); }
/// <summary> /// Send message to HarmonyHub to request to run a sequence /// Result is parsed by OnIq based on ClientCommandType /// </summary> /// <param name="deviceId"></param> /// <param name="command"></param> public void Sequence(int sequenceId) { EnsureConnection(); var iqToSend = new IQ { Type = IqType.get, Namespace = "", From = "1", To = "guest" }; iqToSend.AddChild(HarmonyDocuments.SequenceDocument(sequenceId)); iqToSend.GenerateId(); Xmpp.Send(iqToSend); }
/// <summary> /// Send message to HarmonyHub to request to press a button /// Result is parsed by OnIq based on ClientCommandType /// </summary> /// <param name="deviceId"></param> /// <param name="command"></param> public void PressButton(string deviceId, string command) { EnsureConnection(); var iqToSend = new IQ { Type = IqType.get, Namespace = "", From = "1", To = "guest" }; iqToSend.AddChild(HarmonyDocuments.IRCommandDocument(deviceId, command)); iqToSend.GenerateId(); Xmpp.Send(iqToSend); }
/// <summary> /// Send message to HarmonyHub to start a given activity /// Result is parsed by OnIq based on ClientCommandType /// </summary> /// <param name="activityId"></param> public void StartActivity(string activityId) { EnsureConnection(); var iqToSend = new IQ { Type = IqType.get, Namespace = "", From = "1", To = "guest" }; iqToSend.AddChild(HarmonyDocuments.StartActivityDocument(activityId)); iqToSend.GenerateId(); Xmpp.Send(iqToSend); }
/// <summary> /// Send message to HarmonyHub to start a given activity /// Result is parsed by OnIq based on ClientCommandType /// </summary> /// <param name="activityId"></param> public void StartActivity(string activityId) { _clientCommand = ClientCommandType.StartActivity; var iqToSend = new IQ { Type = IqType.get, Namespace = "", From = "1", To = "guest" }; iqToSend.AddChild(HarmonyDocuments.StartActivityDocument(activityId)); iqToSend.GenerateId(); var iqGrabber = new IqGrabber(Xmpp); iqGrabber.SendIq(iqToSend, 10); WaitForData(5); }
/// <summary> /// Send message to HarmonyHub to request Configuration. /// Result is parsed by OnIq based on ClientCommandType /// </summary> public void GetConfig() { _clientCommand = ClientCommandType.GetConfig; var iqToSend = new IQ { Type = IqType.get, Namespace = "", From = "1", To = "guest" }; iqToSend.AddChild(HarmonyDocuments.ConfigDocument()); iqToSend.GenerateId(); var iqGrabber = new IqGrabber(Xmpp); iqGrabber.SendIq(iqToSend, 10); WaitForData(5); }
/// <summary> /// Send message to HarmonyHub to request to press a button /// Result is parsed by OnIq based on ClientCommandType /// </summary> /// <param name="deviceId"></param> /// <param name="command"></param> public void PressButton(string deviceId, string command) { _clientCommand = ClientCommandType.PressButton; var iqToSend = new IQ { Type = IqType.get, Namespace = "", From = "1", To = "guest" }; iqToSend.AddChild(HarmonyDocuments.IrCommandDocument(deviceId, command)); iqToSend.GenerateId(); var iqGrabber = new IqGrabber(Xmpp); iqGrabber.SendIq(iqToSend, 5); WaitForData(5); }
/// <summary> /// Send message to HarmonyHub with UserAuthToken, wait for SessionToken /// </summary> /// <param name="userAuthToken"></param> /// <returns></returns> public string SwapAuthToken(string userAuthToken) { var iqToSend = new IQ { Type = IqType.get, Namespace = "", From = "1", To = "guest" }; iqToSend.AddChild(HarmonyDocuments.LogitechPairDocument(userAuthToken)); iqToSend.GenerateId(); var iqGrabber = new IqGrabber(Xmpp); iqGrabber.SendIq(iqToSend, 10); WaitForData(5); return(_sessionToken); }
/// <summary> /// Request the configuration from the hub /// </summary> /// <returns>HarmonyConfig</returns> public async Task <Config> GetConfigAsync() { Trace.WriteLine("Harmony-logs: GetConfigAsync"); Trace.WriteLine("Harmony: Fetching configuration..."); var iq = await SendDocumentAsync(HarmonyDocuments.ConfigDocument()).ConfigureAwait(false); Trace.WriteLine("Harmony: Parsing configuration..."); var rawConfig = GetData(iq.ResultIQ); if (rawConfig != null) { Config config = Serializer.FromJson <Config>(rawConfig); Trace.WriteLine("Harmony: Ready"); return(config); } throw new Exception("Harmony: Configuration not found"); }
/// <summary> /// Send message to HarmonyHub, wait for SessionToken /// </summary> /// <returns>session token</returns> public async Task <string> CreateToken() { var iq = await RequestResponseAsync(HarmonyDocuments.LogitechPairDocument()).ConfigureAwait(false); var sessionData = GetData(iq); if (sessionData != null) { foreach (var pair in sessionData.Split(':')) { if (pair.StartsWith("identity")) { return(pair.Split('=')[1]); } } } throw new Exception("Wrong data"); }
/// <summary> /// Send message to HarmonyHub to request current activity /// Result is parsed by OnIq based on ClientCommandType /// </summary> public void GetCurrentActivity() { EnsureConnection(); _clientCommand = ClientCommandType.GetCurrentActivity; var iqToSend = new IQ { Type = IqType.get, Namespace = "", From = "1", To = "guest" }; iqToSend.AddChild(HarmonyDocuments.GetCurrentActivityDocument()); iqToSend.GenerateId(); var iqGrabber = new IqGrabber(Xmpp); iqGrabber.SendIq(iqToSend, 10); WaitForData(5); }
/// <summary> /// Send message to HarmonyHub with UserAuthToken, wait for SessionToken /// </summary> /// <returns></returns> private async Task <string> PairAsync() { Trace.WriteLine("Harmony-logs: Pairing"); var response = await SendDocumentAsync(HarmonyDocuments.LogitechPairDocument()).ConfigureAwait(false); var sessionData = GetData(response.ResultIQ); if (sessionData != null) { foreach (var pair in sessionData.Split(':')) { if (pair.StartsWith("identity")) { return(pair.Split('=')[1]); } } } throw new Exception("Harmony: SwapAuthToken failed"); }
/// <summary> /// Send message to HarmonyHub to request current activity /// Result is parsed by OnIq based on ClientCommandType /// </summary> public void GetCurrentActivity() { EnsureConnection(); var iqToSend = new IQ { Type = IqType.get, Namespace = "", From = "1", To = "guest" }; iqToSend.AddChild(HarmonyDocuments.GetCurrentActivityDocument()); iqToSend.GenerateId(); var iqGrabber = new IqGrabber(Xmpp); var iq = iqGrabber.SendIq(iqToSend, 5000); if (iq != null) { var match = IdentityRegex.Match(iq.InnerXml); if (match.Success) { CurrentActivity = match.Groups[1].ToString().Split('=')[1]; } } }
/// <summary> /// Send message to HarmonyHub to start a given activity /// Result is parsed by OnIq based on ClientCommandType /// </summary> /// <param name="activityId">string</param> public async Task StartActivityAsync(string activityId) { Trace.WriteLine("Harmony: StartActivityAsync"); await SendDocumentAsync(HarmonyDocuments.StartActivityDocument(activityId)).ConfigureAwait(false); }
/// <summary> /// Send message to HarmonyHub to request to press a button /// Result is parsed by OnIq based on ClientCommandType /// </summary> /// <param name="deviceId">string with the ID of the device</param> /// <param name="command">string with the command for the device</param> /// <param name="press">true for press, false for release</param> /// <param name="timestamp">Timestamp for the command, e.g. send a press with 0 and a release with 100</param> public async Task SendCommandAsync(string deviceId, string command, bool press = true, int?timestamp = null) { var document = HarmonyDocuments.IrCommandDocument(deviceId, command, press, timestamp); await FireAndForgetAsync(document).ConfigureAwait(false); }
/// <summary> /// Send message to HarmonyHub to start a given activity /// Result is parsed by OnIq based on ClientCommandType /// </summary> /// <param name="activityId">string</param> public async Task StartActivityAsync(string activityId) { await RequestResponseAsync(HarmonyDocuments.StartActivityDocument(activityId)).ConfigureAwait(false); }