forked from 8devices/restserver-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8dev_restapi.js
More file actions
133 lines (112 loc) · 3.21 KB
/
8dev_restapi.js
File metadata and controls
133 lines (112 loc) · 3.21 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
'use strict';
const EventEmitter = require('events');
const Client = require('node-rest-client').Client;
class Endpoint extends EventEmitter {
constructor(service, id) {
super();
this.service = service;
this.id = id;
this.observations = [];
this.service.on('async-response', resp => {
let id = resp['id'];
let code = resp['status'];
let data = resp['payload'];
if (this.observations[id] !== undefined) {
this.observations[id](code, data);
}
});
service.attachEndpoint(this);
}
getObjects() {
return new Promise((fulfill, reject) => {
this.service.get('/endpoints/' + this.id, (data, resp) => {
if (resp.statusCode == 200) {
fulfill(data);
} else {
reject(resp.statusCode);
}
});
});
}
observe(path, callback) {
return new Promise((fulfill, reject) => {
this.service.put('/subscriptions/' + this.id + path, (data, resp) => {
if (resp.statusCode === 202) {
let id = data['async-response-id'];
console.log('Observe id', data, id);
this.observations[id] = callback;
fulfill(id);
} else {
reject(resp.statusCode);
}
});
});
}
}
class Service extends EventEmitter {
constructor(opts) {
super();
console.log('Init service', opts);
this.config = opts;
this.client = new Client();
this.endpoints = [];
this.pollTimer = setInterval(() => {
this.client.get(this.config['host'] + '/notification/pull', (data, resp) => {
this._processEvents(data);
});
}, 1234);
}
get(path, callback) {
let url = this.config['host'] + path;
console.log('Getting URL', url);
this.client.get(url, callback);
}
put(path, callback, data) {
let args = {
data: data
};
let url = this.config['host'] + path;
console.log('Putting URL', url);
this.client.put(url, args, callback);
}
_processEvents(events) {
for (let i = 0; i < events['registrations'].length; i++) {
let id = events['registrations'][i]['name'];
console.log('Registration from', id);
if (this.endpoints[id]) {
this.endpoints[id].emit('register');
}
}
for (let i = 0; i < events['reg-updates'].length; i++) {
let id = events['reg-updates'][i]['name'];
console.log('Update from', id);
if (this.endpoints[id]) {
this.endpoints[id].emit('update');
}
}
for (let i = 0; i < events['de-registrations'].length; i++) {
let id = events['de-registrations'][i]['name'];
console.log('Deregistration from', id);
if (this.endpoints[id]) {
this.endpoints[id].emit('deregister');
}
}
for (let i = 0; i < events['async-responses'].length; i++) {
let res = events['async-responses'][i];
console.log('Async-response:', res);
this.emit('async-response', res);
}
}
createNode(id) {
if (!this.endpoints[id]) {
console.log('Creating endpoint', id);
this.endpoints[id] = new Endpoint(this, id);
}
return this.endpoints[id];
}
attachEndpoint(ep) {
this.endpoints[ep.id] = ep;
}
}
module.exports.Service = Service;
module.exports.Device = Endpoint;