-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
231 lines (181 loc) · 6.01 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
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
const lwm2mid = require('lwm2m-id')
lwm2mid.Cmd = new lwm2mid._Enum({
read: 0,
write: 1,
discover: 2,
writeAttrs: 3,
execute: 4,
observe: 5,
notify: 6,
ping: 7,
identify: 8,
unknown: 255
})
const ut = {
getCmd: null, // define below
getOid: null, // define below
getRid: null, // define below
getRspCode: null, // define below
getSpecificResrcChar: lwm2mid.getRdef
}
/** ********************************************************************************************** */
/** * Identifier Getters ** */
/** ********************************************************************************************** */
ut.getCmd = function (id) {
const cmdItem = lwm2mid.getCmd(id)
return cmdItem ? { key: cmdItem.key, value: cmdItem.value } : undefined
}
ut.cmdNum = function (id) {
const cmdNum = ut.getCmd(id)
return cmdNum ? cmdNum.value : undefined
}
ut.cmdKey = function (id) {
const cmdKey = ut.getCmd(id)
return cmdKey ? cmdKey.key : undefined
}
ut.getOid = function (oid) {
const oidItem = lwm2mid.getOid(oid)
return oidItem ? { key: oidItem.key, value: oidItem.value } : undefined
}
ut.oidKey = function (oid) {
const oidItem = lwm2mid.getOid(oid)
return oidItem ? oidItem.key : oid.toString() // if undefined, return itself
}
ut.oidNum = function (oid) {
const oidItem = lwm2mid.getOid(oid)
if (oidItem) {
return oidItem.value
} else {
if (oid.toString().match(/^[0-9]+$/)) {
const oidNumber = parseInt(oid)
if (!isNaN(oidNumber)) oid = oidNumber
}
return oid
}
}
ut.getRid = function (oid, rid) {
const ridItem = lwm2mid.getRid(oid, rid)
return ridItem ? { key: ridItem.key, value: ridItem.value } : undefined
}
ut.ridKey = function (oid, rid) {
let ridItem
if (typeof rid === 'undefined') {
rid = oid
ridItem = lwm2mid.getRid(rid) // here, oid is rid
} else {
ridItem = lwm2mid.getRid(oid, rid)
}
return ridItem ? ridItem.key : rid.toString() // if undefined, return itself
}
ut.ridNum = function (oid, rid) {
const ridItem = lwm2mid.getRid(oid, rid)
if (typeof rid === 'undefined') rid = oid
if (ridItem) {
return ridItem.value
} else {
if (rid.toString().match(/^[0-9]+$/)) {
const ridNumber = parseInt(rid)
if (!isNaN(ridNumber)) rid = ridNumber
}
return rid
}
}
ut.getRspCode = function (code) {
const rspItem = lwm2mid.getRspCode(code)
return rspItem ? { key: rspItem.key, value: rspItem.value } : undefined
}
ut.rspCodeKey = function (code) {
const k = ut.getRspCode(code)
return k ? k.key : undefined
}
ut.rspCodeNum = function (code) {
const n = ut.getRspCode(code)
return n ? n.value : undefined
}
/** ********************************************************************************************** */
/** * Tackling Path ** */
/** ********************************************************************************************** */
ut.createPath = function () {
if (arguments.length === 0) throw new TypeError('Each argument should be a string or a number.')
const args = Array.prototype.slice.call(arguments)
const connector = args[0]
let path = ''
args.forEach((arg, i) => {
if (!isValidArgType(arg)) throw new TypeError('Each argument should be a string or a number.')
if (i !== 0) path = path + arg + connector
})
return path.slice(0, path.length - 1)
}
ut.slashPath = function (path) {
if (typeof path !== 'string') throw new TypeError('Input path should be a string.')
path = path.replace(/\./g, '/') // tranform dot notation into slash notation
// if the first char of topic is '/', take it off
if (path[0] === '/') path = path.slice(1)
// if the last char of topic is '/', take it off
if (path[path.length - 1] === '/') path = path.slice(0, path.length - 1)
return path
}
ut.dotPath = function (path) {
if (typeof path !== 'string') throw new TypeError('Input path should be a string.')
path = path.replace(/\//g, '.') // tranform slash notation into dot notation
// if the first char of topic is '.', take it off
if (path[0] === '.') path = path.slice(1)
// if the last char of topic is '.', take it off
if (path[path.length - 1] === '.') path = path.slice(0, path.length - 1)
return path
}
ut.pathItems = function (path) {
return ut.slashPath(path).split('/')
}
ut.buildPathValuePairs = function (rootPath, obj) {
let result = {}
rootPath = ut.dotPath(rootPath)
if (obj && typeof obj === 'object') {
if (rootPath !== undefined && rootPath !== '' && rootPath !== '.' && rootPath !== '/') rootPath = `${rootPath}.`
for (const key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
const n = obj[key]
// Tricky: objList is an array, don't buid its full path, or updating new list will fail
if (n && typeof n === 'object' && key !== 'objList') result = Object.assign(result, ut.buildPathValuePairs(rootPath + key, n))
else result[rootPath + key] = n
}
}
} else {
result[rootPath] = obj
}
return result
}
ut.isGoodResponse = function (status) {
const statusCode = ut.getRspCode(status)
const goodCodes = [200, 201, 202, 204, 205]
let included = false
if (typeof statusCode === 'undefined') return false
goodCodes.forEach((v) => {
if (v === statusCode.value) included = true
})
return included
}
ut.getAccessCtrl = function (oid, rid) {
const chr = lwm2mid.getRdef(oid, rid)
return chr ? chr.access : undefined
} // undefined / resrc characteristic
ut.jsonify = function (str) {
let obj
if (typeof str !== 'string') throw new TypeError('Input str should be a string.')
try {
obj = JSON.parse(str)
} catch (e) {
return str
}
return obj
} // undefined/result
function isValidArgType (param) {
let isValid = true
if (typeof param !== 'number' && typeof param !== 'string') {
isValid = false
} else if (typeof param === 'number') {
isValid = !isNaN(param)
}
return isValid
}
module.exports = ut