-
Notifications
You must be signed in to change notification settings - Fork 21
/
Integration.vue
59 lines (58 loc) · 1.48 KB
/
Integration.vue
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
<template>
<iframe ref="iframe" :src="host" frameborder="0" :name="name"></iframe>
</template>
<script>
export default {
props: {
host: {
type: String,
default () {
return 'https://mqttboard.flespi.io/#/integration'
}
},
name: String
},
data () {
return {}
},
methods: {
send (cmd, payload) {
cmd = `MQTTBoard${this.name ? `|${this.name}` : ''}|${cmd}${payload !== undefined ? `=>${JSON.stringify(payload)}` : ''}`
this.$refs.iframe.contentWindow.postMessage(cmd, '*')
},
messageProcess (message) {
let cmd = '',
payload = null
if (typeof message === 'string' && message.indexOf('MQTTBoard|') === 0) {
let data = message.split('|')
data = data[this.name ? 2 : 1].split('=>')
cmd = data[0]
try {
payload = JSON.parse(data[1])
} catch (e) {
payload = data[1]
}
}
return {
cmd,
payload,
name: this.name
}
},
setSettings (config) {
this.send('SetSettings', config)
},
setActive (index) { this.send('SetActive', index) },
addPublisher (config) { this.send('AddPublisher', config) },
addSubscriber (config) { this.send('AddSubscriber', config) }
},
created () {
window.addEventListener('message', ({ data }) => {
const { cmd, payload, name } = this.messageProcess(data)
if (name === this.name && cmd) {
this.$emit(cmd, payload)
}
})
}
}
</script>