目录

DJI-Dock2-最新固件3205手动上云实现

目录

DJI Dock2 最新固件3205手动上云实现

前言:版本更新但是demo停止更新导致上云频繁失败,根据文档的 组织管理 进行mqtt相关上行消息监听并自动回复

<script setup>
import { onUnmounted } from 'vue';
import MQTT from 'mqtt';
import { uuidv4 } from '@/utils/uuid';
import { ipConfig, SN } from '@/types/ip_sn';

const drcOptions = {
  username: '',
  password: '',
  clientId: '', // 修改为服务器端的客户端ID
  connectTimeout: 600000,
  keepalive: 10,
  clean: true,
  reconnectPeriod: 3000,
};

// 创建DRC客户端实例
const drcClient = MQTT.connect(`ws://${ipConfig.ip}:8083/mqtt`, drcOptions);

// 创建全局变量
const timestamp = () => new Date().getTime(); // 动态获取时间戳
const gatewaySN = SN.DOCK2_gateway;

// 处理DRC链路的消息
drcClient.on('message', (topic, message) => {
  const request = JSON.parse(message.toString());
  console.log(`[DRC链路] 收到消息: topic=${topic}, message=${message.toString()}`);

  // 根据请求方法处理回复
  if (request.method === 'airport_bind_status') {
    handleAirportBindStatus(request);
  } else if (request.method === 'airport_organization_get') {
    handleAirportOrganizationGet(request);
  } else if (request.method === 'airport_organization_bind') {
    handleAirportOrganizationBind(request);
  } else if (request.method === 'update_topo') {
    handlerTopo(request);
  }
});

// 订阅主题
const subscribeTopics = (client, topic) => {
  client.subscribe(topic, (err) => {
    if (err) {
      console.error(`订阅主题失败: ${topic}`, err);
    } else {
      console.log(`订阅主题成功: ${topic}`);
    }
  });
};

drcClient.on('connect', () => {
  console.log("DRC链路连接成功");
  subscribeTopics(drcClient, `thing/product/${gatewaySN}/requests`);
});

drcClient.on('reconnect', () => {
  console.log('DRC链路重连');
});

drcClient.on('error', (err) => {
  console.error('DRC链路错误:', err);
});

// 组件卸载时断开MQTT连接
onUnmounted(() => {
  drcClient.end();
});

// 处理设备绑定状态查询请求
function handleAirportBindStatus(request) {
  const replyTopic = `thing/product/${gatewaySN}/requests_reply`;
  const reply = {
    bid: request.bid,
    tid: uuidv4(), // 新的事务ID
    timestamp: timestamp(),
    method: 'airport_bind_status',
    data: {
      result: 0,
      output: {
        bind_status: [
          {
            sn: '',// 无人机sn
            is_device_bind_organization: true,
            organization_id: '',
            organization_name: '',
            device_callsign: ''
          },
          {
            sn: '',// 机场sn
            is_device_bind_organization: true,
            organization_id: '',
            organization_name: '',
            device_callsign: ''
          }
        ]
      }
    }
  };
  drcClient.publish(replyTopic, JSON.stringify(reply));
  console.log(`[DRC链路] 发送回复: topic=${replyTopic}, message=${JSON.stringify(reply)}`);
}

// 处理查询设备绑定对应的组织信息请求
function handleAirportOrganizationGet(request) {
  const replyTopic = `thing/product/${gatewaySN}/requests_reply`;
  const reply = {
    bid: request.bid,
    tid: uuidv4(), // 新的事务ID
    timestamp: timestamp(),
    method: 'airport_organization_get',
    data: {
      result: 0,
      output: {
        organization_name: 'Test Group One'
      }
    }
  };
  drcClient.publish(replyTopic, JSON.stringify(reply));
  console.log(`[DRC链路] 发送回复: topic=${replyTopic}, message=${JSON.stringify(reply)}`);
}

// 处理设备绑定到组织请求
function handleAirportOrganizationBind(request) {
  const replyTopic = `thing/product/${gatewaySN}/requests_reply`;
  const reply = {
    bid: request.bid,
    tid: uuidv4(), // 新的事务ID
    timestamp: timestamp(),
    method: 'airport_organization_bind',
    data: {
      result: 0,
      output: {
        err_infos: [
          {
            sn: '',// 无人机sn
            err_code: 0 // 0 表示成功
          },
          {
            sn: '',// 机场sn
            err_code: 0 // 0 表示成功
          }
        ]
      }
    }
  };
  drcClient.publish(replyTopic, JSON.stringify(reply));
  console.log(`[DRC链路] 发送回复: topic=${replyTopic}, message=${JSON.stringify(reply)}`);
}

// 设备拓扑更新
function handlerTopo(request) {
  const replyTopic = `thing/product/${gatewaySN}/status`;
  const reply = {
    bid: request.bid,
    tid: uuidv4(), // 新的事务ID
    timestamp: timestamp(),
    method: 'update_topo',
    data: {
      result: 0,
    }
  };
  drcClient.publish(replyTopic, JSON.stringify(reply));
  console.log(`[DRC链路] 发送回复: topic=${replyTopic}, message=${JSON.stringify(reply)}`);
}
</script>

<template>
  <div class="infoItem">
  </div>
</template>

<style scoped>
.infoItem {
  border-radius: 6px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  flex: 1;
  min-width: 50px;
  text-align: left;
}
</style>