提交 0e5ebf4d 作者: 翁进城

mqtt增加账号密码

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