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