-
Notifications
You must be signed in to change notification settings - Fork 1
/
iot.js
142 lines (115 loc) · 4.69 KB
/
iot.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
var iotClient = require('ibmiotf'),
utility = require('./utility.js');
var deviceService = function(config) {
var deviceClient = new iotClient.IotfDevice(config);
var methods = {
connect: function(options) {
var defaultOptions = {
logLevel: 'trace',
onConnected: function() {
console.log('### deviceService.Connected ###');
},
onReConnected: function() {
console.log('### deviceService.Re-connected ###');
},
onDisConnected: function() {
console.log('### deviceService.Disconnected ###');
},
onError: function(argument) {
console.log('### deviceService.Error ###');
console.log(argument);
console.log('### /deviceService.Error ###');
}
};
var o = utility.extend(defaultOptions, options);
console.log('### deviceService.Connecting... ###');
deviceClient.log.setLevel(o.logLevel);
deviceClient.connect();
deviceClient.on('connect', o.onConnected);
deviceClient.on('reconnect', o.onReConnected);
deviceClient.on('disconnect', o.onDisConnected);
deviceClient.on('error', o.onError);
return this;
},
publish: function(options) {
var defaultOptions = {
type: 'status',
format: 'json',
data: { }
};
var o = utility.extend(defaultOptions, options);
console.log('### deviceService.Publishing ###');
console.log(JSON.stringify(o.data));
console.log('### /deviceService.Publishing ###');
deviceClient.publish(o.type, o.format, JSON.stringify(o.data));
return this;
},
disconnect: function() {
deviceClient.disconnect();
}
};
return methods;
};
var appService = function(config) {
var deviceClient = new iotClient.IotfApplication(config);
var methods = {
connect: function(options) {
var defaultOptions = {
logLevel: 'error',
onConnected: function() {
console.log('### appService.Connected ###');
},
onReConnected: function() {
console.log('### appService.Re-connected ###');
},
onDisConnected: function() {
console.log('### appService.Disconnected ###');
},
onError: function(argument) {
console.log('### appService.Error ###');
console.log(argument);
console.log('### /appService.Error ###');
},
onDeviceEvent: function(deviceType, deviceId, eventType, format, payload) {
console.log('### appService.DeviceEvent ###');
console.log(deviceType, deviceId, eventType, format);
console.log(payload);
console.log('### /appService.DeviceEvent ###');
}
};
var o = utility.extend(defaultOptions, options);
console.log('### appService.Connecting... ###');
deviceClient.log.setLevel(o.logLevel);
deviceClient.connect();
deviceClient.on('connect', o.onConnected);
deviceClient.on('reconnect', o.onReConnected);
deviceClient.on('disconnect', o.onDisConnected);
deviceClient.on('error', o.onError);
deviceClient.on('deviceEvent', o.onDeviceEvent);
return this;
},
subscribe: function(deviceType, deviceId, eventType) {
deviceClient.subscribeToDeviceEvents(deviceType, deviceId, eventType, "json");
},
publish: function(options) {
var defaultOptions = {
type: 'type',
id: 'id',
event: 'event',
format: 'json',
data: { }
};
var o = utility.extend(defaultOptions, options);
console.log('### deviceService.Publishing ###');
console.log(JSON.stringify(o.data));
console.log('### /deviceService.Publishing ###');
deviceClient.publishDeviceEvent(o.type, o.id, o.event, o.format, JSON.stringify(o.data));
return this;
},
disconnect: function() {
deviceClient.disconnect();
}
};
return methods;
};
module.exports = { DeviceService: deviceService, AppService: appService };