@meta2d/core 1.0.82 → 1.0.84-alpha.1

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
@@ -257,6 +257,7 @@ export declare class Meta2d {
257
257
  iotTimer: any;
258
258
  iotWebsocketClient: WebSocket;
259
259
  connectIot(): Promise<void>;
260
+ closeIot(): void;
260
261
  connectSqls(): void;
261
262
  connectSSE(net: Network): void;
262
263
  closeSSE(): void;
@@ -264,6 +265,7 @@ export declare class Meta2d {
264
265
  connectNetWebSocket(net: Network): void;
265
266
  getMqttUrl(): Promise<string>;
266
267
  getIotToken(devices: any, type: number): Promise<any>;
268
+ unsubscribeIot(token: string): Promise<any>;
267
269
  doSqlCode(sql: Sql): Promise<void>;
268
270
  randomString(e: number): string;
269
271
  mockValue(data: any): any;
@@ -282,6 +284,7 @@ export declare class Meta2d {
282
284
  url?: string;
283
285
  method?: string;
284
286
  name?: string;
287
+ net?: Network;
285
288
  }): void;
286
289
  setDatas(datas: {
287
290
  dataId?: string;
package/src/core.js CHANGED
@@ -1000,7 +1000,8 @@ export class Meta2d {
1000
1000
  loadLineAnimateDraws() {
1001
1001
  globalStore.lineAnimateDraws = {};
1002
1002
  Object.entries(this.store.data.lineAnimateDraws).forEach(([key, drawFunc]) => {
1003
- globalStore.lineAnimateDraws[key] = eval(drawFunc);
1003
+ // @ts-ignore
1004
+ globalStore.lineAnimateDraws[key] = new Function('ctx', 'pen', 'state', 'index', drawFunc);
1004
1005
  });
1005
1006
  }
1006
1007
  statistics() {
@@ -1370,10 +1371,10 @@ export class Meta2d {
1370
1371
  registerCanvasDraw = registerCanvasDraw;
1371
1372
  registerAnchors = registerAnchors;
1372
1373
  registerLineAnimateDraws = (name, drawFunc) => {
1373
- drawFunc = typeof drawFunc === 'string' ? drawFunc : drawFunc.toString();
1374
1374
  this.store.data.lineAnimateDraws[name] = drawFunc;
1375
1375
  // 同步到store
1376
- globalStore.lineAnimateDraws[name] = eval(drawFunc);
1376
+ // @ts-ignore
1377
+ globalStore.lineAnimateDraws[name] = new Function('ctx', 'pen', 'state', 'index', drawFunc);
1377
1378
  };
1378
1379
  updateLineAnimateDraws(name, option) {
1379
1380
  if (!option)
@@ -1951,9 +1952,6 @@ export class Meta2d {
1951
1952
  ...Object.keys(this.store.bind),
1952
1953
  ...Object.keys(this.store.bindDatas),
1953
1954
  ];
1954
- Object.entries(globalStore.lineAnimateDraws).forEach(([key, drawFunc]) => {
1955
- data.lineAnimateDraws[key] = drawFunc.toString();
1956
- });
1957
1955
  return data;
1958
1956
  }
1959
1957
  copy(pens) {
@@ -2240,6 +2238,8 @@ export class Meta2d {
2240
2238
  headers: net.headers || undefined,
2241
2239
  method: net.method,
2242
2240
  body: net.body,
2241
+ enable: net.enable,
2242
+ index: net.index
2243
2243
  });
2244
2244
  httpIndex += 1;
2245
2245
  }
@@ -2297,7 +2297,7 @@ export class Meta2d {
2297
2297
  iotWebsocketClient;
2298
2298
  async connectIot() {
2299
2299
  const { iot } = this.store.data;
2300
- if (!(iot && iot?.devices?.length)) {
2300
+ if (!(iot && iot?.devices?.length) || (iot && iot.enable === false)) {
2301
2301
  return;
2302
2302
  }
2303
2303
  const url = globalThis.iotUrl || await this.getMqttUrl();
@@ -2340,27 +2340,44 @@ export class Meta2d {
2340
2340
  // };
2341
2341
  // }
2342
2342
  }
2343
+ closeIot() {
2344
+ if (this.iotMqttClient) {
2345
+ const { iot } = this.store.data;
2346
+ if (iot?.token) {
2347
+ this.unsubscribeIot(iot.token);
2348
+ }
2349
+ this.iotMqttClient.end();
2350
+ this.iotMqttClient = undefined;
2351
+ }
2352
+ clearInterval(this.iotTimer);
2353
+ this.iotTimer = undefined;
2354
+ }
2343
2355
  // type SqlType = 'list' | 'get' | 'exec' | 'add' | 'update' | 'delete';
2344
2356
  connectSqls() {
2345
2357
  const { sqls } = this.store.data;
2346
2358
  if (sqls && sqls.length) {
2347
- let sqlIndex = 0;
2348
- sqls.forEach(async (sql) => {
2349
- await this.doSqlCode(sql);
2350
- if (sql.interval) {
2351
- sql.index = sqlIndex;
2352
- this.sqlTimerList[sqlIndex] = setInterval(async () => {
2353
- await this.doSqlCode(sql);
2354
- }, sql.interval);
2355
- sqlIndex += 1;
2359
+ // let sqlIndex = 0;
2360
+ sqls.forEach(async (sql, index) => {
2361
+ if (sql.enable !== false) {
2362
+ await this.doSqlCode(sql);
2363
+ if (sql.interval) {
2364
+ sql.index = index;
2365
+ this.sqlTimerList[index] = setInterval(async () => {
2366
+ await this.doSqlCode(sql);
2367
+ }, sql.interval);
2368
+ // index += 1;
2369
+ }
2356
2370
  }
2357
2371
  });
2358
2372
  }
2359
2373
  }
2360
2374
  connectSSE(net) {
2375
+ if (net.enable === false) {
2376
+ return;
2377
+ }
2361
2378
  this.eventSources[net.index] = new EventSource(net.url, { withCredentials: net.withCredentials });
2362
2379
  this.eventSources[net.index].onmessage = (e) => {
2363
- this.socketCallback(e.data, { type: 'SSE', url: net.url, name: net.name });
2380
+ this.socketCallback(e.data, { type: 'SSE', url: net.url, name: net.name, net });
2364
2381
  };
2365
2382
  this.eventSources[net.index].onerror = (error) => {
2366
2383
  this.store.emitter.emit('error', { type: 'SSE', error });
@@ -2376,6 +2393,9 @@ export class Meta2d {
2376
2393
  });
2377
2394
  }
2378
2395
  connectNetMqtt(net) {
2396
+ if (net.enable === false) {
2397
+ return;
2398
+ }
2379
2399
  if (net.options.clientId && !net.options.customClientId) {
2380
2400
  net.options.clientId = s8();
2381
2401
  }
@@ -2412,7 +2432,8 @@ export class Meta2d {
2412
2432
  topic,
2413
2433
  type: 'mqtt',
2414
2434
  url: net.url,
2415
- name: net.name
2435
+ name: net.name,
2436
+ net
2416
2437
  });
2417
2438
  });
2418
2439
  this.mqttClients[net.index].on('error', (error) => {
@@ -2446,6 +2467,9 @@ export class Meta2d {
2446
2467
  this.websockets[net.index]?.close();
2447
2468
  this.websockets[net.index] = undefined;
2448
2469
  }
2470
+ if (net.enable === false) {
2471
+ return;
2472
+ }
2449
2473
  let url = net.url;
2450
2474
  if (url.indexOf('${') > -1) {
2451
2475
  let keys = url.match(/\$\{([^}]+)\}/g)?.map(m => m.slice(2, -1));
@@ -2457,7 +2481,7 @@ export class Meta2d {
2457
2481
  }
2458
2482
  this.websockets[net.index] = new WebSocket(url, net.protocols || undefined);
2459
2483
  this.websockets[net.index].onmessage = (e) => {
2460
- this.socketCallback(e.data, { type: 'websocket', url: net.url, name: net.name });
2484
+ this.socketCallback(e.data, { type: 'websocket', url: net.url, name: net.name, net });
2461
2485
  };
2462
2486
  this.websockets[net.index].onerror = (error) => {
2463
2487
  this.store.emitter.emit('error', { type: 'websocket', error });
@@ -2509,6 +2533,16 @@ export class Meta2d {
2509
2533
  return JSON.parse(data).token;
2510
2534
  }
2511
2535
  }
2536
+ async unsubscribeIot(token) {
2537
+ const ret = await fetch(`/api/iot/unsubscribe/properties`, {
2538
+ method: 'POST',
2539
+ headers: {
2540
+ Authorization: `Bearer ${getToken()}`,
2541
+ },
2542
+ body: JSON.stringify({ token }),
2543
+ });
2544
+ return ret;
2545
+ }
2512
2546
  async doSqlCode(sql) {
2513
2547
  const method = sql.method || 'get';
2514
2548
  let _sql = sql.sql;
@@ -2520,13 +2554,17 @@ export class Meta2d {
2520
2554
  headers: {
2521
2555
  Authorization: `Bearer ${getCookie('token') || localStorage.getItem('token') || new URLSearchParams(location.search).get('token') || ''}`,
2522
2556
  },
2523
- body: JSON.stringify({ dbid: sql.dbid, sql: _sql, }),
2557
+ body: JSON.stringify({ dbId: sql.dbId || sql.dbid, sql: _sql, }),
2524
2558
  });
2525
2559
  if (res.ok) {
2526
2560
  let data = await res.text();
2527
2561
  if (data) {
2528
2562
  const arr = [];
2529
2563
  data = JSON.parse(data);
2564
+ if (data.error) {
2565
+ this.store.emitter.emit('error', { type: 'sql', error: data.error });
2566
+ return;
2567
+ }
2530
2568
  sql.keys?.forEach((key) => {
2531
2569
  arr.push({ id: sql.bindId + '#' + key, value: getter(data, key.split('#').join('.')) });
2532
2570
  });
@@ -2760,7 +2798,9 @@ export class Meta2d {
2760
2798
  }
2761
2799
  if (!this.store.data.cancelFirstConnect) {
2762
2800
  https.forEach(async (_item) => {
2763
- this.requestHttp(_item);
2801
+ if (_item.enable !== false) {
2802
+ this.requestHttp(_item);
2803
+ }
2764
2804
  });
2765
2805
  }
2766
2806
  // if( enable ){
@@ -2777,18 +2817,25 @@ export class Meta2d {
2777
2817
  // }
2778
2818
  https.forEach((_item, index) => {
2779
2819
  _item.times = 0;
2780
- if (_item.interval !== 0) {
2781
- this.updateTimerList[index] = setInterval(async () => {
2782
- this.requestHttp(_item);
2783
- if (this.store.options.reconnetTimes) {
2784
- // _item.times++;
2785
- if (_item.times >= this.store.options.reconnetTimes) {
2786
- _item.times = 0;
2787
- clearInterval(this.updateTimerList[index]);
2788
- this.updateTimerList[index] = undefined;
2820
+ if (_item.interval !== 0 && _item.enable !== false) {
2821
+ if (_item.once) {
2822
+ setTimeout(async () => {
2823
+ this.requestHttp(_item);
2824
+ }, _item.interval || 1000);
2825
+ }
2826
+ else {
2827
+ this.updateTimerList[index] = setInterval(async () => {
2828
+ this.requestHttp(_item);
2829
+ if (this.store.options.reconnetTimes) {
2830
+ // _item.times++;
2831
+ if (_item.times >= this.store.options.reconnetTimes) {
2832
+ _item.times = 0;
2833
+ clearInterval(this.updateTimerList[index]);
2834
+ this.updateTimerList[index] = undefined;
2835
+ }
2789
2836
  }
2790
- }
2791
- }, _item.interval || 1000);
2837
+ }, _item.interval || 1000);
2838
+ }
2792
2839
  }
2793
2840
  });
2794
2841
  }
@@ -2831,7 +2878,8 @@ export class Meta2d {
2831
2878
  });
2832
2879
  if (res.ok) {
2833
2880
  const data = await res.text();
2834
- this.socketCallback(data, { type: 'http', url: req.url, name: req.name });
2881
+ const net = this.store.data.networks.filter(item => item.protocol === 'http')[req.index];
2882
+ this.socketCallback(data, { type: 'http', url: req.url, name: req.name, net });
2835
2883
  }
2836
2884
  else {
2837
2885
  _req.times++;
@@ -2866,12 +2914,13 @@ export class Meta2d {
2866
2914
  clearInterval(_sqlTimer);
2867
2915
  _sqlTimer = undefined;
2868
2916
  });
2869
- if (this.iotMqttClient) {
2870
- this.iotMqttClient.end();
2871
- this.iotMqttClient = undefined;
2872
- }
2873
- clearInterval(this.iotTimer);
2874
- this.iotTimer = undefined;
2917
+ this.closeIot();
2918
+ // if(this.iotMqttClient){
2919
+ // this.iotMqttClient.end();
2920
+ // this.iotMqttClient = undefined;
2921
+ // }
2922
+ // clearInterval(this.iotTimer);
2923
+ // this.iotTimer = undefined;
2875
2924
  // if(this.iotWebsocketClient){
2876
2925
  // this.iotWebsocketClient.onclose = undefined;
2877
2926
  // this.iotWebsocketClient.close();
@@ -2883,6 +2932,26 @@ export class Meta2d {
2883
2932
  socketCallback(message, context) {
2884
2933
  this.store.emitter.emit('socket', { message, context });
2885
2934
  let _message = message;
2935
+ if (context.net?.socketCbJs) {
2936
+ if (!context.net?.socketFn) {
2937
+ context.net.socketFn = new Function('e', 'context', context.net.socketCbJs);
2938
+ }
2939
+ if (context.net.socketFn) {
2940
+ _message = context.net.socketFn(message, {
2941
+ meta2d: this,
2942
+ type: context.type,
2943
+ topic: context.topic,
2944
+ url: context.url,
2945
+ method: context.method,
2946
+ });
2947
+ if (!_message) {
2948
+ return;
2949
+ }
2950
+ if (_message && _message !== true) {
2951
+ message = _message;
2952
+ }
2953
+ }
2954
+ }
2886
2955
  if (this.socketFn) {
2887
2956
  _message = this.socketFn(message, {
2888
2957
  meta2d: this,
@@ -4114,6 +4183,17 @@ export class Meta2d {
4114
4183
  pen.onResize?.(pen);
4115
4184
  }
4116
4185
  });
4186
+ const videoPens = this.store.data.pens.filter((pen) => pen.name === 'video');
4187
+ videoPens?.forEach((pen) => {
4188
+ const worldRect = pen.calculative.worldRect;
4189
+ if (worldRect.width / this.store.data.scale > rect.width * 0.8) {
4190
+ //作为背景的video
4191
+ pen.calculative.worldRect.x = worldRect.x - wGap / 2;
4192
+ pen.calculative.worldRect.width = worldRect.width + wGap;
4193
+ pen.calculative.worldRect.ex = worldRect.ex + wGap;
4194
+ pen.onResize?.(pen);
4195
+ }
4196
+ });
4117
4197
  }
4118
4198
  //高度拉伸
4119
4199
  if (Math.abs(hGap) > 10) {
@@ -4216,6 +4296,17 @@ export class Meta2d {
4216
4296
  pen.onResize?.(pen);
4217
4297
  }
4218
4298
  });
4299
+ const videoPens = this.store.data.pens.filter((pen) => pen.name === 'video');
4300
+ videoPens?.forEach((pen) => {
4301
+ const worldRect = pen.calculative.worldRect;
4302
+ if (worldRect.height / this.store.data.scale > rect.height * 0.8) {
4303
+ //作为背景的video
4304
+ pen.calculative.worldRect.y = worldRect.y - hGap / 2;
4305
+ pen.calculative.worldRect.height = worldRect.height + hGap;
4306
+ pen.calculative.worldRect.ey = worldRect.ey + hGap;
4307
+ pen.onResize?.(pen);
4308
+ }
4309
+ });
4219
4310
  }
4220
4311
  this.canvas.canvasTemplate.fit = true;
4221
4312
  this.canvas.canvasTemplate.init();