This repository has been archived by the owner on Feb 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
pubsub.js
172 lines (146 loc) · 6.27 KB
/
pubsub.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
'use strict';
module.exports = function(RED) {
var YaaS = require('yaas.js');
var poll = {};
poll.count = 5;
poll.delta = 1;
poll.max = 10;
poll.toString = function() {
var str = '';
for (var i = 0; i < this.max; i++) {
if (this.count === i) {
str += (this.delta > 0) ? '>' : '<';
} else {
str += '-';
}
}
this.count += this.delta;
if (this.count >= this.max || this.count < 0) {
this.delta *= -1;
this.count += this.delta * 2;
}
return str;
};
function YaasPubsubReadNode(config) {
RED.nodes.createNode(this, config);
var node = this;
node.yaasCredentials = RED.nodes.getNode(config.yaasCredentials);
node.topic_owner_client = config.topic_owner_client;
if (config.topic_owner_client === '') {
node.topic_owner_client = node.yaasCredentials.application_id;
}
node.event_type = config.event_type;
node.topic = node.topic_owner_client + '.' + node.event_type;
node.interval = config.interval;
node.number_of_events = config.number_of_events || 1;
node.auto_commit = config.auto_commit;
node.status({fill:'red',shape:'ring',text:'disconnected (' + node.topic + ')'});
console.log('hybris.pubsub.topic=' + node.topic);
//get oauth2 access token
var yaas = new YaaS();
yaas.init(
node.yaasCredentials.client_id, // theClientId
node.yaasCredentials.client_secret, // theClientSecret
'hybris.pubsub.topic=' + node.topic, // theScope,
node.yaasCredentials.application_id // theProjectId
)
.then(function() {
node.status({fill:'yellow',shape:'dot',text:'polling'});
//start inteval polling
node.intervalID = setInterval(function() {
node.status({fill:'green',shape:'dot',text:'polling ' + poll.toString()});
yaas.pubsub.read(node.topic_owner_client, node.event_type, node.number_of_events, node.auto_commit)
.then(function(evt) {
if (evt !== undefined) {
console.log('received event: ' + JSON.stringify(evt));
var theFirstEvent = evt.events[0];
theFirstEvent.token = evt.token;
node.send(theFirstEvent);
}
}, function(err) {
node.status({fill:'red',shape:'dot',text:'error: ' + err});
console.error('ERROR pubsub read:', err);
});
}, node.interval);
}, console.log);
node.on('close', function() {
if (node.intervalID) {
clearInterval(node.intervalID);
}
});
}
RED.nodes.registerType('pubsub_read',YaasPubsubReadNode);
function YaasPubsubPublishNode(config) {
RED.nodes.createNode(this, config);
var node = this;
node.yaasCredentials = RED.nodes.getNode(config.yaasCredentials);
node.application_id = node.yaasCredentials.application_id;
node.event_type = config.event_type;
node.topic = node.application_id + '.' + node.event_type;
node.status({fill:'red',shape:'ring',text:'disconnected'});
var yaas = new YaaS();
yaas.init(
node.yaasCredentials.client_id, // theClientId
node.yaasCredentials.client_secret, // theClientSecret
'hybris.pubsub.topic=' + node.topic, // theScope,
node.application_id // theProjectId
)
.then(function() {
yaas.pubsub.createTopic(node.event_type)
.then(function() {
console.log('topic', node.topic, 'created.');
}, function(error) {
if(error.statusCode === 409) {
console.log('INFO: topic', node.topic, 'exists.');
} else {
console.log('error pubsub publish:', JSON.stringify(error));
try {
node.status({fill:'red',shape:'ring',text:'error: ' + error.body.details[0].message});
} catch(e) {
node.status({fill:'red',shape:'ring',text:'error: ' + JSON.stringify(error)});
}
}
});
node.on('input',function(msg) {
node.log('Publishing', node.topic, ':', msg.payload);
yaas.pubsub.publish(node.application_id, node.event_type, msg.payload)
.then(function() {
node.log('published:', msg.payload);
node.status({fill:'green',shape:'dot',text:'published: ' + msg.payload});
}, console.log);
});
node.status({fill:'green',shape:'dot',text:'ready'});
});
}
RED.nodes.registerType('pubsub_publish',YaasPubsubPublishNode);
function YaasPubsubCommitNode(config) {
RED.nodes.createNode(this, config);
var node = this;
node.yaasCredentials = RED.nodes.getNode(config.yaasCredentials);
node.application_id = node.yaasCredentials.application_id;
node.event_type = config.event_type;
node.topic = node.application_id + '.' + node.event_type;
node.status({fill:'red',shape:'ring',text:'disconnected'});
node.on('input',function(msg) {
node.log('Committing', node.topic, ':', msg.token);
var yaas = new YaaS();
yaas.init(
node.yaasCredentials.client_id, // theClientId
node.yaasCredentials.client_secret, // theClientSecret
'hybris.pubsub.topic=' + node.topic, // theScope,
node.application_id // theProjectId
)
.then(function() {
node.status({fill:'green',shape:'dot',text:'event received'});
yaas.pubsub.commit(node.application_id, node.event_type, msg.token)
.then(function(){
node.log('Message committed:', msg.token);
}, function(err) {
node.status({fill:'red',shape:'dot',text:'error: ' + err});
console.error('ERROR pubsub commit:', err);
});
});
});
}
RED.nodes.registerType('pubsub_commit',YaasPubsubCommitNode);
};