-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzway-api-mock.js
88 lines (72 loc) · 2.06 KB
/
zway-api-mock.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
var net = require('net');
var sockets = {
tcp: function () {
var self = this;
this.send = function (data) {
self.client.write(new Buffer(data));
};
this.onrecv = this.onconnect = this.onclose = function () {
};
this.connect = function (host, port) {
self.client = new net.Socket();
self.client.connect(port, host, function () {
self.onconnect();
});
self.client.on('close', self.onclose);
self.client.on('data', function(d) {
self.onrecv(new Uint8Array(d).buffer);
});
};
this.close = function () {
self.client.end();
};
}
};
function inherits (ctor, superCtor) {
Object.defineProperty(ctor, "super_", {
value: superCtor,
enumerable: false,
writable: false,
configurable: false
});
ctor.prototype = Object.create(superCtor.prototype, {
constructor: {
value: ctor,
enumerable: false,
writable: true,
configurable: true
}
});
}
function AutomationModule(id, controller) {
this.id = id;
this.controller = controller;
}
AutomationModule.prototype.init = function(config) {
this.config = config;
};
function Controller(devices) {
this.callbacks = [];
this.devices = devices || [];
var self = this;
this.devices.on = function(event, callback) {
self.callbacks.push({ event: event, callback: callback });
}
}
Controller.prototype.emit = function (event, payload) {
for (var i = 0; i < this.callbacks.length; i++) {
if (this.callbacks[i].event == event) {
this.callbacks[i].callback(payload);
}
}
};
function Device(props) {
this.props = props;
}
Device.prototype.get = function (name) {
return this.props[name];
};
/*
Remove some capabilties that are not supported by the V8 engine running on zway
*/
Uint8Array.prototype.slice = function () { throw new Error("Not supported on zway"); };