@meta2d/core 1.0.59 → 1.0.61

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.js CHANGED
@@ -203,7 +203,7 @@ export class Meta2d {
203
203
  if (e.value) {
204
204
  _pen = this.findOne(e.value);
205
205
  }
206
- if (this.store.animates.has(_pen) && !_pen.calculative.pause) {
206
+ if (this.store.animates.has(_pen) && !_pen.calculative.pause && _pen.animateName === e.params) {
207
207
  return;
208
208
  }
209
209
  if (e.targetType && e.params) {
@@ -357,6 +357,33 @@ export class Meta2d {
357
357
  }
358
358
  };
359
359
  this.events[EventAction.SendData] = (pen, e) => {
360
+ if (e.list?.length) {
361
+ // if (e.targetType === 'id') {
362
+ const value = {};
363
+ e.list.forEach((item) => {
364
+ const _pen = item.params ? this.findOne(item.params) : pen;
365
+ for (let key in item.value) {
366
+ if (item.value[key] === undefined || item.value[key] === '') {
367
+ value[key] = _pen[key];
368
+ }
369
+ else if (typeof item.value[key] === 'string' && item.value[key]?.indexOf('${') > -1) {
370
+ let keys = item.value[key].match(/(?<=\$\{).*?(?=\})/g);
371
+ if (keys?.length) {
372
+ value[key] = _pen[keys[0]];
373
+ }
374
+ }
375
+ else {
376
+ value[key] = item.value[key];
377
+ }
378
+ }
379
+ });
380
+ if (pen.deviceId) {
381
+ value.deviceId = pen.deviceId;
382
+ }
383
+ this.sendDataToNetWork(value, pen, e);
384
+ return;
385
+ // }
386
+ }
360
387
  const value = deepClone(e.value);
361
388
  if (value && typeof value === 'object') {
362
389
  if (e.targetType === 'id') {
@@ -392,9 +419,30 @@ export class Meta2d {
392
419
  return;
393
420
  }
394
421
  let params = queryURLParams(_pen.iframe.split('?')[1]);
422
+ const value = {};
423
+ if (e.list?.length) {
424
+ e.list.forEach((item) => {
425
+ const _pen = item.params ? this.findOne(item.params) : pen;
426
+ for (let key in item.value) {
427
+ if (item.value[key] === undefined || item.value[key] === '') {
428
+ value[key] = _pen[key];
429
+ }
430
+ else if (typeof item.value[key] === 'string' && item.value[key]?.indexOf('${') > -1) {
431
+ let keys = item.value[key].match(/(?<=\$\{).*?(?=\})/g);
432
+ if (keys?.length) {
433
+ value[key] = _pen[keys[0]];
434
+ }
435
+ }
436
+ else {
437
+ value[key] = item.value[key];
438
+ }
439
+ }
440
+ });
441
+ }
395
442
  _pen.calculative.singleton.div.children[0].contentWindow.postMessage(JSON.stringify({
396
443
  name: e.value,
397
444
  id: params.id,
445
+ value
398
446
  }), '*');
399
447
  return;
400
448
  };
@@ -403,7 +451,27 @@ export class Meta2d {
403
451
  console.warn('[meta2d] Emit value must be a string');
404
452
  return;
405
453
  }
406
- window.parent.postMessage(JSON.stringify(e.value), '*');
454
+ const value = {};
455
+ if (e.list?.length) {
456
+ e.list.forEach((item) => {
457
+ const _pen = item.params ? this.findOne(item.params) : pen;
458
+ for (let key in item.value) {
459
+ if (item.value[key] === undefined || item.value[key] === '') {
460
+ value[key] = _pen[key];
461
+ }
462
+ else if (typeof item.value[key] === 'string' && item.value[key]?.indexOf('${') > -1) {
463
+ let keys = item.value[key].match(/(?<=\$\{).*?(?=\})/g);
464
+ if (keys?.length) {
465
+ value[key] = _pen[keys[0]];
466
+ }
467
+ }
468
+ else {
469
+ value[key] = item.value[key];
470
+ }
471
+ }
472
+ });
473
+ }
474
+ window.parent.postMessage(JSON.stringify({ name: e.value, value }), '*');
407
475
  return;
408
476
  };
409
477
  }
@@ -1074,6 +1142,7 @@ export class Meta2d {
1074
1142
  }
1075
1143
  if (index !== -1 && index !== undefined) {
1076
1144
  const animate = deepClone(pen.animations[index]);
1145
+ animate.animateName = animate.name;
1077
1146
  delete animate.name;
1078
1147
  animate.currentAnimation = index;
1079
1148
  if (!pen.type && animate.frames) {
@@ -1730,6 +1799,7 @@ export class Meta2d {
1730
1799
  }
1731
1800
  updateTimer;
1732
1801
  updateTimerList = [];
1802
+ sqlTimerList = [];
1733
1803
  connectNetwork() {
1734
1804
  this.closeNetwork();
1735
1805
  const { networks } = this.store.data;
@@ -1738,76 +1808,119 @@ export class Meta2d {
1738
1808
  let mqttIndex = 0;
1739
1809
  this.mqttClients = [];
1740
1810
  let websocketIndex = 0;
1811
+ let sqlIndex = 0;
1741
1812
  this.websockets = [];
1742
- networks.forEach((net) => {
1743
- if (net.type === 'subscribe') {
1744
- if (net.protocol === 'mqtt') {
1745
- net.index = mqttIndex;
1746
- if (net.options.clientId && !net.options.customClientId) {
1747
- net.options.clientId = s8();
1813
+ networks.forEach(async (net) => {
1814
+ // if (net.type === 'subscribe') {
1815
+ if (net.protocol === 'mqtt') {
1816
+ net.index = mqttIndex;
1817
+ if (net.options.clientId && !net.options.customClientId) {
1818
+ net.options.clientId = s8();
1819
+ }
1820
+ net.times = 0;
1821
+ this.mqttClients[mqttIndex] = mqtt.connect(net.url, net.options);
1822
+ this.mqttClients[mqttIndex].on('message', (topic, message) => {
1823
+ this.socketCallback(message.toString(), {
1824
+ topic,
1825
+ type: 'mqtt',
1826
+ url: net.url,
1827
+ });
1828
+ });
1829
+ this.mqttClients[mqttIndex].on('error', (error) => {
1830
+ this.store.emitter.emit('error', { type: 'mqtt', error });
1831
+ });
1832
+ this.mqttClients[mqttIndex].on('close', () => {
1833
+ if (this.store.options.reconnetTimes) {
1834
+ net.times++;
1835
+ if (net.times >= this.store.options.reconnetTimes) {
1836
+ net.times = 0;
1837
+ this.mqttClients && this.mqttClients[net.index]?.end();
1838
+ }
1748
1839
  }
1749
- net.times = 0;
1750
- this.mqttClients[mqttIndex] = mqtt.connect(net.url, net.options);
1840
+ });
1841
+ if (net.topics) {
1842
+ this.mqttClients[mqttIndex].subscribe(net.topics.split(','));
1843
+ }
1844
+ mqttIndex += 1;
1845
+ }
1846
+ else if (net.protocol === 'websocket') {
1847
+ net.index = websocketIndex;
1848
+ this.connectNetWebSocket(net);
1849
+ // this.websockets[websocketIndex] = new WebSocket(
1850
+ // net.url,
1851
+ // net.protocols || undefined
1852
+ // );
1853
+ // this.websockets[websocketIndex].onmessage = (e) => {
1854
+ // this.socketCallback(e.data, { type: 'websocket', url: net.url });
1855
+ // };
1856
+ // this.websockets[websocketIndex].onerror = (error) => {
1857
+ // this.store.emitter.emit('error', { type: 'websocket', error });
1858
+ // };
1859
+ // this.websockets[websocketIndex].onclose = () => {
1860
+ // if (this.store.options.reconnetTimes) {
1861
+ // net.times++;
1862
+ // if (net.times >= this.store.options.reconnetTimes) {
1863
+ // net.times = 0;
1864
+ // this.websockets[net.index]?.close();
1865
+ // return;
1866
+ // }
1867
+ // }
1868
+ // console.info('Canvas websocket closed and reconneting...');
1869
+ // };
1870
+ websocketIndex += 1;
1871
+ }
1872
+ else if (net.protocol === 'http') {
1873
+ https.push({
1874
+ url: net.url,
1875
+ interval: net.interval,
1876
+ headers: net.headers || undefined,
1877
+ method: net.method,
1878
+ body: net.body,
1879
+ });
1880
+ }
1881
+ else if (net.protocol === 'iot') {
1882
+ const token = await this.getIotToken(net.devices);
1883
+ //物联网设备
1884
+ if (net.method === 'mqtt') {
1885
+ net.index = mqttIndex;
1886
+ this.mqttClients[mqttIndex] = mqtt.connect(net.url);
1751
1887
  this.mqttClients[mqttIndex].on('message', (topic, message) => {
1752
1888
  this.socketCallback(message.toString(), {
1753
- topic,
1754
- type: 'mqtt',
1889
+ topic: `le5le-iot/properties/${token}`,
1890
+ type: 'iot',
1755
1891
  url: net.url,
1892
+ method: 'mqtt'
1756
1893
  });
1757
1894
  });
1758
1895
  this.mqttClients[mqttIndex].on('error', (error) => {
1759
1896
  this.store.emitter.emit('error', { type: 'mqtt', error });
1760
1897
  });
1761
- this.mqttClients[mqttIndex].on('close', () => {
1762
- if (this.store.options.reconnetTimes) {
1763
- net.times++;
1764
- if (net.times >= this.store.options.reconnetTimes) {
1765
- net.times = 0;
1766
- this.mqttClients && this.mqttClients[net.index]?.end();
1767
- }
1768
- }
1769
- });
1770
- if (net.topics) {
1771
- this.mqttClients[mqttIndex].subscribe(net.topics.split(','));
1772
- }
1898
+ this.mqttClients[mqttIndex].subscribe(`le5le-iot/properties/${token}`);
1773
1899
  mqttIndex += 1;
1774
1900
  }
1775
- else if (net.protocol === 'websocket') {
1901
+ else if (net.method === 'websocket') {
1776
1902
  net.index = websocketIndex;
1777
- this.connectNetWebSocket(net);
1778
- // this.websockets[websocketIndex] = new WebSocket(
1779
- // net.url,
1780
- // net.protocols || undefined
1781
- // );
1782
- // this.websockets[websocketIndex].onmessage = (e) => {
1783
- // this.socketCallback(e.data, { type: 'websocket', url: net.url });
1784
- // };
1785
- // this.websockets[websocketIndex].onerror = (error) => {
1786
- // this.store.emitter.emit('error', { type: 'websocket', error });
1787
- // };
1788
- // this.websockets[websocketIndex].onclose = () => {
1789
- // if (this.store.options.reconnetTimes) {
1790
- // net.times++;
1791
- // if (net.times >= this.store.options.reconnetTimes) {
1792
- // net.times = 0;
1793
- // this.websockets[net.index]?.close();
1794
- // return;
1795
- // }
1796
- // }
1797
- // console.info('Canvas websocket closed and reconneting...');
1798
- // };
1903
+ this.websockets[websocketIndex] = new WebSocket(`${location.protocol === 'https:' ? 'wss' : 'ws'}://${location.host}/api/ws/iot/properties`, token);
1904
+ this.websockets[websocketIndex].onmessage = (e) => {
1905
+ this.socketCallback(e.data, { type: 'iot', method: 'websocket' });
1906
+ };
1907
+ this.websockets[websocketIndex].onerror = (error) => {
1908
+ this.store.emitter.emit('error', { type: 'websocket', error });
1909
+ };
1799
1910
  websocketIndex += 1;
1800
1911
  }
1801
- else if (net.protocol === 'http') {
1802
- https.push({
1803
- url: net.url,
1804
- interval: net.interval,
1805
- headers: net.headers || undefined,
1806
- method: net.method,
1807
- body: net.body,
1808
- });
1912
+ }
1913
+ else if (net.protocol === 'sql') {
1914
+ await this.doSqlCode('list', net.dbId, net.sql);
1915
+ if (net.interval) {
1916
+ net.index = sqlIndex;
1917
+ this.sqlTimerList[sqlIndex] = setInterval(async () => {
1918
+ await this.doSqlCode('list', net.dbId, net.sql);
1919
+ }, net.interval);
1920
+ sqlIndex += 1;
1809
1921
  }
1810
1922
  }
1923
+ // }
1811
1924
  });
1812
1925
  }
1813
1926
  this.onNetworkConnect(https);
@@ -1842,6 +1955,28 @@ export class Meta2d {
1842
1955
  }, 2000);
1843
1956
  };
1844
1957
  }
1958
+ async getIotToken(devices) {
1959
+ const res = await fetch('/api/iot/subscribe/properties', {
1960
+ method: 'POST',
1961
+ body: JSON.stringify({ devices: devices }),
1962
+ });
1963
+ if (res.ok) {
1964
+ const data = await res.text();
1965
+ return JSON.parse(data).token;
1966
+ }
1967
+ }
1968
+ async doSqlCode(type, dbid, sql) {
1969
+ const res = await fetch(`/api/iot/data/sql/${type}`, {
1970
+ method: 'POST',
1971
+ body: JSON.stringify({ dbid, sql, }),
1972
+ });
1973
+ if (res.ok) {
1974
+ const data = await res.text();
1975
+ if (data) {
1976
+ this.socketCallback(data, { type: 'sql', url: `/api/iot/data/sql/${type}` });
1977
+ }
1978
+ }
1979
+ }
1845
1980
  randomString(e) {
1846
1981
  e = e || 32;
1847
1982
  let t = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678', a = t.length, n = '';
@@ -2159,6 +2294,11 @@ export class Meta2d {
2159
2294
  clearInterval(_updateTimer);
2160
2295
  _updateTimer = undefined;
2161
2296
  });
2297
+ this.sqlTimerList &&
2298
+ this.sqlTimerList.forEach((_sqlTimer) => {
2299
+ clearInterval(_sqlTimer);
2300
+ _sqlTimer = undefined;
2301
+ });
2162
2302
  }
2163
2303
  socketCallback(message, context) {
2164
2304
  this.store.emitter.emit('socket', { message, context });
@@ -2169,6 +2309,7 @@ export class Meta2d {
2169
2309
  type: context.type,
2170
2310
  topic: context.topic,
2171
2311
  url: context.url,
2312
+ method: context.method
2172
2313
  });
2173
2314
  if (!_message) {
2174
2315
  return;
@@ -2240,7 +2381,7 @@ export class Meta2d {
2240
2381
  penValues.set(pen, penValue);
2241
2382
  }
2242
2383
  });
2243
- this.store.bind[v.id]?.forEach((p) => {
2384
+ this.store.bind[v.id || v.dataId]?.forEach((p) => {
2244
2385
  const pen = this.store.pens[p.id];
2245
2386
  if (!pen) {
2246
2387
  return;
@@ -2610,8 +2751,19 @@ export class Meta2d {
2610
2751
  pen.events?.forEach((event, index) => {
2611
2752
  if (indexArr.includes(index)) {
2612
2753
  event.actions.forEach((action) => {
2613
- if (this.events[action.action]) {
2614
- this.events[action.action](pen, action);
2754
+ if (action.timeout) {
2755
+ let timer = setTimeout(() => {
2756
+ if (this.events[action.action]) {
2757
+ this.events[action.action](pen, action);
2758
+ clearTimeout(timer);
2759
+ timer = null;
2760
+ }
2761
+ }, action.timeout);
2762
+ }
2763
+ else {
2764
+ if (this.events[action.action]) {
2765
+ this.events[action.action](pen, action);
2766
+ }
2615
2767
  }
2616
2768
  });
2617
2769
  }
@@ -2649,7 +2801,18 @@ export class Meta2d {
2649
2801
  realTime.triggers?.forEach((trigger, index) => {
2650
2802
  if (indexArr.includes(index)) {
2651
2803
  trigger.actions?.forEach((event) => {
2652
- this.events[event.action](pen, event);
2804
+ if (event.timeout) {
2805
+ let timer = setTimeout(() => {
2806
+ if (this.events[event.action]) {
2807
+ this.events[event.action](pen, event);
2808
+ clearTimeout(timer);
2809
+ timer = null;
2810
+ }
2811
+ }, event.timeout);
2812
+ }
2813
+ else {
2814
+ this.events[event.action](pen, event);
2815
+ }
2653
2816
  });
2654
2817
  }
2655
2818
  });
@@ -2681,7 +2844,18 @@ export class Meta2d {
2681
2844
  this.store.globalTriggers[pen.id]?.forEach((trigger, index) => {
2682
2845
  if (indexArr.includes(index)) {
2683
2846
  trigger.actions?.forEach((event) => {
2684
- this.events[event.action](pen, event);
2847
+ if (event.timeout) {
2848
+ let timer = setTimeout(() => {
2849
+ if (this.events[event.action]) {
2850
+ this.events[event.action](pen, event);
2851
+ clearTimeout(timer);
2852
+ timer = null;
2853
+ }
2854
+ }, event.timeout);
2855
+ }
2856
+ else {
2857
+ this.events[event.action](pen, event);
2858
+ }
2685
2859
  });
2686
2860
  }
2687
2861
  });
@@ -2709,7 +2883,18 @@ export class Meta2d {
2709
2883
  }
2710
2884
  if (flag) {
2711
2885
  state.actions?.forEach((event) => {
2712
- this.events[event.action](pen, event);
2886
+ if (event.timeout) {
2887
+ let timer = setTimeout(() => {
2888
+ if (this.events[event.action]) {
2889
+ this.events[event.action](pen, event);
2890
+ clearTimeout(timer);
2891
+ timer = null;
2892
+ }
2893
+ }, event.timeout);
2894
+ }
2895
+ else {
2896
+ this.events[event.action](pen, event);
2897
+ }
2713
2898
  });
2714
2899
  break;
2715
2900
  }
@@ -2721,7 +2906,7 @@ export class Meta2d {
2721
2906
  // 事件冒泡,子执行完,父执行
2722
2907
  this.doEvent(this.store.pens[pen.parentId], eventName);
2723
2908
  };
2724
- doMessageEvent(eventName) {
2909
+ doMessageEvent(eventName, data) {
2725
2910
  if (this.store.messageEvents[eventName]) {
2726
2911
  this.store.messageEvents[eventName].forEach((item) => {
2727
2912
  let flag = false;
@@ -3604,6 +3789,7 @@ export class Meta2d {
3604
3789
  attrs[attr] =
3605
3790
  firstPen[attr] !== undefined ? firstPen[attr] :
3606
3791
  (this.store.options.defaultFormat[attr] ||
3792
+ this.store.data[attr] ||
3607
3793
  this.store.options[attr]);
3608
3794
  });
3609
3795
  }
@@ -3613,6 +3799,7 @@ export class Meta2d {
3613
3799
  formatAttrs.forEach((attr) => {
3614
3800
  attrs[attr] =
3615
3801
  this.store.options.defaultFormat[attr] ||
3802
+ this.store.data[attr] ||
3616
3803
  this.store.options[attr] ||
3617
3804
  undefined;
3618
3805
  });