You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
110 lines
3.7 KiB
110 lines
3.7 KiB
// app.js
|
|
const mqtt = require("mqtt");
|
|
const _ = require("lodash")
|
|
// const aedes = require("aedes")();
|
|
// const mqttserver = require("net").createServer(aedes.handle);
|
|
class AppBootHook {
|
|
constructor(app) {
|
|
this.app = app;
|
|
}
|
|
|
|
configWillLoad() {
|
|
// 此时 config 文件已经被读取并合并,但是还并未生效
|
|
// 这是应用层修改配置的最后时机
|
|
// 注意:此函数只支持同步调用
|
|
}
|
|
|
|
async didLoad() {
|
|
// 所有的配置已经加载完毕
|
|
// 可以用来加载应用自定义的文件,启动自定义的服务
|
|
}
|
|
|
|
async willReady() {
|
|
// 所有的插件都已启动完毕,但是应用整体还未 ready
|
|
// 可以做一些数据初始化等操作,这些操作成功才会启动应用
|
|
}
|
|
|
|
async didReady() {
|
|
|
|
const client = mqtt.connect("mqtt://192.168.1.102:1883", {
|
|
// const client = mqtt.connect("mqtt://127.0.0.1:1883", {
|
|
clean: true,
|
|
connectTimeout: 4000,
|
|
clientId: 'map-editor-' + Math.random().toString(16).substr(2),
|
|
username: 'map-editor',
|
|
password: 'leador',
|
|
});
|
|
client.on("connect", function () {
|
|
console.log("appmqtt服务器连接成功");
|
|
});
|
|
|
|
this.app.messenger.on('plan_action', async data => {
|
|
let result = await this.app.curl('http://192.168.1.102:8086/v1/path', {
|
|
// let result = await this.app.curl('http://127.0.0.1:8086/v1/path', {
|
|
// 必须指定 method
|
|
method: 'POST',
|
|
// 通过 contentType 声明以 JSON 格式发送
|
|
contentType: 'json',
|
|
data,
|
|
// 明确告诉 HttpClient 以 JSON 格式处理返回的响应 body
|
|
dataType: 'json',
|
|
});
|
|
let getmapdata = {
|
|
map: data.map
|
|
}
|
|
let mapResult = await this.app.curl(`http://192.168.1.102:8086/v1/roadmap/geojson?map=${getmapdata.map}`, {
|
|
// let mapResult = await this.app.curl(`http://127.0.0.1:8086/v1/roadmap/geojson?map=${getmapdata.map}`, {
|
|
// 必须指定 method
|
|
method: 'GET',
|
|
// 通过 contentType 声明以 JSON 格式发送
|
|
contentType: 'json',
|
|
// 明确告诉 HttpClient 以 JSON 格式处理返回的响应 body
|
|
dataType: 'json',
|
|
});
|
|
let plaPparams = {
|
|
mapData: mapResult.data
|
|
}
|
|
console.log(7441, data, getmapdata, plaPparams)
|
|
// client.publish("/planResult", JSON.stringify(
|
|
|
|
// ), { qos: 1, retain: 0, })
|
|
// let params = {
|
|
// "timestamp": Date.now(),
|
|
// "pub_timestamp": Date.now(),
|
|
// "status": "ok",
|
|
// "args": [
|
|
// {
|
|
// nid: [],
|
|
// "roadmap": data.map,
|
|
// "coord": data.coord
|
|
// }
|
|
// ]
|
|
// }
|
|
// console.log(741, result, data)
|
|
// client.publish("/robot4inspection/fa49e558574df1ec/planning/service/plan/response", JSON.stringify(params), { qos: 2, retain: 0, })
|
|
});
|
|
this.app.messenger.on('plan_delete', async data => {
|
|
const result = await this.app.curl('http://192.168.1.102:8086/v1/path', {
|
|
// const result = await this.app.curl('http://127.0.0.1:8086/v1/path', {
|
|
// 必须指定 method
|
|
method: 'DELETE',
|
|
// 通过 contentType 声明以 JSON 格式发送
|
|
contentType: 'json',
|
|
data,
|
|
// 明确告诉 HttpClient 以 JSON 格式处理返回的响应 body
|
|
dataType: 'json',
|
|
});
|
|
// client.publish("/delPlanResult", JSON.stringify(result.data), {
|
|
// qos: 1,
|
|
// retain: 1,
|
|
// })
|
|
console.log(7411, result)
|
|
});
|
|
}
|
|
async serverDidReady() {
|
|
// http / https server 已启动,开始接受外部请求
|
|
// 此时可以从 app.server 拿到 server 的实例
|
|
}
|
|
}
|
|
|
|
module.exports = AppBootHook;
|
|
|