pilot-home.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. <template>
  2. <a-layout class="page">
  3. <a-layout-sider class="left" width="40%" style="border-radius: 4px;">
  4. <div style="width:90%; height: 90%; margin: 4vh">
  5. <a-layout style="height: 20%; margin-top: 3vh; background-color: white; ">
  6. <a-layout-sider width="25%" theme="light" align="center">
  7. <a-avatar :size="60" :src="cloudapi">
  8. </a-avatar>
  9. </a-layout-sider>
  10. <a-layout-content style="margin-left: 1vw;">
  11. <div style="height: 50%;">
  12. <a-dropdown :trigger="['click']">
  13. <span style="font-size: 16px; font-weight: bolder">{{ workspaceName }}</span>
  14. <template #overlay>
  15. <a-menu v-model:selectedKeys="state.selectedKeys" @select="onSelectMenu">
  16. <a-menu-item v-for="item in state.projectList" :key="item.id">
  17. {{ item.name }}
  18. </a-menu-item>
  19. </a-menu>
  20. </template>
  21. </a-dropdown>
  22. <RightOutlined style="float: right; margin-top: 5px; color: #8894a0" @click="showStatus" />
  23. </div>
  24. <div style="height: 50%;">
  25. <CloudSyncOutlined v-if="thingState === EStatusValue.CONNECTED" style="color: #75c5f6" />
  26. <SyncOutlined spin v-else />
  27. <span style="color: #737373; margin-left: 3px;">{{ thingState }}</span>
  28. </div>
  29. <a-drawer placement="right" v-model:visible="drawerVisible" width="340px">
  30. <div class="mb10 flex-row flex-justify-center flex-align-center">
  31. <p class="fz14" style="font-weight: 100;">
  32. 模块状态
  33. </p>
  34. </div>
  35. <div class="width-100 mb10 flex-align-start" v-for="m in modules" :key="m.name" style="height: 30px;">
  36. <div class="ml5" style="float: left; color: #000000;">{{ m.name }}:</div>
  37. <div class="ml10" style="float: right; margin-bottom: 8px;">
  38. <span :key="m.state" :class="m.state.value === EStatusValue.CONNECTED ? 'green' : 'red'">{{
  39. m.state.value }}&nbsp;</span>
  40. <a-button-group>
  41. <a-button class="ml5" type="primary" size="small" @click.stop="moduleInstall(m)">安装</a-button>
  42. <a-button class="ml5 mr5" type="danger" size="small" @click.stop="moduleUninstall(m)">卸载</a-button>
  43. </a-button-group>
  44. </div>
  45. <a-divider />
  46. </div>
  47. </a-drawer>
  48. </a-layout-content>
  49. </a-layout>
  50. <a-divider style="height: 2px; background-color: #f5f5f5; margin-top: 3vh;" />
  51. <a-button id="exitBtn" class="fz18" @click="confirmAgain"
  52. style="width: 10vw; height: 10vh; position: fixed; bottom: 13vh; left: 15vw; background-color: #e6e6e6; color: red; border: 0;"
  53. type="primary">
  54. 退出
  55. </a-button>
  56. <a-modal v-model:visible="exitVisible" width="300px" :closable="false">
  57. <template #footer>
  58. <a-button type="text" style="width: 48%; float: left;" @click="onBack">取消</a-button>
  59. <a-button type="text" style="width: 48%;" @click="onExit">确定</a-button>
  60. </template>
  61. <p>退出后,大疆无人机与该服务器之间的数据将不会同步</p>
  62. </a-modal>
  63. </div>
  64. </a-layout-sider>
  65. <a-layout-content class="right flex-column">
  66. <div class="mb5">
  67. <span class="ml5" style="color: #939393;">序列号</span>
  68. </div>
  69. <div class="fz16" style="background-color: white; border-radius: 4px;">
  70. <a-row style="border-bottom: 1px solid #f4f8f9; height: 45px;" align="middle">
  71. <a-col :span="1"></a-col>
  72. <a-col :span="9">
  73. 遥控器SN
  74. </a-col>
  75. <a-col :span="13" class="flex-align-end flex-column">
  76. <span style="color: #737373">{{ device.data.gateway_sn }}</span>
  77. </a-col>
  78. </a-row>
  79. <a-row style="border-bottom: 1px solid #f4f8f9; height: 45px;" align="middle"
  80. v-if="device.data.online_status && device.data.sn">
  81. <a-col :span="1"></a-col>
  82. <a-col :span="9">Aircraft Sn</a-col>
  83. <a-col :span="13" class="flex-align-end flex-column">
  84. <span style="color: #737373">{{ device.data.sn }}</span>
  85. </a-col>
  86. </a-row>
  87. </div>
  88. <div class="mt5 mb5">
  89. <span class="ml5" style="color: #939393;">设置</span>
  90. </div>
  91. <div class="fz16" style="background-color: white; border-radius: 4px;">
  92. <a-row v-if="device.data.online_status && device.data.sn"
  93. style="border-bottom: 1px solid #f4f8f9; height: 45px;" align="middle" @click="bindingDevice">
  94. <a-col :span="1"></a-col>
  95. <a-col :span="11">
  96. 设备绑定
  97. </a-col>
  98. <a-col :span="10" style="text-align: right">
  99. <span v-if="device.data.bound_status" style="color: #737373">Aircraft bound</span>
  100. <span v-else style="color: #737373">Aircraft not bound</span>
  101. </a-col>
  102. <a-col :span="2" class="flex-align-center flex-column">
  103. <RightOutlined style="color: #8894a0; font-size: 20px;" />
  104. </a-col>
  105. </a-row>
  106. <a-row style="border-bottom: 1px solid #f4f8f9; height: 45px;" align="middle" @click="onMediaSetting">
  107. <a-col :span="1"></a-col>
  108. <a-col :span="21">
  109. 上传媒体
  110. </a-col>
  111. <a-col :span="2" class="flex-align-center flex-column">
  112. <RightOutlined style="color: #8894a0; font-size: 20px;" />
  113. </a-col>
  114. </a-row>
  115. <a-row style="border-bottom: 1px solid #f4f8f9; height: 45px;" align="middle" @click="onLiveshareSetting">
  116. <a-col :span="1"></a-col>
  117. <a-col :span="21">
  118. 手动直播
  119. </a-col>
  120. <a-col :span="2" class="flex-align-center flex-column">
  121. <RightOutlined style="color: #8894a0; font-size: 20px;" />
  122. </a-col>
  123. </a-row>
  124. </div>
  125. </a-layout-content>
  126. </a-layout>
  127. </template>
  128. <script lang="ts" setup>
  129. import { message } from 'ant-design-vue'
  130. import { onMounted, reactive, ref } from 'vue'
  131. import { BindBody, bindDevice, getDeviceBySn, getPlatformInfo, getUserInfo } from '/@/api/manage'
  132. import apiPilot, { ApiParam, MapParam, ThingParam, WsParam } from '/@/api/pilot-bridge'
  133. import { getRoot } from '/@/root'
  134. import { EBizCode, EComponentName, EDownloadOwner, ELocalStorageKey, ERouterName, EStatusValue } from '/@/types'
  135. import cloudapi from '/@/assets/icons/cloudapi.png'
  136. import { SwapOutlined, RightOutlined, CloudSyncOutlined, SyncOutlined } from '@ant-design/icons-vue'
  137. import { useMyStore } from '/@/store'
  138. import { DeviceStatus } from '/@/types/device'
  139. import { useConnectWebSocket } from '/@/hooks/use-connect-websocket'
  140. import { apis } from "/@/api/custom/index";
  141. const root = getRoot()
  142. const gatewayState = ref<boolean>(localStorage.getItem(ELocalStorageKey.GatewayOnline) === 'true')
  143. const thingState = ref(EStatusValue.DISCONNECT)
  144. const apiState = ref(EStatusValue.DISCONNECT)
  145. const liveState = ref(EStatusValue.DISCONNECT)
  146. const wsState = ref(EStatusValue.DISCONNECT)
  147. const mapState = ref(EStatusValue.DISCONNECT)
  148. const tsaState = ref(EStatusValue.DISCONNECT)
  149. const mediaState = ref(EStatusValue.DISCONNECT)
  150. const waylineState = ref(EStatusValue.DISCONNECT)
  151. const workspaceName = ref<string>(localStorage.getItem(ELocalStorageKey.WorkspaceName)!)
  152. const username = ref(localStorage.getItem(ELocalStorageKey.Username)!)
  153. const wsId = ref(localStorage.getItem(ELocalStorageKey.WorkspaceId)!)
  154. const components = apiPilot.init()
  155. const exitVisible = ref(false)
  156. const drawerVisible = ref(false)
  157. let minitor: any
  158. interface DeviceInfoData {
  159. data: DeviceStatus
  160. }
  161. const device = reactive<DeviceInfoData>({
  162. data: {
  163. sn: '',
  164. online_status: false,
  165. device_callsign: '',
  166. user_id: '',
  167. user_callsign: '',
  168. bound_status: false,
  169. model: '',
  170. gateway_sn: EStatusValue.DISCONNECT,
  171. domain: -1
  172. }
  173. })
  174. const state = reactive({
  175. selectedKeys: [],
  176. projectList: [],
  177. })
  178. const onSelectMenu = (items: any) => {
  179. console.log(items);
  180. }
  181. const bindParam: BindBody = {
  182. device_sn: '',
  183. user_id: '',
  184. workspace_id: wsId.value
  185. }
  186. const modules = [{
  187. name: 'Cloud',
  188. state: thingState,
  189. module: EComponentName.Thing
  190. }, {
  191. name: 'Api',
  192. state: apiState,
  193. module: EComponentName.Api
  194. }, {
  195. name: 'Live',
  196. state: liveState,
  197. module: EComponentName.Liveshare
  198. }, {
  199. name: 'Ws',
  200. state: wsState,
  201. module: EComponentName.Ws
  202. }, {
  203. name: 'Map',
  204. state: mapState,
  205. module: EComponentName.Map
  206. }, {
  207. name: 'Tsa',
  208. state: tsaState,
  209. module: EComponentName.Tsa
  210. }, {
  211. name: 'Media',
  212. state: mediaState,
  213. module: EComponentName.Media
  214. }, {
  215. name: 'Wayline',
  216. state: waylineState,
  217. module: EComponentName.Mission
  218. }]
  219. const store = useMyStore()
  220. const messageHandler = async (payload: any) => {
  221. if (!payload) {
  222. return
  223. }
  224. switch (payload.biz_code) {
  225. case EBizCode.DeviceOnline: {
  226. console.info('online: ', payload)
  227. if (payload.data.sn === device.data.gateway_sn) {
  228. localStorage.setItem(ELocalStorageKey.GatewayOnline, gatewayState.value.toString())
  229. break
  230. }
  231. if (payload.data.gateway_sn === device.data.gateway_sn) {
  232. device.data = payload.data
  233. localStorage.setItem(ELocalStorageKey.Device, JSON.stringify(device.data))
  234. }
  235. break
  236. }
  237. case EBizCode.DeviceOffline: {
  238. console.info('offline: ', payload)
  239. if (payload.data.sn === device.data.sn) {
  240. device.data.online_status = payload.data.online_status
  241. localStorage.setItem(ELocalStorageKey.Device, JSON.stringify(device.data))
  242. }
  243. break
  244. }
  245. default:
  246. break
  247. }
  248. }
  249. // 监听ws 消息
  250. useConnectWebSocket(messageHandler)
  251. let bindNum: any
  252. onMounted(async () => {
  253. const res = await apis.fetchChangeRecordList();
  254. state.projectList = res.data
  255. state.selectedKeys = res.data
  256. apiPilot.onBackClickReg()
  257. apiPilot.onStopPlatform()
  258. const oldDevice = localStorage.getItem(ELocalStorageKey.Device)
  259. if (oldDevice) {
  260. device.data = JSON.parse(oldDevice)
  261. }
  262. window.connectCallback = (arg: any) => {
  263. connectCallback(arg)
  264. }
  265. window.wsConnectCallback = (arg: any) => {
  266. wsConnectCallback(arg)
  267. }
  268. device.data.gateway_sn = apiPilot.getRemoteControllerSN()
  269. if (device.data.gateway_sn === EStatusValue.DISCONNECT.toString()) {
  270. message.warn('Data is not available, please restart the remote control.')
  271. return
  272. }
  273. device.data.sn = apiPilot.getAircraftSN()
  274. getDeviceInfo()
  275. const isLoaded = apiPilot.isComponentLoaded(EComponentName.Thing)
  276. if (isLoaded) {
  277. username.value = '' + localStorage.getItem(ELocalStorageKey.Username)
  278. workspaceName.value = '' + localStorage.getItem(ELocalStorageKey.WorkspaceName)
  279. refreshStatus()
  280. apiPilot.setPlatformMessage(
  281. '' + localStorage.getItem(ELocalStorageKey.PlatformName),
  282. workspaceName.value,
  283. '' + localStorage.getItem(ELocalStorageKey.WorkspaceDesc)
  284. )
  285. return
  286. }
  287. setWorkspaceInfo()
  288. getUserInfo().then(res => {
  289. username.value = res.data.username
  290. localStorage.setItem(ELocalStorageKey.Username, username.value)
  291. // thing
  292. const param: ThingParam = {
  293. host: res.data.mqtt_addr,
  294. username: res.data.mqtt_username,
  295. password: res.data.mqtt_password,
  296. connectCallback: 'connectCallback'
  297. }
  298. components.set(EComponentName.Thing, param)
  299. apiPilot.loadComponent(EComponentName.Thing, components.get(EComponentName.Thing))
  300. bindParam.device_sn = device.data.gateway_sn
  301. bindParam.user_id = res.data.user_id
  302. bindParam.workspace_id = res.data.workspace_id
  303. })
  304. })
  305. const connectCallback = async (arg: any) => {
  306. if (arg) {
  307. thingState.value = EStatusValue.CONNECTED
  308. // liveshare
  309. apiPilot.loadComponent(EComponentName.Liveshare, components.get(EComponentName.Liveshare))
  310. // ws
  311. const wsParam: WsParam = components.get(EComponentName.Ws)
  312. wsParam.token = apiPilot.getToken()
  313. apiPilot.loadComponent(EComponentName.Ws, components.get(EComponentName.Ws))
  314. // map
  315. const mapParam: MapParam = components.get(EComponentName.Map)
  316. mapParam.userName = username.value
  317. apiPilot.loadComponent(EComponentName.Map, components.get(EComponentName.Map))
  318. // tsa
  319. apiPilot.loadComponent(EComponentName.Tsa, components.get(EComponentName.Tsa))
  320. // media
  321. apiPilot.loadComponent(EComponentName.Media, components.get(EComponentName.Media))
  322. apiPilot.setDownloadOwner(EDownloadOwner.Mine.valueOf())
  323. // mission
  324. apiPilot.loadComponent(EComponentName.Mission, {})
  325. bindNum = setInterval(() => {
  326. if (!bindParam.device_sn) {
  327. device.data.gateway_sn = apiPilot.getRemoteControllerSN()
  328. bindParam.device_sn = device.data.gateway_sn
  329. return
  330. }
  331. bindDevice(bindParam).then(bindRes => {
  332. if (bindRes.code !== 0) {
  333. message.error(bindRes.message)
  334. console.error(bindRes.message)
  335. } else {
  336. clearInterval(bindNum)
  337. }
  338. })
  339. }, 2000)
  340. setTimeout(getDeviceInfo, 3000)
  341. } else {
  342. thingState.value = EStatusValue.DISCONNECT
  343. }
  344. refreshStatus()
  345. }
  346. const wsConnectCallback = async (arg: any) => {
  347. if (arg) {
  348. wsState.value = EStatusValue.CONNECTED
  349. } else {
  350. wsState.value = EStatusValue.DISCONNECT
  351. }
  352. }
  353. const confirmAgain = () => {
  354. exitVisible.value = true
  355. }
  356. const onBack = () => {
  357. exitVisible.value = false
  358. }
  359. const onExit = () => {
  360. localStorage.clear()
  361. apiPilot.stopwebview()
  362. }
  363. const bindingDevice = async () => {
  364. root.$router.push(ERouterName.PILOT_BIND)
  365. }
  366. const onMediaSetting = async (e: any) => {
  367. root.$router.push(ERouterName.PILOT_MEDIA)
  368. }
  369. const onLiveshareSetting = async (e: any) => {
  370. root.$router.push(ERouterName.PILOT_LIVESHARE)
  371. }
  372. const onOpen3rdApp = () => {
  373. const packageName = 'com.dji.sample'
  374. const isInstalled = apiPilot.isAppInstalled(packageName)
  375. if (isInstalled) {
  376. window.open('https://www.dji.com')
  377. } else {
  378. message.error(packageName + ' is not installed.')
  379. }
  380. }
  381. const showStatus = async () => {
  382. minitor = setInterval(() => {
  383. refreshStatus()
  384. if (!drawerVisible.value) {
  385. clearInterval(minitor)
  386. }
  387. }, 2000)
  388. drawerVisible.value = true
  389. }
  390. function setWorkspaceInfo() {
  391. if (localStorage.getItem(ELocalStorageKey.WorkspaceName)) {
  392. apiPilot.setPlatformMessage(
  393. '' + localStorage.getItem(ELocalStorageKey.PlatformName),
  394. workspaceName.value,
  395. '' + localStorage.getItem(ELocalStorageKey.WorkspaceDesc)
  396. )
  397. apiPilot.setWorkspaceId(wsId.value)
  398. return
  399. }
  400. getPlatformInfo().then(res => {
  401. console.log(res)
  402. workspaceName.value = res.data.workspace_name
  403. wsId.value = res.data.workspace_id
  404. localStorage.setItem(ELocalStorageKey.PlatformName, res.data.platform_name)
  405. localStorage.setItem(ELocalStorageKey.WorkspaceName, workspaceName.value)
  406. localStorage.setItem(ELocalStorageKey.WorkspaceDesc, res.data.workspace_desc)
  407. apiPilot.setPlatformMessage(
  408. res.data.platform_name,
  409. workspaceName.value,
  410. res.data.workspace_desc
  411. )
  412. apiPilot.setWorkspaceId(wsId.value)
  413. })
  414. }
  415. function refreshStatus() {
  416. thingState.value = apiPilot.thingGetConnectState() ? EStatusValue.CONNECTED : EStatusValue.DISCONNECT
  417. apiState.value = apiPilot.isComponentLoaded(EComponentName.Api) ? EStatusValue.CONNECTED : EStatusValue.DISCONNECT
  418. liveState.value = apiPilot.isComponentLoaded(EComponentName.Liveshare) ? EStatusValue.CONNECTED : EStatusValue.DISCONNECT
  419. wsState.value = apiPilot.isComponentLoaded(EComponentName.Ws) && apiPilot.wsGetConnectState()
  420. ? EStatusValue.CONNECTED
  421. : EStatusValue.DISCONNECT
  422. mapState.value = apiPilot.isComponentLoaded(EComponentName.Map) ? EStatusValue.CONNECTED : EStatusValue.DISCONNECT
  423. tsaState.value = apiPilot.isComponentLoaded(EComponentName.Tsa) ? EStatusValue.CONNECTED : EStatusValue.DISCONNECT
  424. mediaState.value = apiPilot.isComponentLoaded(EComponentName.Media) ? EStatusValue.CONNECTED : EStatusValue.DISCONNECT
  425. waylineState.value = apiPilot.isComponentLoaded(EComponentName.Mission) ? EStatusValue.CONNECTED : EStatusValue.DISCONNECT
  426. }
  427. function moduleInstall(m: any) {
  428. let param
  429. switch (m.module) {
  430. case EComponentName.Thing:
  431. param = apiPilot.thingGetConfigs()
  432. break
  433. case EComponentName.Api: {
  434. const apiParam: ApiParam = {
  435. host: apiPilot.getHost(),
  436. token: apiPilot.getToken()
  437. }
  438. param = apiParam
  439. break
  440. }
  441. case EComponentName.Map: {
  442. const mapParam: MapParam = components.get(EComponentName.Map)
  443. mapParam.userName = '' + localStorage.getItem(ELocalStorageKey.Username)
  444. param = mapParam
  445. break
  446. }
  447. case EComponentName.Ws: {
  448. const wsParam: WsParam = components.get(EComponentName.Ws)
  449. wsParam.token = '' + localStorage.getItem(ELocalStorageKey.Token)
  450. param = wsParam
  451. break
  452. }
  453. default:
  454. param = components.get(m.module)
  455. }
  456. components.set(m.module, param)
  457. console.info(components.get(m.module))
  458. apiPilot.loadComponent(m.module, components.get(m.module))
  459. refreshStatus()
  460. }
  461. function moduleUninstall(m: any) {
  462. message.info('uninstall ' + m.module)
  463. apiPilot.unloadComponent(m.module)
  464. refreshStatus()
  465. }
  466. function getDeviceInfo() {
  467. if (!device.data.sn || device.data.sn === EStatusValue.DISCONNECT) {
  468. return
  469. }
  470. getDeviceBySn(bindParam.workspace_id, device.data.sn).then(res => {
  471. if (res.code !== 0) {
  472. return
  473. }
  474. device.data.online_status = res.data.status
  475. device.data.bound_status = res.data.bound_status
  476. device.data.device_callsign = res.data.nickname
  477. device.data.model = res.data.device_name
  478. localStorage.setItem(ELocalStorageKey.Device, JSON.stringify(device.data))
  479. })
  480. }
  481. </script>
  482. <style lang="scss" scoped>
  483. @import '/@/styles/index.scss';
  484. .page {
  485. display: flex;
  486. position: absolute;
  487. transition: width 0.2s ease;
  488. height: 100%;
  489. width: 100%;
  490. .left {
  491. height: 90%;
  492. background-color: white;
  493. margin-top: 6vh;
  494. margin-left: 2vh;
  495. }
  496. .right {
  497. height: 90%;
  498. margin-top: 6vh;
  499. margin-left: 5vh;
  500. margin-right: 5vh;
  501. }
  502. }
  503. .green {
  504. color: green
  505. }
  506. .red {
  507. color: red;
  508. }
  509. #exitBtn:hover :active {
  510. background-color: rgb(77, 75, 75);
  511. width: 10vw;
  512. height: 10vh;
  513. position: fixed;
  514. bottom: 13vh;
  515. left: 15vw;
  516. line-height: 10vh;
  517. }
  518. </style>