/// <summary> /// Converts and sends the specified iTachCmd to the BroadLink RM Device. /// </summary> /// <param name="commandShortName">Short name of the command to send.</param> /// <param name="repeat">(Optional, 0-255) Override the number of command repeats specified in the RMCommand. 0 = no repeat, 1 = send twice, etc.</param> public void SendCommandSync(string commandShortName, byte?repeat = null) { if (commandShortName == null) { Logger.Debug("BroadLink command null is not valid"); return; } RMCommand rm = GetCommandFromShortName(commandShortName); if (rm != null) { SendCommandSync(rm, repeat); } else { Logger.Debug("BroadLink command \"" + commandShortName + "\" not found"); } }
private RMCommand GetCommandFromShortName(string name) { BroadLinkCmd broadLinkCmd = BroadLinkCommands.commands.Get(name); if (broadLinkCmd != null) { RMCommand rm = new RMCommand(); rm.Type = broadLinkCmd.type; rm.RepeatCount = broadLinkCmd.repeat; rm.SetPulses(broadLinkCmd.codes); return(rm); } if (iTachCommands.commands.TryGetValue(name, out iTachCmd cmd)) { RMCommand rm = new RMCommand(); rm.Type = RMCommandType.IR; rm.RepeatCount = cmd.Repeat; rm.SetPulses(cmd.GetMicrosecondPulses()); return(rm); } return(null); }
/// <summary> /// Sends the specified RMCommand. May throw an exception if there is a network error. /// </summary> /// <param name="cmd">The command to send.</param> /// <param name="repeat">(Optional, 0-255) Override the number of command repeats specified in the RMCommand. 0 = no repeat, 1 = send twice, etc.</param> /// <returns></returns> public void SendCommandSync(RMCommand cmd, byte?repeat = null) { if (device == null || consecutiveCommandFailures >= 5) { LoadDeviceInfo_Throttled(); if (device == null) { Logger.Debug("BroadLinkController \"" + host + "\" failed to send a command because the device is not responding."); return; } } lock (myLock) { if (repeat != null && cmd.RepeatCount != repeat.Value) { cmd = JsonConvert.DeserializeObject <RMCommand>(JsonConvert.SerializeObject(cmd)); cmd.RepeatCount = repeat.Value; } bool success = false; try { Task <bool> sendTask = device.SendRemoteCommandAsync(cmd); sendTask.Wait(); success = sendTask.Result; } catch (Exception) { } if (success) { consecutiveCommandFailures = 0; } else { consecutiveCommandFailures++; Logger.Debug("BroadLinkController \"" + host + "\" failed to send a command. Consecutive failure #" + consecutiveCommandFailures); } } }