This repository has been archived by the owner on Jul 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 135
/
app.js
362 lines (321 loc) · 10.6 KB
/
app.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
Copyright 2016-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at
http://aws.amazon.com/apache2.0/
or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
(function() {
'use strict';
function LogMsg(type, content){
this.type = type;
this.content = content;
this.createdTime = Date.now();
if (this.type === 'success') {
this.className = 'list-group-item-info';
} else {
this.className = 'list-group-item-danger';
}
}
function LogService(){
this.logs = [];
}
LogService.prototype.log = function(msg) {
var logObj = new LogMsg('success', msg);
this.logs.push(logObj);
};
LogService.prototype.logError = function(msg) {
var logObj = new LogMsg('error', msg);
this.logs.push(logObj);
};
/**
* wrapper of received paho message
* @class
* @param {Paho.MQTT.Message} msg
*/
function ReceivedMsg(msg) {
this.msg = msg;
this.content = msg.payloadString;
this.destination = msg.destinationName;
this.receivedTime = Date.now();
}
/** controller of the app */
function AppController(scope){
this.clientId = 'someClientId';
this.endpoint = null;
this.accessKey = null;
this.secretKey = null;
this.regionName = 'us-east-1';
this.logs = new LogService();
this.clients = new ClientControllerCache(scope, this.logs);
}
AppController.$inject = ['$scope'];
AppController.prototype.createClient = function() {
var options = {
clientId : this.clientId,
endpoint: this.endpoint.toLowerCase(),
accessKey: this.accessKey,
secretKey: this.secretKey,
regionName: this.regionName
};
var client = this.clients.getClient(options);
if (!client.connected) {
client.connect(options);
}
};
AppController.prototype.removeClient = function(clientCtr) {
this.clients.removeClient(clientCtr);
};
// would be better to use a seperate derective
function ClientController(client, logs) {
this.client = client;
this.topicName = 'seattle/traffic';
this.message = null;
this.msgs = [];
this.logs = logs;
var self = this;
this.client.on('connectionLost', function(){
self.logs.logError('Connection lost');
});
this.client.on('messageArrived', function(msg){
self.logs.log('messageArrived in ' + self.id);
self.msgs.push(new ReceivedMsg(msg));
});
this.client.on('connected', function(){
self.logs.log('connected');
});
this.client.on('subscribeFailed', function(e){
self.logs.logError('subscribeFailed ' + e);
});
this.client.on('subscribeSucess', function(){
self.logs.log('subscribeSucess');
});
this.client.on('publishFailed', function(e){
self.logs.log('publishFailed');
});
}
ClientController.prototype.subscribe = function() {
this.client.subscribe(this.topicName);
};
ClientController.prototype.publish = function() {
this.client.publish(this.topicName, this.message);
};
ClientController.prototype.msgInputKeyUp = function($event) {
if ($event.keyCode === 13) {
this.publish();
}
};
function ClientControllerCache(scope, logs){
this.scope = scope;
this.logs = logs;
this.val = [];
}
ClientControllerCache.prototype.getClient = function(options) {
var id = options.accessKey + '>' + options.clientId + '@' + options.endpoint;
for (var i = 0; i < this.val.length; i++) {
var ctr = this.val[i];
if (ctr.id === id) {
return ctr.client;
}
}
var client = new MQTTClient(options, this.scope);
var clientController = new ClientController(client, this.logs);
clientController.id = id;
this.val.push(clientController);
return client;
};
ClientControllerCache.prototype.removeClient = function(clientCtr) {
clientCtr.client.disconnect();
var index = this.val.indexOf(clientCtr);
this.val.splice(index, 1);
};
/**
* utilities to do sigv4
* @class SigV4Utils
*/
function SigV4Utils(){}
SigV4Utils.sign = function(key, msg){
var hash = CryptoJS.HmacSHA256(msg, key);
return hash.toString(CryptoJS.enc.Hex);
};
SigV4Utils.sha256 = function(msg) {
var hash = CryptoJS.SHA256(msg);
return hash.toString(CryptoJS.enc.Hex);
};
SigV4Utils.getSignatureKey = function(key, dateStamp, regionName, serviceName) {
var kDate = CryptoJS.HmacSHA256(dateStamp, 'AWS4' + key);
var kRegion = CryptoJS.HmacSHA256(regionName, kDate);
var kService = CryptoJS.HmacSHA256(serviceName, kRegion);
var kSigning = CryptoJS.HmacSHA256('aws4_request', kService);
return kSigning;
};
/**
* AWS IOT MQTT Client
* @class MQTTClient
* @param {Object} options - the client options
* @param {string} options.endpoint
* @param {string} options.regionName
* @param {string} options.accessKey
* @param {string} options.secretKey
* @param {string} options.clientId
* @param {angular.IScope} [scope] - the angular scope used to trigger UI re-paint, you can
omit this if you are not using angular
*/
function MQTTClient(options, scope){
this.options = options;
this.scope = scope;
this.endpoint = this.computeUrl();
this.clientId = options.clientId;
this.name = this.clientId + '@' + options.endpoint;
this.connected = false;
this.client = new Paho.MQTT.Client(this.endpoint, this.clientId);
this.listeners = {};
var self = this;
this.client.onConnectionLost = function() {
self.emit('connectionLost');
self.connected = false;
};
this.client.onMessageArrived = function(msg) {
self.emit('messageArrived', msg);
};
this.on('connected', function(){
self.connected = true;
});
}
/**
* compute the url for websocket connection
* @private
*
* @method MQTTClient#computeUrl
* @return {string} the websocket url
*/
MQTTClient.prototype.computeUrl = function(){
// must use utc time
var time = moment.utc();
var dateStamp = time.format('YYYYMMDD');
var amzdate = dateStamp + 'T' + time.format('HHmmss') + 'Z';
var service = 'iotdevicegateway';
var region = this.options.regionName;
var secretKey = this.options.secretKey;
var accessKey = this.options.accessKey;
var algorithm = 'AWS4-HMAC-SHA256';
var method = 'GET';
var canonicalUri = '/mqtt';
var host = this.options.endpoint;
var credentialScope = dateStamp + '/' + region + '/' + service + '/' + 'aws4_request';
var canonicalQuerystring = 'X-Amz-Algorithm=AWS4-HMAC-SHA256';
canonicalQuerystring += '&X-Amz-Credential=' + encodeURIComponent(accessKey + '/' + credentialScope);
canonicalQuerystring += '&X-Amz-Date=' + amzdate;
canonicalQuerystring += '&X-Amz-Expires=86400';
canonicalQuerystring += '&X-Amz-SignedHeaders=host';
var canonicalHeaders = 'host:' + host + '\n';
var payloadHash = SigV4Utils.sha256('');
var canonicalRequest = method + '\n' + canonicalUri + '\n' + canonicalQuerystring + '\n' + canonicalHeaders + '\nhost\n' + payloadHash;
console.log('canonicalRequest ' + canonicalRequest);
var stringToSign = algorithm + '\n' + amzdate + '\n' + credentialScope + '\n' + SigV4Utils.sha256(canonicalRequest);
var signingKey = SigV4Utils.getSignatureKey(secretKey, dateStamp, region, service);
console.log('stringToSign-------');
console.log(stringToSign);
console.log('------------------');
console.log('signingKey ' + signingKey);
var signature = SigV4Utils.sign(signingKey, stringToSign);
canonicalQuerystring += '&X-Amz-Signature=' + signature;
var requestUrl = 'wss://' + host + canonicalUri + '?' + canonicalQuerystring;
return requestUrl;
};
/**
* listen to client event, supported events are connected, connectionLost,
* messageArrived(event parameter is of type Paho.MQTT.Message), publishFailed,
* subscribeSucess and subscribeFailed
* @method MQTTClient#on
* @param {string} event
* @param {Function} handler
*/
MQTTClient.prototype.on = function(event, handler) {
if (!this.listeners[event]) {
this.listeners[event] = [];
}
this.listeners[event].push(handler);
};
/** emit event
*
* @method MQTTClient#emit
* @param {string} event
* @param {...any} args - event parameters
*/
MQTTClient.prototype.emit = function(event) {
var listeners = this.listeners[event];
if (listeners) {
var args = Array.prototype.slice.apply(arguments, [1]);
for (var i = 0; i < listeners.length; i++) {
var listener = listeners[i];
listener.apply(null, args);
}
// make angular to repaint the ui, remove these if you don't use angular
if(this.scope && !this.scope.$$phase) {
this.scope.$digest();
}
}
};
/**
* connect to AWS, should call this method before publish/subscribe
* @method MQTTClient#connect
*/
MQTTClient.prototype.connect = function() {
var self = this;
var connectOptions = {
onSuccess: function(){
self.emit('connected');
},
useSSL: true,
timeout: 3,
mqttVersion:4,
onFailure: function() {
self.emit('connectionLost');
}
};
this.client.connect(connectOptions);
};
/**
* disconnect
* @method MQTTClient#disconnect
*/
MQTTClient.prototype.disconnect = function() {
this.client.disconnect();
};
/**
* publish a message
* @method MQTTClient#publish
* @param {string} topic
* @param {string} payload
*/
MQTTClient.prototype.publish = function(topic, payload) {
try {
var message = new Paho.MQTT.Message(payload);
message.destinationName = topic;
this.client.send(message);
} catch (e) {
this.emit('publishFailed', e);
}
};
/**
* subscribe to a topic
* @method MQTTClient#subscribe
* @param {string} topic
*/
MQTTClient.prototype.subscribe = function(topic) {
var self = this;
try{
this.client.subscribe(topic, {
onSuccess: function(){
self.emit('subscribeSucess');
},
onFailure: function(){
self.emit('subscribeFailed');
}
});
}catch(e) {
this.emit('subscribeFailed', e);
}
};
angular.module('awsiot.sample', []).controller('AppController', AppController);
})();