@ledvance/ui-biz-bundle 1.0.74 → 1.0.75

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/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "name": "@ledvance/ui-biz-bundle",
5
5
  "pid": [],
6
6
  "uiid": "",
7
- "version": "1.0.74",
7
+ "version": "1.0.75",
8
8
  "scripts": {},
9
9
  "dependencies": {
10
10
  "@ledvance/base": "^1.x",
@@ -109,29 +109,59 @@ export const musicDefalutDatasource = [
109
109
  },
110
110
  ]
111
111
 
112
- export const dreamMusicData = [
112
+ export type DreamMusicDataType = {
113
+ id: number
114
+ mode: number
115
+ title: string
116
+ value: string
117
+ }
118
+
119
+ export const dreamMusicData: DreamMusicDataType[] = [
113
120
  {
114
- label: Strings.getLang('devicemusic_music_text'),
115
- value: '0101000364230000006400006400786400f064003c6400b464012c64000000'
121
+ id: 0,
122
+ mode: 3,
123
+ title: Strings.getLang('devicemusic_music_text'),
124
+ value: '0101000364320000006400006400786400f064003c6400b464012c64000000'
116
125
  },
117
126
  {
118
- label: Strings.getLang('devicemusic_ball_game_text'),
127
+ id: 1,
128
+ mode: 2,
129
+ title: Strings.getLang('devicemusic_ball_game_text'),
119
130
  value: '0101000264320000006400005000785000f050003c5000b450012c50000000'
120
131
  },
121
132
  {
122
- label: Strings.getLang('devicemusic_game_text'),
133
+ id: 2,
134
+ mode: 2,
135
+ title: Strings.getLang('devicemusic_game_text'),
123
136
  value: '0101001264320000006400006400786400f064003c6400b464012c64000000'
124
137
  },
125
138
  {
126
- label: Strings.getLang('devicemusic_romantic_text'),
139
+ id: 3,
140
+ mode: 2,
141
+ title: Strings.getLang('devicemusic_romantic_text'),
127
142
  value: '0101010264320100006400006400786400f064003c6400b464012c64000000'
128
143
  },
129
144
  {
130
- label: "Genre 5",
145
+ id: 4,
146
+ mode: 0,
147
+ title: "Genre 5",
131
148
  value: '0101020064320100006400006400786400f064003c6400b464012c64'
132
149
  },
133
150
  {
134
- label: "Genre 6",
151
+ id: 5,
152
+ mode: 0,
153
+ title: "Genre 6",
135
154
  value: '0101031064320100006400006400786400f064003c6400b464012c64000000'
136
155
  }
137
156
  ]
157
+
158
+ export const musicTabs = [
159
+ {
160
+ key: 0,
161
+ title: Strings.getLang('devicemusic_switch_tab_device')
162
+ },
163
+ {
164
+ key: 1,
165
+ title: Strings.getLang('devicemusic_switch_tab_smartphone')
166
+ }
167
+ ]
@@ -1,23 +1,23 @@
1
- import React, { useEffect } from 'react'
1
+ import React, { useCallback, useEffect } from 'react'
2
2
  import { StyleSheet, View, Text, Image } from 'react-native'
3
- import { useRoute } from '@react-navigation/core'
3
+ import { useRoute, useNavigation } from '@react-navigation/core'
4
4
  import Page from '@ledvance/base/src/components/Page'
5
5
  import { useDeviceId, useDeviceInfo } from '@ledvance/base/src/models/modules/NativePropsSlice'
6
6
  import Strings from '@ledvance/base/src/i18n'
7
7
  import Img from './res'
8
- import { useEnable, useWorkMode, useMixRgbcw, useMixScene, useMusicData } from './MusicPageActions'
8
+ import { useEnable, useWorkMode, useMixRgbcw, useMixScene, useMusicData, useDreamMusic, DreamLightMicMusicData, getRemoteDreamMusic, DreamLightMicMusicUIState, saveRemoteDreamMusic } from './MusicPageActions'
9
9
  import { useReactive, useUpdateEffect } from 'ahooks'
10
10
  import { NativeApi } from '@ledvance/base/src/api/native'
11
- import { AppleMusicDataType, dreamMusicData, musicDefalutDatasource } from './MusicDataBean'
11
+ import { AppleMusicDataType, musicDefalutDatasource, musicTabs } from './MusicDataBean'
12
12
  import { Dialog, SwitchButton, TabBar, Utils } from 'tuya-panel-kit'
13
13
  import * as MusicManager from './MusicManager'
14
- import { ShowSelectView } from '@ledvance/base/src/components/ShowSelect'
15
- import SelecetGenre from './SelecetGenre'
16
14
  import { JudgeTimeScheduleProps } from "../timeSchedule/TimeScheduleBean"
17
15
  import Spacer from '@ledvance/base/src/components/Spacer'
18
16
  import LdvSlider from '@ledvance/base/src/components/ldvSlider'
19
- import { Buffer } from 'buffer'
20
-
17
+ import TextFieldStyleButton from '@ledvance/base/src/components/TextFieldStyleButton'
18
+ import { SelectPageParams } from '@ledvance/ui-biz-bundle/src/modules/select/SelectPage'
19
+ import { ui_biz_routerKey } from '@ledvance/ui-biz-bundle/src/navigation/Routers'
20
+ import { cloneDeep } from 'lodash'
21
21
  const { convertX: cx } = Utils.RatioUtils
22
22
 
23
23
  export interface MusicPageRouterParams extends JudgeTimeScheduleProps {
@@ -32,86 +32,143 @@ export interface MusicPageRouterParams extends JudgeTimeScheduleProps {
32
32
  const MusicPage = () => {
33
33
  const deviceInfo = useDeviceInfo()
34
34
  const deviceId = useDeviceId()
35
+ const navigation = useNavigation()
35
36
  const props = useRoute().params as MusicPageRouterParams
36
37
  const [workMode, setWorkMode] = useWorkMode(props.work_mode)
37
38
  const [mixLight] = useMixRgbcw(props.mix_rgbcw, props.switch_led)
38
39
  const [, setMixScene] = useMixScene(props.mix_light_scene, props.work_mode)
39
40
  const [, setMusicData] = useMusicData(props.music_data)
41
+ const [dreamMusic, setDreamMusic] = props.dreamMusicDp ? useDreamMusic(props.dreamMusicDp) : []
40
42
  const [switchLed, setSwitchLed] = useEnable(props.switch_led)
41
43
 
42
44
  const state = useReactive({
43
45
  musicEnable: false,
44
- musicMode: 0,
45
- musicType: 1, // 0 设备麦克风, 1 手机麦克风
46
- sensitivity: 100,
46
+ musicType: props.dreamMusicDp ? 0 : 1, // 0 设备麦克风, 1 手机麦克风
47
47
  isColourExist: true,
48
48
  isTempExist: true,
49
- genreName: Strings.getLang('devicemusic_music_text'),
49
+ deviceGenreMode: dreamMusic ? dreamMusic.id : 0,
50
+ phoneGenreMode: 0,
51
+ genreMode: 0,
52
+ currentMusic: cloneDeep(dreamMusic) as DreamLightMicMusicData,
53
+ musicModeData: [] as DreamLightMicMusicUIState[],
54
+ musicSelect: 0
50
55
  })
51
56
 
52
- const openMusic = async (id: number) => {
53
- await setWorkMode('music')
54
- const dataIndex = musicDefalutDatasource.findIndex(item => item?.id === id)
55
- await saveSelect(dataIndex)
56
- await MusicManager.open(
57
- musicDefalutDatasource[dataIndex],
58
- (_: AppleMusicDataType, index: number) => {
59
- state.musicMode = index
60
- },
61
- putMusicDataFn,
62
- { isColourExist: true, isTempExist: true }
63
- )
64
- }
57
+ useEffect(() => {
58
+ if (props.dreamMusicDp) {
59
+ getRemoteDreamMusic(deviceId).then(res => {
60
+ if (res.success) {
61
+ state.musicModeData = res.data
62
+ }
63
+ })
64
+ }
65
+ }, [])
66
+
67
+ useUpdateEffect(() => {
68
+ // 设备麦克风
69
+ if (!state.musicEnable) return
70
+ if (props.dreamMusicDp && state.musicType === 0) {
71
+ openMusic()
72
+ } else {
73
+ changeMusic(state.phoneGenreMode)
74
+ }
75
+ }, [state.deviceGenreMode, state.phoneGenreMode, state.musicModeData])
76
+
77
+ useUpdateEffect(() => {
78
+ if (!state.musicEnable) return
79
+ if (state.musicType === 0) MusicManager.close()
80
+ openMusic()
81
+ }, [state.musicType])
82
+
83
+ const updateRemoteDreamMusic = useCallback(() => {
84
+ const newMusicData = cloneDeep(state.musicModeData).map(music => {
85
+ return music.id === state.currentMusic.id ? {
86
+ ...state.currentMusic,
87
+ title: music.title
88
+ } : music
89
+ })
90
+ setDreamMusic && setDreamMusic({
91
+ ...state.currentMusic,
92
+ power: true
93
+ }, {}).then()
94
+ saveRemoteDreamMusic(deviceId, newMusicData).then()
95
+ }, [state.currentMusic, state.musicModeData])
65
96
 
66
- const closeMusic = async () => {
97
+ const openMusic = useCallback(async () => {
98
+ if (workMode !== 'music') {
99
+ savePreWorkmode().then()
100
+ }
101
+ const extraDp = {}
102
+ if (!switchLed) extraDp[props.switch_led] = true
103
+ if (workMode !== 'music') extraDp[props.work_mode] = 'music'
104
+ if (props.dreamMusicDp && state.musicType === 0) {
105
+ setDreamMusic && setDreamMusic({
106
+ ...state.currentMusic,
107
+ power: true
108
+ }, extraDp).then()
109
+ } else {
110
+ if (workMode !== 'music') await setWorkMode('music')
111
+ if (!switchLed) setSwitchLed(true)
112
+ await MusicManager.open(
113
+ musicDefalutDatasource[state.phoneGenreMode],
114
+ () => { },
115
+ (data) => {
116
+ putMusicDataFn(data, {})
117
+ },
118
+ { isColourExist: true, isTempExist: true }
119
+ )
120
+ }
121
+ }, [state.musicType, state.phoneGenreMode, props.dreamMusicDp, state.currentMusic, switchLed, workMode])
122
+
123
+ const closeMusic = useCallback(async () => {
67
124
  await MusicManager.close()
68
- await saveSelect('')
69
- await NativeApi.putJson(deviceId, 'musicSelect', JSON.stringify({ type: '' }));
70
125
  const res: any = await NativeApi.getJson(deviceId, 'musicPreWorkMode')
71
126
  const workMode = res?.data && JSON.parse(res?.data)?.workMode
72
- if (props.isMixRGBWLamp) {
73
- const mixRes: any = await NativeApi.getJson(deviceId, 'musicPreMixLight')
74
- const getLight = mixRes?.data && JSON.parse(mixRes?.data)?.mixLight
75
- await setMixScene({
76
- version: 0,
77
- id: 0,
78
- lamps: [
79
- {
80
- enable: getLight?.whiteLightSwitch,
81
- nodes: [{
82
- brightness: getLight?.brightness,
83
- changeTime: 0,
84
- changeType: 0,
85
- colorTemp: getLight?.colorTempPercent,
86
- intervalTime: 0,
87
- hue: 0,
88
- sat: 0,
89
- lightness: 0
90
- }],
91
- type: 2
92
- },
93
- {
94
- enable: getLight?.colorLightSwitch,
95
- nodes: [{
96
- intervalTime: 0,
97
- changeTime: 0,
98
- changeType: 0,
99
- hue: getLight?.hue,
100
- sat: getLight?.sat,
101
- lightness: getLight?.lightness,
102
- brightness: 0,
103
- colorTemp: 0,
104
- }],
105
- type: 3
106
- },
107
- ]
108
- })
127
+ if (state.musicType === 1) {
128
+ if (props.isMixRGBWLamp) {
129
+ const mixRes: any = await NativeApi.getJson(deviceId, 'musicPreMixLight')
130
+ const getLight = mixRes?.data && JSON.parse(mixRes?.data)?.mixLight
131
+ await setMixScene({
132
+ version: 0,
133
+ id: 0,
134
+ lamps: [
135
+ {
136
+ enable: getLight?.whiteLightSwitch,
137
+ nodes: [{
138
+ brightness: getLight?.brightness,
139
+ changeTime: 0,
140
+ changeType: 0,
141
+ colorTemp: getLight?.colorTempPercent,
142
+ intervalTime: 0,
143
+ hue: 0,
144
+ sat: 0,
145
+ lightness: 0
146
+ }],
147
+ type: 2
148
+ },
149
+ {
150
+ enable: getLight?.colorLightSwitch,
151
+ nodes: [{
152
+ intervalTime: 0,
153
+ changeTime: 0,
154
+ changeType: 0,
155
+ hue: getLight?.hue,
156
+ sat: getLight?.sat,
157
+ lightness: getLight?.lightness,
158
+ brightness: 0,
159
+ colorTemp: 0,
160
+ }],
161
+ type: 3
162
+ },
163
+ ]
164
+ })
165
+ }
109
166
  }
110
167
  await setWorkMode(workMode)
111
- }
168
+ }, [state.musicType])
112
169
 
113
170
  const saveSelect = async (id: string | number) => {
114
- await NativeApi.putJson(deviceId, 'musicSelect', JSON.stringify({ type: id }));
171
+ await NativeApi.putJson(deviceId, 'musicSelect', JSON.stringify({ type: id, musicType: state.musicType }));
115
172
  }
116
173
 
117
174
  const savePreWorkmode = async () => {
@@ -123,52 +180,52 @@ const MusicPage = () => {
123
180
 
124
181
  const getSelect = async () => {
125
182
  const res: any = await NativeApi.getJson(deviceId, 'musicSelect')
126
- const type = JSON.parse(res?.data)?.type
127
- state.genreName = musicDefalutDatasource[type !== '' && type || 0]?.title
128
- state.musicEnable = workMode === 'music'
129
- if (type !== '') {
130
- await openMusic(type)
183
+ const type = res?.data ? JSON.parse(res?.data)?.type : 0
184
+ state.musicType = res?.data ? JSON.parse(res?.data)?.musicType : props.dreamMusicDp ? 0 : 1
185
+ state.phoneGenreMode = musicDefalutDatasource[type !== '' && type]?.id
186
+ state.musicEnable = workMode === 'music' && !!switchLed
187
+ if (type !== '' && state.musicType === 1 && state.musicEnable) {
188
+ openMusic()
131
189
  }
132
190
  }
133
191
 
134
192
  const changeMusic = async (id: number) => {
135
- await setWorkMode('music')
136
- const dataIndex = musicDefalutDatasource.findIndex(item => item?.id === id)
137
- await saveSelect(dataIndex)
193
+ if (workMode !== 'music') await setWorkMode('music')
194
+ if (!switchLed) setSwitchLed(true)
195
+ await saveSelect(id)
196
+ await MusicManager.close()
138
197
  await MusicManager.open(
139
- musicDefalutDatasource[dataIndex],
140
- (_: AppleMusicDataType, index: number) => {
141
- state.musicMode = index
198
+ musicDefalutDatasource[id],
199
+ (_: AppleMusicDataType) => {
200
+ },
201
+ (data) => {
202
+ putMusicDataFn(data, {})
142
203
  },
143
- putMusicDataFn,
144
204
  { isColourExist: true, isTempExist: true }
145
205
  )
146
206
  }
147
207
 
148
- const putMusicDataFn = (data: any) => {
149
- setMusicData(data).then()
208
+ const putMusicDataFn = (data: any, extraDp = {}) => {
209
+ setMusicData(data, extraDp).then()
150
210
  }
151
211
 
152
- useUpdateEffect(() => {
153
- if (workMode !== 'music' && state.musicEnable) {
154
- savePreWorkmode().then()
155
- }
156
- state.musicEnable ? openMusic(0) : closeMusic()
157
- }, [state.musicEnable])
158
-
159
212
  useEffect(() => {
160
213
  getSelect().then()
161
214
  }, [])
162
215
 
163
- useEffect(() =>{
164
- dreamMusicData.forEach((music)=>{
165
- console.log(Buffer.from(music.value, 'base64').toString('hex'), music.label)
216
+ const getSelectModeData = useCallback((): [string | undefined, any[]] => {
217
+ const genreModeData = props.dreamMusicDp && state.musicType === 0 ? state.musicModeData : musicDefalutDatasource
218
+ const genreMode = props.dreamMusicDp && state.musicType === 0 ? state.deviceGenreMode : state.phoneGenreMode
219
+ const title = genreModeData.find(mode => mode.id === genreMode)?.title
220
+ const selectModeData = genreModeData.map(music => {
221
+ return {
222
+ text: music.title,
223
+ selected: music.id === genreMode,
224
+ value: music.id,
225
+ }
166
226
  })
167
- })
168
-
169
- const tabRadios = Array.from(Array(2), (v, k) => k + 1).map((v) => {
170
- return { key: `${v}`, title: `Tab${v}` };
171
- });
227
+ return [title, selectModeData]
228
+ }, [state.deviceGenreMode, state.phoneGenreMode, state.musicModeData, state.musicType, props.dreamMusicDp])
172
229
 
173
230
  return (
174
231
  <Page
@@ -181,7 +238,7 @@ const MusicPage = () => {
181
238
  value={state.musicEnable}
182
239
  thumbStyle={{ elevation: 0 }}
183
240
  onValueChange={async v => {
184
- if (!switchLed) {
241
+ if (!switchLed && v) {
185
242
  Dialog.confirm({
186
243
  title: Strings.getLang('feature_activate_dialog_text'),
187
244
  subTitle: Strings.getLang('music_reactivate_dialog_text'),
@@ -189,23 +246,33 @@ const MusicPage = () => {
189
246
  confirmText: Strings.getLang('cancel_dialog_delete_item_sleepschedule_answer_yes_text'),
190
247
  onConfirm: async (_, { close }) => {
191
248
  close()
192
- await setSwitchLed(true)
193
249
  state.musicEnable = v
250
+ await openMusic()
194
251
  },
195
252
  })
196
253
  } else {
197
254
  state.musicEnable = v
255
+ if (v) {
256
+ await openMusic()
257
+ } else {
258
+ await closeMusic()
259
+ }
198
260
  }
199
261
  }} />
200
- <TabBar
262
+ {props.dreamMusicDp && <TabBar
201
263
  type='radio'
202
- tabs={tabRadios}
203
- style={{ borderRadius: cx(8), backgroundColor: '#f6f6f6' }}
264
+ tabs={musicTabs}
265
+ style={{ borderRadius: cx(8), backgroundColor: '#f6f6f6', height: cx(36) }}
204
266
  tabTextStyle={{ color: '#000' }}
205
267
  tabActiveTextStyle={{ color: '#000', fontWeight: 'bold' }}
206
- />
268
+ activeKey={state.musicType}
269
+ onChange={async (v) => {
270
+ state.musicType = Number(v)
271
+ await saveSelect(state.phoneGenreMode)
272
+ }}
273
+ />}
207
274
  <Spacer />
208
- <Text style={styles.tiptext}>{Strings.getLang(state.musicType === 1 ? 'devicemusic_description_text' : 'group_feature_music_secondsection_note')}</Text>
275
+ <Text style={styles.tiptext}>{Strings.getLang(state.musicType === 0 ? 'devicemusic_devicemic_description_text' : 'devicemusic_description_text')}</Text>
209
276
  <View style={{ alignItems: 'center', marginVertical: cx(38) }}>
210
277
  <Image
211
278
  style={{ width: cx(200), height: cx(204) }}
@@ -216,27 +283,39 @@ const MusicPage = () => {
216
283
  {
217
284
  state.musicEnable &&
218
285
  <>
219
- {props.dreamMusicDp &&
286
+ {props.dreamMusicDp && state.musicType === 0 &&
220
287
  <LdvSlider
221
288
  title={Strings.getLang('devicemusic_switch_tab_device_sensitivity')}
222
- value={state.sensitivity}
289
+ value={state.currentMusic?.sensitivity || 50}
223
290
  min={1}
224
291
  max={100}
225
292
  onValueChange={() => { }}
226
- onSlidingComplete={() => { }}
293
+ onSlidingComplete={(v: number) => {
294
+ if (state.currentMusic) {
295
+ state.currentMusic.sensitivity = v
296
+ updateRemoteDreamMusic()
297
+ }
298
+ }}
227
299
  subTitleStr={''} />
228
300
  }
229
- <SelecetGenre
230
- value={state.genreName}
301
+ <TextFieldStyleButton
302
+ text={getSelectModeData()[0] || Strings.getLang('devicemusic_music_text')}
231
303
  placeholder={Strings.getLang('devicemusic_subheadline_text')}
232
- onChange={
233
- () => {
234
- return ShowSelectView(Strings.getLang('devicemusic_subheadline_text'), musicDefalutDatasource, state.genreName, async (dataInfo: any) => {
235
- state.genreName = dataInfo?.title
236
- await changeMusic(dataInfo?.id)
237
- })
304
+ onPress={() => {
305
+ const paramsSelect: SelectPageParams<number> = {
306
+ title: Strings.getLang('devicemusic_subheadline_text'),
307
+ data: getSelectModeData()[1],
308
+ onSelect: selectPageData => {
309
+ if (props.dreamMusicDp && state.musicType === 0) {
310
+ state.deviceGenreMode = selectPageData.value
311
+ state.currentMusic = state.musicModeData.find(music => music.id === selectPageData.value) as DreamLightMicMusicData
312
+ } else {
313
+ state.phoneGenreMode = selectPageData.value
314
+ }
315
+ },
238
316
  }
239
- }
317
+ navigation.navigate(ui_biz_routerKey.ui_biz_select_page, paramsSelect)
318
+ }}
240
319
  />
241
320
  </>
242
321
  }
@@ -1,106 +1,192 @@
1
1
  import { useState } from 'react'
2
- import { useUpdateEffect} from "ahooks"
3
- import { useDp } from '@ledvance/base/src/models/modules/NativePropsSlice'
2
+ import { useUpdateEffect } from "ahooks"
3
+ import { useDp, useDps } from '@ledvance/base/src/models/modules/NativePropsSlice'
4
4
  import { ColorParser } from '@ledvance/base/src/utils/ColorParser'
5
- import { dp2Obj, obj2Dp} from '@ledvance/ui-biz-bundle/src/modules/timeSchedule/mix/MixLightActions'
5
+ import { dp2Obj, obj2Dp } from '@ledvance/ui-biz-bundle/src/modules/timeSchedule/mix/MixLightActions'
6
6
  import { MixLightBean } from '@ledvance/ui-biz-bundle/src/modules/timeSchedule/mix/MixLightBean'
7
- export const useEnable = (dpKey:string) =>{
8
- return useDp(dpKey)
7
+ import { Formatter } from '@tuya/tuya-panel-lamp-sdk'
8
+ import { DreamMusicDataType, dreamMusicData } from './MusicDataBean'
9
+ import { Result } from '@ledvance/base/src/models/modules/Result'
10
+ import { NativeApi } from '@ledvance/base/src/api/native'
11
+ const DreamMusicFormatter = new Formatter.DreamLightMicMusicFormatter()
12
+ export const useEnable = (dpKey: string) => {
13
+ return useDp(dpKey)
9
14
  }
10
15
 
11
- export const useWorkMode = (dpKey:string) =>{
12
- return useDp(dpKey)
16
+ export const useWorkMode = (dpKey: string) => {
17
+ return useDp(dpKey)
13
18
  }
14
19
 
15
- export const useMixRgbcw = (mixRgbcwDp:string, mixSwitchDp:string) =>{
20
+ export const useMixRgbcw = (mixRgbcwDp: string, mixSwitchDp: string) => {
16
21
 
17
- const [mixRgbcw, setMixRgbcw]: [string, (v:any) => void] = useDp(mixRgbcwDp)
18
- const [mixLightState, setMixLightState] = useState(mixRgbcwDp ? dp2Obj(mixRgbcw) : undefined)
19
- useUpdateEffect(() =>{
20
- const newMixLIghtState = mixRgbcwDp ? dp2Obj(mixRgbcw) : undefined
21
- setMixLightState(newMixLIghtState)
22
- }, [mixRgbcw])
22
+ const [mixRgbcw, setMixRgbcw]: [string, (v: any) => void] = useDp(mixRgbcwDp)
23
+ const [mixLightState, setMixLightState] = useState(mixRgbcwDp ? dp2Obj(mixRgbcw) : undefined)
24
+ useUpdateEffect(() => {
25
+ const newMixLIghtState = mixRgbcwDp ? dp2Obj(mixRgbcw) : undefined
26
+ setMixLightState(newMixLIghtState)
27
+ }, [mixRgbcw])
23
28
 
24
- const setMixRgbcwDp = async (mixLightBean: MixLightBean) =>{
25
- const dpValue = obj2Dp(mixLightBean)
26
- const enable = mixLightBean.whiteLightSwitch === mixLightBean.colorLightSwitch ? mixLightBean.whiteLightSwitch : undefined
27
- const dps: { [p: string]: any } = {
28
- [mixRgbcwDp]: dpValue
29
- }
30
- if(enable !== undefined){
31
- dps[mixSwitchDp] = enable
32
- }
33
- const controlResult: any = await setMixRgbcw(dps)
34
- return {
35
- result: { success: controlResult.result },
36
- dps: { [mixRgbcwDp]: dpValue },
37
- }
29
+ const setMixRgbcwDp = async (mixLightBean: MixLightBean) => {
30
+ const dpValue = obj2Dp(mixLightBean)
31
+ const enable = mixLightBean.whiteLightSwitch === mixLightBean.colorLightSwitch ? mixLightBean.whiteLightSwitch : undefined
32
+ const dps: { [p: string]: any } = {
33
+ [mixRgbcwDp]: dpValue
34
+ }
35
+ if (enable !== undefined) {
36
+ dps[mixSwitchDp] = enable
37
+ }
38
+ const controlResult: any = await setMixRgbcw(dps)
39
+ return {
40
+ result: { success: controlResult.result },
41
+ dps: { [mixRgbcwDp]: dpValue },
42
+ }
43
+ }
44
+ return [mixLightState, setMixRgbcwDp]
45
+ }
46
+
47
+ export const useMixScene = (mixSceneDp: string, mixWorkMode: string): any[] => {
48
+ const [mixScene, setMixScene]: [string, (v: any) => void] = useDp(mixSceneDp)
49
+ const [mixSceneState, setMixSceneState] = useState(mixSceneDp ? mixSceneDp2Obj(mixScene) : undefined)
50
+ useUpdateEffect(() => {
51
+ const newMixLIghtState = mixSceneDp ? mixSceneDp2Obj(mixScene) : undefined
52
+ setMixSceneState(newMixLIghtState)
53
+ }, [mixScene])
54
+
55
+ const setMixRgbcwDp = async (mixScene: MixSceneInfo) => {
56
+ const dpValue = mixSceneObj2Dp(mixScene)
57
+ const controlResult: any = await setMixScene(dpValue)
58
+ return {
59
+ result: { success: controlResult.result },
60
+ dps: { [mixWorkMode]: 'scene', [mixSceneDp]: dpValue },
38
61
  }
39
- return [mixLightState, setMixRgbcwDp]
62
+ }
63
+ return [mixSceneState, setMixRgbcwDp]
64
+ }
65
+
66
+ export const useMusicData = (dpKey: string): any[] => {
67
+ const [dps, setDps] = useDps()
68
+ const musicData = dps[dpKey]
69
+ const setMusicDp = async (data, extraDp = {}) => {
70
+ const musicDpData = ColorParser.encodeControlData(
71
+ data.mode,
72
+ data.hue,
73
+ data.saturation,
74
+ data.value,
75
+ data.brightness,
76
+ data.temperature
77
+ );
78
+ const sendData = {
79
+ [dpKey]: musicDpData,
80
+ ...extraDp
81
+ }
82
+ const controlResult: any = await setDps(sendData)
83
+ return {
84
+ result: { success: controlResult.result },
85
+ dps: { [dpKey]: musicDpData },
86
+ }
87
+ }
88
+ return [musicData, setMusicDp]
89
+ }
90
+ interface DreamLightMicMusicColor {
91
+ hue: number;
92
+ saturation: number;
93
+ }
94
+ export interface DreamLightMicMusicData {
95
+ v: number;
96
+ power: boolean;
97
+ id: number;
98
+ isLight: number;
99
+ mode: number;
100
+ speed: number;
101
+ sensitivity: number;
102
+ a: number;
103
+ b: number;
104
+ c: number;
105
+ brightness: number;
106
+ colors: DreamLightMicMusicColor[];
107
+ }
108
+
109
+ export interface DreamLightMicMusicUIState extends DreamLightMicMusicData {
110
+ title: string
40
111
  }
41
112
 
42
- export const useMixScene = (mixSceneDp:string, mixWorkMode:string) : any[] =>{
43
- const [mixScene, setMixScene]: [string, (v:any) => void] = useDp(mixSceneDp)
44
- const [mixSceneState, setMixSceneState] = useState(mixSceneDp ? mixSceneDp2Obj(mixScene) : undefined)
45
- useUpdateEffect(() =>{
46
- const newMixLIghtState = mixSceneDp ? mixSceneDp2Obj(mixScene) : undefined
47
- setMixSceneState(newMixLIghtState)
48
- }, [mixScene])
113
+ export const useDreamMusic = (dpKey: string): [DreamLightMicMusicData | "", (music: DreamLightMicMusicData, extraDp: Record<any,any>) => Promise<Result<any>>] => {
114
+ const [dps, setDps]: [any, (v: any) => Promise<Result<any>>] = useDps()
115
+ const dreamMusicDp: string = dps[dpKey]
116
+ const dreamMusic = dreamMusicDp && DreamMusicFormatter.parse(dreamMusicDp)
117
+ const setDreamMusicFn = (music: DreamLightMicMusicData, extraDp: Record<any,any> = {}) => {
118
+ const musicHex = DreamMusicFormatter.format(music)
119
+ return setDps({
120
+ [dpKey]: musicHex,
121
+ ...extraDp
122
+ })
123
+ }
124
+ return [dreamMusic, setDreamMusicFn]
125
+ }
49
126
 
50
- const setMixRgbcwDp = async (mixScene: MixSceneInfo) =>{
51
- const dpValue = mixSceneObj2Dp(mixScene)
52
- const controlResult: any = await setMixScene(dpValue)
127
+ export const getRemoteDreamMusic = async (devId: string) => {
128
+ const res = await NativeApi.getJson(devId, 'dreamlightmic_music_data')
129
+ if (res.success && res.data) {
130
+ return {
131
+ success: true,
132
+ data: JSON.parse(res.data)?.map(item => remoteDreamMusicUIState(item)),
133
+ }
134
+ } else {
135
+ if (res.msg?.includes('资源未找到')) {
136
+ const res = await NativeApi.putJson(devId, 'dreamlightmic_music_data', JSON.stringify(dreamMusicData))
137
+ if (res.success) {
53
138
  return {
54
- result: { success: controlResult.result },
55
- dps: { [mixWorkMode]: 'scene',[mixSceneDp]: dpValue },
139
+ success: true,
140
+ data: dreamMusicData.map(item => remoteDreamMusicUIState(item))
56
141
  }
142
+ }
143
+ return { success: false }
57
144
  }
58
- return [mixSceneState, setMixRgbcwDp]
59
- }
60
-
61
- export const useMusicData = (dpKey:string): any[] =>{
62
- const [musicData, setMusicData] = useDp(dpKey)
63
- const setMusicDp = async (data) =>{
64
- const musicDpData = ColorParser.encodeControlData(
65
- data.mode,
66
- data.hue,
67
- data.saturation,
68
- data.value,
69
- data.brightness,
70
- data.temperature
71
- );
72
- const controlResult: any = await setMusicData(musicDpData)
73
- return {
74
- result: { success: controlResult.result },
75
- dps: { [dpKey]: musicDpData},
76
- }
145
+ return { success: false }
146
+ }
147
+ }
148
+
149
+ const remoteDreamMusicUIState = (remoteMusic: DreamMusicDataType) => {
150
+ const musicInfo = DreamMusicFormatter.parse(remoteMusic.value)
151
+ return {
152
+ ...musicInfo,
153
+ title: remoteMusic.title
154
+ }
155
+ }
156
+
157
+ export function saveRemoteDreamMusic(devId: string, musics: DreamLightMicMusicData[]) {
158
+ const newMusic = musics.map(music => {
159
+ return {
160
+ ...music,
161
+ value: DreamMusicFormatter.format(music)
77
162
  }
78
- return [musicData, setMusicDp]
163
+ })
164
+ return NativeApi.putJson(devId, 'dreamlightmic_music_data', JSON.stringify(newMusic))
79
165
  }
80
166
 
81
167
 
82
168
  export interface MixSceneInfo {
83
- version: number
84
- id: any
85
- lamps: MixSceneLampInfo[]
169
+ version: number
170
+ id: any
171
+ lamps: MixSceneLampInfo[]
86
172
  }
87
173
 
88
174
  export interface MixSceneLampInfo {
89
- enable: boolean
90
- // 1:W 只有亮度,2:CW 亮度+色温,3:RGB HSV,4:RGBC HSV+色温,5:RGBCW HSV+色温+亮度
91
- type: number
92
- nodes: MixSceneNodeInfo[]
175
+ enable: boolean
176
+ // 1:W 只有亮度,2:CW 亮度+色温,3:RGB HSV,4:RGBC HSV+色温,5:RGBCW HSV+色温+亮度
177
+ type: number
178
+ nodes: MixSceneNodeInfo[]
93
179
  }
94
180
 
95
181
  export interface MixSceneNodeInfo {
96
- intervalTime: number
97
- changeTime: number
98
- changeType: number
99
- hue: number
100
- sat: number
101
- lightness: number
102
- brightness: number
103
- colorTemp: number
182
+ intervalTime: number
183
+ changeTime: number
184
+ changeType: number
185
+ hue: number
186
+ sat: number
187
+ lightness: number
188
+ brightness: number
189
+ colorTemp: number
104
190
  }
105
191
 
106
192
  /**
@@ -108,69 +194,69 @@ export interface MixSceneNodeInfo {
108
194
  * @returns MixSceneInfo
109
195
  */
110
196
  export function mixSceneDp2Obj(dp: string): MixSceneInfo | undefined {
111
- if (!(!!dp)) {
112
- return undefined
197
+ if (!(!!dp)) {
198
+ return undefined
199
+ }
200
+ const result = {} as MixSceneInfo
201
+ result.version = parseInt(dp.slice(0, 2), 16)
202
+ result.id = parseInt(dp.slice(2, 4), 16)
203
+ result.lamps = []
204
+ let lampsHex = dp.slice(4)
205
+ while (lampsHex.length != 0) {
206
+ const lamp = { nodes: [] as MixSceneNodeInfo[] } as MixSceneLampInfo
207
+ lamp.enable = parseInt(lampsHex.slice(0, 2), 16) === 1
208
+ lampsHex = lampsHex.slice(2)
209
+ const nodeCount = parseInt(lampsHex.slice(0, 2), 16)
210
+ lampsHex = lampsHex.slice(2)
211
+ if (nodeCount === 0) {
212
+ result.lamps.push(lamp)
213
+ continue
113
214
  }
114
- const result = {} as MixSceneInfo
115
- result.version = parseInt(dp.slice(0, 2), 16)
116
- result.id = parseInt(dp.slice(2, 4), 16)
117
- result.lamps = []
118
- let lampsHex = dp.slice(4)
119
- while (lampsHex.length != 0) {
120
- const lamp = { nodes: [] as MixSceneNodeInfo[] } as MixSceneLampInfo
121
- lamp.enable = parseInt(lampsHex.slice(0, 2), 16) === 1
122
- lampsHex = lampsHex.slice(2)
123
- const nodeCount = parseInt(lampsHex.slice(0, 2), 16)
124
- lampsHex = lampsHex.slice(2)
125
- if (nodeCount === 0) {
126
- result.lamps.push(lamp)
127
- continue
128
- }
129
- lamp.type = parseInt(lampsHex.slice(0, 2), 16)
130
- lampsHex = lampsHex.slice(2)
131
- for (let i = 0; i < nodeCount; i++) {
132
- const node = {} as MixSceneNodeInfo
133
- node.intervalTime = parseInt(lampsHex.slice(0, 2), 16)
134
- node.changeTime = parseInt(lampsHex.slice(2, 4), 16)
135
- node.changeType = parseInt(lampsHex.slice(4, 6), 16)
136
- lampsHex = lampsHex.slice(6)
137
- switch (lamp.type) {
138
- case 1:
139
- node.brightness = parseInt(lampsHex.slice(0, 4), 16) / 10
140
- lampsHex = lampsHex.slice(4)
141
- break
142
- case 2:
143
- node.brightness = parseInt(lampsHex.slice(0, 4), 16) / 10
144
- node.colorTemp = parseInt(lampsHex.slice(4, 8), 16) / 10
145
- lampsHex = lampsHex.slice(8)
146
- break
147
- case 3:
148
- node.hue = parseInt(lampsHex.slice(0, 4), 16)
149
- node.sat = parseInt(lampsHex.slice(4, 8), 16) / 10
150
- node.lightness = parseInt(lampsHex.slice(8, 12), 16) / 10
151
- lampsHex = lampsHex.slice(12)
152
- break
153
- case 4:
154
- node.hue = parseInt(lampsHex.slice(0, 4), 16)
155
- node.sat = parseInt(lampsHex.slice(4, 8), 16) / 10
156
- node.lightness = parseInt(lampsHex.slice(8, 12), 16) / 10
157
- node.colorTemp = parseInt(lampsHex.slice(12, 16), 16) / 10
158
- lampsHex = lampsHex.slice(16)
159
- break
160
- case 5:
161
- node.hue = parseInt(lampsHex.slice(0, 4), 16)
162
- node.sat = parseInt(lampsHex.slice(4, 8), 16) / 10
163
- node.lightness = parseInt(lampsHex.slice(8, 12), 16) / 10
164
- node.brightness = parseInt(lampsHex.slice(12, 16), 16) / 10
165
- node.colorTemp = parseInt(lampsHex.slice(16, 20), 16) / 10
166
- lampsHex = lampsHex.slice(20)
167
- break
168
- }
169
- lamp.nodes.push(node)
170
- }
171
- result.lamps.push(lamp)
215
+ lamp.type = parseInt(lampsHex.slice(0, 2), 16)
216
+ lampsHex = lampsHex.slice(2)
217
+ for (let i = 0; i < nodeCount; i++) {
218
+ const node = {} as MixSceneNodeInfo
219
+ node.intervalTime = parseInt(lampsHex.slice(0, 2), 16)
220
+ node.changeTime = parseInt(lampsHex.slice(2, 4), 16)
221
+ node.changeType = parseInt(lampsHex.slice(4, 6), 16)
222
+ lampsHex = lampsHex.slice(6)
223
+ switch (lamp.type) {
224
+ case 1:
225
+ node.brightness = parseInt(lampsHex.slice(0, 4), 16) / 10
226
+ lampsHex = lampsHex.slice(4)
227
+ break
228
+ case 2:
229
+ node.brightness = parseInt(lampsHex.slice(0, 4), 16) / 10
230
+ node.colorTemp = parseInt(lampsHex.slice(4, 8), 16) / 10
231
+ lampsHex = lampsHex.slice(8)
232
+ break
233
+ case 3:
234
+ node.hue = parseInt(lampsHex.slice(0, 4), 16)
235
+ node.sat = parseInt(lampsHex.slice(4, 8), 16) / 10
236
+ node.lightness = parseInt(lampsHex.slice(8, 12), 16) / 10
237
+ lampsHex = lampsHex.slice(12)
238
+ break
239
+ case 4:
240
+ node.hue = parseInt(lampsHex.slice(0, 4), 16)
241
+ node.sat = parseInt(lampsHex.slice(4, 8), 16) / 10
242
+ node.lightness = parseInt(lampsHex.slice(8, 12), 16) / 10
243
+ node.colorTemp = parseInt(lampsHex.slice(12, 16), 16) / 10
244
+ lampsHex = lampsHex.slice(16)
245
+ break
246
+ case 5:
247
+ node.hue = parseInt(lampsHex.slice(0, 4), 16)
248
+ node.sat = parseInt(lampsHex.slice(4, 8), 16) / 10
249
+ node.lightness = parseInt(lampsHex.slice(8, 12), 16) / 10
250
+ node.brightness = parseInt(lampsHex.slice(12, 16), 16) / 10
251
+ node.colorTemp = parseInt(lampsHex.slice(16, 20), 16) / 10
252
+ lampsHex = lampsHex.slice(20)
253
+ break
254
+ }
255
+ lamp.nodes.push(node)
172
256
  }
173
- return result
257
+ result.lamps.push(lamp)
258
+ }
259
+ return result
174
260
  }
175
261
 
176
262
  /**
@@ -178,52 +264,52 @@ export function mixSceneDp2Obj(dp: string): MixSceneInfo | undefined {
178
264
  * @param obj
179
265
  */
180
266
  export function mixSceneObj2Dp(obj: MixSceneInfo) {
181
- const versionHex = obj.version.toString(16).padStart(2, '0')
182
- const sceneNoHex = obj.id.toString(16).padStart(2, '0')
183
- const lampsHex = obj.lamps.map(lamp => {
184
- let lampHex = lamp.enable ? '01' : '00'
185
- const nodes = lamp.nodes || []
186
- lampHex += nodes.length.toString(16).padStart(2, '0')
187
- if (nodes.length === 0 || !lamp.enable) {
188
- return lampHex
189
- }
190
- lampHex += lamp.type.toString(16).padStart(2, '0')
191
- lampHex += nodes.map(node => {
192
- let nodeHex = ''
193
- nodeHex += node.intervalTime.toString(16).padStart(2, '0')
194
- nodeHex += node.changeTime.toString(16).padStart(2, '0')
195
- nodeHex += node.changeType.toString(16).padStart(2, '0')
196
- switch (lamp.type) { // 1:W 只有亮度,2:CW 亮度+色温,3:RGB HSV,4:RGBC HSV+色温,5:RGBCW HSV+色温+亮度
197
- case 1:
198
- nodeHex += (node.brightness * 10).toString(16).padStart(4, '0')
199
- break
200
- case 2:
201
- nodeHex += (node.brightness * 10).toString(16).padStart(4, '0')
202
- nodeHex += (node.colorTemp * 10).toString(16).padStart(4, '0')
203
- break
204
- case 3:
205
- nodeHex += node.hue.toString(16).padStart(4, '0')
206
- nodeHex += (node.sat * 10).toString(16).padStart(4, '0')
207
- nodeHex += (node.lightness * 10).toString(16).padStart(4, '0')
208
- break
209
- case 4:
210
- nodeHex += node.hue.toString(16).padStart(4, '0')
211
- nodeHex += (node.sat * 10).toString(16).padStart(4, '0')
212
- nodeHex += (node.lightness * 10).toString(16).padStart(4, '0')
213
- nodeHex += (node.colorTemp * 10).toString(16).padStart(4, '0')
214
- break
215
- case 5:
216
- nodeHex += node.hue.toString(16).padStart(4, '0')
217
- nodeHex += (node.sat * 10).toString(16).padStart(4, '0')
218
- nodeHex += (node.lightness * 10).toString(16).padStart(4, '0')
219
- nodeHex += (node.brightness * 10).toString(16).padStart(4, '0')
220
- nodeHex += (node.colorTemp * 10).toString(16).padStart(4, '0')
221
- break
222
- }
223
- return nodeHex
224
- }).join('')
225
- return lampHex
267
+ const versionHex = obj.version.toString(16).padStart(2, '0')
268
+ const sceneNoHex = obj.id.toString(16).padStart(2, '0')
269
+ const lampsHex = obj.lamps.map(lamp => {
270
+ let lampHex = lamp.enable ? '01' : '00'
271
+ const nodes = lamp.nodes || []
272
+ lampHex += nodes.length.toString(16).padStart(2, '0')
273
+ if (nodes.length === 0 || !lamp.enable) {
274
+ return lampHex
275
+ }
276
+ lampHex += lamp.type.toString(16).padStart(2, '0')
277
+ lampHex += nodes.map(node => {
278
+ let nodeHex = ''
279
+ nodeHex += node.intervalTime.toString(16).padStart(2, '0')
280
+ nodeHex += node.changeTime.toString(16).padStart(2, '0')
281
+ nodeHex += node.changeType.toString(16).padStart(2, '0')
282
+ switch (lamp.type) { // 1:W 只有亮度,2:CW 亮度+色温,3:RGB HSV,4:RGBC HSV+色温,5:RGBCW HSV+色温+亮度
283
+ case 1:
284
+ nodeHex += (node.brightness * 10).toString(16).padStart(4, '0')
285
+ break
286
+ case 2:
287
+ nodeHex += (node.brightness * 10).toString(16).padStart(4, '0')
288
+ nodeHex += (node.colorTemp * 10).toString(16).padStart(4, '0')
289
+ break
290
+ case 3:
291
+ nodeHex += node.hue.toString(16).padStart(4, '0')
292
+ nodeHex += (node.sat * 10).toString(16).padStart(4, '0')
293
+ nodeHex += (node.lightness * 10).toString(16).padStart(4, '0')
294
+ break
295
+ case 4:
296
+ nodeHex += node.hue.toString(16).padStart(4, '0')
297
+ nodeHex += (node.sat * 10).toString(16).padStart(4, '0')
298
+ nodeHex += (node.lightness * 10).toString(16).padStart(4, '0')
299
+ nodeHex += (node.colorTemp * 10).toString(16).padStart(4, '0')
300
+ break
301
+ case 5:
302
+ nodeHex += node.hue.toString(16).padStart(4, '0')
303
+ nodeHex += (node.sat * 10).toString(16).padStart(4, '0')
304
+ nodeHex += (node.lightness * 10).toString(16).padStart(4, '0')
305
+ nodeHex += (node.brightness * 10).toString(16).padStart(4, '0')
306
+ nodeHex += (node.colorTemp * 10).toString(16).padStart(4, '0')
307
+ break
308
+ }
309
+ return nodeHex
226
310
  }).join('')
227
- return versionHex + sceneNoHex + lampsHex
311
+ return lampHex
312
+ }).join('')
313
+ return versionHex + sceneNoHex + lampsHex
228
314
  }
229
315