forked from bitfocus/companion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrest_poll.js
120 lines (100 loc) · 3.2 KB
/
rest_poll.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
/*
* This file is part of the Companion project
* Copyright (c) 2018 Bitfocus AS
* Authors: William Viker <[email protected]>, Håkon Nessjøen <[email protected]>
*
* This program is free software.
* You should have received a copy of the MIT licence as well as the Bitfocus
* Individual Contributor License Agreement for companion along with
* this program.
*
* You can be released from the requirements of the license by purchasing
* a commercial license. Buying such a license is mandatory as soon as you
* develop commercial activities involving the Companion software without
* disclosing the source code of your own applications.
*
*/
var debug = require('debug')('lib/rest_poll');
var shortid = require('shortid');
function rest_poll(system) {
var self = this;
self.running = {};
system.on('rest_poll', function(instance_id, interval, url, data, poll_obj_cb, result_cb) {
var poll_id = shortid.generate();
if (self.running[instance_id] === undefined) {
self.running[instance_id] = {};
}
self.running[instance_id][poll_id] = {
instance: instance_id,
id: poll_id,
interval: interval,
url: url,
type: 'post',
waiting: false,
data: data,
result_cb: result_cb,
timer: setInterval(function(instance_id, poll_id) {
var obj = self.running[instance_id][poll_id];
if (obj.waiting === true) {
debug("Skipping this cycle for",poll_id);
}
else {
system.emit('rest', obj.url, obj.data, function(err, res) {
debug("got reply for",obj.id,obj.url);
obj.waiting = false;
obj.result_cb(err, res);
});
}
}.bind(null, instance_id, poll_id), interval)
};
poll_obj_cb(null, self.running[instance_id][poll_id]);
console.log("Rest poll added", self.running);
});
system.on('rest_poll_get', function(instance_id, interval, url, poll_obj_cb, result_cb) {
var poll_id = shortid.generate();
if (self.running[instance_id] === undefined) {
self.running[instance_id] = {};
}
self.running[instance_id][poll_id] = {
instance: instance_id,
id: poll_id,
type: 'get',
interval: interval,
url: url,
waiting: false,
result_cb: result_cb,
timer: setInterval(function(instance_id, poll_id) {
var obj = self.running[instance_id][poll_id];
if (obj.waiting === true) {
debug("Skipping this cycle for",poll_id);
}
else {
system.emit('rest_get', obj.url, function(err, res) {
debug("got reply for",obj.id,obj.url);
obj.waiting = false;
obj.result_cb(err, res);
});
}
}.bind(null, instance_id, poll_id), interval)
};
poll_obj_cb(null, self.running[instance_id][poll_id]);
console.log("Rest poll added", self.running);
});
system.on('rest_poll_destroy', function(instance_id) {
debug("Clearing poll intervals for",instance_id);
if (self.running[instance_id] !== undefined) {
for (var poll_id in self.running[instance_id]) {
var poll = self.running[instance_id][poll_id];
if (poll.timer !== undefined) {
debug("Killing interval for",poll.instance,poll.url);
clearInterval(poll.timer);
delete self.running[instance_id][poll_id];
}
}
}
});
return self;
}
exports = module.exports = function (system) {
return new rest_poll(system);
};