@dobot-plus/template 1.2.10 → 1.2.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- import { ReactNode, useEffect } from 'react'
1
+ import { ReactNode, useEffect, useRef } from 'react'
2
2
  import { MqttWebSocketClient } from '../utils/mqtt'
3
3
  import dptConfig from '../../dpt.json'
4
4
  import MainConfig from '../../configs/Main.json'
@@ -27,6 +27,9 @@ export type DobotPlusAppProps =
27
27
  export function DobotPlusApp(props: DobotPlusAppProps) {
28
28
  const { children, useMqtt } = props
29
29
  const { portData } = useSelector((state: IStoreState) => state.toolReducer)
30
+ const mqttConnected = useRef(false)
31
+ const mqttClientRef = useRef<MqttWebSocketClient | null>(null)
32
+
30
33
  useEffect(() => {
31
34
  const pluginName = MainConfig.name
32
35
  const pluginVersion = MainConfig.version
@@ -35,11 +38,11 @@ export function DobotPlusApp(props: DobotPlusAppProps) {
35
38
  const { topic, onMessage, port } = props
36
39
 
37
40
  const ip = process.env.NODE_ENV === 'production' ? portData.ip : dptConfig.ip
38
-
39
- if (ip && onMessage) {
41
+ if (ip && onMessage && !mqttConnected.current && portData.isSync) {
40
42
  const serverUrl = `ws://${ip}:${port || 8083}/mqtt`
41
43
  const clientId = `${pluginName}_clientId`
42
44
  const mqttClient = new MqttWebSocketClient(serverUrl, clientId)
45
+ mqttClientRef.current = mqttClient
43
46
  mqttClient.connect()
44
47
  mqttClient.subscribe(topic || `${pluginName}Status`, (topic, buf) => {
45
48
  let data = {}
@@ -50,13 +53,14 @@ export function DobotPlusApp(props: DobotPlusAppProps) {
50
53
  }
51
54
  onMessage(data)
52
55
  })
56
+ mqttConnected.current = true
53
57
  }
54
58
  }
55
59
 
56
60
  getPluginPort()
57
61
  .then((res) => {
58
62
  const { data } = res
59
- const pluginKey = data[`${pluginName}_v${pluginVersion}`]
63
+ const pluginKey = `${pluginName}_v${pluginVersion}`
60
64
  if (data) {
61
65
  console.log('pluginPorts', data)
62
66
  const pluginPort = data[pluginKey]
@@ -72,7 +76,19 @@ export function DobotPlusApp(props: DobotPlusAppProps) {
72
76
  .catch((err) => {
73
77
  console.log(err)
74
78
  })
75
- }, [portData.isSync])
79
+
80
+ return () => {
81
+ if (mqttClientRef.current) {
82
+ try {
83
+ mqttClientRef.current.disconnect()
84
+ } catch (e) {
85
+ console.warn('mqtt disconnect error', e)
86
+ }
87
+ mqttClientRef.current = null
88
+ }
89
+ mqttConnected.current = false
90
+ }
91
+ }, [portData.isSync, useMqtt])
76
92
 
77
93
  return <>{children}</>
78
94
  }
@@ -23,12 +23,12 @@ instance.interceptors.response.use(
23
23
  )
24
24
 
25
25
  export function request(config: AxiosRequestConfig, port?: number) {
26
- const { portData } = store.getState().toolReducer
26
+ const { portData, pluginPort } = store.getState().toolReducer
27
27
 
28
28
  const options: AxiosRequestConfig = {
29
29
  ...config,
30
30
  withCredentials: true,
31
- baseURL: `http://${portData.ip}:${port}/dobotPlus/${pluginConfig.name}_v${pluginConfig.version}`
31
+ baseURL: `http://${portData.ip}:${port || pluginPort}/dobotPlus/${pluginConfig.name}_v${pluginConfig.version}`
32
32
  }
33
33
 
34
34
  if (process.env.NODE_ENV !== 'production') {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dobot-plus/template",
3
- "version": "1.2.10",
3
+ "version": "1.2.11",
4
4
  "engines": {
5
5
  "node": "20"
6
6
  },