Skip to content
This repository has been archived by the owner on Feb 17, 2021. It is now read-only.

Commit

Permalink
feat(simulator): connect web UI
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed Dec 2, 2019
1 parent 15ef09f commit 73edee9
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
23 changes: 22 additions & 1 deletion azuredeploy.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"contentVersion": "2.0.0.0",
"parameters": {
"iotHubName": {
"type": "string",
Expand Down Expand Up @@ -31,6 +31,14 @@
"metadata": {
"description": "Specifies the number of provisioned IoT Hub units. Restricted to 1 unit for the F1 SKU. Can be set up to maximum number allowed for subscription."
}
},
"DeviceUiStorageName": {
"type": "string",
"defaultValue": "bifravstdeviceui",
"maxLength": 24,
"metadata": {
"description": "Storage account name must be between 3 and 24 characters in length and use numbers and lower-case letters only."
}
}
},
"variables": {
Expand Down Expand Up @@ -78,6 +86,19 @@
]
},
"dependsOn": ["[parameters('iotHubName')]"]
},
{
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2019-04-01",
"name": "[parameters('DeviceUiStorageName')]",
"location": "[parameters('location')]",
"sku": {
"name": "Standard_LRS"
},
"kind": "StorageV2",
"properties": {
"supportsHttpsTrafficOnly": "true"
}
}
],
"outputs": {
Expand Down
32 changes: 31 additions & 1 deletion cli/commands/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import { connect } from 'mqtt'
import { v4 } from 'uuid'
import { parse, URLSearchParams } from 'url'
import { DeviceRegistrationState } from 'azure-iot-provisioning-service/lib/interfaces'
import { uiServer, WebSocketConnection } from '@bifravst/device-ui-server'

const deviceUiUrl = process.env.DEVICE_UI_LOCATION || ''

export const connectCommand = ({
certsDir,
Expand Down Expand Up @@ -130,8 +133,35 @@ export const connectCommand = ({
version: 4,
})

client.on('connect', () => {
let wsConnection: WebSocketConnection

client.on('connect', async () => {
console.log(chalk.green('Connected:'), chalk.blueBright(deviceId))

await uiServer({
deviceUiUrl,
deviceId: deviceId,
onUpdate: update => {
console.log(chalk.magenta('<'), chalk.cyan(JSON.stringify(update)))
},
onWsConnection: c => {
console.log(chalk.magenta('[ws]'), chalk.cyan('connected'))
wsConnection = c
},
})
})

client.on('message', (topic, payload) => {
if (wsConnection) {
console.log(chalk.magenta('[ws>'), JSON.stringify({
topic,
payload,
}))
wsConnection.send(JSON.stringify({
topic,
payload,
}))
}
})

client.on('error', err => {
Expand Down

0 comments on commit 73edee9

Please sign in to comment.