-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmodules.js
More file actions
255 lines (210 loc) · 7.1 KB
/
modules.js
File metadata and controls
255 lines (210 loc) · 7.1 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
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
/*
* modules.js
*
* David Janes
* IOTDB.org
* 2014-02-16
* "Valentines's Day"
*
* Copyright [2013-2016] [David P. Janes]
*
* Installed Module Management / Package Manager
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
"use strict";
const _ = require('./helpers');
const assert = require('assert');
const events = require('events');
const logger = _.logger.make({
name: 'iotdb',
module: 'modules',
});
let _require = require;
const make = () => {
const self = Object.assign({}, events.EventEmitter.prototype);
const iotdb = require("./iotdb");
const settings = iotdb.settings();
events.EventEmitter.call(self);
self.setMaxListeners(0);
const _moduled = {}
let _bridges = [];
/**
* If use_bindings is false, bindings that come with modules
* will _not_ be loaded
*/
const use_bindings = settings.get("/use_bindings/all", true) ? true : false;
/**
* Return all the Modules that have been registered
*/
self.modules = () => _.values(_moduled);
/**
* Manually add another module
*
* <Module>.use is like setup, except that it is only
* called if you explicitly use use().
*/
self.use = function (module_name, module) {
if (!module) {
module = _require(module_name);
}
module = _.d.clone.deep(module);
assert(_.is.String(module_name), "first argument must be a string");
assert(_.is.Object(module), "second argument must an object or inferred, got: " + typeof module);
if (module.binding && !module.Bridge) {
return _use_binding(module.binding);
}
if (!use_bindings) {
module.bindings = []
} else if (!settings.get("/use_bindings/modules/" + module_name, true)) {
module.bindings = []
}
module.module_name = module_name;
_moduled[module_name] = module
_load_bridges();
if (module.setup) {
module.setup(iotdb);
}
if (module.use) {
module.use();
}
};
/**
* For loading outside of the normal module system
* e.g. use("./model-folder")
*/
const _use_binding = binding => {
const module_name = binding.bridge;
assert(_.is.String(module_name), "_use_binding: binding.bridge must be a String");
const old_module = _moduled[module_name];
assert(old_module, "_use_binding: binding.module does not refer to an existing module");
const new_module = _.d.clone.shallow(old_module);
const new_binding = _.d.clone.shallow(binding);
new_binding.bridge = new_module.Bridge;
new_binding.__sideload = true;
new_module.bindings = Object.assign([], old_module.bindings);
new_module.bindings.splice(0, 0, new_binding);
// console.log("HERE:DEBUG", new_module.bindings)
// process.exit()
_moduled[module_name] = new_module
}
const _load_master = () => {
_.mapObject(settings.get("modules"), (module_folder, module_name) => {
try {
const module = require(module_folder);
module.module_name = module_name;
module.module_folder = module_folder;
if (!use_bindings) {
module.bindings = []
} else if (!settings.get("/use_bindings/modules/" + module_name, true)) {
module.bindings = []
}
_moduled[module_name] = module
} catch (x) {
logger.error({
method: "_load",
module_name: module_name,
module_folder: module_folder,
cause: "likely the module being imported is not set up correctly",
error: _.error.message(x),
stack: x.stack,
}, "unexpected exception loading module");
return;
}
});
};
const _load_bridges = () => {
_bridges = _.values(_moduled)
.filter(module => module.Bridge)
.map(module => {
module.Bridge.module_name = module.module_name;
// module.Bridge.bridge_name = _.id.to_dash_case((new module.Bridge()).name());
return module.Bridge;
});
};
/**
* A module is a complete package of code, corresponding
* to an NPM package (which would have been a better name).
* A module has one Bridge, one or more Bindings, and
* one or more Models
* <p>
* This will return the module with the module_name,
* or undefined if not found.
*/
self.module = module_name => _moduled[module_name];
/**
* The Bridge is the code that knows how to talk to
* Things. It can be further parameterized by a Binding.
* <p>
* There is one Bridge per Module (maybe more in the future).
*/
self.bridges = () => _bridges;
/**
* Find a bridge by the *MODULES* name. For the module
* part we don't use the Bridge's self-identified
* name except for debug purposes
*/
self.bridge = function (module_name) {
const module = _moduled[module_name];
return module && module.Bridge ? module.Bridge : null;
};
const _setup_binding = binding => {
binding = _.d.clone.deep(binding);
binding.bandd = {
model: binding.model
};
binding.model_id = binding.model["iot:model-id"];
assert(binding.model_id, "models must have 'iot:model-id'");
return binding;
}
self.bindings = () =>
_.flatten(
_.values(_moduled)
.filter(module => module.bindings)
.map(module => module.bindings)
.map(bindings => bindings
.filter(binding => binding.bridge)
.filter(binding => binding.model)
.map(_setup_binding)
), true)
.filter(binding => binding.bridge)
.filter(binding => settings.get("/enabled/modules/" + binding.bridge.module_name, true));
const _load_setup = () => _.values(_moduled)
.filter(module => module.setup)
.forEach(module => module.setup(iotdb));
_load_master();
_load_bridges();
_load_setup();
return self;
}
let _modules;
/**
*/
const instance = () => {
if (!_modules) {
_modules = make();
}
return _modules;
}
const reset = () => {
_modules = null;
};
/*
* API
*/
exports.instance = instance;
exports.reset = reset;
exports.shims = {
reset: () => _modules = null,
require: r => _require = r,
};