-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcoap-client.js
187 lines (168 loc) · 5.46 KB
/
coap-client.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
const HOSTNAME = 'coap.thethings.io'
, API_VERSION = 'v2'
, coap = require('coap')
, events = require('events')
, util = require('util')
function parametersToQuery(parameters) {
var query = ''
for (var param in parameters) {
query += param + '=' + parameters[param] + '&'
}
return query.substring(0, query.length - 1)
}
var Client = module.exports = function Client(config) {
if (!(this instanceof Client)) {
return new Client(config)
}
events.EventEmitter.call(this)
this.thingToken = config.thingToken
this.activationCode = config.activationCode
var that = this
if (!this.thingToken && this.activationCode) {
var activationRequest = this.activateThing(this.activationCode)
activationRequest.on('data', function (data) {
if (data.status === 'error') {
that.emit('error', 'error activating thing')
} else if (data.status === 'created') {
that.emit('activated', data)
that.thingToken = data.thingToken
that.emit('ready')
} else {
that.emit('error', 'unknown activation response')
}
})
activationRequest.end()
}
if (this.thingToken) {
setImmediate(function () {//maybe a process.nextTick would be better
that.emit('ready')
})
}
}
function Req(request, object, callback) {
if (callback === undefined && typeof object === 'function') {
callback = object
}
var that = this
events.EventEmitter.call(this)
this.request = request
this.object = object
this.timeout = null
this._restartTimeout = function (time) {
if (that.timeout) {
clearTimeout(this.timeout)
}
that.timeout = setTimeout(function () {
that.emit('disconected')
}, time + 1000)
return that.timeout
}
if (request.keepAlive) {
that._restartTimeout(request.keepAlive)
}
request.on('response', function (res) {
res.on('data', function (chunk) {
if (request.keepAlive) {
that._restartTimeout(request.keepAlive)
if (chunk.toString() === '{}') {
that.emit('keepAlive')
return
}
chunk = JSON.parse(chunk)
if (chunk.status === 'success' && chunk.message === 'subscribed') {
that.emit('subscribed')
return
} else if (chunk.status === 'error') {
if (callback !== undefined) {
return callback(chunk.message)
} else {
that.emit('error', chunk.message)
}
}
} else {
chunk = JSON.parse(chunk)
}
that.emit('data', chunk)
if (callback !== undefined) {
callback(null, chunk)
}
})
})
request.on('error', function (error) {
if (callback === undefined) {
that.emit('error', error)
} else {
callback(error)
}
})
this.end = function () {
request.end(JSON.stringify(this.object))
}
if (callback !== undefined) {
this.end()
}
}
util.inherits(Req, events.EventEmitter)
util.inherits(Client, events.EventEmitter)
Client.prototype.activateThing = function (activatonCode, callback) {
var request = coap.request({
hostname: HOSTNAME,
pathname: '/' + API_VERSION + '/things',
method: 'POST'
})
var req = new Req(request, {activationCode: activatonCode}, callback)
return req
}
Client.prototype.thingRead = function (key, parameters, callback) {
if (typeof parameters === 'function') {
callback = parameters
}
if (typeof parameters !== 'object') {
parameters = {}
}
var request = coap.request({
hostname: HOSTNAME,
pathname: '/' + API_VERSION + '/things/' + this.thingToken + '/resources/' + key + '?' + parametersToQuery(parameters)
})
var req = new Req(request, callback)
return req
}
Client.prototype.thingWrite = function (object, parameters, callback) {
if (typeof parameters === 'function') {
callback = parameters
}
if (typeof parameters !== 'object') {
parameters = {}
}
var request = coap.request({
hostname: HOSTNAME,
pathname: '/' + API_VERSION + '/things/' + this.thingToken + '?' + parametersToQuery(parameters),
method: 'POST'
}
)
if (object === null || object === undefined) {
throw 'Object to write not defined'
}
var req = new Req(request, object, callback)
return req
}
Client.prototype.thingSubscribe = function (parameters, callback) {
if (typeof parameters === 'function') {
callback = parameters
}
if (typeof parameters !== 'object') {
parameters = {}
}
if (!parameters.keepAlive) {
parameters.keepAlive = 60000//one min
}
var request = coap.request({
hostname: HOSTNAME,
pathname: '/' + API_VERSION + '/things/' + this.thingToken + '?' + parametersToQuery(parameters),
observe: true
}
)
request.keepAlive = parameters.keepAlive
var req = new Req(request, callback)
return req
}