@meta2d/core 1.0.88 → 1.0.90

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/src/core.d.ts CHANGED
@@ -30,6 +30,7 @@ export declare class Meta2d {
30
30
  topic?: string;
31
31
  url?: string;
32
32
  method?: string;
33
+ net?: any;
33
34
  }) => boolean | string;
34
35
  events: Record<number, (pen: Pen, e: Event, params?: any) => void>;
35
36
  map: ViewMap;
package/src/core.js CHANGED
@@ -18,6 +18,7 @@ import { HotkeyType } from './data';
18
18
  import { Message, messageList } from './message';
19
19
  import { closeJetLinks, connectJetLinks, getSendData, sendJetLinksData } from './utils/jetLinks';
20
20
  import { le5leTheme } from './theme';
21
+ const echartReg = /^echarts/;
21
22
  export class Meta2d {
22
23
  store;
23
24
  canvas;
@@ -555,7 +556,7 @@ export class Meta2d {
555
556
  if (item.prop) {
556
557
  if (item.id && item.id !== '固定值') {
557
558
  const pen = this.findOne(item.id);
558
- value[item.prop] = pen[item.key];
559
+ value[item.prop] = getter(pen, item.key); // pen[item.key];
559
560
  }
560
561
  else {
561
562
  if (typeof item.value === 'string' && item.value.includes('${')) {
@@ -666,6 +667,7 @@ export class Meta2d {
666
667
  else {
667
668
  this.fitView(true, 10);
668
669
  }
670
+ document.title = data.name + "-" + window.name;
669
671
  }
670
672
  }
671
673
  doSendDataEvent(value, topics) {
@@ -1281,8 +1283,9 @@ export class Meta2d {
1281
1283
  const initJs = this.store.data.initJs;
1282
1284
  if (initJs && initJs.trim()) {
1283
1285
  try {
1284
- const fn = new Function('context', initJs);
1286
+ let fn = new Function('context', initJs);
1285
1287
  fn({ meta2d: this });
1288
+ fn = null;
1286
1289
  }
1287
1290
  catch (e) {
1288
1291
  console.warn('initJs error', e);
@@ -1617,10 +1620,12 @@ export class Meta2d {
1617
1620
  this.store.animateMap.delete(pen);
1618
1621
  });
1619
1622
  this.initImageCanvas(pens);
1620
- setTimeout(() => {
1621
- this.canvas?.calcActiveRect();
1622
- this.render();
1623
- }, 20);
1623
+ if (this.store.data.locked === LockState.None) {
1624
+ setTimeout(() => {
1625
+ this.canvas?.calcActiveRect();
1626
+ this.render();
1627
+ }, 20);
1628
+ }
1624
1629
  }
1625
1630
  startVideo(idOrTagOrPens) {
1626
1631
  let pens;
@@ -2506,6 +2511,20 @@ export class Meta2d {
2506
2511
  });
2507
2512
  }
2508
2513
  }
2514
+ // 稳定连接配置
2515
+ if (!options.hasOwnProperty("keepalive")) {
2516
+ Object.assign(options, { keepallive: 30 });
2517
+ }
2518
+ // clean为false 时,clientId 是必填项
2519
+ if (options.clientId && !options.hasOwnProperty("clean")) {
2520
+ Object.assign(options, { clean: false });
2521
+ }
2522
+ if (!options.hasOwnProperty("reconnectPeriod")) {
2523
+ Object.assign(options, { reconnectPeriod: 0 });
2524
+ }
2525
+ if (!options.hasOwnProperty("connectTimeout")) {
2526
+ Object.assign(options, { connectTimeout: 10 * 1000 });
2527
+ }
2509
2528
  this.mqttClients[net.index] = mqtt.connect(url, options);
2510
2529
  this.mqttClients[net.index].on('message', (topic, message) => {
2511
2530
  this.socketCallback(message.toString(), {
@@ -2519,6 +2538,7 @@ export class Meta2d {
2519
2538
  this.mqttClients[net.index].on('error', (error) => {
2520
2539
  this.store.emitter.emit('error', { type: 'mqtt', error });
2521
2540
  });
2541
+ let reconnectDelay = 1000;
2522
2542
  this.mqttClients[net.index].on('close', () => {
2523
2543
  if (this.store.options.reconnetTimes) {
2524
2544
  net.times++;
@@ -2526,20 +2546,39 @@ export class Meta2d {
2526
2546
  net.times = 0;
2527
2547
  this.mqttClients && this.mqttClients[net.index]?.end();
2528
2548
  }
2549
+ setTimeout(() => {
2550
+ if (net.times < this.store.options.reconnetTimes) {
2551
+ this.mqttClients[net.index].reconnect(options);
2552
+ reconnectDelay = Math.min(reconnectDelay * 2, 10 * 1000);
2553
+ }
2554
+ }, reconnectDelay);
2529
2555
  }
2530
2556
  });
2531
- if (net.topics) {
2532
- let topics = net.topics;
2533
- if (topics.indexOf('${') > -1) {
2534
- let keys = topics.match(/\$\{([^}]+)\}/g)?.map(m => m.slice(2, -1));
2535
- if (keys) {
2536
- keys.forEach((key) => {
2537
- topics = topics.replace(`\${${key}}`, this.getDynamicParam(key));
2557
+ this.mqttClients[net.index].on('connect', (connack) => {
2558
+ reconnectDelay = 1000;
2559
+ if (!connack.sessionPresent) {
2560
+ // 创建了新会话或没有找到旧会话,需要重新订阅主题
2561
+ if (net.topics) {
2562
+ let topics = net.topics;
2563
+ if (topics.indexOf('${') > -1) {
2564
+ let keys = topics.match(/\$\{([^}]+)\}/g)?.map(m => m.slice(2, -1));
2565
+ if (keys) {
2566
+ keys.forEach((key) => {
2567
+ topics = topics.replace(`\${${key}}`, this.getDynamicParam(key));
2568
+ });
2569
+ }
2570
+ }
2571
+ // QoS=1 是 MQTT 中最常用的级别,它能在保证大部分消息可靠传递
2572
+ this.mqttClients[net.index].subscribe(topics.split(','), { qos: 1 }, (err) => {
2573
+ if (err)
2574
+ console.error("订阅失败:", err);
2538
2575
  });
2539
2576
  }
2540
2577
  }
2541
- this.mqttClients[net.index].subscribe(topics.split(','));
2542
- }
2578
+ else {
2579
+ //已恢复之前的会话,可以接收离线消息,不需要重新订阅,之前的订阅已恢复
2580
+ }
2581
+ });
2543
2582
  }
2544
2583
  connectNetWebSocket(net) {
2545
2584
  if (this.websockets[net.index]) {
@@ -3022,7 +3061,7 @@ export class Meta2d {
3022
3061
  if (res.ok) {
3023
3062
  const data = await res.text();
3024
3063
  const net = this.store.data.networks.filter(item => item.protocol === 'http')[req.index];
3025
- this.socketCallback(data, { type: 'http', url: req.url, name: req.name, net });
3064
+ this.socketCallback(data, { type: 'http', method: req.method, url: req.url, name: req.name, net });
3026
3065
  }
3027
3066
  else {
3028
3067
  _req.times++;
@@ -3102,6 +3141,7 @@ export class Meta2d {
3102
3141
  topic: context.topic,
3103
3142
  url: context.url,
3104
3143
  method: context.method,
3144
+ net: context.net
3105
3145
  });
3106
3146
  if (!_message) {
3107
3147
  return;
@@ -3293,6 +3333,11 @@ export class Meta2d {
3293
3333
  initPens = deepClone(pens);
3294
3334
  }
3295
3335
  pens.forEach((pen) => {
3336
+ if (pen.name === 'echarts' && !pen.onBeforeValue) {
3337
+ const hasEchartsStartKey = Object.keys(data).some(key => echartReg.test(key));
3338
+ if (hasEchartsStartKey)
3339
+ return;
3340
+ }
3296
3341
  const afterData = pen.onBeforeValue
3297
3342
  ? pen.onBeforeValue(pen, data)
3298
3343
  : data;
@@ -4592,12 +4637,23 @@ export class Meta2d {
4592
4637
  const { offsetWidth: width, offsetHeight: height } = canvas;
4593
4638
  this.resize(width, height);
4594
4639
  const padding = formatPadding(viewPadding);
4595
- const rect = this.getRect();
4640
+ let rect = null;
4641
+ const w = this.store.data.width || this.store.options.width;
4642
+ const h = this.store.data.height || this.store.options.height;
4643
+ if (w && h) {
4644
+ rect = {
4645
+ width: w * this.store.data.scale,
4646
+ height: h * this.store.data.scale,
4647
+ };
4648
+ }
4649
+ else {
4650
+ rect = this.getRect();
4651
+ }
4596
4652
  const ratio = (width - padding[1] - padding[3]) / rect.width;
4597
4653
  this.scale(ratio * this.store.data.scale);
4598
4654
  this.topView(padding[0]);
4599
4655
  if (pageMode) {
4600
- this.canvas.scroll.changeMode();
4656
+ this.canvas.scroll.changeMode(padding[0]);
4601
4657
  }
4602
4658
  }
4603
4659
  screenView(viewPadding = 10, WorH = true) {
@@ -4622,7 +4678,20 @@ export class Meta2d {
4622
4678
  return;
4623
4679
  const rect = this.getRect();
4624
4680
  const viewCenter = this.getViewCenter();
4625
- const pensRect = this.getPenRect(rect);
4681
+ const w = this.store.data.width || this.store.options.width;
4682
+ const h = this.store.data.height || this.store.options.height;
4683
+ let pensRect = null;
4684
+ if (w && h) {
4685
+ pensRect = {
4686
+ x: 0,
4687
+ y: 0,
4688
+ width: w,
4689
+ height: h,
4690
+ };
4691
+ }
4692
+ else {
4693
+ pensRect = this.getPenRect(rect);
4694
+ }
4626
4695
  calcCenter(pensRect);
4627
4696
  const { center } = pensRect;
4628
4697
  const { scale, origin, x: dataX, y: dataY } = this.store.data;