-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPublisher.agent.nut
More file actions
100 lines (89 loc) · 3.63 KB
/
Publisher.agent.nut
File metadata and controls
100 lines (89 loc) · 3.63 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
// MIT License
//
// Copyright 2017 Electric Imp
//
// SPDX-License-Identifier: MIT
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
// EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// OAuth 2.0 library required for GooglePubSub
#require "OAuth2.agent.lib.nut:2.0.0"
#require "GooglePubSub.agent.lib.nut:1.1.0"
// GooglePubSub.Publisher and GooglePubSub.Topics demo.
// Creates a topic if it does not exist (topic name is specified as constructor argument)
// and periodically publishes messages to the topic.
// Messages are published every 10 seconds, every message contains integer increasing data
// and measureTime attribute with data measurement time in seconds since the epoch format.
class Publisher {
_topicName = null;
_topics = null;
_publisher = null;
_counter = 0;
constructor(projectId, oAuthTokenProvider, topicName) {
_topicName = topicName;
_topics = GooglePubSub.Topics(projectId, oAuthTokenProvider);
_publisher = GooglePubSub.Publisher(projectId, oAuthTokenProvider, topicName);
}
// Returns a message to be published
function getData() {
_counter++;
return GooglePubSub.Message(_counter, { "measureTime" : time().tostring() });
}
// Periodically publishes messages to the specified topic
function publishData() {
_publisher.publish(getData(), function (error, messageIds) {
if (error) {
server.error("Publish request failed: " + error.details);
}
else {
server.log("Data published successfully");
}
});
imp.wakeup(10.0, function () {
publishData();
}.bindenv(this));
}
// Checks if the specified topic exists and optionally creates it if not,
// then periodically publishes messages to the topic
function publish() {
_topics.obtain(_topicName, { "autoCreate" : true }, function (error) {
if (error) {
server.error("Topic obtain request failed: " + error.details);
}
else {
publishData();
}
}.bindenv(this));
}
}
// Configuration constants, substitute with real values
const PROJECT_ID = "...";
const GOOGLE_ISS = "...";
const GOOGLE_SECRET_KEY = "...";
// obtaining OAuth2 Access Tokens Provider
local oAuthTokenProvider = OAuth2.JWTProfile.Client(
OAuth2.DeviceFlow.GOOGLE,
{
"iss" : GOOGLE_ISS,
"jwtSignKey" : GOOGLE_SECRET_KEY,
"scope" : "https://www.googleapis.com/auth/pubsub"
});
const TOPIC_NAME = "test_topic";
// Start Application
publisher <- Publisher(PROJECT_ID, oAuthTokenProvider, TOPIC_NAME);
publisher.publish();