-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevice.js
139 lines (125 loc) · 4.17 KB
/
device.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
'use strict';
const fs = require('fs');
const jwt = require('jsonwebtoken');
const request = require('retry-request');
//CONFIGURATION
var projectId = "MYPROJECTID";
var cloudRegion = "MYCLOUDREGION";
var registryId = "MYREGISTRYID";
var deviceId = "MYDEVICEID";
var privateKeyPath = "private_key.pem";
var pathSuffix = ":publishEvent";
var algorithm = "RS256";
var tokenExpMins = 20;
//END CONFIGURATION
let iatTime = parseInt(Date.now() / 1000);
let authToken = createJwt(projectId, privateKeyPath, algorithm);
const devicePath = `projects/${projectId}/locations/${cloudRegion}/registries/${registryId}/devices/${deviceId}`;
const urlBase = `https://cloudiotdevice.googleapis.com/v1/${devicePath}`;
const url = `${urlBase}${pathSuffix}`;
function createJwt (projectId, privateKeyPath, algorithm) {
const token = {
'iat': parseInt(Date.now() / 1000),
'exp': parseInt(Date.now() / 1000) + 20 * 60,
'aud': projectId
};
const privateKey = fs.readFileSync(privateKeyPath);
return jwt.sign(token, privateKey, { algorithm: algorithm });
}
var deviceNames = ['SBS01', 'SBS02', 'SBS03', 'SBS04', 'SBS05']
function generateRandomPayload (){
var rnd = Math.random();
if (rnd < 0.20){
return getFlowValue();
}
if (rnd>=0.20 && rnd<0.55){
return getTemperatureValues();
}
if (rnd>=0.55 && rnd<0.70){
return getHumidityValues();
}
if (rnd>=0.7 && rnd<=1){
return getSoundValues();
}
}
function getFlowValue(){
var data = {}
data['deviceValue'] = Math.random()*100+60;
data['deviceParameter'] = 'Flow';
data['deviceId'] = deviceNames[Math.floor(Math.random() * deviceNames.length)];
var date = new Date();
data['dateTime'] = date.toISOString();
return data;
}
function getTemperatureValues(){
var data = {}
data['deviceValue'] = Math.random()*20+30;
data['deviceParameter'] = 'Temperature';
data['deviceId'] = deviceNames[Math.floor(Math.random() * deviceNames.length)];
var date = new Date();
data['dateTime'] = date.toISOString();
return data;
}
function getHumidityValues(){
var data = {}
data['deviceValue'] = Math.random()*70+50;
data['deviceParameter'] = 'Humidity';
data['deviceId'] = deviceNames[Math.floor(Math.random() * deviceNames.length)];
var date = new Date();
data['dateTime'] = date.toISOString();
return data;
}
function getSoundValues(){
var data = {}
data['deviceValue'] = Math.random()*120+60;
data['deviceParameter'] = 'Sound';
data['deviceId'] = deviceNames[Math.floor(Math.random() * deviceNames.length)];
var date = new Date();
data['dateTime'] = date.toISOString();
return data;
}
function publishAsync (authToken) {
const payload = JSON.stringify(generateRandomPayload());
console.log('Publishing message:', payload);
const binaryData = Buffer.from(payload).toString('base64');
const postData = {
binary_data: binaryData
};
const options = {
url: url,
headers: {
'authorization': `Bearer ${authToken}`,
'content-type': 'application/json',
'cache-control': 'no-cache'
},
body: postData,
json: true,
method: 'POST',
retries: 5,
shouldRetryFn:
function (incomingHttpMessage) {
return incomingHttpMessage.statusMessage !== 'OK';
}
};
const delayMs = 1000;
console.log(JSON.stringify(request));
request(options, function (error, response, body) {
if (error) {
console.error('Received error: ', error);
} else if (response.body.error) {
console.error('Received error: ' + JSON.stringify(response.body.error));
} else {
console.log('Message sent.');
}
setTimeout(function () {
let secsFromIssue = parseInt(Date.now() / 1000) - iatTime;
if (secsFromIssue > tokenExpMins * 60) {
iatTime = parseInt(Date.now() / 1000);
console.log(`\tRefreshing token after ${secsFromIssue} seconds.`);
authToken = createJwt(projectId, privateKeyFile, algorithm);
}
publishAsync(authToken);
}, delayMs);
});
}
publishAsync(authToken);