-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
109 lines (86 loc) · 3.03 KB
/
app.js
File metadata and controls
109 lines (86 loc) · 3.03 KB
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
const express = require('express')
const path = require('path')
const bodyParser = require('body-parser')
const hub = require('./app.iothub.js')
const http = require('http')
const WebSocket = require('ws')
const EventHubReader = require('./app.eventHub')
const { exit } = require('process')
const port = process.env.PORT || 3000
const connectionString = process.env.IOTHUB_CONNECTION_STRING
const eventHubConsumerGroup = process.env.EVENTHUB_CONSUMER_GROUP || '$Default'
if (!connectionString || connectionString.length < 10) {
console.error('IOTHUB_CONNECTION_STRING not found ' + connectionString)
exit()
}
const [HostName] = connectionString.split(';')
const hubName = HostName.split('=')[1]
const app = express()
const router = express.Router()
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.use('/api', router)
app.use(express.static('wwwroot'))
const server = http.createServer(app)
const wss = new WebSocket.Server({ server })
router.get('/', (req, res, next) => res.sendFile('index.html', { root: path.join(__dirname, 'wwwroot/index.html') }))
router.get('/hubInfo', (req, res) => {
res.json(hubName)
})
router.get('/getDevices', async (req, res) => {
if (connectionString.length > 0) {
const devices = await hub.getDeviceList(connectionString)
res.json(devices.result)
} else {
res.json({})
}
})
router.get('/getDeviceTwin', async (req, res) => {
const result = await hub.getDeviceTwin(connectionString, req.query.deviceId)
res.json(result.responseBody)
})
router.post('/updateDeviceTwin', async (req, res) => {
const result = await hub.updateDeviceTwin(connectionString, req.body.deviceId, req.body.propertyName, req.body.propertyValue)
res.json(result.responseBody)
})
router.get('/removeDevice', async (req, res) => {
const result = await hub.removeDevice(connectionString, req.query.deviceId)
res.json(result.responseBody)
})
router.post('/invokeCommand', async (req, res) => {
const result = await hub.invokeDeviceMethod(
connectionString,
req.body.deviceId,
req.body.commandName,
req.body.payload)
res.json(result)
})
const eventHubReader = new EventHubReader(connectionString, eventHubConsumerGroup)
server.listen(port, () => console.log(`App listening on port ${port} | Hub: ${hubName} | ConsumerGroup: ${eventHubConsumerGroup}`))
const devicesToListen = []
wss.on('connection', (ws, req) => {
const did = req.url.substring(6)
if (devicesToListen.indexOf(did) === -1) {
devicesToListen.push(did)
}
})
;(async () => {
await eventHubReader.startReadMessage((message, date, deviceId) => {
// console.log(deviceId)
// console.log(message)
const payload = {
IotData: message,
MessageDate: date || Date.now().toISOString(),
DeviceId: deviceId
}
if (devicesToListen.indexOf(deviceId) > -1) {
// console.log(payload)
wss.clients.forEach((client) => {
// console.log(client)
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify(payload))
}
})
}
})
})()