1
0
forked from dyf/APP
Files
APP/utils/MqPlus.js

91 lines
1.6 KiB
JavaScript
Raw Normal View History

2026-05-19 17:38:56 +08:00
import MqttClient from '@/utils/mqtt.js';
class MqPlus {
constructor() {
this.mq = null;
this.init();
}
init(callback) {
this.mq = new MqttClient();
this.mq.connect(() => {
console.log("连接成功");
if(callback){
callback();
}
});
}
//通用发送消息
sendData(topic, payload) {
let task=()=>{
if (payload === undefined || payload === null) {
console.error("消息内容为空")
return false;
}
if (typeof payload === 'object') {
payload = JSON.stringify(payload);
}
this.mq.publish(topic, payload);
}
if(!this.mq){
this.init(task)
}else{
task();
}
}
//发送手机号登陆与推送数据
sendUsrLogin() {
if(!this.mq){
return
}
let pushid=uni.getStorageSync("push_cid");
let phone=uni.getStorageSync("phone");
this.sendData("app/login",{phone:phone,pushid:pushid});
}
//发送充放电数据,chargeType1放电0充电
sendCharge(deviceid,chargeType,battary){
if(!this.mq){
return;
}
let json={
deviceid:deviceid,
sta_system:chargeType,
}
if(battary!==undefined){
json.battary=battary;
}
this.sendData("app/charge",json);
}
//发送报警
sendWarning(deviceid,warn){
if(!this.mq){
return;
}
let json={
deviceid:deviceid,
sta_ShakeBit:warn,
}
this.sendData("app/warn",json);
}
}
let mqplusInstance = null;
export default {
getMqPlus: function() {
if (!mqplusInstance) {
mqplusInstance = new MqPlus();
}
return mqplusInstance;
}
}