author | StephaneLenclud |
Mon, 29 Aug 2016 17:36:02 +0200 | |
changeset 256 | 51b86efdc448 |
parent 253 | 2dae7a163fff |
child 257 | 3f1d16d233dc |
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@236 | 22 |
public string FunctionName { 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@236 | 51 |
brief += " do " + FunctionName; |
StephaneLenclud@236 | 52 |
|
StephaneLenclud@236 | 53 |
return brief; |
StephaneLenclud@236 | 54 |
} |
StephaneLenclud@236 | 55 |
|
StephaneLenclud@250 | 56 |
|
StephaneLenclud@250 | 57 |
protected override void DoConstruct() |
StephaneLenclud@250 | 58 |
{ |
StephaneLenclud@250 | 59 |
base.DoConstruct(); |
StephaneLenclud@250 | 60 |
|
StephaneLenclud@250 | 61 |
if (SelectCommand == null) |
StephaneLenclud@250 | 62 |
{ |
StephaneLenclud@250 | 63 |
SelectCommand = new PropertyButton { Text = "None"}; |
StephaneLenclud@250 | 64 |
} |
StephaneLenclud@250 | 65 |
SelectCommand.ClickEventHandler = ClickEventHandler; |
StephaneLenclud@250 | 66 |
} |
StephaneLenclud@250 | 67 |
|
StephaneLenclud@236 | 68 |
/// <summary> |
StephaneLenclud@236 | 69 |
/// |
StephaneLenclud@236 | 70 |
/// </summary> |
StephaneLenclud@236 | 71 |
protected override void DoExecute() |
StephaneLenclud@236 | 72 |
{ |
StephaneLenclud@236 | 73 |
//Fire and forget our command |
StephaneLenclud@236 | 74 |
//TODO: check if the harmony client connection is opened |
StephaneLenclud@236 | 75 |
if (Program.HarmonyClient!=null) |
StephaneLenclud@236 | 76 |
{ |
StephaneLenclud@256 | 77 |
// Wait synchronously for now until we figure out how we could do async stuff in EAR. |
StephaneLenclud@256 | 78 |
// TODO: Have an abort option in EAR. For instance we don't want to keep sending Harmony command if one failed. |
StephaneLenclud@256 | 79 |
Program.HarmonyClient.TrySendCommandAsync(DeviceId, FunctionName).Wait(10*1000); |
StephaneLenclud@236 | 80 |
} |
StephaneLenclud@236 | 81 |
else |
StephaneLenclud@236 | 82 |
{ |
StephaneLenclud@253 | 83 |
Trace.WriteLine("WARNING: No Harmony client connection."); |
StephaneLenclud@256 | 84 |
} |
StephaneLenclud@236 | 85 |
} |
StephaneLenclud@236 | 86 |
|
StephaneLenclud@250 | 87 |
|
StephaneLenclud@250 | 88 |
/// <summary> |
StephaneLenclud@250 | 89 |
/// |
StephaneLenclud@250 | 90 |
/// </summary> |
StephaneLenclud@250 | 91 |
/// <returns></returns> |
StephaneLenclud@250 | 92 |
public override bool IsValid() |
StephaneLenclud@250 | 93 |
{ |
StephaneLenclud@250 | 94 |
if (Program.HarmonyConfig != null) |
StephaneLenclud@250 | 95 |
{ |
StephaneLenclud@250 | 96 |
foreach (HarmonyHub.Device d in Program.HarmonyConfig.Devices) |
StephaneLenclud@250 | 97 |
{ |
StephaneLenclud@250 | 98 |
if (d.Id.Equals(DeviceId)) |
StephaneLenclud@250 | 99 |
{ |
StephaneLenclud@250 | 100 |
foreach (HarmonyHub.ControlGroup cg in d.ControlGroups) |
StephaneLenclud@250 | 101 |
{ |
StephaneLenclud@250 | 102 |
foreach (HarmonyHub.Function f in cg.Functions) |
StephaneLenclud@250 | 103 |
{ |
StephaneLenclud@250 | 104 |
if (f.Name.Equals(FunctionName)) |
StephaneLenclud@250 | 105 |
{ |
StephaneLenclud@250 | 106 |
//We found our device and our function |
StephaneLenclud@250 | 107 |
return true; |
StephaneLenclud@250 | 108 |
} |
StephaneLenclud@250 | 109 |
} |
StephaneLenclud@250 | 110 |
} |
StephaneLenclud@250 | 111 |
} |
StephaneLenclud@250 | 112 |
} |
StephaneLenclud@250 | 113 |
} |
StephaneLenclud@250 | 114 |
|
StephaneLenclud@250 | 115 |
return false; |
StephaneLenclud@250 | 116 |
} |
StephaneLenclud@250 | 117 |
|
StephaneLenclud@250 | 118 |
|
StephaneLenclud@250 | 119 |
void ClickEventHandler(object sender, EventArgs e) |
StephaneLenclud@250 | 120 |
{ |
StephaneLenclud@250 | 121 |
FormSelectHarmonyCommand dlg = new FormSelectHarmonyCommand(); |
StephaneLenclud@250 | 122 |
DialogResult res = CodeProject.Dialog.DlgBox.ShowDialog(dlg); |
StephaneLenclud@250 | 123 |
if (res == DialogResult.OK) |
StephaneLenclud@250 | 124 |
{ |
StephaneLenclud@250 | 125 |
DeviceId = dlg.DeviceId; |
StephaneLenclud@250 | 126 |
FunctionName = dlg.FunctionName; |
StephaneLenclud@250 | 127 |
SelectCommand.Text = Brief(); |
StephaneLenclud@250 | 128 |
//Tell observer the object itself changed |
StephaneLenclud@250 | 129 |
OnPropertyChanged("Brief"); |
StephaneLenclud@250 | 130 |
} |
StephaneLenclud@250 | 131 |
|
StephaneLenclud@250 | 132 |
} |
StephaneLenclud@250 | 133 |
|
StephaneLenclud@236 | 134 |
} |
StephaneLenclud@236 | 135 |
} |