@ray-js/ray-ipc-utils 1.1.0-beta-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.
@@ -0,0 +1,709 @@
1
+ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
2
+ import "core-js/modules/esnext.iterator.map.js";
3
+ import _find from 'lodash/find';
4
+ // IPC Kit相关
5
+ /**
6
+ *
7
+ * @param deviceId 设备id
8
+ * @returns 获取设备信息
9
+ */
10
+ export const getDevInfo = deviceId => {
11
+ return new Promise(resolve => {
12
+ try {
13
+ ty.device.getDeviceInfo({
14
+ deviceId,
15
+ success: res => {
16
+ resolve({
17
+ code: 0,
18
+ data: res
19
+ });
20
+ },
21
+ fail: err => {
22
+ resolve({
23
+ code: -1,
24
+ msg: err
25
+ });
26
+ }
27
+ });
28
+ } catch (err) {
29
+ resolve({
30
+ code: -1,
31
+ msg: String(err)
32
+ });
33
+ }
34
+ });
35
+ };
36
+
37
+ /**
38
+ * 获取本地缓存
39
+ * @param key 保存的key属性
40
+ *
41
+ */
42
+
43
+ export const getNativeStorage = key => {
44
+ return new Promise(resolve => {
45
+ try {
46
+ ty.getStorage({
47
+ key,
48
+ success: res => {
49
+ resolve({
50
+ code: 0,
51
+ data: res
52
+ });
53
+ },
54
+ fail: err => {
55
+ resolve({
56
+ code: -1,
57
+ msg: err
58
+ });
59
+ }
60
+ });
61
+ } catch (err) {
62
+ resolve({
63
+ code: -1,
64
+ msg: String(err)
65
+ });
66
+ }
67
+ });
68
+ };
69
+
70
+ /**
71
+ * 存储数据到本地缓存
72
+ * @param key 保存的key属性
73
+ *
74
+ */
75
+
76
+ export const setNativeStorage = (key, data) => {
77
+ return new Promise(resolve => {
78
+ try {
79
+ ty.setStorage({
80
+ key,
81
+ data,
82
+ success: res => {
83
+ resolve({
84
+ code: 0,
85
+ data: res || true
86
+ });
87
+ },
88
+ fail: err => {
89
+ resolve({
90
+ code: -1,
91
+ msg: err
92
+ });
93
+ }
94
+ });
95
+ } catch (err) {
96
+ resolve({
97
+ code: -1,
98
+ msg: String(err)
99
+ });
100
+ }
101
+ });
102
+ };
103
+
104
+ /**
105
+ * 清除本地缓存
106
+ * @param key 保存的key属性
107
+ *
108
+ */
109
+
110
+ export const removeNativeStorage = key => {
111
+ return new Promise(resolve => {
112
+ try {
113
+ ty.removeStorage({
114
+ key,
115
+ success: res => {
116
+ resolve({
117
+ code: 0,
118
+ data: res || true
119
+ });
120
+ },
121
+ fail: err => {
122
+ resolve({
123
+ code: -1,
124
+ msg: err
125
+ });
126
+ }
127
+ });
128
+ } catch (err) {
129
+ resolve({
130
+ code: -1,
131
+ msg: String(err)
132
+ });
133
+ }
134
+ });
135
+ };
136
+
137
+ /**
138
+ * DP点下发, 包含针对低功耗设备情况下的DP点下发
139
+ * @param key 保存的key属性
140
+ *
141
+ */
142
+
143
+ export const publishDps = (deviceId, dps) => {
144
+ const ipcMethod = ty.device.publishDps;
145
+ // typeof ty.ipc?.publishDps === 'function' ? ty.ipc.publishDps : ty.device.publishDps;
146
+ return new Promise(resolve => {
147
+ try {
148
+ var _ty$ipc;
149
+ ipcMethod(_objectSpread(_objectSpread({
150
+ deviceId: deviceId,
151
+ dps,
152
+ mode: 2
153
+ }, typeof ((_ty$ipc = ty.ipc) === null || _ty$ipc === void 0 ? void 0 : _ty$ipc.publishDps) !== 'function' && {
154
+ pipelines: []
155
+ }), {}, {
156
+ options: {},
157
+ success: response => {
158
+ resolve({
159
+ code: 0,
160
+ data: response
161
+ });
162
+ },
163
+ fail: err => {
164
+ resolve({
165
+ code: -1,
166
+ msg: err
167
+ });
168
+ }
169
+ }));
170
+ } catch (err) {
171
+ resolve({
172
+ code: -1,
173
+ msg: String(err)
174
+ });
175
+ }
176
+ });
177
+ };
178
+
179
+ /**
180
+ * 根据DPCode获取最新的DP点的值
181
+ * @param key 保存的key属性
182
+ *
183
+ */
184
+
185
+ export const getDpStateValue = async (deviceId, dpCode) => {
186
+ try {
187
+ const {
188
+ dps,
189
+ schema
190
+ } = await ty.device.getDeviceInfo({
191
+ deviceId
192
+ });
193
+ const dpSchema = _find(schema, item => item.code === dpCode);
194
+ if (dpSchema) {
195
+ const {
196
+ id
197
+ } = dpSchema;
198
+ return {
199
+ code: 0,
200
+ data: dps[id]
201
+ };
202
+ }
203
+ return {
204
+ code: -1,
205
+ msg: `${dpCode} is not exist`
206
+ };
207
+ } catch (error) {
208
+ return {
209
+ code: -1,
210
+ msg: error
211
+ };
212
+ }
213
+ };
214
+
215
+ /**
216
+ * 根据DPCode获取DpId
217
+ * @param key 保存的key属性
218
+ *
219
+ */
220
+
221
+ export const getDpIdByCode = async (deviceId, dpCode) => {
222
+ try {
223
+ const {
224
+ schema
225
+ } = await ty.device.getDeviceInfo({
226
+ deviceId
227
+ });
228
+ const dpSchema = _find(schema, item => item.code === dpCode);
229
+ if (dpSchema) {
230
+ const {
231
+ id
232
+ } = dpSchema;
233
+ return {
234
+ code: 0,
235
+ data: id
236
+ };
237
+ }
238
+ return {
239
+ code: -1,
240
+ msg: `${dpCode} is not exist`
241
+ };
242
+ } catch (error) {
243
+ return {
244
+ code: -1,
245
+ msg: error
246
+ };
247
+ }
248
+ };
249
+
250
+ /**
251
+ * 通过短链形式跳转小程序
252
+ * @param url 小程序链接
253
+ * @param position 弹出位置 bottom|right
254
+ *
255
+ */
256
+
257
+ export const goToMiniProgramByShortLink = function (url) {
258
+ let position = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'bottom';
259
+ return new Promise(resolve => {
260
+ try {
261
+ ty.navigateToMiniProgram({
262
+ shortLink: url,
263
+ position: position,
264
+ success: res => {
265
+ resolve({
266
+ code: 0,
267
+ data: res
268
+ });
269
+ },
270
+ fail: err => {
271
+ resolve({
272
+ code: -1,
273
+ msg: err
274
+ });
275
+ }
276
+ });
277
+ } catch (err) {
278
+ resolve({
279
+ code: -1,
280
+ msg: String(err)
281
+ });
282
+ }
283
+ });
284
+ };
285
+
286
+ /**
287
+ * 获取视频码率
288
+ * @param deviceId
289
+ * @param extendParam 扩展参数
290
+ * @returns
291
+ */
292
+ export const getVideoBitrateKbps = function (deviceId) {
293
+ let extendParam = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
294
+ return new Promise(resolve => {
295
+ try {
296
+ ty.ipc.getVideoBitrateKbps({
297
+ deviceId,
298
+ extendParam: extendParam,
299
+ success: res => {
300
+ resolve({
301
+ data: res
302
+ });
303
+ },
304
+ fail: err => {
305
+ resolve({
306
+ code: -1,
307
+ msg: err
308
+ });
309
+ }
310
+ });
311
+ } catch (err) {
312
+ resolve({
313
+ code: -1,
314
+ msg: String(err)
315
+ });
316
+ }
317
+ });
318
+ };
319
+
320
+ // networkStatusDidChangedEvent,
321
+ // 事件:监听网络状态事件
322
+ // 通用
323
+ // ty.onNetworkStatusChange
324
+ export const networkStatusDidChangedEvent = callback => {
325
+ ty.onNetworkStatusChange(callback);
326
+ };
327
+
328
+ /**
329
+ * 获取手机当前横竖屏状态
330
+ * 需引入IPCKit,且在>=1.0.0-TTT432.1版本才可使用
331
+ * @returns
332
+ */
333
+ export const getMobileOrientation = () => {
334
+ return new Promise(resolve => {
335
+ try {
336
+ ty.getSystemInfo({
337
+ success: res => {
338
+ const {
339
+ deviceOrientation = ''
340
+ } = res;
341
+ resolve({
342
+ data: deviceOrientation
343
+ });
344
+ },
345
+ fail: error => {
346
+ resolve({
347
+ code: -1,
348
+ msg: error
349
+ });
350
+ }
351
+ });
352
+ } catch (err) {
353
+ resolve({
354
+ code: -1,
355
+ msg: String(err)
356
+ });
357
+ }
358
+ });
359
+ };
360
+
361
+ /**
362
+ * 跳转到webView
363
+ * @param key 保存的key属性
364
+ *
365
+ */
366
+
367
+ export const jumpToWebView = url => {
368
+ return new Promise(resolve => {
369
+ try {
370
+ ty.openInnerH5({
371
+ url: url,
372
+ success: res => {
373
+ resolve({
374
+ code: 0,
375
+ data: res
376
+ });
377
+ },
378
+ fail: err => {
379
+ resolve({
380
+ code: -1,
381
+ msg: err
382
+ });
383
+ }
384
+ });
385
+ } catch (err) {
386
+ resolve({
387
+ code: -1,
388
+ msg: String(err)
389
+ });
390
+ }
391
+ });
392
+ };
393
+
394
+ /*
395
+ * 屏幕旋转设置,auto(暂不支持) / portrait / landscape。pad 模式下不支持屏幕旋转
396
+ * 需引入MiniKit,且在>=2.4.1版本才可使用
397
+ * @param pageOrientation
398
+ * @returns
399
+ */
400
+ export const setScreenOrientation = pageOrientation => {
401
+ return new Promise(resolve => {
402
+ try {
403
+ ty.setPageOrientation({
404
+ pageOrientation: pageOrientation,
405
+ success: res => {
406
+ resolve({
407
+ data: res
408
+ });
409
+ },
410
+ fail: error => {
411
+ resolve({
412
+ code: -1,
413
+ msg: error
414
+ });
415
+ }
416
+ });
417
+ } catch (err) {
418
+ resolve({
419
+ code: -1,
420
+ msg: String(err)
421
+ });
422
+ }
423
+ });
424
+ };
425
+
426
+ /**
427
+ * 画中画:是否支持画中画:只有安卓在用
428
+ * @param deviceId
429
+ * @param extendParam 扩展参数
430
+ * @returns
431
+ */
432
+ export const isSupportFloatWindow = function (deviceId) {
433
+ let extendParam = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
434
+ return new Promise(resolve => {
435
+ try {
436
+ ty.ipc.isSupportFloatWindow({
437
+ deviceId,
438
+ extendParam: extendParam,
439
+ success: res => {
440
+ resolve({
441
+ data: res
442
+ });
443
+ },
444
+ fail: error => {
445
+ resolve({
446
+ code: -1,
447
+ msg: error
448
+ });
449
+ }
450
+ });
451
+ } catch (err) {
452
+ resolve({
453
+ code: -1,
454
+ msg: String(err)
455
+ });
456
+ }
457
+ });
458
+ };
459
+
460
+ /**
461
+ * 画中画:开启画中画
462
+ * @param deviceId
463
+ * @param extendParam 扩展参数
464
+ * @returns
465
+ */
466
+ export const openFloatWindow = function (deviceId) {
467
+ let extendParam = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
468
+ return new Promise(resolve => {
469
+ try {
470
+ ty.ipc.openFloatWindow({
471
+ deviceId,
472
+ extendParam: extendParam,
473
+ success: res => {
474
+ resolve({
475
+ data: res
476
+ });
477
+ },
478
+ fail: err => {
479
+ resolve({
480
+ code: -1,
481
+ msg: err
482
+ });
483
+ }
484
+ });
485
+ } catch (err) {
486
+ resolve({
487
+ code: -1,
488
+ msg: String(err)
489
+ });
490
+ }
491
+ });
492
+ };
493
+
494
+ /**
495
+ * IPC-APP 缓存支持的对讲方式:是否支持对讲
496
+ * @param deviceId
497
+ * @param extendParam 扩展参数
498
+ * @returns
499
+ */
500
+ export const isSupportedTalk = function (deviceId) {
501
+ let extendParam = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
502
+ return new Promise(resolve => {
503
+ try {
504
+ ty.ipc.isSupportedTalk({
505
+ deviceId,
506
+ extendParam: extendParam,
507
+ success: res => {
508
+ resolve({
509
+ data: res
510
+ });
511
+ },
512
+ fail: error => {
513
+ resolve({
514
+ code: -1,
515
+ msg: error
516
+ });
517
+ }
518
+ });
519
+ } catch (err) {
520
+ resolve({
521
+ code: -1,
522
+ msg: String(err)
523
+ });
524
+ }
525
+ });
526
+ };
527
+
528
+ /**
529
+ * 设备支持的对讲模式
530
+ * @param deviceId
531
+ * @param extendParam 扩展参数
532
+ * @returns 设备支持的对讲模式。回掉方法的参数为数值型,0 未知;1:单向对讲;2:双向对讲。
533
+ */
534
+ export const getCurrentSupportedTalkMode = function (deviceId) {
535
+ let extendParam = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
536
+ return new Promise(resolve => {
537
+ try {
538
+ ty.ipc.getCurrentSupportedTalkMode({
539
+ deviceId,
540
+ extendParam: extendParam,
541
+ success: res => {
542
+ resolve({
543
+ code: 0,
544
+ data: res
545
+ });
546
+ },
547
+ fail: error => {
548
+ resolve({
549
+ code: -1,
550
+ msg: error
551
+ });
552
+ }
553
+ });
554
+ } catch (err) {
555
+ resolve({
556
+ code: -1,
557
+ msg: String(err)
558
+ });
559
+ }
560
+ });
561
+ };
562
+
563
+ /**
564
+ * 处理 IPC URL 跳转
565
+ * @param {string} nativeUrl - 构建好的目标 URL。
566
+ * @returns {Promise<IRes>} 返回操作结果。
567
+ */
568
+ const handleIpcUrl = nativeUrl => {
569
+ return new Promise(resolve => {
570
+ ty.ipc.gotoCameraSettingsRouter({
571
+ url: nativeUrl,
572
+ success: res => {
573
+ resolve({
574
+ code: 0,
575
+ data: res
576
+ });
577
+ },
578
+ fail: err => {
579
+ resolve({
580
+ code: -1,
581
+ msg: err
582
+ });
583
+ }
584
+ });
585
+ });
586
+ };
587
+
588
+ /**
589
+ * 处理通用 URL 跳转
590
+ * @param {string} nativeUrl - 目标url。
591
+ * @returns {Promise<IRes>} 返回操作结果。
592
+ */
593
+ const handleGeneralUrl = nativeUrl => {
594
+ return new Promise(resolve => {
595
+ ty.canIUseRouter({
596
+ url: nativeUrl,
597
+ success: res => {
598
+ if (res.result) {
599
+ ty.router({
600
+ url: nativeUrl,
601
+ success: res => resolve({
602
+ code: 0,
603
+ data: res
604
+ }),
605
+ fail: err => resolve({
606
+ code: -1,
607
+ msg: err
608
+ })
609
+ });
610
+ } else {
611
+ resolve({
612
+ code: -1,
613
+ msg: res
614
+ });
615
+ }
616
+ },
617
+ fail: err => resolve({
618
+ code: -1,
619
+ msg: err
620
+ })
621
+ });
622
+ });
623
+ };
624
+
625
+ /**
626
+ * 构建目标 URL
627
+ *
628
+ * @param {string} url - 目标页面的路由路径。
629
+ * @param {Object} param - 跳转时携带的参数。
630
+ * @param {boolean} isGeneral - 是否为通用 URL。
631
+ * @returns {string} - 拼接后的 URL。
632
+ */
633
+ const buildUrl = (url, param, isGeneral) => {
634
+ const queryParams = Object.keys(param).map(key => `${key}=${param[key]}`).join('&');
635
+ return isGeneral ? `${url}?${queryParams}` : `thingSmart://${url}?${queryParams}`;
636
+ };
637
+
638
+ /**
639
+ * 跳转 APP 原生页面
640
+ * @param {string} url 页面路由
641
+ * @param {string} deviceId 设备Id
642
+ * @param {string} theme 主题 dark|light 默认取APP 主题色
643
+ * @param {Object} extraParams 额外参数, 额外页面需要单独拼接的参数
644
+ * @param {boolean} isGeneral 是否为通用页面,IPC原生业务页面不用传, 默认false
645
+ *
646
+ */
647
+
648
+ export const goToIpcPageNativeRoute = async function (url, deviceId) {
649
+ let theme = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : undefined;
650
+ let extraParams = arguments.length > 3 ? arguments[3] : undefined;
651
+ let isGeneral = arguments.length > 4 ? arguments[4] : undefined;
652
+ try {
653
+ const systemInfo = await ty.getSystemInfo();
654
+ const pageTheme = theme || (systemInfo === null || systemInfo === void 0 ? void 0 : systemInfo.theme);
655
+ const param = _objectSpread({
656
+ extra_camera_uuid: deviceId,
657
+ theme: pageTheme === 'dark' ? 1 : 2,
658
+ devId: deviceId
659
+ }, extraParams);
660
+ // 匹配url
661
+ const nativeUrl = buildUrl(url, param, isGeneral);
662
+ if (isGeneral) {
663
+ return handleGeneralUrl(nativeUrl);
664
+ }
665
+ return handleIpcUrl(nativeUrl);
666
+ } catch (err) {
667
+ return {
668
+ code: -1,
669
+ msg: String(err)
670
+ };
671
+ }
672
+ };
673
+
674
+ /**
675
+ * 跳转 通用帮助反馈小程序
676
+ */
677
+
678
+ export const goToGeneralHelpMini = async () => {
679
+ const result = await goToMiniProgramByShortLink('godzilla://tynxbsaqmkp28eanwp/pages/v2/problemSubmit/index');
680
+ return result;
681
+ };
682
+
683
+ /**
684
+ * 跳转 Ipc帮助反馈小程序
685
+ * @param {number} errCode 错误码
686
+ * @param {string} deviceId 设备id
687
+ * @param {number} helpNum 帮助编号
688
+ * @param {boolean} helpFeedBack 是否是反馈
689
+ * @param {string} brandColor 品牌色
690
+ */
691
+
692
+ export const goToIpcHelpMini = async function (errCode, deviceId, helpNum, helpFeedBack) {
693
+ let brandColor = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : '#ff592a';
694
+ try {
695
+ const {
696
+ homeId
697
+ } = await ty.home.getCurrentHomeInfo();
698
+ const {
699
+ uuid
700
+ } = await getDevInfo(deviceId);
701
+ const result = await goToMiniProgramByShortLink(`godzilla://tybxwaylc6inpkrgeu?errCode=${errCode}&deviceId=${deviceId}&homeId=${homeId}&instanceId=${uuid}&helpNum=${helpNum}&helpFeedBack=${helpFeedBack}&themeColor=${brandColor}`);
702
+ return result;
703
+ } catch (err) {
704
+ return {
705
+ code: -1,
706
+ msg: String(err)
707
+ };
708
+ }
709
+ };
package/lib/props.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ export interface IProps {
2
+ text: string;
3
+ }
4
+ export declare const defaultProps: IProps;
package/lib/props.js ADDED
@@ -0,0 +1,3 @@
1
+ export const defaultProps = {
2
+ text: 'Component Demo'
3
+ };