Browse Source

模拟蓝牙连接/断开

master
zhangjiming 4 months ago
parent
commit
46b8c003f7
  1. 3
      public/index.html
  2. 29
      public/main.js
  3. 38
      src/routes/cmd.ts

3
public/index.html

@ -14,6 +14,9 @@
<!-- <button id="upload">连续上报</button> --> <!-- <button id="upload">连续上报</button> -->
<button id="endRecord">结束测量</button> <button id="endRecord">结束测量</button>
<button id="disconnect-ble">断开蓝牙</button>
<button id="connect-ble">连接蓝牙</button>
</div> </div>
</div> </div>
<script src="lib/zepto.min.js"></script> <script src="lib/zepto.min.js"></script>

29
public/main.js

@ -41,4 +41,31 @@ $("#endRecord").on("click", () => {
}, },
}); });
}); });
$('#disconnect-ble').on("click", ()=> {
$.ajax({
type: "POST",
url: "/api/cmd/disconnect-ble",
data: JSON.stringify({}),
contentType: "application/json",
success: res => {
console.log("Success", res);
},
error: err => {
console.error("Error", err);
},
})
})
$('#connect-ble').on("click", ()=> {
$.ajax({
type: "POST",
url: "/api/cmd/connect-ble",
data: JSON.stringify({}),
contentType: "application/json",
success: res => {
console.log("Success", res);
},
error: err => {
console.error("Error", err);
},
})
})

38
src/routes/cmd.ts

@ -1,19 +1,33 @@
import express from "express"; import express from "express";
import { delay } from "../utils/helper"; import { delay } from "../utils/helper";
import { WsProxy } from "../utils/wss";
const router = express.Router(); const router = express.Router();
router.post("/", async (req, res) => {
await delay(200);
// setTimeout(() => {
// wsSend(req.app.locals["wss"], {
// type: "cmd",
// data: {
// commandId: req.body.commandId,
// status: "D0000",
// },
// });
// }, 2000);
res.json({ code: "00000", msg: "执行成功" });
router.post("/disconnect-ble", async (req, res) => {
WsProxy.sendMobile({
type: "peripheral-status",
data: {
connected: false, //是否已连接蓝牙
power: 60, //电量
inclinatorX: 0.276, //x轴倾斜
inclinatorY: 3.019, //y轴倾斜
temperature: 32.026, //温度
},
});
res.json({ success: true });
});
router.post("/connect-ble", async (req, res) => {
WsProxy.sendMobile({
type: "peripheral-status",
data: {
connected: true, //是否已连接蓝牙
power: 60, //电量
inclinatorX: 0.276, //x轴倾斜
inclinatorY: 3.019, //y轴倾斜
temperature: 32.026, //温度
},
});
res.json({ success: true });
}); });
export default router; export default router;
Loading…
Cancel
Save