-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwa-mqtt.js
117 lines (114 loc) · 3.84 KB
/
wa-mqtt.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
import {
html,
LitElement
} from 'lit';
export class wa_mqtt extends LitElement {
static get properties() {
return {
pub: {
type: String
},
sub: {
type: String
},
set: {
type: String
},
get: {
type: String
},
func: {
type: String
}
}
}
constructor() {
super();
wa_mqtt.Server = {
server: 'wss://mqtt1.webduino.io/mqtt',
userName: 'webduino',
password: 'webduino'
}
wa_mqtt.subs = {}
}
async publish(topic, msg) {
await wa_mqtt.mClient.send({
'topic': topic,
'message': '' + msg
});
}
async firstUpdated() {
var self = this;
if (!wa_mqtt.mClient) {
wa_mqtt.isConnect = false;
wa_mqtt.mClient = new webduino.module.mqttClient();
await wa_mqtt.mClient.connect(wa_mqtt.Server);
console.log("mqttClient connect ok");
wa_mqtt.isConnect = true;
}
if (self.pub) {
var data = self.get.split('.');
var eleId = data[0];
var attr = data[1];
var ele = document.getElementById(eleId);
if (attr == 'on' && ele.tagName == 'MWC-ICON-BUTTON-TOGGLE') {
ele.addEventListener('icon-button-toggle-change', function (evt) {
wa_mqtt.mClient.send({
topic: self.pub,
message: "" + evt.detail.isOn
});
})
} else if (attr == 'input' && ele.tagName == 'MWC-SLIDER') {
ele.addEventListener('input', function (evt) {
wa_mqtt.mClient.send({
topic: self.pub,
message: "" + parseInt(this.value)
})
})
} else if (attr == 'checked' && ele.tagName == 'MWC-SWITCH') {
ele.addEventListener('input', function (evt) {
wa_mqtt.mClient.send({
topic: self.pub,
message: "" + (this.checked != true)
})
})
}
}
if (self.sub) {
var id = setInterval(async function () {
if (wa_mqtt.isConnect) {
if (!wa_mqtt.subs[self.sub]) {
wa_mqtt.subs[self.sub] = [];
await wa_mqtt.mClient.onMessage(self.sub,
async (msg) => {
var evtList = wa_mqtt.subs[self.sub];
for (var i = 0; i < evtList.length; i++) {
evtList[i](msg);
}
});
}
wa_mqtt.subs[self.sub].push(
async (msg) => {
var data = self.set.split('.');
if (msg == 'true') msg = true
if (msg == 'false') msg = false
var eleId = data[0],
attr = data[1]
if (document.getElementById(eleId) == null) {
window[eleId][attr](msg);
} else {
var ele = document.getElementById(eleId);
ele[attr] = msg;
}
}
);
clearInterval(id);
}
}, 100);
}
}
render() {
return html `<div></div>`;
}
}
customElements.define('wa-mqtt', wa_mqtt);