-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathActionsManager.cs
More file actions
163 lines (130 loc) · 5.22 KB
/
ActionsManager.cs
File metadata and controls
163 lines (130 loc) · 5.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Crestron.SimplSharp;
namespace Daniels.Common
{
public class ActionInfo
{
public readonly uint Id;
public readonly string Name;
public readonly string Help;
public readonly string Parameters;
public readonly bool ReadOnly;
public ActionInfo(uint id, string name, string help, bool readOnly, string parameters)
{
Id = id;
Name = name;
Help = help;
ReadOnly = readOnly;
Parameters = parameters;
}
}
public class ActionsManager: IEnumerable<ActionInfo>
{
private static ActionsManager _instance;
private static uint _id = 0;
private static Dictionary<uint, ActionInfo> _actionInfos = new Dictionary<uint, ActionInfo>();
private static Dictionary<uint, ActionFunction> _actionFunctions = new Dictionary<uint, ActionFunction>();
public delegate void ActionFunction(string actionParameters);
private ActionsManager()
{
}
#region Static Methods
public static ActionsManager GetInstance()
{
if (_instance == null)
_instance = new ActionsManager();
return _instance;
}
public static uint RegisterAction(ActionFunction function, string name, string help)
{
if(function == null)
throw new ArgumentNullException("function");
if (_actionFunctions.ContainsValue(function))
throw new ArgumentOutOfRangeException("function", "Action already registered");
if (name == null)
throw new ArgumentNullException("name");
if (String.IsNullOrEmpty(name))
throw new ArgumentOutOfRangeException("name");
if (_actionInfos.Values.FirstOrDefault(a => a.Name == name) != null)
throw new ArgumentOutOfRangeException("name", "Action with name " + name + " already registered");
uint actionId = ++_id;
string actionHelp = (help == null) ? String.Empty : help;
_actionFunctions.Add(actionId, function);
_actionInfos.Add(actionId, new ActionInfo(actionId, name, actionHelp, true, String.Empty));
return actionId;
}
public static uint AddCutomAction(uint id, string actionName, string parameters, string help)
{
if (!_actionFunctions.ContainsKey(id))
throw new ArgumentOutOfRangeException("id");
if (actionName == null)
throw new ArgumentNullException("name");
if (String.IsNullOrEmpty(actionName))
throw new ArgumentOutOfRangeException("name");
if (_actionInfos.Values.FirstOrDefault(a => a.Name == actionName) != null)
throw new ArgumentOutOfRangeException("name", "Action with name " + actionName + " already registered");
if (String.IsNullOrEmpty(parameters))
throw new ArgumentNullException("parameters");
string actionHelp = (help == null) ? String.Empty : help;
uint actionId = ++_id;
_actionInfos.Add(id, new ActionInfo(actionId, actionName, actionHelp, false, parameters));
return actionId;
}
public static bool RemoveCustomAction(uint id)
{
if (!_actionFunctions.ContainsKey(id))
throw new ArgumentOutOfRangeException("id");
var kv = _actionInfos.FirstOrDefault(x => x.Value.Id == id);
if (kv.IsNull())
throw new ArgumentOutOfRangeException("id");
if(kv.Value.ReadOnly)
return false;
return _actionInfos.Remove(id);
}
public static void InvokeAction(uint id)
{
var kv = _actionInfos.FirstOrDefault(x => x.Value.Id == id);
if (kv.IsNull())
throw new ArgumentOutOfRangeException("id");
_actionFunctions[kv.Key].Invoke(kv.Value.Parameters);
}
#endregion Static Methods
#region Properties
public ActionInfo this[uint id]
{
get
{
if (_actionInfos.ContainsKey(id))
return _actionInfos[id];
else
throw new IndexOutOfRangeException();
}
}
public uint Length
{
get { return (uint)_actionInfos.Count; }
}
#endregion Properties
#region IEnumerable<ActionInfo> Implementation
public IEnumerator<ActionInfo> GetEnumerator()
{
return _actionInfos.Values.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion IEnumerable<ActionInfo> Implementation
/*
private static ReadOnlyDictionary<string, ActionFunction> _actionsReadOnly = new ReadOnlyDictionary<string, ActionFunction>(_actions);
public static ReadOnlyDictionary<string, ActionFunction> Actions
{
get { return _actionsReadOnly; }
}
*/
}
}