App.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. <script setup lang="ts">
  2. import { onUnmounted, ref, watch } from 'vue'
  3. import Login from '@/components/login.vue'
  4. import Gauge from '@/components/gauge.vue'
  5. import Record from '@/components/record.vue'
  6. import Battery from '@/components/battery.vue'
  7. import Loading from '@/components/loading.vue'
  8. import Signal from '@/components/signal.vue'
  9. const sleep = (ms: number) => new Promise((res) => setTimeout(res, ms))
  10. const iceServers = [
  11. {
  12. urls: [ 'stun:caner.top:3478' ]
  13. },
  14. {
  15. urls: 'turn:caner.top:3478',
  16. username: 'admin',
  17. credential: '123456'
  18. }
  19. ]
  20. const Peer = ref(null as null | RTCPeerConnection | undefined)
  21. const isLogin = ref(false)
  22. const remoteVideo = ref()
  23. const showLoading = ref(true)
  24. const winMaxOrMin = ref(false)
  25. const micState = ref(false)
  26. const audioState = ref(false)
  27. const audioStateNum = ref(0)
  28. const warnAudio = ref(false)
  29. const quantity = ref(0)
  30. const error = ref('')
  31. const conctrlData = ref({
  32. v0: 128, v1: 128, v2: 128, v3: 128
  33. })
  34. const conctrlAnimation = ref(0)
  35. const conctrlGrears = ref(false)
  36. const conctrlNum = ref(0)
  37. const SpeedValue = ref(0)
  38. const signalValue = ref(0)
  39. // 发送控制数据
  40. function sendContrlData() {
  41. if (showLoading.value) return
  42. window.$electron.send('sendMqtt', { type: 'conctrl', conctrl: { ...conctrlData.value } })
  43. conctrlAnimation.value = requestAnimationFrame(sendContrlData)
  44. }
  45. // 关闭
  46. function close(err?: string) {
  47. if (Peer.value) Peer.value?.close()
  48. if (remoteVideo.value) remoteVideo.value.srcObject = null
  49. isLogin.value = false
  50. showLoading.value = false
  51. error.value = err || ''
  52. Peer.value = null
  53. audioStateNum.value = 0
  54. quantity.value = 0
  55. winMaxOrMin.value = false
  56. cancelAnimationFrame(conctrlAnimation.value)
  57. window.$electron.send('closeMqtt')
  58. console.log('close mqtt')
  59. }
  60. // 初始化rtc
  61. function initRTC() {
  62. try {
  63. console.log('start RTC')
  64. Peer.value = new RTCPeerConnection({
  65. iceServers,
  66. bundlePolicy: 'max-bundle'
  67. })
  68. // listen state
  69. Peer.value.onicegatheringstatechange = () => {
  70. console.log('GatheringState: ', Peer.value?.iceGatheringState)
  71. if (Peer.value?.iceGatheringState === 'complete') {
  72. const answer = Peer.value.localDescription
  73. console.log('send answer', answer)
  74. window.$electron.send('sendMqtt', answer?.toJSON())
  75. }
  76. }
  77. // listen track
  78. Peer.value.ontrack = async (evt) => {
  79. console.log('track', evt)
  80. remoteVideo.value.srcObject = evt.streams[0]
  81. }
  82. // listen changestate·
  83. Peer.value.oniceconnectionstatechange = async () => {
  84. const state = Peer.value?.iceConnectionState
  85. console.log('StateChange', state)
  86. if (
  87. state === 'failed'
  88. || state === 'disconnected'
  89. || state === 'closed'
  90. ) {
  91. close('P2P通信失败')
  92. }
  93. // ICE连接成功
  94. if (state === 'connected') {
  95. await sleep(3000)
  96. showLoading.value = false
  97. sendContrlData()
  98. }
  99. }
  100. console.log('RTC SUCCESS')
  101. } catch (error) {
  102. close('RTC 初始化失败')
  103. }
  104. }
  105. // 登录
  106. function login(data: { name: string, room: string, url: string }) {
  107. window.$electron.send('loginMqtt', { room: data.room, name: data.name, url: data.url })
  108. }
  109. // 档位计算
  110. function countContrlData(v: number) {
  111. // 转10进制
  112. const num = parseInt(v.toString(), 10)
  113. if (conctrlNum.value % 2) {
  114. // 倒档
  115. return Math.floor(num / 2)
  116. }
  117. // 前进
  118. return Math.floor(((128 - (num / 2)) + 128))
  119. }
  120. // 发送语音
  121. function sendAudio(blob: Blob) {
  122. window.$electron.send('sendMqtt', { type: 'Meadia', Meadia: blob })
  123. }
  124. // 窗口事件
  125. function titleEvent(type: string) {
  126. if (type === 'maxWin') winMaxOrMin.value = !winMaxOrMin.value
  127. if (type === 'closeWin') { close() } else { window.$electron?.send(type, winMaxOrMin.value) }
  128. }
  129. // mqtt
  130. window.$electron.on('message', async (msg: any) => {
  131. switch (msg.type) {
  132. case 'connect':
  133. console.log('mqtt connected')
  134. isLogin.value = true
  135. showLoading.value = true
  136. initRTC()
  137. break
  138. case 'disconnect':
  139. close('服务器连接失败')
  140. break
  141. case 'join':
  142. console.log('join mqtt channel', msg)
  143. window.$electron.send('sendMqtt', { type: 'startRTC' })
  144. break
  145. case 'leave':
  146. close('对方断开连接')
  147. break
  148. case 'offer':
  149. {
  150. console.log('get offer?', msg)
  151. await Peer.value?.setRemoteDescription(msg)
  152. const answerd = await Peer.value?.createAnswer()
  153. await Peer.value?.setLocalDescription(answerd)
  154. }
  155. break
  156. case 'power':
  157. quantity.value = msg.power
  158. console.log('电量', msg.power)
  159. break
  160. case 'speed':
  161. SpeedValue.value = Math.floor(msg.speed)
  162. console.log('速度')
  163. break
  164. case 'signal':
  165. signalValue.value = Math.floor(msg.signal)
  166. console.log('信号')
  167. break
  168. case 'contrl':
  169. {
  170. const db = msg.content
  171. // 倒档 | 前进 =>右拨片
  172. conctrlGrears.value = db[6] === 2
  173. // 鸣笛 =>回车
  174. warnAudio.value = !!db[54]
  175. // 录音 =>左拨片
  176. micState.value = db[6] === 1
  177. // 静音 =>L3
  178. audioState.value = db[6] === 64
  179. // 控制
  180. conctrlData.value = {
  181. v0: 128,
  182. v1: 128,
  183. v2: parseInt(db[44].toString(), 10), // 方向盘
  184. v3: countContrlData(db[46]) // 油门
  185. }
  186. }
  187. console.log('遥控数据', conctrlData.value)
  188. break
  189. default:
  190. break
  191. }
  192. })
  193. // 关闭loadingwin
  194. window.$electron.send('close-loading')
  195. // 监听按钮状态
  196. watch([ audioState, warnAudio, conctrlGrears ], () => {
  197. if (showLoading.value) return
  198. if (audioState.value) {
  199. audioStateNum.value++
  200. window.$electron.send('sendMqtt', { type: 'contrlAudio', contrlAudio: !!(audioStateNum.value % 2) })
  201. }
  202. if (conctrlGrears.value) {
  203. conctrlNum.value++
  204. }
  205. if (warnAudio.value) window.$electron.send('sendMqtt', { type: 'warnAudio', contrlAudio: !!(audioStateNum.value % 2) })
  206. })
  207. onUnmounted(() => close())
  208. </script>
  209. <template>
  210. <template v-if="isLogin">
  211. <video
  212. ref="remoteVideo"
  213. autoplay
  214. playsinline
  215. />
  216. <div class="marke">
  217. <div class="marke-left">
  218. <!-- 音频状态 -->
  219. <Record
  220. class="marke-audio"
  221. :size="25"
  222. :audio-state="micState"
  223. @callBack="sendAudio"
  224. />
  225. <!-- 喇叭状态 -->
  226. <Icon
  227. name="audio"
  228. :size="25"
  229. :color="(!!(audioStateNum % 2)) ? '#00CED1' : 'rgba(0, 0, 0, 0.3)'"
  230. />
  231. <!-- 电量状态 -->
  232. <Battery :quantity="quantity" />
  233. <Signal :signal-value="signalValue" />
  234. </div>
  235. <div class="marke-right">
  236. <Icon
  237. name="min"
  238. :size="20"
  239. color="#fff"
  240. @click="titleEvent('minWin')"
  241. />
  242. <Icon
  243. :name="winMaxOrMin ? 'max' : 'maxMin'"
  244. :size="20"
  245. color="#fff"
  246. @click="titleEvent('maxWin')"
  247. />
  248. <Icon
  249. name="close"
  250. :size="20"
  251. color="#fff"
  252. @click="titleEvent('closeWin')"
  253. />
  254. </div>
  255. </div>
  256. <!-- 码数 -->
  257. <div class="gauge">
  258. <Gauge
  259. :value="SpeedValue"
  260. :gears="conctrlNum % 2 ? '倒档' : '前进'"
  261. />
  262. </div>
  263. <Loading v-if="showLoading" />
  264. </template>
  265. <Login
  266. v-else
  267. v-model="error"
  268. @loginBack="login"
  269. />
  270. </template>
  271. <style scoped lang="scss">
  272. video {
  273. background: black;
  274. object-fit: fill;
  275. font-size: 0;
  276. }
  277. .marke {
  278. position: fixed;
  279. top: 0;
  280. left: 0;
  281. width: available;
  282. width: -webkit-fill-available;
  283. height: 35px;
  284. z-index: 1;
  285. background: #666666;
  286. display: flex;
  287. align-items: center;
  288. justify-content: flex-end;
  289. overflow: hidden;
  290. border-top-left-radius: 13px;
  291. border-top-right-radius: 13px;
  292. &>div {
  293. display: flex;
  294. align-items: center;
  295. }
  296. &-left {
  297. justify-content: center;
  298. flex: 1;
  299. -webkit-app-region: drag;
  300. padding-left: 110px;
  301. height: 100%;
  302. &>* {
  303. margin: 0 18px;
  304. }
  305. }
  306. &-right {
  307. display: flex;
  308. align-items: center;
  309. margin-right: 15px;
  310. &>* {
  311. cursor: pointer;
  312. &:not(:first-child) {
  313. margin-left: 15px;
  314. }
  315. }
  316. }
  317. }
  318. .gauge {
  319. position: fixed;
  320. bottom: 0;
  321. left: 50%;
  322. transform: translate(-50%, 0);
  323. width: 30vw;
  324. height: 15vw;
  325. max-width: 460px;
  326. max-height: 230px;
  327. z-index: 9;
  328. }
  329. /* 隐藏滚动条 */
  330. ::-webkit-scrollbar {
  331. width: 0 !important;
  332. display: none;
  333. }
  334. </style>