2023-02-09 13:23:28 +01:00
|
|
|
let wasm_inited_resolve;
|
2023-02-17 14:13:23 +01:00
|
|
|
const wasm_inited = new Promise((resolve) => {
|
|
|
|
wasm_inited_resolve = resolve;
|
|
|
|
});
|
2023-02-09 13:23:28 +01:00
|
|
|
|
|
|
|
document.addEventListener("wasm-loaded", () => {
|
2023-02-17 14:13:23 +01:00
|
|
|
wasm_inited_resolve(wasm_bindgen);
|
2023-02-09 13:23:28 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
document.addEventListener("alpine:init", () => {
|
|
|
|
Alpine.store("mqtt", {
|
|
|
|
connected: false,
|
|
|
|
_client: null,
|
2023-02-17 14:13:23 +01:00
|
|
|
_topics: null,
|
2023-02-09 13:23:28 +01:00
|
|
|
_c: null,
|
|
|
|
|
|
|
|
_pendingPromises: {},
|
|
|
|
|
|
|
|
sendTime(time) {
|
|
|
|
if (!this.connected) return null;
|
|
|
|
|
|
|
|
const id = uuidv4();
|
2023-02-17 14:13:23 +01:00
|
|
|
const promise = new Promise((resolve, reject) => {
|
|
|
|
this._pendingPromises[id] = [resolve, reject];
|
|
|
|
});
|
2023-02-09 13:23:28 +01:00
|
|
|
this._publish({
|
|
|
|
id: id, // used to prevent replay attacks and to identify confirm messages
|
|
|
|
kind: "Time", // can be "time" or "confirm"
|
|
|
|
time: time, // only used for "time"
|
|
|
|
});
|
|
|
|
|
|
|
|
return promise;
|
|
|
|
},
|
|
|
|
|
|
|
|
async connect() {
|
|
|
|
if (this.connected) return;
|
2023-02-17 14:13:23 +01:00
|
|
|
|
2023-02-09 13:23:28 +01:00
|
|
|
const password = Alpine.store("localState").password;
|
|
|
|
const that = this;
|
|
|
|
const brokerDomain = "broker.emqx.io";
|
|
|
|
const url = `wss://${brokerDomain}:8084/mqtt`;
|
|
|
|
|
|
|
|
if (!password) return false;
|
|
|
|
|
2023-02-17 14:13:23 +01:00
|
|
|
const { Crypto } = await wasm_inited;
|
2023-02-09 13:23:28 +01:00
|
|
|
|
|
|
|
Alpine.store("localState")._state = 5;
|
|
|
|
|
|
|
|
// derive key from password
|
|
|
|
this._c = new Crypto(password, brokerDomain);
|
|
|
|
|
|
|
|
console.log("Test", this._encrypt("test"));
|
|
|
|
|
2023-02-17 14:13:23 +01:00
|
|
|
this._topics = {
|
|
|
|
time: Crypto.sha256(
|
|
|
|
this._encrypt(`org.speedclimbing.ok-ready-go.${password}.time`)
|
|
|
|
).toString(),
|
|
|
|
confirmation: Crypto.sha256(
|
|
|
|
this._encrypt(
|
|
|
|
`org.speedclimbing.ok-ready-go.${password}.confirmation`
|
|
|
|
)
|
|
|
|
).toString(),
|
|
|
|
};
|
2023-02-09 13:23:28 +01:00
|
|
|
|
|
|
|
console.log("Connecting to MQTT broker...");
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
// Clean session
|
|
|
|
clean: true,
|
|
|
|
connectTimeout: 4000,
|
|
|
|
};
|
|
|
|
|
|
|
|
this._client = mqtt.connect(url, options);
|
|
|
|
|
|
|
|
this._client.on("connect", () => {
|
|
|
|
Alpine.store("localState")._state = 0;
|
|
|
|
|
2023-02-17 14:13:23 +01:00
|
|
|
that._client.subscribe(that._topics.confirmation);
|
2023-02-09 13:23:28 +01:00
|
|
|
this.connected = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
this._client.on("message", (topic, message) => {
|
|
|
|
// message is Buffer
|
|
|
|
message = that._decrypt(message.toString());
|
|
|
|
const data = JSON.parse(message);
|
|
|
|
|
2023-02-17 14:13:23 +01:00
|
|
|
if (
|
|
|
|
topic !== that._topics.confirmation ||
|
|
|
|
data.kind !== "Confirm" ||
|
|
|
|
Object.keys(this._pendingPromises).indexOf(data.id) === -1
|
|
|
|
)
|
|
|
|
return;
|
2023-02-09 13:23:28 +01:00
|
|
|
|
|
|
|
console.log("<<< ", data);
|
|
|
|
this._pendingPromises[data.id][0]();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
disconnect() {
|
2023-02-17 14:13:23 +01:00
|
|
|
if (!this.connected) return;
|
2023-02-09 13:23:28 +01:00
|
|
|
|
|
|
|
this._client.end(true);
|
|
|
|
|
|
|
|
this._client = null;
|
|
|
|
this.connected = false;
|
2023-02-17 14:13:23 +01:00
|
|
|
this._topics = null;
|
2023-02-09 13:23:28 +01:00
|
|
|
|
|
|
|
for (const promiseId in this._pendingPromises) {
|
|
|
|
this._pendingPromises[promiseId][1]();
|
|
|
|
}
|
|
|
|
|
|
|
|
this._pendingPromises = {};
|
|
|
|
},
|
|
|
|
|
|
|
|
_publish(data) {
|
|
|
|
const encryptedData = this._encrypt(JSON.stringify(data));
|
|
|
|
console.log(">>> ", data);
|
2023-02-17 14:13:23 +01:00
|
|
|
this._client.publish(this._topics.time, encryptedData, {
|
2023-02-09 13:23:28 +01:00
|
|
|
qos: 1,
|
|
|
|
retain: false,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
_encrypt(data) {
|
|
|
|
return this._c.encrypt(data);
|
|
|
|
},
|
|
|
|
|
|
|
|
_decrypt(data) {
|
|
|
|
return this._c.decrypt(data);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|