first commit
This commit is contained in:
152
server/router_handler/mqtt_router_handle.js
Normal file
152
server/router_handler/mqtt_router_handle.js
Normal file
@ -0,0 +1,152 @@
|
||||
|
||||
const db=require("../comman/db")
|
||||
const mqttserver=require("../comman/mqqthttp")
|
||||
const {log} = require("debug");
|
||||
exports.getSwitchStat=(req, res) => {
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
exports.getSwitchList= async (req, res) => {
|
||||
|
||||
|
||||
let listonline=await mqttserver.queryalldeviceonline()
|
||||
|
||||
|
||||
|
||||
sql='SELECT * FROM `remoteswitch` WHERE 1'
|
||||
db.query(sql,(err,result)=>{
|
||||
|
||||
let devicelist=new Array()
|
||||
|
||||
for(rowindex in result) {
|
||||
// console.log(result)
|
||||
|
||||
Data={
|
||||
|
||||
ID: rowindex,
|
||||
SwitchID:result[rowindex].id,
|
||||
Type:result[rowindex].type,
|
||||
Detail:result[rowindex].tips,
|
||||
SwitchData: [
|
||||
{ Stat: result[rowindex].switch1===1?true:false, ID: 1},
|
||||
{ Stat: result[rowindex].switch2===1?true:false, ID: 2},
|
||||
{ Stat: result[rowindex].switch3===1?true:false, ID: 3},
|
||||
{Stat: result[rowindex].switch4===1?true:false, ID: 4}
|
||||
|
||||
]
|
||||
}
|
||||
if (listonline.includes(result[rowindex].id))
|
||||
{
|
||||
Data.isonline=true;
|
||||
}else
|
||||
{
|
||||
continue
|
||||
Data.isonline=false;
|
||||
}
|
||||
|
||||
//res.send(Data);
|
||||
|
||||
devicelist.push(Data);
|
||||
|
||||
|
||||
}
|
||||
//console.log(devicelist)
|
||||
backdata={
|
||||
|
||||
devicelist:devicelist
|
||||
}
|
||||
res.send(backdata);
|
||||
// console.log(devicelist[0].SwitchData)
|
||||
|
||||
// res.send("sdfsdfsdf");
|
||||
// res.send(devicelist);
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
exports.SetSwitch=async (req, res) => {
|
||||
|
||||
|
||||
|
||||
req.body=req.query
|
||||
//console.log(req)
|
||||
|
||||
|
||||
|
||||
Data={
|
||||
id:req.body.Data.SwitchID,
|
||||
sid:req.body.Data.number,
|
||||
st:(req.body.Data.stat==="true"?1:0)
|
||||
}
|
||||
|
||||
//console.log("data send")
|
||||
// console.log(Data)
|
||||
let mqttclient = global.MqttClient
|
||||
mqttclient.publish("remoteShutter",JSON.stringify(Data));
|
||||
message= await waitForMessage("remoteShuttersend")
|
||||
message=JSON.parse(message)
|
||||
// console.log("message send")
|
||||
// console.log(message)
|
||||
Data=req.body.Data
|
||||
for (i=0;i<4;i++)
|
||||
{
|
||||
Data.SwitchData[i]=message.st[i]
|
||||
}
|
||||
delete Data.number
|
||||
backdata={
|
||||
index:req.body.index,
|
||||
|
||||
Data://Data
|
||||
{
|
||||
|
||||
ID: req.body.Data.ID,
|
||||
SwitchID:message.id,
|
||||
Type:req.body.Data.Type,
|
||||
Detail:req.body.Data.Detail,
|
||||
isonline:true,
|
||||
SwitchData: [
|
||||
{ Stat: message.st[0], ID: 1},
|
||||
{ Stat: message.st[1], ID: 2},
|
||||
{ Stat: message.st[2], ID: 3},
|
||||
{Stat: message.st[3], ID: 4}
|
||||
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
console.log(backdata.Data)
|
||||
res.send(backdata);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
function waitForMessage(topic) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let client = global.MqttClient
|
||||
client.subscribe(topic, (err) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
client.on('message', (t, message) => {
|
||||
if (t === topic) {
|
||||
client.unsubscribe(topic);
|
||||
resolve(message.toString());
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
Reference in New Issue
Block a user