@ledvance/ui-biz-bundle 1.1.70 → 1.1.71

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.
@@ -1,232 +1,244 @@
1
- import { NativeApi, getFeature, putFeature } from '@ledvance/base/src/api/native';
2
- import {
3
- DefMoodOption,
4
- LightCategory,
5
- MixRemoteMoodInfo,
6
- MoodInfo,
7
- MoodPageParams,
8
- MoodUIInfo,
9
- RemoteMoodInfo,
10
- } from './Interface';
11
- import { parseJSON } from '@tuya/tuya-panel-lamp-sdk/lib/utils';
12
- import { getDefMoodList } from './MoodInfo';
13
- import {
14
- MixCeilingDpObj,
15
- MixDp2Obj,
16
- MixObj2Dp,
17
- dp2Obj,
18
- obj2Dp,
19
- stripDp2Obj,
20
- stripObj2Dp,
21
- } from './MoodParse';
22
- import { useDp, useDps } from '@ledvance/base/src/models/modules/NativePropsSlice';
23
- import { useState } from 'react';
24
- import { useUpdateEffect } from 'ahooks';
25
- import { WorkMode } from '@ledvance/base/src/utils/interface';
26
- import { Result } from '@ledvance/base/src/models/modules/Result';
27
-
28
- const SceneFeatureId = 'SimplifyScene';
29
- const MixLightSceneListFeatureId = 'MixLightSceneList';
30
-
31
- export const useWorkMode = (dp: string): [WorkMode, (value: WorkMode) => Promise<Result<any>>] => {
32
- return useDp(dp);
33
- };
34
-
35
- export const setRemoteMoodList = async (
36
- devId: string,
37
- isFeature: boolean,
38
- remoteSceneList: RemoteMoodInfo[] | MixRemoteMoodInfo[],
39
- featureId: string
40
- ) => {
41
- if (isFeature) {
42
- const res = await putFeature(devId, featureId, remoteSceneList);
43
- return {
44
- success: res.result,
45
- msg: res.msg,
46
- data: res.data,
47
- };
48
- } else {
49
- const res = await NativeApi.putJson(devId, featureId, JSON.stringify(remoteSceneList));
50
- return res;
51
- }
52
- };
53
-
54
- const getMoodInfo = (mainDp: string, option: LightCategory, secondaryDp: string) => {
55
- const moodInfo = !!(option.isStringLight || option.isStripLight)
56
- ? stripDp2Obj(mainDp, option.isStringLight)
57
- : option.isMixLight
58
- ? MixDp2Obj(mainDp)
59
- : option.isCeilingLight
60
- ? MixCeilingDpObj(mainDp, secondaryDp)
61
- : dp2Obj(mainDp, option.isFanLight, option.isUVCFan);
62
- return moodInfo;
63
- };
64
-
65
- const getMoodDp = (moodInfo: MoodUIInfo, option: LightCategory) => {
66
- const moodDp = !!(option.isStringLight || option.isStripLight)
67
- ? stripObj2Dp(moodInfo, option.isStringLight)
68
- : option.isMixLight
69
- ? MixObj2Dp(moodInfo)
70
- : obj2Dp(moodInfo, option.isFanLight, option.isUVCFan);
71
- return moodDp;
72
- };
73
-
74
- const remoteMoodInfo2MoodUIState = (
75
- remoteMoodInfo,
76
- index: number,
77
- option: DefMoodOption
78
- ): MoodUIInfo => {
79
- const moodInfo = option.isMixLight
80
- ? MixDp2Obj(remoteMoodInfo.value)
81
- : getMoodInfo(remoteMoodInfo.i, option, remoteMoodInfo.s);
82
- return {
83
- name: option.isMixLight ? remoteMoodInfo.name : remoteMoodInfo.n,
84
- image: '',
85
- ...moodInfo,
86
- };
87
- };
88
-
89
- export const getRemoteMoodList = async (
90
- devId: string,
91
- option: DefMoodOption,
92
- featureId = SceneFeatureId
93
- ) => {
94
- const isFeature = !(option.isCeilingLight || option.isStringLight || option.isStripLight);
95
- if (isFeature) {
96
- const moodFeatureId = option.isMixLight ? MixLightSceneListFeatureId : SceneFeatureId;
97
- // await setRemoteMoodList(devId, isFeature, getDefMoodList(option), moodFeatureId);
98
- const res = await getFeature(devId, moodFeatureId);
99
- console.log(res, '< --- res=====')
100
- if (!res.result) {
101
- return { success: false };
102
- }
103
- if (!res.data) {
104
- const result = await setRemoteMoodList(
105
- devId,
106
- isFeature,
107
- getDefMoodList(option),
108
- moodFeatureId
109
- );
110
- if (!result.success) {
111
- return { success: false };
112
- }
113
- return {
114
- success: true,
115
- data: result.data.map((item, index) => remoteMoodInfo2MoodUIState(item, index, option)),
116
- };
117
- }
118
- return {
119
- success: true,
120
- data: res.data.map((item, index) => remoteMoodInfo2MoodUIState(item, index, option)),
121
- };
122
- } else {
123
- const moodFeatureId = option.isCeilingLight ? SceneFeatureId : featureId;
124
- const res = await NativeApi.getJson(devId, moodFeatureId);
125
- const isNormalData = Array.isArray(parseJSON(res?.data));
126
- if (res.success && isNormalData) {
127
- return {
128
- success: true,
129
- data:
130
- res.data &&
131
- parseJSON(res.data).map((item, index) => remoteMoodInfo2MoodUIState(item, index, option)),
132
- };
133
- } else {
134
- if (res.msg?.includes('资源未找到') || !isNormalData) {
135
- const defaultScene = getDefMoodList(option);
136
- const res = await setRemoteMoodList(devId, isFeature, defaultScene, moodFeatureId);
137
- if (res.success) {
138
- return {
139
- success: true,
140
- data: defaultScene.map((item, index) =>
141
- remoteMoodInfo2MoodUIState(item, index, option)
142
- ),
143
- };
144
- }
145
- return { success: false };
146
- }
147
- return { success: false };
148
- }
149
- }
150
- };
151
-
152
- export const useMoodScene = (params: MoodPageParams): [MoodInfo, (moodInfo: MoodUIInfo) => Promise<Result<any>>] => {
153
- const [dps, setDps] = useDps();
154
- const main = dps[params.mainDp];
155
- const secondary = params.secondaryDp ? dps[params.secondaryDp] : '';
156
- const [moodState, setMoodState] = useState(getMoodInfo(main, params, secondary));
157
-
158
- useUpdateEffect(() => {
159
- setMoodState(getMoodInfo(main, params, secondary));
160
- }, [main, secondary]);
161
-
162
- const setMoodFn = (moodInfo: MoodUIInfo) => {
163
- const beforeDps = {}
164
- const afterDps = {}
165
- if (moodInfo) {
166
- const { mainLamp, secondaryLamp } = moodInfo
167
- if (mainLamp?.nodes?.length) {
168
- const mainHex = getMoodDp(params.isCeilingLight ? {...moodInfo, id: mainLamp.id!} : moodInfo, params);
169
- beforeDps[params.mainDp] = mainHex
170
- afterDps[params.mainWorkMode] = WorkMode.Scene;
171
- afterDps[params.mainSwitch] = true;
172
- }
173
- if (params.isCeilingLight && secondaryLamp.nodes?.length && params.secondaryDp) {
174
- const secondaryHex = stripObj2Dp({
175
- ...moodInfo,
176
- mainLamp: moodInfo.secondaryLamp,
177
- id: secondaryLamp.id!
178
- });
179
- beforeDps[params.secondaryDp] = secondaryHex
180
- afterDps[params.secondaryWorkMode!] = WorkMode.Scene
181
- afterDps[params.secondarySwitch!] = true
182
- }
183
- afterDps[params.switchLedDp] = true
184
- }
185
- setDps(afterDps).then()
186
- return setDps(beforeDps)
187
- };
188
- return [moodState, setMoodFn];
189
- };
190
-
191
- export const saveMoodList = (
192
- devId: string,
193
- moodList: MoodUIInfo[],
194
- option: LightCategory,
195
- featureId = SceneFeatureId
196
- ) => {
197
- const remoteMoods: any = moodList.map(s => {
198
- if (option.isMixLight) {
199
- return {
200
- name: s.name,
201
- image: '',
202
- value: MixObj2Dp(s),
203
- };
204
- }
205
- return {
206
- n: s.name,
207
- i: getMoodDp(option.isCeilingLight ? { ...s, id: s.mainLamp.id! } : s, option),
208
- s: s.secondaryLamp?.nodes?.length && option.isCeilingLight
209
- ? stripObj2Dp({
210
- ...s,
211
- mainLamp: s.secondaryLamp,
212
- id: s.secondaryLamp.id!
213
- })
214
- : '',
215
- t: 0,
216
- e: false,
217
- };
218
- });
219
- const isFeature = !(
220
- option.isCeilingLight ||
221
- option.isStringLight ||
222
- option.isStripLight ||
223
- option.isMatterLight ||
224
- option.isUVCFan
225
- );
226
- const moodFeatureId = !!(option.isStringLight || option.isStripLight)
227
- ? featureId
228
- : option.isMixLight
229
- ? MixLightSceneListFeatureId
230
- : SceneFeatureId;
231
- return setRemoteMoodList(devId, isFeature, remoteMoods, moodFeatureId);
232
- };
1
+ import { NativeApi, getFeature, putFeature } from '@ledvance/base/src/api/native';
2
+ import {
3
+ DefMoodOption,
4
+ LightCategory,
5
+ MixRemoteMoodInfo,
6
+ MoodInfo,
7
+ MoodPageParams,
8
+ MoodUIInfo,
9
+ RemoteMoodInfo,
10
+ } from './Interface';
11
+ import { parseJSON } from '@tuya/tuya-panel-lamp-sdk/lib/utils';
12
+ import { getDefMoodList } from './MoodInfo';
13
+ import {
14
+ MixCeilingDpObj,
15
+ MixDp2Obj,
16
+ MixObj2Dp,
17
+ dp2Obj,
18
+ obj2Dp,
19
+ stripDp2Obj,
20
+ stripObj2Dp,
21
+ } from './MoodParse';
22
+ import { useDp, useDps } from '@ledvance/base/src/models/modules/NativePropsSlice';
23
+ import { useState } from 'react';
24
+ import { useUpdateEffect } from 'ahooks';
25
+ import { WorkMode } from '@ledvance/base/src/utils/interface';
26
+ import { Result } from '@ledvance/base/src/models/modules/Result';
27
+
28
+ const SceneFeatureId = 'SimplifyScene';
29
+ const MixLightSceneListFeatureId = 'MixLightSceneList';
30
+
31
+ export const useWorkMode = (dp: string): [WorkMode, (value: WorkMode) => Promise<Result<any>>] => {
32
+ return useDp(dp);
33
+ };
34
+
35
+ export const useSwitchLed = (dp: string): [boolean, (value: boolean) => Promise<Result<any>>] => {
36
+ return useDp(dp);
37
+ };
38
+
39
+ export const setRemoteMoodList = async (
40
+ devId: string,
41
+ isFeature: boolean,
42
+ remoteSceneList: RemoteMoodInfo[] | MixRemoteMoodInfo[],
43
+ featureId: string
44
+ ) => {
45
+ if (isFeature) {
46
+ const res = await putFeature(devId, featureId, remoteSceneList);
47
+ return {
48
+ success: res.result,
49
+ msg: res.msg,
50
+ data: res.data,
51
+ };
52
+ } else {
53
+ const res = await NativeApi.putJson(devId, featureId, JSON.stringify(remoteSceneList));
54
+ return res;
55
+ }
56
+ };
57
+
58
+ const getMoodInfo = (mainDp: string, option: LightCategory, secondaryDp: string) => {
59
+ const moodInfo = !!(option.isStringLight || option.isStripLight)
60
+ ? stripDp2Obj(mainDp, option.isStringLight)
61
+ : option.isMixLight
62
+ ? MixDp2Obj(mainDp)
63
+ : option.isCeilingLight
64
+ ? MixCeilingDpObj(mainDp, secondaryDp)
65
+ : dp2Obj(mainDp, option.isFanLight, option.isUVCFan);
66
+ return moodInfo;
67
+ };
68
+
69
+ const getMoodDp = (moodInfo: MoodUIInfo, option: LightCategory) => {
70
+ const moodDp = !!(option.isStringLight || option.isStripLight)
71
+ ? stripObj2Dp(moodInfo, option.isStringLight)
72
+ : option.isMixLight
73
+ ? MixObj2Dp(moodInfo)
74
+ : obj2Dp(moodInfo, option.isFanLight, option.isUVCFan);
75
+ return moodDp;
76
+ };
77
+
78
+ const remoteMoodInfo2MoodUIState = (
79
+ remoteMoodInfo,
80
+ _: number,
81
+ option: DefMoodOption
82
+ ): MoodUIInfo => {
83
+ const moodInfo = option.isMixLight
84
+ ? MixDp2Obj(remoteMoodInfo.value)
85
+ : getMoodInfo(remoteMoodInfo.i, option, remoteMoodInfo.s);
86
+ return {
87
+ name: option.isMixLight ? remoteMoodInfo.name : remoteMoodInfo.n,
88
+ image: '',
89
+ ...moodInfo,
90
+ };
91
+ };
92
+
93
+ export const getRemoteMoodList = async (
94
+ devId: string,
95
+ option: DefMoodOption,
96
+ featureId = SceneFeatureId
97
+ ) => {
98
+ const isFeature = !(option.isCeilingLight || option.isStringLight || option.isStripLight);
99
+ if (isFeature) {
100
+ const moodFeatureId = option.isMixLight ? MixLightSceneListFeatureId : SceneFeatureId;
101
+ // await setRemoteMoodList(devId, isFeature, getDefMoodList(option), moodFeatureId);
102
+ const res = await getFeature(devId, moodFeatureId);
103
+ if (!res.result) {
104
+ return { success: false };
105
+ }
106
+ if (!res.data) {
107
+ const result = await setRemoteMoodList(
108
+ devId,
109
+ isFeature,
110
+ getDefMoodList(option),
111
+ moodFeatureId
112
+ );
113
+ if (!result.success) {
114
+ return { success: false };
115
+ }
116
+ return {
117
+ success: true,
118
+ data: result.data.map((item, index) => remoteMoodInfo2MoodUIState(item, index, option)),
119
+ };
120
+ }
121
+ return {
122
+ success: true,
123
+ data: res.data.map((item, index) => remoteMoodInfo2MoodUIState(item, index, option)),
124
+ };
125
+ } else {
126
+ const moodFeatureId = option.isCeilingLight ? SceneFeatureId : featureId;
127
+ // const defaultScene = getDefMoodList(option);
128
+ // await setRemoteMoodList(devId, isFeature, defaultScene, moodFeatureId);
129
+ const res = await NativeApi.getJson(devId, moodFeatureId);
130
+ const isNormalData = Array.isArray(parseJSON(res?.data));
131
+ if (res.success && isNormalData) {
132
+ return {
133
+ success: true,
134
+ data:
135
+ res.data &&
136
+ parseJSON(res.data).map((item, index) => remoteMoodInfo2MoodUIState(item, index, option)),
137
+ };
138
+ } else {
139
+ if (res.msg?.includes('资源未找到') || !isNormalData) {
140
+ const defaultScene = getDefMoodList(option);
141
+ const res = await setRemoteMoodList(devId, isFeature, defaultScene, moodFeatureId);
142
+ if (res.success) {
143
+ return {
144
+ success: true,
145
+ data: defaultScene.map((item, index) =>
146
+ remoteMoodInfo2MoodUIState(item, index, option)
147
+ ),
148
+ };
149
+ }
150
+ return { success: false };
151
+ }
152
+ return { success: false };
153
+ }
154
+ }
155
+ };
156
+
157
+ export const useMoodScene = (params: MoodPageParams): [MoodInfo, (moodInfo: MoodUIInfo) => Promise<Result<any>>] => {
158
+ const [dps, setDps] = useDps();
159
+ const main = dps[params.mainDp];
160
+ const secondaryWorkMode = params.secondaryWorkMode ? dps[params.secondaryWorkMode] : ''
161
+ const secondary = params.secondaryDp && secondaryWorkMode === WorkMode.Scene ? dps[params.secondaryDp] : '';
162
+ const [moodState, setMoodState] = useState(getMoodInfo(main, params, secondary));
163
+
164
+ useUpdateEffect(() => {
165
+ setMoodState(getMoodInfo(main, params, secondary));
166
+ }, [main, secondary]);
167
+
168
+ const setMoodFn = (moodInfo: MoodUIInfo) => {
169
+ const mainDps = {}
170
+ const secondaryDps = {}
171
+ if (moodInfo) {
172
+ const { mainLamp, secondaryLamp } = moodInfo
173
+ if (mainLamp?.nodes?.length) {
174
+ const mainHex = getMoodDp(params.isCeilingLight ? {...moodInfo, id: mainLamp.id!} : moodInfo, params);
175
+ mainDps[params.mainDp] = mainHex
176
+ mainDps[params.mainWorkMode] = WorkMode.Scene;
177
+ mainDps[params.mainSwitch] = true;
178
+ }
179
+ if (params.isCeilingLight && params.secondaryDp) {
180
+ if (secondaryLamp.nodes?.length){
181
+ const secondaryHex = stripObj2Dp({
182
+ ...moodInfo,
183
+ mainLamp: secondaryLamp,
184
+ id: secondaryLamp.id!,
185
+ version: 1
186
+ });
187
+ console.log(secondaryHex, secondaryLamp, 'hexxxxx')
188
+ secondaryDps[params.secondaryDp] = secondaryHex
189
+ secondaryDps[params.secondaryWorkMode!] = WorkMode.Scene
190
+ secondaryDps[params.secondarySwitch!] = true
191
+ }else{
192
+ secondaryDps[params.secondaryWorkMode!] = WorkMode.Colour
193
+ }
194
+ setDps(secondaryDps).then()
195
+ }
196
+ mainLamp[params.switchLedDp] = true
197
+ }
198
+ return setDps(mainDps)
199
+ };
200
+ return [moodState, setMoodFn];
201
+ };
202
+
203
+ export const saveMoodList = (
204
+ devId: string,
205
+ moodList: MoodUIInfo[],
206
+ option: LightCategory,
207
+ featureId = SceneFeatureId
208
+ ) => {
209
+ const remoteMoods: any = moodList.map(s => {
210
+ if (option.isMixLight) {
211
+ return {
212
+ name: s.name,
213
+ image: '',
214
+ value: MixObj2Dp(s),
215
+ };
216
+ }
217
+ return {
218
+ n: s.name,
219
+ i: getMoodDp(option.isCeilingLight ? { ...s, id: s.mainLamp.id! } : s, option),
220
+ s: s.secondaryLamp?.nodes?.length && option.isCeilingLight
221
+ ? stripObj2Dp({
222
+ ...s,
223
+ mainLamp: s.secondaryLamp,
224
+ id: s.secondaryLamp.id!
225
+ })
226
+ : '',
227
+ t: 0,
228
+ e: false,
229
+ };
230
+ });
231
+ const isFeature = !(
232
+ option.isCeilingLight ||
233
+ option.isStringLight ||
234
+ option.isStripLight ||
235
+ option.isMatterLight ||
236
+ option.isUVCFan
237
+ );
238
+ const moodFeatureId = !!(option.isStringLight || option.isStripLight)
239
+ ? featureId
240
+ : option.isMixLight
241
+ ? MixLightSceneListFeatureId
242
+ : SceneFeatureId;
243
+ return setRemoteMoodList(devId, isFeature, remoteMoods, moodFeatureId);
244
+ };