Browse Source

test mqtt

caner 1 year ago
parent
commit
4755f97004
6 changed files with 61 additions and 13 deletions
  1. 4 1
      .eslintrc.json
  2. 1 0
      src-tauri/src/lib.rs
  3. 13 0
      src/pages/login/index.vue
  4. 33 0
      src/services/mqtt.service.ts
  5. 1 1
      src/services/service.ts
  6. 9 11
      src/store/index.ts

+ 4 - 1
.eslintrc.json

@@ -39,6 +39,9 @@
     "no-new": 0,
     "no-new": 0,
     "func-names": 0,
     "func-names": 0,
     "no-nested-ternary": 0,
     "no-nested-ternary": 0,
-    "import/no-extraneous-dependencies": 0 
+    "import/no-extraneous-dependencies": 0 ,
+    "max-classes-per-file": 0,
+    "class-methods-use-this": 0,
+    "no-return-await": 0
   }
   }
 }
 }

+ 1 - 0
src-tauri/src/lib.rs

@@ -1,4 +1,5 @@
 use tauri::{generate_context, generate_handler, Builder};
 use tauri::{generate_context, generate_handler, Builder};
+
 // 条件编译属性cfg 的属性attr
 // 条件编译属性cfg 的属性attr
 pub fn run() {
 pub fn run() {
     Builder::default()
     Builder::default()

+ 13 - 0
src/pages/login/index.vue

@@ -47,6 +47,7 @@
             type="info"
             type="info"
             block
             block
             :disabled="disabled"
             :disabled="disabled"
+            @click="login"
           >
           >
             加入
             加入
           </n-button>
           </n-button>
@@ -57,14 +58,26 @@
 </template>
 </template>
 <script setup lang="ts">
 <script setup lang="ts">
 import { computed, ref } from 'vue'
 import { computed, ref } from 'vue'
+import {
+  connect, disconnect, publish, listen, Payload
+} from '@kuyoonjo/tauri-plugin-mqtt'
 import topBar from '@/components/topBar.vue'
 import topBar from '@/components/topBar.vue'
+import useStore from '@/store/index'
 
 
 const err = ref('')
 const err = ref('')
 const name = ref('')
 const name = ref('')
 const room = ref('')
 const room = ref('')
 const url = ref('')
 const url = ref('')
 const disabled = computed(() => !name.value || !room.value || !url.value || !url.value.includes('mqtts://'))
 const disabled = computed(() => !name.value || !room.value || !url.value || !url.value.includes('mqtts://'))
+const store = useStore()
 
 
+async function login() {
+  if (disabled.value) return
+  const newUrl = `${url.value}?username=${room.value}&password=${name.value}`
+  console.log(123, newUrl)
+
+  connect(store.mqtt_client_id, newUrl)
+}
 </script>
 </script>
 <style scoped lang="scss">
 <style scoped lang="scss">
 .login {
 .login {

+ 33 - 0
src/services/mqtt.service.ts

@@ -0,0 +1,33 @@
+import { EventCallback } from '@tauri-apps/api/event'
+import {
+  connect, disconnect, publish, listen, Payload
+} from '@kuyoonjo/tauri-plugin-mqtt'
+import { injectable, Service } from './service'
+
+/**
+ * MQTT服务
+ */
+@injectable
+export default class MqttService extends Service {
+  private mqtt_client_id: string = 'mqtt_contrl_id'
+
+  public mqtt_channel: string = ''
+
+  async connect(url: string, name: string, room: string) {
+    const newUrl = `${url}?username=${room}&password=${name}`
+    return await connect(this.mqtt_client_id, newUrl)
+  }
+
+  async disconnect() {
+    await disconnect(this.mqtt_client_id)
+  }
+
+  async publish(payload: string) {
+    if (!this.mqtt_channel) return
+    await publish(this.mqtt_client_id, this.mqtt_channel, 0, false, payload)
+  }
+
+//   (callback: EventCallback<Payload>) {
+//     listen(callback)
+//   }
+}

+ 1 - 1
src/services/service.ts

@@ -61,4 +61,4 @@ export function mutex(target: any, property: string) {
     }
     }
   })
   })
   return target[property]
   return target[property]
-}
+}

+ 9 - 11
src/store/index.ts

@@ -1,27 +1,25 @@
 import { defineStore } from 'pinia'
 import { defineStore } from 'pinia'
 
 
 export interface UserInfo {
 export interface UserInfo {
-  id: number,
+  url: string,
   name: string,
   name: string,
-  opmId: number,
-  satoken: string
+  room: string
 }
 }
 
 
 // id必填,且需要唯一
 // id必填,且需要唯一
 const useStore = defineStore('index', {
 const useStore = defineStore('index', {
   state: () => ({
   state: () => ({
-    userInfro: {} as UserInfo,
-    token: '',
-    isCheckPermission: false,
-    loading: false
+    userInfro: {
+      url: '',
+      name: '',
+      room: ''
+    },
+    mqtt_client_id: 'mqtt_contrl_id',
+    mqtt_channel: ''
   }),
   }),
   actions: {
   actions: {
     setUserInfo(data: UserInfo) {
     setUserInfo(data: UserInfo) {
       this.userInfro = data
       this.userInfro = data
-      this.token = data.satoken || ''
-    },
-    setCheckPermission(data: boolean) {
-      this.isCheckPermission = data
     }
     }
   },
   },
   persist: {
   persist: {