@dobot-plus/template 1.2.10 → 1.2.12
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.
- package/.dobot/components/DobotPlusApp.tsx +21 -5
- package/.dobot/http/axios.ts +2 -2
- package/.dobot/utils/rem.js +105 -2
- package/package.json +1 -1
|
@@ -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 =
|
|
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
|
-
|
|
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
|
}
|
package/.dobot/http/axios.ts
CHANGED
|
@@ -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/.dobot/utils/rem.js
CHANGED
|
@@ -1,4 +1,97 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* 判断当前浏览器是否是PC端浏览器
|
|
3
|
+
* @returns {boolean} 是PC浏览器返回true,否则返回false
|
|
4
|
+
*/
|
|
5
|
+
export function isPCBrowser() {
|
|
6
|
+
// 检测用户代理字符串
|
|
7
|
+
const userAgent = window.navigator.userAgent
|
|
8
|
+
|
|
9
|
+
// 检查是否有移动设备的特征
|
|
10
|
+
const mobileKeywords = [
|
|
11
|
+
'Android',
|
|
12
|
+
'iPhone',
|
|
13
|
+
'iPad',
|
|
14
|
+
'iPod',
|
|
15
|
+
'BlackBerry',
|
|
16
|
+
'Windows Phone',
|
|
17
|
+
'MeeGo',
|
|
18
|
+
'SymbianOS',
|
|
19
|
+
'IEMobile',
|
|
20
|
+
'Mobile',
|
|
21
|
+
'Opera Mini',
|
|
22
|
+
'Opera Mobi',
|
|
23
|
+
'webOS',
|
|
24
|
+
'Tablet',
|
|
25
|
+
'Pad'
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
const hasMobileKeyword = mobileKeywords.some(
|
|
29
|
+
(keyword) => userAgent.indexOf(keyword) !== -1
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
// 检测是否是常见的PC操作系统
|
|
33
|
+
const isPCOS =
|
|
34
|
+
(userAgent.indexOf('Windows') !== -1 &&
|
|
35
|
+
userAgent.indexOf('Windows Phone') === -1) ||
|
|
36
|
+
userAgent.indexOf('Macintosh') !== -1 ||
|
|
37
|
+
(userAgent.indexOf('Linux') !== -1 && userAgent.indexOf('Android') === -1)
|
|
38
|
+
|
|
39
|
+
// 检测屏幕尺寸(一般PC屏幕较大)
|
|
40
|
+
const hasLargeScreen =
|
|
41
|
+
window.screen.width >= 1024 && window.screen.height >= 768
|
|
42
|
+
|
|
43
|
+
// 检测是否支持鼠标事件但不支持触摸事件
|
|
44
|
+
// 注意:许多现代PC也支持触摸事件,所以这个检测不是非常可靠
|
|
45
|
+
const hasTouchSupport =
|
|
46
|
+
'ontouchstart' in window ||
|
|
47
|
+
navigator.maxTouchPoints > 0 ||
|
|
48
|
+
navigator.msMaxTouchPoints > 0
|
|
49
|
+
|
|
50
|
+
// 精确检测是否是平板
|
|
51
|
+
const isTablet =
|
|
52
|
+
/Tablet|iPad/i.test(userAgent) || (hasTouchSupport && hasLargeScreen)
|
|
53
|
+
|
|
54
|
+
// 综合判断
|
|
55
|
+
// 1. 没有移动设备关键词 且 是PC操作系统
|
|
56
|
+
// 2. 或者有大屏幕但不是平板设备
|
|
57
|
+
return (!hasMobileKeyword && isPCOS) || (hasLargeScreen && !isTablet)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const DOBOT_STUDIO_PRO_CONFIG = {
|
|
61
|
+
DEFAULT_WIDTH: 1920, // 默认屏幕宽度
|
|
62
|
+
MIN_WIDTH: 1440, // 16px 字体大小的最小屏幕宽度
|
|
63
|
+
DOBOT_PLUS_ROOT_FONT_SIZE: 100 // Dobot Plus 根元素字体大小
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function setFontSize() {
|
|
67
|
+
const innerWidth = window.parent
|
|
68
|
+
? window.parent.window.innerWidth
|
|
69
|
+
: window.innerWidth
|
|
70
|
+
if (!innerWidth) return
|
|
71
|
+
let scale = 1
|
|
72
|
+
// 窗口宽度小于最小宽度时,缩放比例为当前窗口宽度与最小宽度的比值
|
|
73
|
+
if (innerWidth < DOBOT_STUDIO_PRO_CONFIG.MIN_WIDTH) {
|
|
74
|
+
scale = innerWidth / DOBOT_STUDIO_PRO_CONFIG.MIN_WIDTH
|
|
75
|
+
}
|
|
76
|
+
if (
|
|
77
|
+
innerWidth >= DOBOT_STUDIO_PRO_CONFIG.MIN_WIDTH &&
|
|
78
|
+
innerWidth <= DOBOT_STUDIO_PRO_CONFIG.DEFAULT_WIDTH
|
|
79
|
+
) {
|
|
80
|
+
// 窗口宽度在最小宽度和默认宽度之间时,按比例缩放,1440-1920 屏幕区间,上位机字体范围 14px ~ 16px
|
|
81
|
+
scale = Math.min(
|
|
82
|
+
1,
|
|
83
|
+
Math.max(innerWidth / DOBOT_STUDIO_PRO_CONFIG.DEFAULT_WIDTH, 14 / 16)
|
|
84
|
+
)
|
|
85
|
+
}
|
|
86
|
+
// 窗口宽度大于默认宽度时,缩放比例为当前窗口宽度与默认宽度的比值
|
|
87
|
+
if (innerWidth > DOBOT_STUDIO_PRO_CONFIG.DEFAULT_WIDTH) {
|
|
88
|
+
scale = innerWidth / DOBOT_STUDIO_PRO_CONFIG.DEFAULT_WIDTH
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
document.documentElement.style.fontSize = `${scale * DOBOT_STUDIO_PRO_CONFIG.DOBOT_PLUS_ROOT_FONT_SIZE}px`
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function reSize(doc, win) {
|
|
2
95
|
var docEl = doc.documentElement,
|
|
3
96
|
resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
|
|
4
97
|
recalc = function () {
|
|
@@ -18,4 +111,14 @@
|
|
|
18
111
|
if (!doc.addEventListener) return
|
|
19
112
|
win.addEventListener(resizeEvt, recalc, false)
|
|
20
113
|
doc.addEventListener('DOMContentLoaded', recalc, false)
|
|
21
|
-
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// 判断是否是PC端浏览器
|
|
117
|
+
if (isPCBrowser()) {
|
|
118
|
+
setFontSize()
|
|
119
|
+
window.onresize = function () {
|
|
120
|
+
setFontSize()
|
|
121
|
+
}
|
|
122
|
+
} else {
|
|
123
|
+
reSize(document, window)
|
|
124
|
+
}
|