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.

heartbeatService.ts 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { ref } from 'vue'
  2. import { requestNew } from "@/utils/network/request.js";
  3. const bluetoothUtil = require("../../static/etcUtil/index.js");
  4. const datas = require("../../static/etcUtil/datas.js");
  5. // ====================== 心跳机制 ======================
  6. // ====================== 心跳相关状态 ======================
  7. const heartbeatInterval = ref(null); // 心跳定时器ID
  8. const heartbeatDelay = 10000; // 心跳间隔 (5秒)
  9. const heartbeatTimeout = 60000; // 心跳超时时间 (20秒)
  10. const heartbeatTimer = ref<number | null>(null); // 心跳超时定时器ID
  11. const heartbeatData = ['00A40000023F00']; // 心跳数据 (根据设备协议定义)
  12. export const startHeartbeat = () => {
  13. stopHeartbeat(); // 先停止可能存在的旧定时器
  14. // 启动心跳定时器
  15. heartbeatInterval.value = setInterval(() => {
  16. sendHeartbeat();
  17. }, heartbeatDelay);
  18. // 立即发送一次心跳
  19. // sendHeartbeat();
  20. // setTimeout(() => {
  21. // stopHeartbeat();
  22. // }, heartbeatTimeout)
  23. };
  24. export const stopHeartbeat = () => {
  25. if (heartbeatInterval.value !== null) {
  26. clearInterval(heartbeatInterval.value);
  27. heartbeatInterval.value = null;
  28. }
  29. if (heartbeatTimer.value !== null) {
  30. clearTimeout(heartbeatTimer.value);
  31. heartbeatTimer.value = null;
  32. }
  33. };
  34. const sendHeartbeat = () => {
  35. console.log('执行时间'+ new Date())
  36. bluetoothUtil.transCmd(heartbeatData, '20', function (res) {
  37. console.log('心跳执行成功!' + new Date())
  38. }, (err) => {
  39. console.log("结束心跳", err)
  40. stopHeartbeat();
  41. // errorLog(heartbeatData, JSON.stringify(err) + '心跳执行失败!', '---')
  42. });
  43. };
  44. const errorLog = (cmd, error, step) => {
  45. console.log('激活异常提交')
  46. const options = {
  47. type: 2,
  48. data: {
  49. cardId: '1',
  50. obuId: '1',
  51. orderType: "1",
  52. factory: datas.getData("deviceName") + "_" + datas.getData("deviceNameZW"),
  53. orderNo: '1',
  54. cmd,
  55. error,
  56. step
  57. },
  58. method: "POST",
  59. showLoading: false,
  60. };
  61. requestNew('/iaw/api/active/errorLog/add', options).then((res) => {
  62. console.log("错误提交成功", res);
  63. });
  64. };