This repository has been archived by the owner on Jan 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
DataProcessor.cs
223 lines (189 loc) · 9.45 KB
/
DataProcessor.cs
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
using MbientLab.MetaWear.Core;
using static MbientLab.MetaWear.Impl.Module;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Runtime.Serialization;
using static MbientLab.MetaWear.Impl.RouteComponent;
using MbientLab.MetaWear.Core.DataProcessor;
namespace MbientLab.MetaWear.Impl {
[DataContract]
abstract class EditorImplBase : SerializableType, IEditor {
[DataMember] internal byte[] config;
[DataMember] internal readonly DataTypeBase source;
internal EditorImplBase(byte[] config, DataTypeBase source, IModuleBoardBridge bridge) : base(bridge) {
this.config = config;
this.source = source;
}
}
[DataContract]
class NullEditor : EditorImplBase {
internal NullEditor(byte[] config, DataTypeBase source, IModuleBoardBridge bridge) : base(config, source, bridge) { }
}
[KnownType(typeof(AccumulatorEditorInner))]
[KnownType(typeof(AverageEditorInner))]
[KnownType(typeof(CounterEditorInner))]
[KnownType(typeof(SingleValueComparatorEditor))]
[KnownType(typeof(MultiValueComparatorEditor))]
[KnownType(typeof(MapEditorInner))]
[KnownType(typeof(PassthroughEditorInner))]
[KnownType(typeof(PulseEditorInner))]
[KnownType(typeof(ThresholdEditorInner))]
[KnownType(typeof(DifferentialEditorInner))]
[KnownType(typeof(TimeEditorInner))]
[KnownType(typeof(PackerEditorInner))]
[KnownType(typeof(NullEditor))]
[KnownType(typeof(DataTypeBase))]
[DataContract]
class DataProcessor : ModuleImplBase, IDataProcessor {
internal static String createIdentifier(DataTypeBase dataType, DataProcessor dataprocessor, Version firmware, byte revision) {
byte register = Util.clearRead(dataType.eventConfig[1]);
switch (register) {
case NOTIFY:
case STATE:
var processor = dataprocessor.lookupProcessor(dataType.eventConfig[2]);
DataProcessorConfig config = DataProcessorConfig.from(firmware, revision, processor.Item2.config);
return config.CreateIdentifier(register == STATE, dataType.eventConfig[2]);
default:
return null;
}
}
internal class ProcessorEntry {
internal byte id, offset, length;
internal byte[] source, config;
}
internal const byte TYPE_ACCOUNTER = 0x11, TYPE_PACKER = 0x10;
internal const byte TIME_PASSTHROUGH_REVISION = 1, ENHANCED_STREAMING_REVISION = 2, HPF_REVISION = 2, EXPANDED_DELAY = 2;
internal const byte ADD = 2,
NOTIFY = 3,
STATE = 4,
PARAMETER = 5,
REMOVE = 6,
NOTIFY_ENABLE = 7,
REMOVE_ALL = 8;
[DataMember] internal Dictionary<byte, Tuple<DataTypeBase, EditorImplBase>> activeProcessors= new Dictionary<byte, Tuple<DataTypeBase, EditorImplBase>>();
[DataMember] internal Dictionary<string, byte> nameToId = new Dictionary<string, byte>();
private TimedTask<byte> createProcessorTask;
private TimedTask<byte[]> pullProcessorConfigTask;
private Dictionary<string, IForcedDataProducer> stateDataProducers;
public DataProcessor(IModuleBoardBridge bridge) : base(bridge) {
}
public override void tearDown() {
bridge.sendCommand(new byte[] { (byte)DATA_PROCESSOR, REMOVE_ALL });
foreach(var it in nameToId.Keys) {
bridge.removeProducerName(it);
}
activeProcessors.Clear();
nameToId.Clear();
}
internal override void restoreTransientVars(IModuleBoardBridge bridge) {
base.restoreTransientVars(bridge);
foreach (Tuple<DataTypeBase, EditorImplBase> it in activeProcessors.Values) {
it.Item2.restoreTransientVars(bridge);
}
}
protected override void init() {
createProcessorTask = new TimedTask<byte>();
pullProcessorConfigTask = new TimedTask<byte[]>();
stateDataProducers = new Dictionary<string, IForcedDataProducer>();
bridge.addRegisterResponseHandler(Tuple.Create((byte) DATA_PROCESSOR, ADD), response => createProcessorTask.SetResult(response[2]));
bridge.addRegisterResponseHandler(Tuple.Create((byte)DATA_PROCESSOR, Util.setRead(ADD)), response => pullProcessorConfigTask.SetResult(response));
}
public IForcedDataProducer State(string name) {
if (stateDataProducers.TryGetValue(name, out var producer)) {
return producer;
}
if (nameToId.TryGetValue(name, out var id) && activeProcessors.TryGetValue(id, out var value)) {
producer = new ForcedDataProducer(value.Item1, bridge);
stateDataProducers.Add(name, producer);
return producer;
}
return null;
}
public T Edit<T>(string name) where T : class, IEditor {
return nameToId.TryGetValue(name, out var id) && activeProcessors.TryGetValue(id, out var value) ?
value.Item2 as T : null;
}
internal void remove(byte id, bool sync) {
activeProcessors.Remove(id);
if (sync) {
bridge.sendCommand(new byte[] { (byte)DATA_PROCESSOR, REMOVE, id });
}
}
internal async Task<Queue<byte>> queueDataProcessors(LinkedList<Tuple<DataTypeBase, EditorImplBase>> pendingProcessors) {
var successfulProcessors = new Queue<byte>();
try {
while (pendingProcessors.Count != 0) {
Tuple<DataTypeBase, EditorImplBase> current = pendingProcessors.First.Value;
DataTypeBase input = current.Item2.source.input;
byte[] filterConfig = new byte[input.eventConfig.Length + 1 + current.Item2.config.Length];
filterConfig[input.eventConfig.Length] = (byte)(((input.attributes.length() - 1) << 5) | input.attributes.offset);
Array.Copy(input.eventConfig, 0, filterConfig, 0, input.eventConfig.Length);
Array.Copy(current.Item2.config, 0, filterConfig, input.eventConfig.Length + 1, current.Item2.config.Length);
var id = await createProcessorTask.Execute("Did not receive data processor id within {0}ms", bridge.TimeForResponse,
() => bridge.sendCommand(DATA_PROCESSOR, ADD, filterConfig));
pendingProcessors.RemoveFirst();
current.Item2.source.eventConfig[2] = id;
if (current.Item1 != null) {
current.Item1.eventConfig[2] = id;
}
activeProcessors[id] = current;
successfulProcessors.Enqueue(id);
}
} catch (TimeoutException e) {
foreach (byte it in successfulProcessors) {
removeProcessor(true, it);
}
throw e;
}
return successfulProcessors;
}
void removeProcessor(bool sync, byte id) {
if (sync && activeProcessors.TryGetValue(id, out Tuple<DataTypeBase, EditorImplBase> value)) {
bridge.sendCommand(new byte[] { (byte) DATA_PROCESSOR, REMOVE, value.Item2.source.eventConfig[2] });
}
activeProcessors.Remove(id);
}
internal Tuple<DataTypeBase, EditorImplBase> lookupProcessor(byte id) {
return activeProcessors.TryGetValue(id, out var result) ? result : null;
}
internal async Task<Stack<ProcessorEntry>> pullChainAsync(byte id) {
var entries = new Stack<ProcessorEntry>();
var terminate = false;
var readId = id;
while (!terminate) {
if (activeProcessors.TryGetValue(readId, out var processor)) {
var entry = new ProcessorEntry {
id = readId,
config = processor.Item2.config
};
entries.Push(entry);
if (processor.Item2.source.eventConfig[0] == (byte)DATA_PROCESSOR) {
readId = processor.Item2.source.eventConfig[2];
} else {
terminate = true;
}
} else {
var config = await pullProcessorConfigTask.Execute("Did not receive data processor config within {0}ms", bridge.TimeForResponse,
() => bridge.sendCommand(new byte[] { (byte)DATA_PROCESSOR, Util.setRead(ADD), readId }));
var entry = new ProcessorEntry {
id = readId,
offset = (byte)(config[5] & 0x1f),
length = (byte)(((config[5] >> 5) & 0x7) + 1),
source = new byte[3],
config = new byte[config.Length - 6]
};
Array.Copy(config, 2, entry.source, 0, entry.source.Length);
Array.Copy(config, 6, entry.config, 0, entry.config.Length);
entries.Push(entry);
if (config[2] == (byte)DATA_PROCESSOR) {
readId = config[4];
} else {
terminate = true;
}
}
}
return entries;
}
}
}