-
Notifications
You must be signed in to change notification settings - Fork 11
/
shadowService.js
153 lines (137 loc) · 4.95 KB
/
shadowService.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
const mqtt = require('mqtt')
const redis = require('redis')
const {getTopics, removeNulledProps, errObj} = require('./util')
const _ = require('lodash')
let client
let redisClient
let log
module.exports = (options, _log) => {
client = mqtt.connect(`ws://${options.host}:${options.httpPort}/mqqt`)
redisClient = redis.createClient()
log = _log
client.on('connect', () => {
client.subscribe('$aws/things/+/shadow/update')
client.subscribe('$aws/things/+/shadow/delete')
client.subscribe('$aws/things/+/shadow/get')
log('Shadow service connected to IOT broker.')
})
client.on('message', (topic, message) => {
const serialNumber = topic.split('/')[2]
const action = topic.split('/')[4]
switch (action) {
case 'update':
handleUpdateTopic(serialNumber, JSON.parse(message.toString()))
break
case 'get':
log(`Retrieving shadow for '${serialNumber}'`)
handleGetTopic(serialNumber)
break
case 'delete':
log(`Deleting shadow for '${serialNumber}'`)
handleDeleteTopic(serialNumber)
}
})
return {client, redisClient}
}
const handleUpdateTopic = (serialNumber, shadow) => {
redisClient.get(serialNumber, (err, result) => {
if (err) {
return publishError(err)
}
if (shadow.state.desired) {
if (!result) {
return publishError('Can\'t update desired state for non existing thing.')
}
return updateShadowDesired(serialNumber, JSON.parse(result), shadow)
} else {
return updateShadow(serialNumber, (result ? JSON.parse(result) : null), shadow)
}
})
}
const updateShadow = (serialNumber, prevShadow, shadow) => {
log(`Updating shadow reported state for '${serialNumber}'`)
const newShadow = _.merge(prevShadow, shadow)
updateThingDb(serialNumber, newShadow).then((version) => {
publishAccepted(newShadow, serialNumber, version)
publishDocuments(prevShadow, serialNumber, newShadow, version)
}).catch((err) => publishError(err, serialNumber))
}
const updateShadowDesired = (serialNumber, prevShadow, desiredShadow) => {
log(`Updating shadow desired state for '${serialNumber}'`)
const newShadow = _.merge(prevShadow, desiredShadow)
updateThingDb(serialNumber, newShadow).then((version) => {
publishAccepted(desiredShadow, serialNumber, version)
publishDelta(desiredShadow, serialNumber, version)
publishDocuments(prevShadow, serialNumber, newShadow, version)
}).catch((err) => publishError(err, serialNumber))
}
const publishAccepted = (shadow, serialNumber, version) => {
client.publish(getTopics(serialNumber).updateAccepted, JSON.stringify(Object.assign(shadow, {
timestamp: new Date().getTime(),
version
})))
}
const publishDelta = (shadow, serialNumber, version) => {
client.publish(getTopics(serialNumber).updateDelta, JSON.stringify({
timestamp: new Date().getTime(),
version,
state: shadow.state.desired
}))
}
const publishDocuments = (prevShadow, serialNumber, currentShadow, version) => {
client.publish(getTopics(serialNumber).updateDocuments, JSON.stringify({
previous: Object.assign({}, prevShadow, {version: --version}),
current: Object.assign({}, currentShadow, {version}),
timestamp: new Date().getTime()
}))
}
const publishError = (err, serialNumber) => {
client.publish(getTopics(serialNumber).updateRejected, JSON.stringify(errObj(err.toString())))
}
const updateThingDb = (serialNumber, shadow, isDelete) => {
return new Promise((resolve, reject) => {
const newShadow = removeNulledProps(shadow)
redisClient.set(serialNumber, JSON.stringify(newShadow), (err) => {
if (err) {
return reject(err)
}
if (isDelete) {
redisClient.set(`${serialNumber}Version`, '1', (err, version) => {
if (err) {
return reject(err)
}
return resolve(version)
})
} else {
redisClient.incr(`${serialNumber}Version`, (err, version) => {
if (err) {
return reject(err)
}
return resolve(version)
})
}
})
})
}
const handleGetTopic = (serialNumber) => {
redisClient.get(serialNumber, (err, result) => {
if (err || !result) {
return client.publish(getTopics(serialNumber).getRejected, JSON.stringify(errObj(err ? err.toString() : 'No shadow stored for that serial number')))
}
const shadow = JSON.parse(result)
redisClient.get(`${serialNumber}Version`, (err, version) => {
return client.publish(getTopics(serialNumber).getAccepted, JSON.stringify(Object.assign({}, shadow, {
version,
timestamp: new Date().getTime()
})))
})
})
}
const handleDeleteTopic = (serialNumber) => {
updateThingDb(serialNumber, null, true).then((version) => {
client.publish(getTopics(serialNumber).deleteAccepted, JSON.stringify({
version,
timestamp: new Date().getTime()
}))
}).catch((err) => client.publish(getTopics(serialNumber).deleteRejected, JSON.stringify(errObj(err.toString()))))
}