@zhangqingcq/vgce 0.1.2 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zhangqingcq/vgce",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "Vector graphics configure editor. svg组态编辑器。基于vue3.3+ts+element-plus+vite",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -60,7 +60,7 @@
60
60
  "echarts": "^5.4.1",
61
61
  "element-plus": "^2.3.8",
62
62
  "lodash-es": "^4.17.21",
63
- "mqtt": "^4.3.7",
63
+ "mqtt": "^5.1.3",
64
64
  "pinia": "^2.1.3",
65
65
  "vue": "^3.3.4",
66
66
  "vue-echarts": "^6.5.1",
@@ -233,7 +233,16 @@
233
233
  sub(m.url, m.user, m.pwd, m.topics, (topics: string, message: string) => {
234
234
  console.log(topics)
235
235
  console.log(message.toString())
236
- //暂时先暴露给外部,让用户自己处理消息,后期功能会补上
236
+ //暴露给外部,让用户自己处理消息,message可以用JSON.parse解析成对象(后端推给前端的MQTT消息内容需要是JSON格式)
237
+ //用户拿到消息后可以配合setNodeAttrByID方法更新界面
238
+ //setNodeAttrByID的参数id可以在传给本组件的props.data(用户传进来的,自然知道值是多少)里done_json找到
239
+ /*如何找到指定组件的两种方案:
240
+ 1.用你的项目里前后端约定的svg组件唯一标识符替换掉编辑器生成的id(必须保证唯一),然后调用setNodeAttrByID改变组件属性。
241
+ 2.如果不想改动id(避免因不能保证手动改过的id唯一性导致编辑器功能异常),可以在config里给想要改变的组件的配置文件的props里增加一个字段,
242
+ 如deviceCode(svg-text的配置文件里有被注释的例子),然后在编辑组态时,给对应组件填上对应的deviceCode(这样deviceCode就和组件id实现
243
+ 了映射关系),并保存,后台给前台推MQTT消息时带上指定的deviceCode,前台预览时,在收到MQTT消息后,凭借消息里的deviceCode在done_json
244
+ 找到组件的id(可以用vue的computed计算一份deviceCode和id的映射关系存到一个对象里,这样在需要id时可直接在计算出的对象凭借deviceCode
245
+ 直接取到),即可用setNodeAttrByID改变组件属性*/
237
246
  emit('onMessage', {
238
247
  topics,
239
248
  message
@@ -242,6 +251,13 @@
242
251
  }
243
252
  }
244
253
 
254
+ /**
255
+ * 根据组件id设置相应属性
256
+ * @param id done_json里元素的id
257
+ * @param attr 属性,如svg-text的文字内容是:props.text.val
258
+ * @param val 需要设置的值
259
+ * @example setNodeAttrByID('Q5cZBSDXTP','props.text.val','新的文字内容')
260
+ */
245
261
  const setNodeAttrByID = (id: string, attr: string, val: any) => {
246
262
  return setArrItemByID(id, attr, val, preview_data.done_json)
247
263
  }
@@ -13,6 +13,11 @@ export const svg_text: IConfigItem = {
13
13
  actual_rect: true
14
14
  },
15
15
  props: {
16
+ /*deviceCode:{
17
+ title:'设备编码',
18
+ type:EConfigItemPropsType.Input,
19
+ val:''
20
+ },*/
16
21
  text: {
17
22
  title: '文字内容',
18
23
  type: EConfigItemPropsType.Textarea,
@@ -4,9 +4,15 @@ import type { MqttClient, PacketCallback } from 'mqtt'
4
4
  let client: MqttClient
5
5
 
6
6
  export const sub = (url: string, user: string, pwd: string, topics: string, callback: Function) => {
7
- client = connect(url, {
8
- username: user,
9
- password: pwd
7
+ const _url = url.trim()
8
+ if (!/^wss?:\/\/.*$/.test(_url)) {
9
+ console.error('编辑器MQTT通信只支持ws协议 (url必须以"ws://"开头)')
10
+ return
11
+ }
12
+ client = connect(_url, {
13
+ username: user.trim(),
14
+ password: pwd.trim(),
15
+ reconnectPeriod: 600000 /*如果连不上,10分钟后重试*/
10
16
  })
11
17
 
12
18
  client.on('connect', () => {
@@ -19,9 +25,14 @@ export const sub = (url: string, user: string, pwd: string, topics: string, call
19
25
  }
20
26
 
21
27
  export const pub = (url: string, user: string, pwd: string, topics: string, data: any, callback: PacketCallback) => {
22
- client = connect(url, {
23
- username: user,
24
- password: pwd
28
+ const _url = url.trim()
29
+ if (!/^wss?:\/\/.*$/.test(_url)) {
30
+ console.error('编辑器MQTT通信只支持ws协议 (url必须以"ws://"开头)')
31
+ return
32
+ }
33
+ client = connect(_url, {
34
+ username: user.trim(),
35
+ password: pwd.trim()
25
36
  })
26
37
 
27
38
  client.on('connect', () => {