-
Notifications
You must be signed in to change notification settings - Fork 5
/
instrumentregistry.js
273 lines (232 loc) · 8.01 KB
/
instrumentregistry.js
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
var visir = visir || {};
visir.InstrumentRegistry = function(extService)
{
this._instruments = [];
this._registeredTypes = {
circuit: 0,
dcpower: 0,
functiongenerator: 0,
multimeter: 0,
oscilloscope: 0
};
this._listeners = [];
function InstrInfo(type, name, swf) { return { type: type, displayname: name, swf: swf } };
this._instrumentInfo = {
AgilentOscilloscope: InstrInfo("oscilloscope", visir.Lang.GetMessage("oscilloscope"), "oscilloscope/oscilloscope.swf"),
Breadboard: InstrInfo("circuit", visir.Lang.GetMessage("breadboard"), "breadboard/breadboard.swf"),
FlukeMultimeter: InstrInfo("multimeter", visir.Lang.GetMessage("multimeter"), "multimeter/multimeter.swf"),
HPFunctionGenerator: InstrInfo("functiongenerator", visir.Lang.GetMessage("func_gen"), "functiongenerator/functiongenerator.swf"),
NationalInstrumentOscilloscope: InstrInfo("oscilloscope", visir.Lang.GetMessage("oscilloscope"), ""),
TripleDC: InstrInfo("dcpower", visir.Lang.GetMessage("dc_power"), "tripledc/tripledc.swf")
}
this._extServices = extService || null;
if (visir.Config) visir.Config.SetInstrRegistry(this); // XXX: maybe this need to be more configurable..
}
visir.InstrumentRegistry.prototype._Reset = function()
{
this._instruments = [];
this._registeredTypes = {
circuit: 0,
dcpower: 0,
functiongenerator: 0,
multimeter: 0,
oscilloscope: 0
};
}
visir.InstrumentRegistry.prototype.CreateInstrument = function()
{
function construct(constructor, args) {
function F() {
return constructor.apply(this, args);
}
F.prototype = constructor.prototype;
return new F();
}
if (arguments.length < 2) throw "Invalid number of arguments to CreateInstrument";
var name = arguments[0];
var id = this._NextInstrID(name);
arguments[0] = id; // replace the first argument with the id before passing them along.
arguments[1] = $(arguments[1]); // get the jquery dom node
var newinstr = construct(visir[name], arguments);
var entry = { instrument: newinstr, id: id, domnode: arguments[1], instrInfo: this._instrumentInfo[name], name: name };
this._instruments.push(entry);
if (this._extServices && typeof newinstr.UseExteralService == "function") {
newinstr.UseExteralService(this._extServices);
}
return newinstr;
}
visir.InstrumentRegistry.prototype._NextInstrID = function(name)
{
//XXX: check the db for instr type and update the counts
if (!this._instrumentInfo[name]) throw "Instrument name not found in InstrumentRegistry";
return ++this._registeredTypes[this._instrumentInfo[name].type];
}
visir.InstrumentRegistry.prototype.WriteRequest = function()
{
var out = "";
for(var i=0;i<this._instruments.length; i++) {
out += this._instruments[i].instrument.WriteRequest();
}
return out;
}
visir.InstrumentRegistry.prototype.ReadResponse = function(response)
{
for(var i=0;i<this._instruments.length; i++) {
this._instruments[i].instrument.ReadResponse(response);
}
}
visir.InstrumentRegistry.prototype.ReadSave = function(response)
{
var $response = $(response);
$response.find('protocol').remove();
for(var i=0;i<this._instruments.length; i++) {
if (typeof (this._instruments[i].instrument.ReadSave) == "function") {
this._instruments[i].instrument.ReadSave($response);
}
}
if (visir.Config.Get("displayManuals") == false) {
$(".manual_link").remove();
}
}
visir.InstrumentRegistry.prototype.WriteSave = function(includeRequest)
{
$xml = $('<save version="2" />');
var instrumentlist = "";
var instrumentlistvalues = "";
var firstTime = true;
for(var i = 0; i < this._instruments.length; i++) {
if (i>0) {
instrumentlist += "|";
}
instrumentlist += this._instruments[i].name;
if (visir.Config.Get("unrFormat")) {
if (this._instruments[i].name == "HPFunctionGenerator" || this._instruments[i].name == "TripleDC") {
instrumentlistvalues += this._instruments[i].name + "#" + this._instruments[i].instrument._ReadCurrentValues().toString();
if (firstTime) {
instrumentlistvalues += "|";
firstTime = false;
}
}
}
}
var $instruments = $('<instruments />').attr("htmlinstruments", instrumentlist);
$xml.append($instruments);
if (visir.Config.Get("unrFormat")) {
var $instruments_values = $('<instrumentsvalues />').attr("htmlinstrumentsvalues", instrumentlistvalues);
$xml.append($instruments_values);
}
for(var i=0;i<this._instruments.length; i++) {
if (typeof (this._instruments[i].instrument.WriteSave) == "function") {
$xml.append(this._instruments[i].instrument.WriteSave());
}
}
if (includeRequest === true) {
var requestContents = this.WriteRequest();
var $protocol = $("<protocol version=\"1.3\"><request></request></protocol>");
$protocol.find("request").append($(requestContents));
$xml.append($protocol);
}
return $("<root />").append($xml).html();
}
visir.InstrumentRegistry.prototype.MakeRequest = function(transport)
{
var me = this;
transport.Request(this.WriteRequest(), function(res) { me.ReadResponse(res); } );
}
// XXX: don't know if this is going to stay here or in some other class
//
visir.InstrumentRegistry.prototype._CreateInstrContainer = function(type)
{
var id = this._registeredTypes[type]+1;
return $('<div class="instrument" id="' + type + id + '" />');
}
visir.InstrumentRegistry.prototype._CreateInstrFromSWF = function(swf, $loc)
{
for (var key in this._instrumentInfo) {
if (this._instrumentInfo[key].swf == swf) {
var $ctnr = this._CreateInstrContainer(this._instrumentInfo[key].type);
var newinstr = this.CreateInstrument(key, $ctnr);
$loc.append($ctnr);
return newinstr;
}
}
return null;
}
visir.InstrumentRegistry.prototype.LoadExperimentFromURL = function(url, $loc)
{
var me = this;
$.get(url, function(data) {
me.LoadExperiment(data, $loc);
});
}
visir.InstrumentRegistry.prototype.CreateInstrFromJSClass = function(classname, $loc)
{
if (classname == "TripleDC") {
if (!visir.TripleDC && visir.LlTripleDC) {
visir.TripleDC = visir.LlTripleDC;
}
}
trace("creating instrument from js name: " + classname);
var $ctnr = this._CreateInstrContainer(this._instrumentInfo[classname].type);
this.CreateInstrument(classname, $ctnr);
$loc.append($ctnr);
}
visir.InstrumentRegistry.prototype.LooksLikeSaveXML = function($xml)
{
if ($xml.prop("tagName") != "SAVE") return false;
var versionNr = parseInt($xml.attr("version"), 10);
if (versionNr < 2) return false;
return true;
}
visir.InstrumentRegistry.prototype.LoadExperiment = function(xmldata, $loc)
{
var $xml = $(xmldata);
if (!this.LooksLikeSaveXML($xml)) {
alert("Failed to load experiment, invalid save data");
return;
}
$loc.find(".instrument").remove();
this._Reset();
var $instr = $xml.find("instruments");
var flashlocs = $instr.attr("list");
var swfs = flashlocs ? flashlocs.split("|") : [];
for(var i=0;i<swfs.length; i++) {
trace("creating instrument from swf: " + swfs[i]);
this._CreateInstrFromSWF(swfs[i], $loc);
}
var htmlinstr = $instr.attr("htmlinstruments");
var htmlarr = htmlinstr ? htmlinstr.split("|") : [];
for(var i=0; i<htmlarr.length; i++) {
this.CreateInstrFromJSClass(htmlarr[i], $loc);
}
this.ReadSave($xml);
this.Notify("onExperimentLoaded");
$("body").trigger("configChanged");
}
visir.InstrumentRegistry.prototype.AddListener = function(listenTo)
{
this._listeners.push(listenTo);
}
// XXX: do we need to fix arguments? lets see..
visir.InstrumentRegistry.prototype.Notify = function(func)
{
for(var i=0;i<this._listeners.length; i++) {
if (typeof this._listeners[i][func] == "function") this._listeners[i][func]();
}
}
visir.InstrumentRegistry.prototype.RemoveInstrument = function(instrument)
{
for(var i in this._instruments) {
if (this._instruments[i] === instrument) {
// XXX: what should we do with the registeredTypes? This is not really correct
--this._registeredTypes[this._instrumentInfo[this._instruments[i].name].type];
this._instruments[i].domnode.remove();
this._instruments.splice(i, 1);
}
}
}
visir.InstrumentRegistry.prototype.GetNrInstrOfType = function(type)
{
trace("reg: " + this._registeredTypes[type] + " " + type);
return this._registeredTypes[type];
}