提交 0e5ebf4d 作者: 翁进城

mqtt增加账号密码

上级 064b725c
流水线 #10849 已通过 于阶段
{
"name": "mmc-stl-vue2",
"version": "1.1.2",
"version": "1.1.3",
"description": "科比特前端标准化组件",
"main": "index.js",
"scripts": {
......
import mqtt from 'mqtt';
import mqtt from "mqtt/dist/mqtt";
import orders from "./orders";
function uint8array2json(uint8array) {
......@@ -22,8 +22,22 @@ export default {
} */
},
orders, //所有指令
username: 'tmj',
password: 'Tmj@192D9'
},
mutations: {
/**
* 单纯的给state赋值
* @param {*} param0
* @param {*} data {key: '', value}
*/
setState(state, data) {
try {
state[data.key] = data.value;
} catch (e) {
console.log("setDate err", e);
}
},
setClient(state, data) {
state.client = data;
},
......@@ -43,13 +57,14 @@ export default {
},
actions: {
init({ commit, state }, data) {
let protocol = data.url.replace(/(.+)\:\/\/.*/, "$1");
let clientId = Date.now();
let client = mqtt.connect(data.url, {
// protocol,
username: state.username,
password: state.password,
clientId,
});
console.log("MQTT client", client);
commit("setClient", client);
commit("setClientId", clientId);
......@@ -64,6 +79,8 @@ export default {
if (!state.dataSet[deviceHardId]) {
state.dataSet[deviceHardId] = {};
}
data.$time = Date.now(); // 打上时间戳
state.dataSet[deviceHardId][data.type] = data;
commit("setDataSet", state.dataSet);
});
......@@ -92,9 +109,10 @@ export default {
* @param {} param0
*/
end({ state, commit }) {
state.client.end();
commit('setClient', null);
commit("setClientId", '');
console.log('MMCMQTT: 断开mqtt连接')
state.client?.end();
commit("setClient", null);
commit("setClientId", "");
},
/**
* 订阅
......@@ -107,7 +125,7 @@ export default {
}
let { client } = state;
if (client) {
console.log("订阅数据a", topic);
console.log("订阅数据", topic);
client.subscribe(topic, callback);
}
},
......@@ -172,4 +190,4 @@ export default {
});
},
},
};
\ No newline at end of file
};
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论