@ledvance/ui-biz-bundle 1.1.68 → 1.1.70

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,374 +1,425 @@
1
- import {
2
- BiorhythmBean,
3
- BiorhythmGradientType,
4
- BiorhythmGradientTypeMap,
5
- BiorhythmGradientTypeMap2,
6
- colorTemperatureValue,
7
- colorTempPercent,
8
- getDefRepeatPeriod,
9
- Period,
10
- Plan,
11
- RemoteBiorhythmBean,
12
- } from './BiorhythmBean'
13
- import { useCallback, useEffect, useState } from 'react'
14
- import { NativeResult, Result } from '@ledvance/base/src/models/modules/Result'
15
- import { useDeviceId, useDp } from '@ledvance/base/src/models/modules/NativePropsSlice'
16
- import { getFeature, NativeApi, putFeature } from '@ledvance/base/src/api/native'
17
- import { hex2Int, spliceByStep } from '@ledvance/base/src/utils/common'
18
- import I18n from '@ledvance/base/src/i18n'
19
- import BiologicalRes from './res/BiologicalRes'
20
- import dayjs from 'dayjs'
21
- import { to16 } from '@tuya/tuya-panel-lamp-sdk/lib/utils'
22
- import { useUpdateEffect } from 'ahooks'
23
-
24
- type UseBiorhythmType = (dpKey: string, disabledFeature?: boolean) => [BiorhythmBean, SetBiorhythmType];
25
- type SetBiorhythmType = (biorhythmObj: BiorhythmBean, pushFeature?: boolean) => Promise<Result<any>>;
26
- let biorhythmTimer: undefined | number = undefined
27
-
28
- export const useBiorhythm: UseBiorhythmType = (dpKey: string, disabledFeature?: boolean) => {
29
- const [dp, setDp] = useDp<string, (v: string) => any>(dpKey)
30
- const deviceId = useDeviceId()
31
- const [biorhythmState, setBiorhythmState] = useState(dp2Obj(dp))
32
-
33
- const getBiorhythm = useCallback(() =>{
34
- const biorhythm = dp2Obj(dp)
35
- getRemoteBiorhythm(deviceId, biorhythm).then(res => {
36
- if (res.success && res.data) {
37
- setBiorhythmState(dto2Vo({
38
- ...res.data,
39
- enable: biorhythm.enable,
40
- repeatPeriod: biorhythm.repeatPeriod.filter(r => r.enabled).map(r => r.index),
41
- gradientWay: BiorhythmGradientTypeMap[biorhythm.gradient]
42
- }))
43
- }
44
- })
45
- }, [dp])
46
-
47
- useEffect(() => {
48
- if(disabledFeature) return
49
- biorhythmTimer = setTimeout(() =>{
50
- getBiorhythm()
51
- }, 250)
52
-
53
- return () =>{
54
- clearTimeout(biorhythmTimer)
55
- }
56
- }, [])
57
-
58
- useUpdateEffect(() =>{
59
- getBiorhythm()
60
- }, [dp])
61
-
62
- const setBiorhythmFn = async (biorhythmObj: BiorhythmBean, pushFeature: boolean = true) =>{
63
- const dpValue = obj2Dp(biorhythmObj)
64
- if(pushFeature){
65
- const putFeatureRes = await putFeature(deviceId, biorhythmFeatureId, vo2Dto(biorhythmObj))
66
- if (putFeatureRes.result) {
67
- return setDp(dpValue)
68
- }
69
- }else{
70
- return setDp(dpValue)
71
- }
72
- return { success: false }
73
- }
74
- return [biorhythmState, setBiorhythmFn]
75
- }
76
-
77
- const biorhythmFeatureId = 'Biorhythm'
78
-
79
- async function getRemoteBiorhythm(deviceId: string, defBiorhythmState: BiorhythmBean): Promise<Result<RemoteBiorhythmBean>> {
80
- const res: NativeResult<RemoteBiorhythmBean> = await getFeature(deviceId, biorhythmFeatureId)
81
- if (!res.data) {
82
- const defData = vo2Dto(defBiorhythmState)
83
- const pushRes = await putFeature(deviceId, biorhythmFeatureId, defData)
84
- if (pushRes.result) {
85
- return getRemoteBiorhythm(deviceId, defBiorhythmState)
86
- }
87
- return {
88
- success: false,
89
- msg: pushRes.msg,
90
- }
91
- }
92
- return {
93
- success: true,
94
- data: res.data,
95
- }
96
- }
97
-
98
- export const userOperation = async (deviceId: string, value: boolean) => {
99
- const currentTime = dayjs().format('YYYY-MM-DD')
100
- const json = JSON.stringify({ status: value, suspendTime: value && currentTime || '' })
101
- return await NativeApi.putJson(deviceId, 'suspendTime', json)
102
- }
103
-
104
- export function dp2Obj(dp: string): BiorhythmBean {
105
- if (!dp || dp === '0000000000') {
106
- return dp2Obj('0000007f0501060000000000000001061e00000000141901090000000000646401140000000000503201171e000000000000')
107
- }
108
- let dpCopy = dp
109
- // 版本
110
- hex2Int(dpCopy.slice(0, 2))
111
- dpCopy = dpCopy.slice(2)
112
- // 开关
113
- const enable = hex2Int(dpCopy.slice(0, 2)) === 1
114
- dpCopy = dpCopy.slice(2)
115
- // 模式:00 全程渐变,0F 直接渐变
116
- const gradient =
117
- dpCopy.slice(0, 2).toLowerCase() === '00'
118
- ? BiorhythmGradientType.EntireGradient
119
- : BiorhythmGradientType.DirectGradient
120
- dpCopy = dpCopy.slice(2)
121
- // 重复周期
122
- const repeatPeriod: Period[] = hex2Int(dpCopy.slice(0, 2))
123
- .toString(2)
124
- .padStart(7, '0')
125
- .split('')
126
- .reverse()
127
- .map((p, index) => {
128
- return {
129
- index: index === 0 ? 7 : index,
130
- title: getRepeatPeriodTitleByIndex(index),
131
- enabled: p === '1',
132
- }
133
- })
134
- dpCopy = dpCopy.slice(2)
135
- // 节点个数 (每个节点长度18),最多8个节点
136
- hex2Int(dpCopy.slice(0, 2))
137
- dpCopy = dpCopy.slice(2)
138
- // 节点列表
139
- const planList: Plan[] = dpCopy === '00' ? [] : spliceByStep(dpCopy, 18).map((planHex, index) => {
140
- let hex = planHex
141
- // 节点开关
142
- const enable = hex2Int(hex.slice(0, 2)) === 1
143
- hex = hex.slice(2)
144
- // 小时
145
- const hour = hex2Int(hex.slice(0, 2))
146
- hex = hex.slice(2)
147
- // 分钟
148
- const minute = hex2Int(hex.slice(0, 2))
149
- hex = hex.slice(2)
150
- const time = hour * 60 + minute
151
- // 色调 (色相)
152
- hex2Int(hex.slice(0, 4))
153
- hex = hex.slice(4)
154
- // 饱和度
155
- hex2Int(hex.slice(0, 2))
156
- hex = hex.slice(2)
157
- // 明度
158
- hex2Int(hex.slice(0, 2))
159
- hex = hex.slice(2)
160
- // 亮度
161
- const brightness = hex2Int(hex.slice(0, 2))
162
- hex = hex.slice(2)
163
- // 色温
164
- const colorTemperature = hex2Int(hex.slice(0, 2))
165
- return {
166
- index,
167
- enable,
168
- icon: getNodeIconByIndex(index).icon,
169
- iconId: getNodeIconByIndex(index).iconId,
170
- time,
171
- name: getNodeNameByIndex(index),
172
- colorTemperature,
173
- brightness,
174
- action: [
175
- {
176
- uri: 'model/attribute/set/LightCtrl/ColorTemperature',
177
- startValue: `${colorTemperatureValue(colorTemperature)}`,
178
- },
179
- {
180
- uri: 'model/attribute/set/LightCtrl/Brightness',
181
- startValue: `${brightness}`,
182
- },
183
- ],
184
- }
185
- })
186
- return {
187
- enable: enable,
188
- gradient: gradient,
189
- repeatPeriod: repeatPeriod,
190
- planList: planList,
191
- }
192
- }
193
-
194
- function getNodeNameByIndex(index: number): string {
195
- switch (index) {
196
- case 0:
197
- return I18n.getLang('bio_ryhthm_default_field_text')
198
- case 1:
199
- return I18n.getLang('bio_ryhthm_default_field_text2')
200
- case 2:
201
- return I18n.getLang('bio_ryhthm_default_field_text3')
202
- case 3:
203
- return I18n.getLang('bio_ryhthm_default_field_text4')
204
- case 4:
205
- return I18n.getLang('bio_ryhthm_default_field_text5')
206
- default:
207
- return `Node ${index + 1}`
208
- }
209
- }
210
-
211
- interface NodeIcon {
212
- icon: string
213
- iconId: number
214
- }
215
-
216
- function getNodeIconByIndex(index: number): NodeIcon {
217
- switch (index) {
218
- case 0:
219
- return {
220
- icon: BiologicalRes.rhythm_icon1,
221
- iconId: 1
222
- }
223
- case 1:
224
- return {
225
- icon: BiologicalRes.rhythm_icon2,
226
- iconId: 2
227
- }
228
- case 2:
229
- return {
230
- icon: BiologicalRes.rhythm_icon3,
231
- iconId: 9
232
- }
233
- case 3:
234
- return {
235
- icon: BiologicalRes.rhythm_icon4,
236
- iconId: 3
237
- }
238
- case 4:
239
- return {
240
- icon: BiologicalRes.rhythm_icon12,
241
- iconId: 5
242
- }
243
- case 5:
244
- return {
245
- icon: BiologicalRes.rhythm_icon1,
246
- iconId: 1
247
- }
248
- case 6:
249
- return {
250
- icon: BiologicalRes.rhythm_icon2,
251
- iconId: 2
252
- }
253
- case 7:
254
- return {
255
- icon: BiologicalRes.rhythm_icon3,
256
- iconId: 9
257
- }
258
- default:
259
- return {
260
- icon: BiologicalRes.rhythm_icon1,
261
- iconId: 1
262
- }
263
- }
264
- }
265
-
266
- function getRepeatPeriodTitleByIndex(index: number): string {
267
- let title = ''
268
- switch (index) {
269
- case 0:
270
- title = I18n.getLang('bio_ryhthm_default_weekday7_text')
271
- break
272
- case 1:
273
- title = I18n.getLang('bio_ryhthm_default_weekday1_text')
274
- break
275
- case 2:
276
- title = I18n.getLang('bio_ryhthm_default_weekday2_text')
277
- break
278
- case 3:
279
- title = I18n.getLang('bio_ryhthm_default_weekday3_text')
280
- break
281
- case 4:
282
- title = I18n.getLang('bio_ryhthm_default_weekday4_text')
283
- break
284
- case 5:
285
- title = I18n.getLang('bio_ryhthm_default_weekday5_text')
286
- break
287
- case 6:
288
- title = I18n.getLang('bio_ryhthm_default_weekday6_text')
289
- break
290
- }
291
- return title
292
- }
293
-
294
- function obj2Dp(obj: BiorhythmBean): string {
295
- const versionHex = '00'
296
- const enableHex = obj.enable ? '01' : '00'
297
- const gradientHex = obj.gradient === BiorhythmGradientType.EntireGradient ? '00' : '0F'
298
- const repeatPeriodHex = parseInt(
299
- obj.repeatPeriod
300
- .map(p => (p.enabled ? '1' : '0'))
301
- .reverse()
302
- .join(''),
303
- 2,
304
- )
305
- .toString(16)
306
- .padStart(2, '0')
307
- const planCountHex = obj.planList.length.toString(16).padStart(2, '0')
308
- const planListHex = obj.planList
309
- .map(plan => {
310
- const enableHex = plan.enable ? '01' : '00'
311
- const hourHex = to16(Math.trunc(plan.time / 60))
312
- const minuteHex = to16(plan.time % 60)
313
- const hsvHex = '00000000'
314
- const brightnessHex = plan.brightness.toString(16).padStart(2, '0')
315
- const colorTemperatureHex = plan.colorTemperature.toString(16).padStart(2, '0')
316
- return enableHex + hourHex + minuteHex + hsvHex + brightnessHex + colorTemperatureHex
317
- })
318
- .join('')
319
- return versionHex + enableHex + gradientHex + repeatPeriodHex + planCountHex + planListHex
320
- }
321
-
322
- /**
323
- * BiorhythmBean 转 RemoteBiorhythmBean
324
- */
325
- function vo2Dto(biorhythmBean: BiorhythmBean): RemoteBiorhythmBean {
326
- return {
327
- enable: biorhythmBean.enable,
328
- repeatPeriod: biorhythmBean.repeatPeriod.filter(r => r.enabled).map(r => r.index),
329
- gradientWay: BiorhythmGradientTypeMap[biorhythmBean.gradient],
330
- rhythmPlan: biorhythmBean.planList.map(plan => {
331
- const hour = Math.trunc(plan.time / 60).toString().padStart(2, '0')
332
- const min = (plan.time % 60).toString().padStart(2, '0')
333
- return {
334
- name: plan.name,
335
- rhythmIcon: plan.icon,
336
- enable: plan.enable,
337
- startTime: `${hour}:${min}`,
338
- sustain: plan.index, // 持续时间涂鸦这边没有,直接用来存ID
339
- action: plan.action,
340
- iconId: plan.iconId,
341
- }
342
- }),
343
- }
344
- }
345
-
346
- /**
347
- * RemoteBiorhythmBean BiorhythmBean
348
- */
349
- function dto2Vo(remoteBiorhythmBean: RemoteBiorhythmBean): BiorhythmBean {
350
- return {
351
- enable: remoteBiorhythmBean.enable,
352
- repeatPeriod: getDefRepeatPeriod().map(repeatItem => {
353
- return {
354
- ...repeatItem,
355
- enabled: remoteBiorhythmBean.repeatPeriod.includes(repeatItem.index),
356
- }
357
- }),
358
- gradient: BiorhythmGradientTypeMap2[remoteBiorhythmBean.gradientWay],
359
- planList: remoteBiorhythmBean.rhythmPlan.map(plan => {
360
- const startTime = plan.startTime.split(':')
361
- return {
362
- index: plan.sustain,
363
- enable: plan.enable,
364
- icon: plan.rhythmIcon,
365
- time: Number(startTime[0]) * 60 + Number(startTime[1]),
366
- name: plan.name,
367
- colorTemperature: colorTempPercent(parseInt(plan.action[0].startValue)),
368
- brightness: parseInt(plan.action[1].startValue),
369
- action: plan.action,
370
- iconId: plan.iconId,
371
- }
372
- }),
373
- }
374
- }
1
+ import {
2
+ BiorhythmBean,
3
+ BiorhythmGradientType,
4
+ BiorhythmGradientTypeMap,
5
+ BiorhythmGradientTypeMap2,
6
+ colorTemperatureValue,
7
+ colorTempPercent,
8
+ getDefRepeatPeriod,
9
+ Period,
10
+ Plan,
11
+ RemoteBiorhythmBean,
12
+ } from './BiorhythmBean'
13
+ import { useCallback, useEffect, useState } from 'react'
14
+ import { NativeResult, Result } from '@ledvance/base/src/models/modules/Result'
15
+ import { useDeviceId, useDp } from '@ledvance/base/src/models/modules/NativePropsSlice'
16
+ import { getFeature, NativeApi, putFeature } from '@ledvance/base/src/api/native'
17
+ import { hex2Int, spliceByStep } from '@ledvance/base/src/utils/common'
18
+ import I18n from '@ledvance/base/src/i18n'
19
+ import BiologicalRes from './res/BiologicalRes'
20
+ import dayjs from 'dayjs'
21
+ import { to16 } from '@tuya/tuya-panel-lamp-sdk/lib/utils'
22
+ import { useUpdateEffect } from 'ahooks'
23
+
24
+ type UseBiorhythmType = (dpKey: string, disabledFeature?: boolean) => [BiorhythmBean, SetBiorhythmType];
25
+ type SetBiorhythmType = (biorhythmObj: BiorhythmBean, pushFeature?: boolean) => Promise<Result<any>>;
26
+ let biorhythmTimer: undefined | number = undefined
27
+
28
+ export const useBiorhythm: UseBiorhythmType = (dpKey: string, disabledFeature?: boolean) => {
29
+ const [dp, setDp] = useDp<string, (v: string) => any>(dpKey)
30
+ const deviceId = useDeviceId()
31
+ const [biorhythmState, setBiorhythmState] = useState(dp2Obj(dp))
32
+
33
+ const getBiorhythm = useCallback(() => {
34
+ const biorhythm = dp2Obj(dp)
35
+ getRemoteBiorhythm(deviceId, biorhythm).then(res => {
36
+ if (res.success && res.data) {
37
+ setBiorhythmState(dto2Vo({
38
+ ...res.data,
39
+ enable: biorhythm.enable,
40
+ repeatPeriod: biorhythm.repeatPeriod.filter(r => r.enabled).map(r => r.index),
41
+ gradientWay: BiorhythmGradientTypeMap[biorhythm.gradient]
42
+ }))
43
+ }
44
+ })
45
+ }, [dp])
46
+
47
+ useEffect(() => {
48
+ if (disabledFeature) return
49
+ biorhythmTimer = setTimeout(() => {
50
+ getBiorhythm()
51
+ }, 250)
52
+
53
+ return () => {
54
+ clearTimeout(biorhythmTimer)
55
+ }
56
+ }, [])
57
+
58
+ useUpdateEffect(() => {
59
+ getBiorhythm()
60
+ }, [dp])
61
+
62
+ const setBiorhythmFn = async (biorhythmObj: BiorhythmBean, pushFeature: boolean = true) => {
63
+ const dpValue = obj2Dp(biorhythmObj)
64
+ if (pushFeature) {
65
+ const putFeatureRes = await putFeature(deviceId, biorhythmFeatureId, vo2Dto(biorhythmObj))
66
+ if (putFeatureRes.result) {
67
+ return setDp(dpValue)
68
+ }
69
+ } else {
70
+ return setDp(dpValue)
71
+ }
72
+ return { success: false }
73
+ }
74
+ return [biorhythmState, setBiorhythmFn]
75
+ }
76
+
77
+ const biorhythmFeatureId = 'Biorhythm'
78
+
79
+ async function getRemoteBiorhythm(deviceId: string, defBiorhythmState: BiorhythmBean): Promise<Result<RemoteBiorhythmBean>> {
80
+ const res: NativeResult<RemoteBiorhythmBean> = await getFeature(deviceId, biorhythmFeatureId)
81
+ if (!res.data) {
82
+ const defData = vo2Dto(defBiorhythmState)
83
+ const pushRes = await putFeature(deviceId, biorhythmFeatureId, defData)
84
+ if (pushRes.result) {
85
+ return getRemoteBiorhythm(deviceId, defBiorhythmState)
86
+ }
87
+ return {
88
+ success: false,
89
+ msg: pushRes.msg,
90
+ }
91
+ }
92
+ return {
93
+ success: true,
94
+ data: res.data,
95
+ }
96
+ }
97
+
98
+ interface RhythmModeSuspendState {
99
+ status: boolean
100
+ suspendTime: string
101
+ }
102
+
103
+ export const useRhythmSuspend = (dp: string): [boolean, () => void, (v: string | undefined) => Promise<Result<any>>] => {
104
+ const [suspendTime, setSuspendTime] = useState<string | undefined>()
105
+ const [biorhythm] = useBiorhythm(dp, true)
106
+ const devId = useDeviceId()
107
+ const currentTime = dayjs().format('YYYY-MM-DD')
108
+
109
+ useEffect(() => {
110
+ getSuspendTime()
111
+ }, [])
112
+
113
+ useUpdateEffect(() => {
114
+ if (biorhythm.enable) {
115
+ if (suspendTime !== currentTime) {
116
+ getSuspendTime()
117
+ }
118
+ } else {
119
+ if (suspendTime !== undefined) {
120
+ setSuspendTime(undefined)
121
+ putSuspendTime(undefined)
122
+ }
123
+ }
124
+ }, [biorhythm.enable, suspendTime])
125
+
126
+ const getSuspendTime = useCallback(() => {
127
+ if (suspendTime !== currentTime) {
128
+ NativeApi.getJson(devId, 'suspendTime').then(res => {
129
+ if (res.success && res.data) {
130
+ const suspend = JSON.parse(res.data) as RhythmModeSuspendState
131
+ setSuspendTime(suspend.suspendTime)
132
+ }
133
+ })
134
+ }
135
+ }, [suspendTime])
136
+
137
+ const putSuspendTime = async (tiem?: string) => {
138
+ return await NativeApi.putJson(devId, 'suspendTime', JSON.stringify({ status: true, suspendTime: tiem }))
139
+ }
140
+
141
+ const setRhythSuspend = useCallback(async (time: string | undefined) => {
142
+ if (time === suspendTime) return { success: true }
143
+ if (!biorhythm.enable) {
144
+ setSuspendTime(undefined)
145
+ return await putSuspendTime(undefined)
146
+ }
147
+ const res = await putSuspendTime(time)
148
+ setSuspendTime(time)
149
+ return res
150
+ }, [suspendTime, biorhythm.enable])
151
+
152
+ return [suspendTime === currentTime, getSuspendTime, setRhythSuspend]
153
+ }
154
+
155
+ export function dp2Obj(dp: string): BiorhythmBean {
156
+ if (!dp || dp === '0000000000') {
157
+ return dp2Obj('0000007f0501060000000000000001061e00000000141901090000000000646401140000000000503201171e000000000000')
158
+ }
159
+ let dpCopy = dp
160
+ // 版本
161
+ hex2Int(dpCopy.slice(0, 2))
162
+ dpCopy = dpCopy.slice(2)
163
+ // 开关
164
+ const enable = hex2Int(dpCopy.slice(0, 2)) === 1
165
+ dpCopy = dpCopy.slice(2)
166
+ // 模式:00 全程渐变,0F 直接渐变
167
+ const gradient =
168
+ dpCopy.slice(0, 2).toLowerCase() === '00'
169
+ ? BiorhythmGradientType.EntireGradient
170
+ : BiorhythmGradientType.DirectGradient
171
+ dpCopy = dpCopy.slice(2)
172
+ // 重复周期
173
+ const repeatPeriod: Period[] = hex2Int(dpCopy.slice(0, 2))
174
+ .toString(2)
175
+ .padStart(7, '0')
176
+ .split('')
177
+ .reverse()
178
+ .map((p, index) => {
179
+ return {
180
+ index: index === 0 ? 7 : index,
181
+ title: getRepeatPeriodTitleByIndex(index),
182
+ enabled: p === '1',
183
+ }
184
+ })
185
+ dpCopy = dpCopy.slice(2)
186
+ // 节点个数 (每个节点长度18),最多8个节点
187
+ hex2Int(dpCopy.slice(0, 2))
188
+ dpCopy = dpCopy.slice(2)
189
+ // 节点列表
190
+ const planList: Plan[] = dpCopy === '00' ? [] : spliceByStep(dpCopy, 18).map((planHex, index) => {
191
+ let hex = planHex
192
+ // 节点开关
193
+ const enable = hex2Int(hex.slice(0, 2)) === 1
194
+ hex = hex.slice(2)
195
+ // 小时
196
+ const hour = hex2Int(hex.slice(0, 2))
197
+ hex = hex.slice(2)
198
+ // 分钟
199
+ const minute = hex2Int(hex.slice(0, 2))
200
+ hex = hex.slice(2)
201
+ const time = hour * 60 + minute
202
+ // 色调 (色相)
203
+ hex2Int(hex.slice(0, 4))
204
+ hex = hex.slice(4)
205
+ // 饱和度
206
+ hex2Int(hex.slice(0, 2))
207
+ hex = hex.slice(2)
208
+ // 明度
209
+ hex2Int(hex.slice(0, 2))
210
+ hex = hex.slice(2)
211
+ // 亮度
212
+ const brightness = hex2Int(hex.slice(0, 2))
213
+ hex = hex.slice(2)
214
+ // 色温
215
+ const colorTemperature = hex2Int(hex.slice(0, 2))
216
+ return {
217
+ index,
218
+ enable,
219
+ icon: getNodeIconByIndex(index).icon,
220
+ iconId: getNodeIconByIndex(index).iconId,
221
+ time,
222
+ name: getNodeNameByIndex(index),
223
+ colorTemperature,
224
+ brightness,
225
+ action: [
226
+ {
227
+ uri: 'model/attribute/set/LightCtrl/ColorTemperature',
228
+ startValue: `${colorTemperatureValue(colorTemperature)}`,
229
+ },
230
+ {
231
+ uri: 'model/attribute/set/LightCtrl/Brightness',
232
+ startValue: `${brightness}`,
233
+ },
234
+ ],
235
+ }
236
+ })
237
+ return {
238
+ enable: enable,
239
+ gradient: gradient,
240
+ repeatPeriod: repeatPeriod,
241
+ planList: planList,
242
+ }
243
+ }
244
+
245
+ function getNodeNameByIndex(index: number): string {
246
+ switch (index) {
247
+ case 0:
248
+ return I18n.getLang('bio_ryhthm_default_field_text')
249
+ case 1:
250
+ return I18n.getLang('bio_ryhthm_default_field_text2')
251
+ case 2:
252
+ return I18n.getLang('bio_ryhthm_default_field_text3')
253
+ case 3:
254
+ return I18n.getLang('bio_ryhthm_default_field_text4')
255
+ case 4:
256
+ return I18n.getLang('bio_ryhthm_default_field_text5')
257
+ default:
258
+ return `Node ${index + 1}`
259
+ }
260
+ }
261
+
262
+ interface NodeIcon {
263
+ icon: string
264
+ iconId: number
265
+ }
266
+
267
+ function getNodeIconByIndex(index: number): NodeIcon {
268
+ switch (index) {
269
+ case 0:
270
+ return {
271
+ icon: BiologicalRes.rhythm_icon1,
272
+ iconId: 1
273
+ }
274
+ case 1:
275
+ return {
276
+ icon: BiologicalRes.rhythm_icon2,
277
+ iconId: 2
278
+ }
279
+ case 2:
280
+ return {
281
+ icon: BiologicalRes.rhythm_icon3,
282
+ iconId: 9
283
+ }
284
+ case 3:
285
+ return {
286
+ icon: BiologicalRes.rhythm_icon4,
287
+ iconId: 3
288
+ }
289
+ case 4:
290
+ return {
291
+ icon: BiologicalRes.rhythm_icon12,
292
+ iconId: 5
293
+ }
294
+ case 5:
295
+ return {
296
+ icon: BiologicalRes.rhythm_icon1,
297
+ iconId: 1
298
+ }
299
+ case 6:
300
+ return {
301
+ icon: BiologicalRes.rhythm_icon2,
302
+ iconId: 2
303
+ }
304
+ case 7:
305
+ return {
306
+ icon: BiologicalRes.rhythm_icon3,
307
+ iconId: 9
308
+ }
309
+ default:
310
+ return {
311
+ icon: BiologicalRes.rhythm_icon1,
312
+ iconId: 1
313
+ }
314
+ }
315
+ }
316
+
317
+ function getRepeatPeriodTitleByIndex(index: number): string {
318
+ let title = ''
319
+ switch (index) {
320
+ case 0:
321
+ title = I18n.getLang('bio_ryhthm_default_weekday7_text')
322
+ break
323
+ case 1:
324
+ title = I18n.getLang('bio_ryhthm_default_weekday1_text')
325
+ break
326
+ case 2:
327
+ title = I18n.getLang('bio_ryhthm_default_weekday2_text')
328
+ break
329
+ case 3:
330
+ title = I18n.getLang('bio_ryhthm_default_weekday3_text')
331
+ break
332
+ case 4:
333
+ title = I18n.getLang('bio_ryhthm_default_weekday4_text')
334
+ break
335
+ case 5:
336
+ title = I18n.getLang('bio_ryhthm_default_weekday5_text')
337
+ break
338
+ case 6:
339
+ title = I18n.getLang('bio_ryhthm_default_weekday6_text')
340
+ break
341
+ }
342
+ return title
343
+ }
344
+
345
+ function obj2Dp(obj: BiorhythmBean): string {
346
+ const versionHex = '00'
347
+ const enableHex = obj.enable ? '01' : '00'
348
+ const gradientHex = obj.gradient === BiorhythmGradientType.EntireGradient ? '00' : '0F'
349
+ const repeatPeriodHex = parseInt(
350
+ obj.repeatPeriod
351
+ .map(p => (p.enabled ? '1' : '0'))
352
+ .reverse()
353
+ .join(''),
354
+ 2,
355
+ )
356
+ .toString(16)
357
+ .padStart(2, '0')
358
+ const planCountHex = obj.planList.length.toString(16).padStart(2, '0')
359
+ const planListHex = obj.planList
360
+ .map(plan => {
361
+ const enableHex = plan.enable ? '01' : '00'
362
+ const hourHex = to16(Math.trunc(plan.time / 60))
363
+ const minuteHex = to16(plan.time % 60)
364
+ const hsvHex = '00000000'
365
+ const brightnessHex = plan.brightness.toString(16).padStart(2, '0')
366
+ const colorTemperatureHex = plan.colorTemperature.toString(16).padStart(2, '0')
367
+ return enableHex + hourHex + minuteHex + hsvHex + brightnessHex + colorTemperatureHex
368
+ })
369
+ .join('')
370
+ return versionHex + enableHex + gradientHex + repeatPeriodHex + planCountHex + planListHex
371
+ }
372
+
373
+ /**
374
+ * BiorhythmBean 转 RemoteBiorhythmBean
375
+ */
376
+ function vo2Dto(biorhythmBean: BiorhythmBean): RemoteBiorhythmBean {
377
+ return {
378
+ enable: biorhythmBean.enable,
379
+ repeatPeriod: biorhythmBean.repeatPeriod.filter(r => r.enabled).map(r => r.index),
380
+ gradientWay: BiorhythmGradientTypeMap[biorhythmBean.gradient],
381
+ rhythmPlan: biorhythmBean.planList.map(plan => {
382
+ const hour = Math.trunc(plan.time / 60).toString().padStart(2, '0')
383
+ const min = (plan.time % 60).toString().padStart(2, '0')
384
+ return {
385
+ name: plan.name,
386
+ rhythmIcon: plan.icon,
387
+ enable: plan.enable,
388
+ startTime: `${hour}:${min}`,
389
+ sustain: plan.index, // 持续时间涂鸦这边没有,直接用来存ID
390
+ action: plan.action,
391
+ iconId: plan.iconId,
392
+ }
393
+ }),
394
+ }
395
+ }
396
+
397
+ /**
398
+ * RemoteBiorhythmBean 转 BiorhythmBean
399
+ */
400
+ function dto2Vo(remoteBiorhythmBean: RemoteBiorhythmBean): BiorhythmBean {
401
+ return {
402
+ enable: remoteBiorhythmBean.enable,
403
+ repeatPeriod: getDefRepeatPeriod().map(repeatItem => {
404
+ return {
405
+ ...repeatItem,
406
+ enabled: remoteBiorhythmBean.repeatPeriod.includes(repeatItem.index),
407
+ }
408
+ }),
409
+ gradient: BiorhythmGradientTypeMap2[remoteBiorhythmBean.gradientWay],
410
+ planList: remoteBiorhythmBean.rhythmPlan.map(plan => {
411
+ const startTime = plan.startTime.split(':')
412
+ return {
413
+ index: plan.sustain,
414
+ enable: plan.enable,
415
+ icon: plan.rhythmIcon,
416
+ time: Number(startTime[0]) * 60 + Number(startTime[1]),
417
+ name: plan.name,
418
+ colorTemperature: colorTempPercent(parseInt(plan.action[0].startValue)),
419
+ brightness: parseInt(plan.action[1].startValue),
420
+ action: plan.action,
421
+ iconId: plan.iconId,
422
+ }
423
+ }),
424
+ }
425
+ }