-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
215 lines (210 loc) · 7.23 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
import { AzDpsClient, createHmac } from './AzDpsClient.js'
import { AzIoTHubClient, ackPayload } from './AzIoTHubClient.js'
const createApp = () => {
let telemetryInterval
/** @type {AzIoTHubClient} client */
let client
// @ts-ignore
const app = new Vue({
el: '#app',
data: {
saveConfig: true,
viewDpsForm: false,
disableDeviceKey: false,
runningProvision: false,
/** @type {ConnectionInfo} */
connectionInfo: {
scopeId: '',
hubName: '',
deviceId: 'device' + Date.now(),
deviceKey: '',
modelId: 'dtmi:com:example:Thermostat;1',
status: 'Disconnected',
connected: false
},
/** @type {Array<CommandInfo>} */
commands: [],
reportedJson: '{}',
desiredJson: '{}',
desiredCalls: [],
reportedPropJson: '{ deviceStatus: "200 OK" }',
telemetryJson: '{ temperature: %d }',
sentMessages: 0,
isTelemetryRunning: false
},
async created () {
/** @type { ConnectionInfo } connInfo */
const connInfo = JSON.parse(window.localStorage.getItem('connectionInfo') || '{}')
connInfo.deviceId = connInfo.deviceId || 'device' + Date.now()
if (connInfo.scopeId) {
this.connectionInfo.scopeId = connInfo.scopeId
if (connInfo.masterKey) {
this.connectionInfo.masterKey = connInfo.masterKey
this.connectionInfo.deviceKey = await createHmac(this.connectionInfo.masterKey, this.connectionInfo.deviceId)
}
}
if (connInfo.hubName) {
this.connectionInfo.hubName = connInfo.hubName
this.connectionInfo.deviceId = connInfo.deviceId
this.connectionInfo.deviceKey = connInfo.deviceKey
this.connectionInfo.modelId = connInfo.modelId
}
},
methods: {
async provision () {
window.localStorage.setItem('connectionInfo',
JSON.stringify(
{
scopeId: this.connectionInfo.scopeId,
hubName: this.connectionInfo.hubName,
deviceId: this.connectionInfo.deviceId,
deviceKey: this.connectionInfo.deviceKey,
masterKey: this.connectionInfo.masterKey,
modelId: this.connectionInfo.modelId
}))
const dpsClient = new AzDpsClient(this.connectionInfo.scopeId, this.connectionInfo.deviceId, this.connectionInfo.deviceKey, this.connectionInfo.modelId)
this.runningProvision = true
const result = await dpsClient.registerDevice()
this.runningProvision = false
if (result.status === 'assigned') {
this.connectionInfo.hubName = result.registrationState.assignedHub
} else {
console.log(result)
this.connectionInfo.hubName = result.status
}
this.viewDpsForm = false
},
async refreshDeviceId () {
this.connectionInfo.deviceId = 'device' + Date.now()
await this.updateDeviceKey()
},
async connect () {
if (this.saveConfig) {
window.localStorage.setItem('connectionInfo',
JSON.stringify(
{
scopeId: this.connectionInfo.scopeId,
hubName: this.connectionInfo.hubName,
deviceId: this.connectionInfo.deviceId,
deviceKey: this.connectionInfo.deviceKey,
masterKey: this.connectionInfo.masterKey,
modelId: this.connectionInfo.modelId
}))
}
let host = this.connectionInfo.hubName
if (host.indexOf('.azure-devices.net') === -1) {
host += '.azure-devices.net'
}
client = new AzIoTHubClient(host,
this.connectionInfo.deviceId,
this.connectionInfo.deviceKey,
this.connectionInfo.modelId)
client.setDirectMehodCallback((method, payload, rid) => {
const response = JSON.stringify({ responsePayload: 'sample response' })
/** @type {CommandInfo} */
const command = { method, payload, rid, response, dirty: false }
this.commands.push(command)
})
client.setDesiredPropertyCallback(desired => {
this.desiredCalls.push(desired)
this.readTwin()
})
client.disconnectCallback = (err) => {
console.log(err)
this.connectionInfo.connected = false
this.connectionInfo.status = 'Disconnected'
}
await client.connect()
this.connectionInfo.status = 'Connected'
this.connectionInfo.connected = true
await this.readTwin()
},
async readTwin () {
if (client.connected) {
const twin = await client.getTwin()
this.reportedJson = JSON.stringify(twin.reported)
this.desiredJson = JSON.stringify(twin.desired)
} else {
console.log('not connected')
}
},
async reportProp () {
const payload = this.reportedPropJson
const updateResult = await client.updateTwin(payload)
if (updateResult === 204) {
await this.readTwin()
}
},
/**
*
* @param {CommandInfo} cmd
* @param {number} status
*/
cmdResponse (cmd, status) {
// console.log('sending response ' + method + response + rid)
client.commandResponse(cmd.method, cmd.response, cmd.rid, status)
cmd.dirty = true
},
clearCommands () {
this.commands = []
},
startTelemetry () {
telemetryInterval = setInterval(() => {
this.sentMessages++
const telemetryMessage = this.telemetryJson.replace('%d', this.sentMessages)
client.sendTelemetry(telemetryMessage)
}, 1000)
this.isTelemetryRunning = true
},
stopTelemetry () {
clearInterval(telemetryInterval)
this.isTelemetryRunning = false
this.sentMessages = 0
},
async ackDesired (dc, status) {
const dco = JSON.parse(dc)
const payload = ackPayload(dco, status, dco.$version)
const updateResult = await client.updateTwin(JSON.stringify(payload))
if (updateResult === 204) {
await this.readTwin()
this.desiredCalls = []
} else console.log('error updating ack' + updateResult)
},
showDpsForm () {
this.disableDeviceKey = false
this.viewDpsForm = !this.viewDpsForm
},
clearUpdates () {
this.desiredCalls = []
},
async updateDeviceKey () {
if (this.viewDpsForm) {
if (this.connectionInfo.masterKey.length > 0) {
this.disableDeviceKey = true
this.connectionInfo.deviceKey = await createHmac(this.connectionInfo.masterKey, this.connectionInfo.deviceId)
} else {
this.disableDeviceKey = false
}
}
}
},
computed: {
connectionString () {
let host = this.connectionInfo.hubName
if (host.indexOf('.azure-devices.net') === -1) {
host += '.azure-devices.net'
}
return `HostName=${host};DeviceId=${this.connectionInfo.deviceId};SharedAccessKey=${this.connectionInfo.deviceKey}`
}
},
filters: {
pretty: function (value) {
return JSON.stringify(JSON.parse(value), null, 2)
}
}
})
return app
}
(() => {
createApp()
})()