forked from williamwilliamwilliam/react-native-iothub-device
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
126 lines (97 loc) · 3.98 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import {NativeEventEmitter, NativeModules} from 'react-native';
export const {IoTHubDeviceModule} = NativeModules;
export async function initialize(onConnectionStatusChange, onDeviceTwinPropertyRetrieved, onMessageReceived, onDeviceTwinStatusCallback, onEventCallback) {
new NativeEventEmitter(IoTHubDeviceModule).addListener('onConnectionStatusChange', (event) => onConnectionStatusChange(event));
new NativeEventEmitter(IoTHubDeviceModule).addListener('onDesiredPropertyUpdate', (event) => {
if (event.propertyJson) {
const property = JSON.parse(event.propertyJson);
onDesiredPropertyUpdate(property.property);
}
});
/**
* All properties retrieved when first connected, and retrieved when an update is made remotely.
*/
new NativeEventEmitter(IoTHubDeviceModule).addListener('onDeviceTwinPropertyRetrieved', (event) => {
if (event && event.propertyJson) {
try {
onDeviceTwinPropertyRetrieved(JSON.parse(event.propertyJson));
} catch (error) {
console.error(error);
}
}
});
/**
* When device operations are invoked from this device, IoT Hub will send response messages.
*/
new NativeEventEmitter(IoTHubDeviceModule).addListener('onDeviceTwinStatusCallback', (event) => {
onDeviceTwinStatusCallback(event);
});
new NativeEventEmitter(IoTHubDeviceModule).addListener('onMessageReceived', (event) => {
onMessageReceived(event);
});
new NativeEventEmitter(IoTHubDeviceModule).addListener('onEventCallback', (event) => {
onEventCallback(event);
});
new NativeEventEmitter(IoTHubDeviceModule).addListener('log', (event) => {
console.debug("IOT Native Module LOG:", event.message + " @ " + event.timeStamp);
});
new NativeEventEmitter(IoTHubDeviceModule).addListener('debug', (event) => {
console.debug("IOT Native Module DEBUG LOG:", event);
});
new NativeEventEmitter(IoTHubDeviceModule).addListener('error', (event) => {
console.debug("IOT Native Module ERROR:", event);
});
await IoTHubDeviceModule.Initialize();
}
export async function connect(connectionString, desiredPropertySubscriptions) {
return await IoTHubDeviceModule.connectToHub(connectionString, desiredPropertySubscriptions);
}
export async function disconnect() {
return await IoTHubDeviceModule.disconnectFromHub();
}
// export async function requestTwinProperties() {
// return await IoTHubDeviceModule.requestTwinProperties();
// }
/**
* Returns Promise. Doesn't actually return device twin - it makes a request to your hub
* to send you all Device Twin properties via the onDeviceTwinPropertyRetrieved callback.
*
* @param propertyKey
* @param success
* @param failure
* @returns {*}
*/
export function subscribeToTwinDesiredProperties(propertyKey, success, failure) {
return IoTHubDeviceModule.subscribeToTwinDesiredProperties(propertyKey, success, failure);
}
/**
* @param {Object[]} properties - Example input: {testValue:12345, testValue2:"12345", testValue3: true}
* @returns {Promise}
*/
export function reportProperties(properties) {
//translate simple json map to a key/value array
const keyValueArray = [];
Object.keys(properties).forEach(key => {
keyValueArray.push({
key,
value: properties[key]
});
});
return IoTHubDeviceModule.sendReportedProperties(keyValueArray);
}
/**
* @param {Object[]} properties - Example input: {testValue:12345, testValue2:"12345", testValue3: true}
@param {Object} eventJson - Json object to send as a message
* @returns {Promise}
*/
export function sendMessage(properties, eventJson) {
const keyValueArray = [];
Object.keys(properties).forEach(key => {
keyValueArray.push({
key,
value: properties[key]
});
});
let message = JSON.stringify(eventJson);
return IoTHubDeviceModule.sendMessage(keyValueArray, message);
}