forked from mitipi/serverless-iot-offline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
64 lines (58 loc) · 1.94 KB
/
util.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
const path = require('path')
const fs = require('fs')
const removeNulledProps = (shadow) => {
let obj = JSON.parse(JSON.stringify(shadow))
Object.keys(obj).forEach(key => {
if (obj[key] && typeof obj[key] === 'object') {
removeNulledProps(obj[key])
} else if (obj[key] == null) {
delete obj[key]
}
});
return obj
}
module.exports.removeNulledProps = removeNulledProps
module.exports.getTopics = (serialNumber) => {
return {
updateAccepted: `$aws/things/${serialNumber}/shadow/update/accepted`,
updateDocuments: `$aws/things/${serialNumber}/shadow/update/documents`,
updateDelta: `$aws/things/${serialNumber}/shadow/update/delta`,
updateRejected: `$aws/things/${serialNumber}/shadow/update/rejected`,
getAccepted: `$aws/things/${serialNumber}/shadow/get/accepted`,
getRejected: `$aws/things/${serialNumber}/shadow/get/rejected`,
deleteAccepted: `$aws/things/${serialNumber}/shadow/delete/accepted`,
deleteRejected: `$aws/things/${serialNumber}/shadow/delete/rejected`
}
}
module.exports.seedShadows = (seedPath, redisClient) => {
const location = path.join(process.cwd(), seedPath)
fs.exists(location, (exists) => {
if (exists) {
const shadows = require(location)
Object.keys(shadows).forEach((serialNumber) => {
redisClient.set(serialNumber, JSON.stringify(shadows[serialNumber]))
redisClient.set(`${serialNumber}Version`, '1')
})
}
})
}
module.exports.seedPolicies = (seedPath, redisClient) => {
if(!seedPath) return
const location = path.join(process.cwd(), seedPath)
fs.exists(location, (exists) => {
if (exists) {
const policies = require(location)
Object.keys(policies).forEach((policyName) => {
redisClient.set(policyName, JSON.stringify(policies[policyName]))
})
}
})
}
module.exports.errObj = (message) => {
return {
code: 500,
message,
timestamp: new Date().getTime(),
clientToken: 'token'
}
}