-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathindex.js
110 lines (94 loc) · 3.32 KB
/
index.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
// import AWS from 'aws-sdk/dist/aws-sdk-react-native';
import {AWSIoTData} from './aws-iot-device-sdk-js-react-native.js';
/**
* Use this class with redux
*
* @param {Object} configuration
* @return {{ AWSIoTMQTTClient }}
* @constructor
*/
export function AWSIoTMQTTClient(configuration) {
const self = { };
self.service = null;
self.store = null;
configuration.type = configuration.type === 'shadow' ? 'thingShadow' : 'device';
const config = {
//
// Set the AWS region we will operate in.
//
region: configuration.region,
host: configuration.host,
//
// Connect via secure WebSocket
//
protocol: 'wss',
//
// Set the maximum reconnect time to 8 seconds; this is a browser application
// so we don't want to leave the user waiting too long for reconnection after
// re-connecting to the network/re-opening their laptop/etc...
//
maximumReconnectTimeMs: 1000,
//
// Enable console debugging information (optional)
//
debug: true,
//
// IMPORTANT: the AWS access key ID, secret key, and sesion token must be
// initialized with empty strings.
//
accessKeyId: '',
secretKey: '',
sessionToken: '',
};
if (configuration) {
Object.assign(config, configuration);
}
self.connect = function(accessKeyId, secretAccessKey, sessionToken) {
config.accessKeyId = accessKeyId;
config.secretKey = secretAccessKey;
config.sessionToken = sessionToken;
self.service = AWSIoTData[config.type](config);
self.config = config;
self.service.on('connect', self.onConnect);
self.service.on('reconnect', self.onReconnect);
self.service.on('offline', self.onOffline);
self.service.on('error', self.onError);
self.service.on('message', self.onMessage);
self.service.on('status', self.onStatus);
self.service.on('delta', self.onDelta);
self.service.on('close', self.onClose);
self.service.on('timeout', self.onTimeout);
};
self.setStore = function(store) {
self.store = store;
};
self.onConnect = function() {
self.store.dispatch({type: 'MQTT_ON_CONNECT'});
};
self.onReconnect = function() {
self.store.dispatch({type: 'MQTT_ON_RECONNECT'});
};
self.onOffline = function() {
self.store.dispatch({type: 'MQTT_ON_OFFLINE'});
};
self.onError = function(error) {
self.store.dispatch({type: 'MQTT_ON_ERROR', error});
};
self.onMessage = function(topic, payload) {
self.store.dispatch({type: 'MQTT_ON_MESSAGE', topic, payload});
};
self.onStatus = function(thingId, statusType, clientToken, stateObject) {
self.store.dispatch({type: 'MQTT_ON_STATUS', thingId, statusType, clientToken, stateObject});
};
self.onDelta = function(thingName, stateObject) {
self.store.dispatch({type: 'MQTT_ON_DELTA', thingName, stateObject});
};
self.onClose = function() {
self.store.dispatch({type: 'MQTT_ON_CLOSE'});
};
self.onTimeout = function(thingName, clientToken) {
self.store.dispatch({type: 'MQTT_ON_TIMEOUT', thingName, clientToken});
};
return self;
}
export default AWSIoTMQTTClient;