author | StephaneLenclud |
Tue, 30 Aug 2016 21:14:18 +0200 | |
changeset 261 | e2729a990e8b |
parent 257 | 3f1d16d233dc |
child 262 | c4749a27966d |
permissions | -rw-r--r-- |
StephaneLenclud@236 | 1 |
using Ear = SharpLib.Ear; |
StephaneLenclud@236 | 2 |
using SharpLib.Ear; |
StephaneLenclud@236 | 3 |
using System; |
StephaneLenclud@236 | 4 |
using System.Collections.Generic; |
StephaneLenclud@253 | 5 |
using System.Diagnostics; |
StephaneLenclud@236 | 6 |
using System.Linq; |
StephaneLenclud@236 | 7 |
using System.Text; |
StephaneLenclud@236 | 8 |
using System.Threading.Tasks; |
StephaneLenclud@236 | 9 |
using System.Runtime.Serialization; |
StephaneLenclud@250 | 10 |
using System.Windows.Forms; |
StephaneLenclud@236 | 11 |
|
StephaneLenclud@236 | 12 |
namespace SharpDisplayManager |
StephaneLenclud@236 | 13 |
{ |
StephaneLenclud@236 | 14 |
[DataContract] |
StephaneLenclud@236 | 15 |
[AttributeObject(Id = "Harmony.Command", Name = "Harmony Command", Description = "Send a command to your Logitech Harmony Hub.")] |
StephaneLenclud@236 | 16 |
class ActionHarmonyCommand : Ear.Action |
StephaneLenclud@236 | 17 |
{ |
StephaneLenclud@236 | 18 |
[DataMember] |
StephaneLenclud@236 | 19 |
public string DeviceId { get; set; } = ""; |
StephaneLenclud@236 | 20 |
|
StephaneLenclud@236 | 21 |
[DataMember] |
StephaneLenclud@257 | 22 |
public string Command { get; set; } = ""; |
StephaneLenclud@236 | 23 |
|
StephaneLenclud@250 | 24 |
[DataMember] |
StephaneLenclud@250 | 25 |
[AttributeObjectProperty( |
StephaneLenclud@250 | 26 |
Id = "Harmony.Command.SelectCommand", |
StephaneLenclud@250 | 27 |
Name = "Select command", |
StephaneLenclud@250 | 28 |
Description = "Click to select a command." |
StephaneLenclud@250 | 29 |
)] |
StephaneLenclud@250 | 30 |
public PropertyButton SelectCommand { get; set; } = new PropertyButton { Text = "None" }; |
StephaneLenclud@250 | 31 |
|
StephaneLenclud@236 | 32 |
/// <summary> |
StephaneLenclud@236 | 33 |
/// |
StephaneLenclud@236 | 34 |
/// </summary> |
StephaneLenclud@236 | 35 |
/// <returns></returns> |
StephaneLenclud@236 | 36 |
public override string Brief() |
StephaneLenclud@236 | 37 |
{ |
StephaneLenclud@238 | 38 |
string brief="Harmony: "; |
StephaneLenclud@236 | 39 |
|
StephaneLenclud@236 | 40 |
if (Program.HarmonyConfig != null) |
StephaneLenclud@236 | 41 |
{ |
StephaneLenclud@236 | 42 |
//What if the device ID is not there anymore? |
StephaneLenclud@236 | 43 |
brief += Program.HarmonyConfig.DeviceNameFromId(DeviceId); |
StephaneLenclud@236 | 44 |
} |
StephaneLenclud@236 | 45 |
else |
StephaneLenclud@236 | 46 |
{ |
StephaneLenclud@236 | 47 |
//No config found just show the device ID then. |
StephaneLenclud@236 | 48 |
brief += DeviceId; |
StephaneLenclud@236 | 49 |
} |
StephaneLenclud@236 | 50 |
|
StephaneLenclud@257 | 51 |
// TODO: Fetch function label from command |
StephaneLenclud@257 | 52 |
brief += " do " + Command; |
StephaneLenclud@236 | 53 |
|
StephaneLenclud@236 | 54 |
return brief; |
StephaneLenclud@236 | 55 |
} |
StephaneLenclud@236 | 56 |
|
StephaneLenclud@250 | 57 |
|
StephaneLenclud@250 | 58 |
protected override void DoConstruct() |
StephaneLenclud@250 | 59 |
{ |
StephaneLenclud@250 | 60 |
base.DoConstruct(); |
StephaneLenclud@250 | 61 |
|
StephaneLenclud@250 | 62 |
if (SelectCommand == null) |
StephaneLenclud@250 | 63 |
{ |
StephaneLenclud@250 | 64 |
SelectCommand = new PropertyButton { Text = "None"}; |
StephaneLenclud@250 | 65 |
} |
StephaneLenclud@250 | 66 |
SelectCommand.ClickEventHandler = ClickEventHandler; |
StephaneLenclud@250 | 67 |
} |
StephaneLenclud@250 | 68 |
|
StephaneLenclud@236 | 69 |
/// <summary> |
StephaneLenclud@236 | 70 |
/// |
StephaneLenclud@236 | 71 |
/// </summary> |
StephaneLenclud@258 | 72 |
protected override async Task DoExecute() |
StephaneLenclud@236 | 73 |
{ |
StephaneLenclud@236 | 74 |
if (Program.HarmonyClient!=null) |
StephaneLenclud@236 | 75 |
{ |
StephaneLenclud@258 | 76 |
// Send our command and wait for it async |
StephaneLenclud@258 | 77 |
await Program.HarmonyClient.TrySendKeyPressAsync(DeviceId, Command); |
StephaneLenclud@236 | 78 |
} |
StephaneLenclud@236 | 79 |
else |
StephaneLenclud@236 | 80 |
{ |
StephaneLenclud@253 | 81 |
Trace.WriteLine("WARNING: No Harmony client connection."); |
StephaneLenclud@256 | 82 |
} |
StephaneLenclud@236 | 83 |
} |
StephaneLenclud@236 | 84 |
|
StephaneLenclud@250 | 85 |
|
StephaneLenclud@250 | 86 |
/// <summary> |
StephaneLenclud@250 | 87 |
/// |
StephaneLenclud@250 | 88 |
/// </summary> |
StephaneLenclud@250 | 89 |
/// <returns></returns> |
StephaneLenclud@250 | 90 |
public override bool IsValid() |
StephaneLenclud@250 | 91 |
{ |
StephaneLenclud@250 | 92 |
if (Program.HarmonyConfig != null) |
StephaneLenclud@250 | 93 |
{ |
StephaneLenclud@250 | 94 |
foreach (HarmonyHub.Device d in Program.HarmonyConfig.Devices) |
StephaneLenclud@250 | 95 |
{ |
StephaneLenclud@250 | 96 |
if (d.Id.Equals(DeviceId)) |
StephaneLenclud@250 | 97 |
{ |
StephaneLenclud@250 | 98 |
foreach (HarmonyHub.ControlGroup cg in d.ControlGroups) |
StephaneLenclud@250 | 99 |
{ |
StephaneLenclud@250 | 100 |
foreach (HarmonyHub.Function f in cg.Functions) |
StephaneLenclud@250 | 101 |
{ |
StephaneLenclud@257 | 102 |
if (f.Action.Command.Equals(Command)) |
StephaneLenclud@250 | 103 |
{ |
StephaneLenclud@250 | 104 |
//We found our device and our function |
StephaneLenclud@250 | 105 |
return true; |
StephaneLenclud@250 | 106 |
} |
StephaneLenclud@250 | 107 |
} |
StephaneLenclud@250 | 108 |
} |
StephaneLenclud@250 | 109 |
} |
StephaneLenclud@250 | 110 |
} |
StephaneLenclud@250 | 111 |
} |
StephaneLenclud@250 | 112 |
|
StephaneLenclud@250 | 113 |
return false; |
StephaneLenclud@250 | 114 |
} |
StephaneLenclud@250 | 115 |
|
StephaneLenclud@257 | 116 |
/// <summary> |
StephaneLenclud@257 | 117 |
/// |
StephaneLenclud@257 | 118 |
/// </summary> |
StephaneLenclud@257 | 119 |
/// <param name="sender"></param> |
StephaneLenclud@257 | 120 |
/// <param name="e"></param> |
StephaneLenclud@250 | 121 |
void ClickEventHandler(object sender, EventArgs e) |
StephaneLenclud@250 | 122 |
{ |
StephaneLenclud@250 | 123 |
FormSelectHarmonyCommand dlg = new FormSelectHarmonyCommand(); |
StephaneLenclud@250 | 124 |
DialogResult res = CodeProject.Dialog.DlgBox.ShowDialog(dlg); |
StephaneLenclud@250 | 125 |
if (res == DialogResult.OK) |
StephaneLenclud@250 | 126 |
{ |
StephaneLenclud@250 | 127 |
DeviceId = dlg.DeviceId; |
StephaneLenclud@257 | 128 |
Command = dlg.Command; |
StephaneLenclud@250 | 129 |
SelectCommand.Text = Brief(); |
StephaneLenclud@250 | 130 |
//Tell observer the object itself changed |
StephaneLenclud@250 | 131 |
OnPropertyChanged("Brief"); |
StephaneLenclud@250 | 132 |
} |
StephaneLenclud@250 | 133 |
|
StephaneLenclud@250 | 134 |
} |
StephaneLenclud@250 | 135 |
|
StephaneLenclud@236 | 136 |
} |
StephaneLenclud@236 | 137 |
} |