@ledvance/base 1.2.82 → 1.2.83

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.
Files changed (165) hide show
  1. package/.babelrc +31 -31
  2. package/.eslintignore +5 -5
  3. package/.eslintrc.js +27 -27
  4. package/.prettierrc.js +1 -1
  5. package/.versionrc +5 -5
  6. package/localazy.json +848 -848
  7. package/localazy.keys.json +4 -4
  8. package/package.json +74 -74
  9. package/rn-cli.config.js +8 -8
  10. package/src/api/native.d.ts +71 -71
  11. package/src/api/native.ts +423 -423
  12. package/src/api/nativeEventEmitter.d.ts +3 -3
  13. package/src/api/nativeEventEmitter.ts +125 -125
  14. package/src/components/AdvanceCard.d.ts +25 -25
  15. package/src/components/AdvanceCard.tsx +117 -117
  16. package/src/components/AdvanceList.d.ts +9 -9
  17. package/src/components/AdvanceList.tsx +69 -69
  18. package/src/components/ApplyForDeviceItem.d.ts +6 -6
  19. package/src/components/ApplyForDeviceItem.tsx +83 -83
  20. package/src/components/ApplyForDeviceList.d.ts +7 -7
  21. package/src/components/ApplyForDeviceList.tsx +61 -61
  22. package/src/components/ApplyForText.d.ts +6 -6
  23. package/src/components/ApplyForText.tsx +52 -52
  24. package/src/components/Card.d.ts +10 -10
  25. package/src/components/Card.tsx +40 -40
  26. package/src/components/Cell.d.ts +21 -21
  27. package/src/components/Cell.tsx +69 -69
  28. package/src/components/CircularProgress.d.ts +8 -8
  29. package/src/components/CircularProgress.tsx +42 -42
  30. package/src/components/ColorAdjustView.d.ts +12 -12
  31. package/src/components/ColorAdjustView.tsx +64 -64
  32. package/src/components/ColorExtractor.d.ts +73 -73
  33. package/src/components/ColorExtractor.tsx +374 -374
  34. package/src/components/ColorTempAdjustView.d.ts +13 -13
  35. package/src/components/ColorTempAdjustView.tsx +57 -57
  36. package/src/components/ColorsLine.d.ts +7 -7
  37. package/src/components/ColorsLine.tsx +47 -47
  38. package/src/components/CustomListDialog.d.ts +15 -15
  39. package/src/components/CustomListDialog.tsx +66 -65
  40. package/src/components/DeleteButton.d.ts +10 -10
  41. package/src/components/DeleteButton.tsx +40 -40
  42. package/src/components/Dialog.d.ts +10 -10
  43. package/src/components/Dialog.tsx +38 -38
  44. package/src/components/DrawToolView.d.ts +46 -46
  45. package/src/components/DrawToolView.tsx +262 -262
  46. package/src/components/FanAdjustView.d.ts +23 -23
  47. package/src/components/FanAdjustView.tsx +141 -141
  48. package/src/components/InfoText.d.ts +11 -11
  49. package/src/components/InfoText.tsx +45 -45
  50. package/src/components/LampAdjustView.d.ts +10 -10
  51. package/src/components/LampAdjustView.tsx +75 -75
  52. package/src/components/LampAdjustView2.d.ts +24 -24
  53. package/src/components/LampAdjustView2.tsx +104 -104
  54. package/src/components/LinearGradientLine.d.ts +9 -9
  55. package/src/components/LinearGradientLine.tsx +63 -63
  56. package/src/components/LoadingView.tsx +39 -39
  57. package/src/components/MoodColorsLine.d.ts +9 -9
  58. package/src/components/MoodColorsLine.tsx +38 -38
  59. package/src/components/Page.d.ts +21 -21
  60. package/src/components/Page.tsx +104 -104
  61. package/src/components/Popup.d.ts +7 -7
  62. package/src/components/Popup.tsx +71 -71
  63. package/src/components/Segmented.d.ts +14 -14
  64. package/src/components/Segmented.tsx +76 -76
  65. package/src/components/ShowSelect.d.ts +1 -1
  66. package/src/components/ShowSelect.tsx +158 -158
  67. package/src/components/SocketItem.d.ts +13 -13
  68. package/src/components/SocketItem.tsx +87 -87
  69. package/src/components/Spacer.d.ts +7 -7
  70. package/src/components/Spacer.tsx +13 -13
  71. package/src/components/Stepper.d.ts +19 -19
  72. package/src/components/Stepper.tsx +130 -130
  73. package/src/components/StripAdjustView.d.ts +16 -16
  74. package/src/components/StripAdjustView.tsx +129 -129
  75. package/src/components/StripLightView.d.ts +14 -14
  76. package/src/components/StripLightView.tsx +24 -24
  77. package/src/components/Summary.tsx +116 -116
  78. package/src/components/Tag.d.ts +8 -8
  79. package/src/components/Tag.tsx +56 -56
  80. package/src/components/TextButton.d.ts +9 -9
  81. package/src/components/TextButton.tsx +31 -31
  82. package/src/components/TextField.d.ts +9 -9
  83. package/src/components/TextField.tsx +92 -92
  84. package/src/components/TextFieldStyleButton.d.ts +8 -8
  85. package/src/components/TextFieldStyleButton.tsx +85 -85
  86. package/src/components/UATabTitle.d.ts +6 -6
  87. package/src/components/UATabTitle.tsx +31 -31
  88. package/src/components/UATabs.d.ts +12 -12
  89. package/src/components/UATabs.tsx +77 -77
  90. package/src/components/connect.d.ts +10 -10
  91. package/src/components/connect.tsx +20 -20
  92. package/src/components/ldvColorBrightness.d.ts +1 -1
  93. package/src/components/ldvColorBrightness.tsx +18 -18
  94. package/src/components/ldvColorSlider.d.ts +14 -14
  95. package/src/components/ldvColorSlider.tsx +131 -131
  96. package/src/components/ldvItemView.d.ts +12 -12
  97. package/src/components/ldvItemView.tsx +43 -43
  98. package/src/components/ldvPickerView.d.ts +13 -13
  99. package/src/components/ldvPickerView.tsx +97 -97
  100. package/src/components/ldvPresetView.d.ts +2 -2
  101. package/src/components/ldvPresetView.tsx +86 -86
  102. package/src/components/ldvSaturation.d.ts +2 -2
  103. package/src/components/ldvSaturation.tsx +22 -22
  104. package/src/components/ldvSlider.d.ts +15 -15
  105. package/src/components/ldvSlider.tsx +103 -103
  106. package/src/components/ldvSwitch.d.ts +10 -10
  107. package/src/components/ldvSwitch.tsx +52 -52
  108. package/src/components/ldvTemperatureSlider.d.ts +13 -13
  109. package/src/components/ldvTemperatureSlider.tsx +139 -139
  110. package/src/components/ldvTopBar.d.ts +10 -10
  111. package/src/components/ldvTopBar.tsx +76 -76
  112. package/src/components/ldvTopName.d.ts +9 -9
  113. package/src/components/ldvTopName.tsx +63 -63
  114. package/src/components/segmentControl.d.ts +1 -1
  115. package/src/components/segmentControl.tsx +65 -65
  116. package/src/components/weekSelect.d.ts +7 -7
  117. package/src/components/weekSelect.tsx +98 -98
  118. package/src/composeLayout.d.ts +30 -30
  119. package/src/composeLayout.tsx +261 -261
  120. package/src/config/dark-theme.ts +61 -61
  121. package/src/config/light-theme.ts +61 -61
  122. package/src/hooks/Hooks.d.ts +2 -2
  123. package/src/hooks/Hooks.ts +8 -8
  124. package/src/i18n/index.d.ts +16 -16
  125. package/src/i18n/index.ts +37 -37
  126. package/src/i18n/strings.d.ts +15953 -15953
  127. package/src/i18n/strings.ts +23723 -23723
  128. package/src/main.tsx +9 -9
  129. package/src/models/GlobalParams.d.ts +14 -14
  130. package/src/models/GlobalParams.ts +14 -14
  131. package/src/models/TuyaApi.d.ts +29 -29
  132. package/src/models/TuyaApi.ts +92 -92
  133. package/src/models/combine.d.ts +26 -26
  134. package/src/models/combine.ts +17 -17
  135. package/src/models/configureStore.d.ts +9 -9
  136. package/src/models/configureStore.ts +33 -33
  137. package/src/models/index.d.ts +31 -31
  138. package/src/models/index.ts +22 -22
  139. package/src/models/modules/NativePropsSlice.d.ts +61 -61
  140. package/src/models/modules/NativePropsSlice.tsx +467 -467
  141. package/src/models/modules/Result.d.ts +14 -14
  142. package/src/models/modules/Result.ts +15 -15
  143. package/src/models/modules/common.d.ts +44 -44
  144. package/src/models/modules/common.ts +161 -161
  145. package/src/res/index.d.ts +70 -70
  146. package/src/res/index.ts +73 -73
  147. package/src/utils/ColorParser.d.ts +66 -66
  148. package/src/utils/ColorParser.ts +189 -189
  149. package/src/utils/ColorUtils.d.ts +71 -71
  150. package/src/utils/ColorUtils.tsx +448 -448
  151. package/src/utils/Support.d.ts +13 -13
  152. package/src/utils/Support.ts +108 -108
  153. package/src/utils/TypeUtils.d.ts +3 -3
  154. package/src/utils/cctUtils.d.ts +1 -1
  155. package/src/utils/cctUtils.ts +110 -110
  156. package/src/utils/common.d.ts +35 -35
  157. package/src/utils/common.ts +379 -379
  158. package/src/utils/index.d.ts +26 -26
  159. package/src/utils/index.ts +191 -191
  160. package/src/utils/interface.d.ts +6 -6
  161. package/src/utils/interface.ts +50 -11
  162. package/src/utils/loopsCompare.d.ts +1 -1
  163. package/src/utils/loopsCompare.ts +62 -62
  164. package/tsconfig.json +50 -50
  165. package/update-localazy.py +52 -52
@@ -1,467 +1,467 @@
1
- import { createSlice, PayloadAction } from '@reduxjs/toolkit'
2
- import {useSelector} from '../index'
3
- import { Dispatch, useCallback } from 'react'
4
- import { DpsResult, Result } from './Result'
5
- import { DevInfo, DpValue } from 'tuya-panel-kit'
6
- import { NativeApi } from '../../api/native'
7
- import { useDispatch } from 'react-redux'
8
- import { GlobalParams } from '../GlobalParams'
9
- import { isNumber, snakeCase } from 'lodash'
10
- import { PropertyValueTypes } from '../../utils/TypeUtils'
11
- import { DeviceInfo as GroupDeviceInfo } from '../../api/native'
12
-
13
- export interface NativeProps {
14
- familyName: string
15
- role: 0 | 1 | 2 | 3
16
- deviceInfo: DeviceInfo
17
- uaGroupInfo: UAGroupInfo
18
- timeSchedule?: boolean
19
- energieverbrauch?: object
20
- moods: any[]
21
- flags: any[]
22
- flagModeState: FlagModeState
23
- is24HourClock: boolean
24
- timeZone: string
25
- }
26
-
27
- interface FlagModeState {
28
- flagMode: boolean
29
- flagId?: number
30
- }
31
-
32
- export interface DeviceInfo {
33
- devId: string
34
- pId: string
35
- dps: any
36
- }
37
-
38
- export interface UAGroupInfo {
39
- tyGroupId: number
40
- pId: string
41
- dps: any
42
- config: any
43
- groupDevices: GroupDeviceInfo[]
44
- }
45
-
46
- const initialState: NativeProps = {
47
- familyName: '',
48
- role: 2,
49
- deviceInfo: {
50
- devId: '',
51
- pId: '',
52
- dps: {},
53
- },
54
- uaGroupInfo: {
55
- tyGroupId: -1,
56
- pId: '',
57
- dps: {},
58
- config: {},
59
- groupDevices: []
60
- },
61
- timeSchedule: false,
62
- energieverbrauch: {},
63
- moods: [],
64
- flags: [],
65
- flagModeState:{
66
- flagMode: false,
67
- flagId: undefined
68
- },
69
- is24HourClock: true,
70
- timeZone: 'Europe/Berlin'
71
- }
72
-
73
- // energy generation
74
- const productList = [
75
- 'rcqe1i17x0vrvws7',
76
- ]
77
-
78
- const fanProductList = [
79
- 'urcqn70htlshvigb',
80
- ]
81
-
82
- const nativePropsSlice = createSlice({
83
- name: 'ldvModules',
84
- initialState: initialState,
85
- reducers: {
86
- setNativeProps: (state, action: PayloadAction<NativeProps>) => {
87
- state.deviceInfo.devId = action.payload.deviceInfo.devId
88
- if (!!action.payload.familyName) {
89
- state.familyName = action.payload.familyName
90
- }
91
- if (isNumber(action.payload.role)) {
92
- state.role = action.payload.role
93
- }
94
- if (!!action.payload.deviceInfo.pId) {
95
- state.deviceInfo.pId = action.payload.deviceInfo.pId
96
- }
97
- state.deviceInfo.dps = { ...state.deviceInfo.dps, ...action.payload.deviceInfo.dps }
98
- },
99
- setGroupNativeProps: (state, action: PayloadAction<NativeProps>) => {
100
- state.uaGroupInfo.tyGroupId = action.payload.uaGroupInfo.tyGroupId
101
- if (!!action.payload.familyName) {
102
- state.familyName = action.payload.familyName
103
- }
104
- if (isNumber(action.payload.role)) {
105
- state.role = action.payload.role
106
- }
107
- if (!!action.payload.uaGroupInfo.pId) {
108
- state.uaGroupInfo.pId = action.payload.uaGroupInfo.pId
109
- }
110
- state.uaGroupInfo.dps = { ...state.uaGroupInfo.dps, ...action.payload.uaGroupInfo.dps }
111
- state.uaGroupInfo.config = action.payload.uaGroupInfo.config
112
- },
113
- setGroupConfig: (state, action: PayloadAction<any>) => {
114
- state.uaGroupInfo.config = { ...state.uaGroupInfo.config, ...action.payload }
115
- },
116
- setDps(state, action: PayloadAction<any>) {
117
- const dpKeys = Object.keys(action.payload)
118
- dpKeys.forEach(dp => {
119
- state.deviceInfo.dps[dp] = action.payload[dp]
120
- })
121
- },
122
- setGroupDps(state, action: PayloadAction<any>) {
123
- const dpKeys = Object.keys(action.payload)
124
- dpKeys.forEach(dp => {
125
- state.uaGroupInfo.dps[dp] = action.payload[dp]
126
- })
127
- },
128
- setTimeSchedule(state, action: PayloadAction<any>) {
129
- state.timeSchedule = action.payload
130
- },
131
- setEnergieverbrauch(state, action: PayloadAction<any>) {
132
- state.energieverbrauch = action.payload
133
- },
134
- setMoods(state, action: PayloadAction<any>) {
135
- state.moods = action.payload
136
- },
137
- setFlags(state, action: PayloadAction<any>) {
138
- state.flags = action.payload
139
- },
140
- setFlagMode(state, action: PayloadAction<any>) {
141
- state.flagModeState = action.payload
142
- },
143
- setGroupDevices(state, action: PayloadAction<any>) {
144
- state.uaGroupInfo.groupDevices = action.payload
145
- },
146
- setSystemTimeFormat(state, action: PayloadAction<any>) {
147
- state.is24HourClock = action.payload
148
- },
149
- setTimeZone(state, action: PayloadAction<any>) {
150
- state.timeZone = action.payload
151
- }
152
- },
153
- })
154
-
155
- type AsyncBlockType<T> = () => Promise<DpsResult<T>>
156
- type SyncBlockType<T> = () => DpsResult<T>
157
-
158
- function asyncSetDps<T>(dispatch: Dispatch<any>, block: AsyncBlockType<T> | SyncBlockType<T>): Promise<Result<T>> {
159
- return new Promise<Result<T>>(async (res, _rej) => {
160
- const dpsResult = await block()
161
- if (dpsResult.result.success) {
162
- // 只在结果成功时才改变 redux 数据
163
- dispatch(setDps(dpsResult.dps))
164
- }
165
- res(dpsResult.result)
166
- })
167
- }
168
-
169
- function simpleSetDps<T>(dispatch: Dispatch<any>): (deviceId: string, dps: any) => Promise<Result<T>> {
170
- return (deviceId, dps) => {
171
- return asyncSetDps(dispatch, async () => {
172
- console.log('发送dp数据:', deviceId, JSON.stringify(dps))
173
- const res = await NativeApi.setDps(deviceId, dps)
174
- if (res.result) {
175
- return {
176
- result: { success: true },
177
- dps: dps,
178
- }
179
- }
180
- return {
181
- result: { success: false, msg: res.msg },
182
- }
183
- })
184
- }
185
- }
186
-
187
- function simpleSetDp<T>(dispatch: Dispatch<any>): (deviceId: string, dp: string, value: any) => Promise<Result<T>> {
188
- return (deviceId, dp, value) => {
189
- const fun = simpleSetDps<T>(dispatch)
190
- return fun(deviceId, { [dp]: value })
191
- }
192
- }
193
-
194
- const useDeviceId = () => {
195
- return useSelector(store => store.ldvModules.deviceInfo.devId)
196
- }
197
-
198
- const useGroupId = () => {
199
- return useSelector(store => store.ldvModules.uaGroupInfo.tyGroupId)
200
- }
201
-
202
- const useFamilyName: () => string = () => {
203
- return useSelector(store => store.ldvModules.familyName)
204
- }
205
-
206
- const useRole: () => 0 | 1 | 2 | 3 = () => {
207
- return useSelector(store => store.ldvModules.role)
208
- }
209
-
210
- function useDp<T, R extends any>(dp: string): [T, (value: T) => Promise<Result<R>>] {
211
- const deviceId = useDeviceId()
212
- const dispatch = useDispatch()
213
- const dpValue = useSelector(store => store.ldvModules.deviceInfo.dps[dp])
214
- const setDp = useCallback((dpValue: T) => {
215
- return simpleSetDp<R>(dispatch)(deviceId, dp, dpValue)
216
- }, [])
217
- return [dpValue, setDp]
218
- }
219
-
220
- function useGroupDp<T>(dp: string): [T] {
221
- const dpValue = useSelector(store => store.ldvModules.uaGroupInfo.dps[dp])
222
- return [dpValue]
223
- }
224
-
225
- function useScaledDp<R extends any>(dp: string, scaled: number = 10): [number, (value: number) => Promise<Result<R>>] {
226
- const [sd, setSd] = useDp<number, R>(dp)
227
- const setScaledDpValue = useCallback(async (value: number) => {
228
- return await setSd(value * scaled)
229
- }, [setSd])
230
- return [sd / scaled, setScaledDpValue]
231
- }
232
-
233
- function useDps<R>(): [any, (dps: any) => Promise<Result<R>>] {
234
- const deviceId = useDeviceId()
235
- const dispatch = useDispatch()
236
- const dps = useSelector(store => store.ldvModules.deviceInfo.dps)
237
- const setDps = useCallback((dps: any) => {
238
- return simpleSetDps<R>(dispatch)(deviceId, dps)
239
- }, [])
240
- return [dps, setDps]
241
- }
242
-
243
- interface DpState {
244
- switch: boolean;
245
-
246
- [dpCode: string]: DpValue;
247
- }
248
-
249
- const useDeviceInfo: () => DevInfo<DpState> = () => {
250
- return useSelector(state => state.devInfo)
251
- }
252
-
253
- const useTimeSchedule = (): [v: boolean | undefined, f: any] => {
254
- const dps = useSelector(store => store.ldvModules.timeSchedule)
255
- const dispatch = useDispatch()
256
- const setTimeScheduleFn = (value: boolean) => {
257
- dispatch(setTimeSchedule(value))
258
- }
259
- return [dps, setTimeScheduleFn]
260
- }
261
-
262
- const useMoods = (): [any[], (v: any[]) => void] => {
263
- const dps = useSelector(store => store.ldvModules.moods)
264
- const dispatch = useDispatch()
265
- const setMoodsFn = (value: any[]) => {
266
- dispatch(setMoods(value))
267
- }
268
- return [dps, setMoodsFn]
269
- }
270
-
271
- const useFlags = (): [any[], (v: any[]) => void] => {
272
- const dps = useSelector(store => store.ldvModules.flags)
273
- const dispatch = useDispatch()
274
- const setFlagsFn = (value: any[]) => {
275
- dispatch(setFlags(value))
276
- }
277
- return [dps, setFlagsFn]
278
- }
279
-
280
- const useFlagMode = (): [FlagModeState, (v: FlagModeState) => void] =>{
281
- const dps = useSelector(store => store.ldvModules.flagModeState)
282
- const dispatch = useDispatch()
283
- const setFlagModeFn = (value: FlagModeState) => {
284
- dispatch(setFlagMode(value))
285
- }
286
- return [dps, setFlagModeFn]
287
- }
288
-
289
- const useGroupDevices = (): [GroupDeviceInfo[], (v: GroupDeviceInfo[]) => void] => {
290
- const dps = useSelector(store => store.ldvModules.uaGroupInfo.groupDevices)
291
- const dispatch = useDispatch()
292
- const setGroupDevicesFn = (value: GroupDeviceInfo[]) => {
293
- dispatch(setGroupDevices(value))
294
- }
295
- return [dps, setGroupDevicesFn]
296
- }
297
-
298
- const useSystemTimeFormate = (): boolean => {
299
- return useSelector(store => store.ldvModules.is24HourClock)
300
- }
301
-
302
- const useTimeZone = (): string => {
303
- return useSelector(store => store.ldvModules.timeZone)
304
- }
305
-
306
- const useTimeZoneCity = (): string => {
307
- return useSelector(store => store.ldvModules.timeZone).split('/')[1]
308
- }
309
-
310
- const useEnergieverbrauch = () => {
311
- const dps = useSelector(store => store.ldvModules.energieverbrauch)
312
- const dispatch = useDispatch()
313
- const setEnergieverbrauchFn = (value) => {
314
- dispatch(setEnergieverbrauch(value))
315
- }
316
- return [dps, setEnergieverbrauchFn]
317
- }
318
-
319
- export const useEngergyGeneration = () => {
320
- const { productId } = useDeviceInfo()
321
- return productList.some(val => val === productId)
322
- }
323
-
324
- export function useUAGroupInfo(): UAGroupInfo {
325
- return useSelector(state => state.ldvModules.uaGroupInfo)
326
- }
327
-
328
- export function putGroupTYDps(tyGroupId: number, dps: Record<string, any>) {
329
- return NativeApi.putGroupTYDps(tyGroupId, JSON.stringify(dps))
330
- }
331
-
332
- export function useGroupEzvizConfig<GC, T extends PropertyValueTypes<GC>>(featureKey: keyof GC, defConfig?: T): [T, (newConfig: T) => Promise<Result<any>>] {
333
- const tyGroupId = useUAGroupInfo().tyGroupId
334
- const config = useUAGroupInfo().config || {}
335
- const dispatch = useDispatch()
336
- const { setGroupConfig } = nativePropsSlice.actions
337
- const setConfig = useCallback(async (newConfig: T) => {
338
- const configs = {
339
- [featureKey]: newConfig
340
- }
341
- const res = await NativeApi.putGroupEzvizConfig(tyGroupId, JSON.stringify(configs))
342
- if (res.success) {
343
- // 只在结果成功时才改变 redux 数据
344
- dispatch(setGroupConfig(configs))
345
- }
346
- return res
347
- }, [])
348
- return [config[featureKey] ?? defConfig, setConfig]
349
- }
350
-
351
- export function useGroupConfig<T>(): [T, (dps: any, newConfig: T) => Promise<Result<any>>] {
352
- const tyGroupId = useUAGroupInfo().tyGroupId
353
- const config = useUAGroupInfo().config || {}
354
- const dispatch = useDispatch()
355
- const { setGroupConfig } = nativePropsSlice.actions
356
- const setConfig = useCallback(async (dps: any, newConfig: T) => {
357
- // 发送控制命令
358
- const res = await NativeApi.groupControl(tyGroupId, JSON.stringify(dps), JSON.stringify(newConfig))
359
- if (res.success) {
360
- // 只在结果成功时才改变 redux 数据
361
- dispatch(setGroupConfig(newConfig))
362
- }
363
- return res
364
- }, [])
365
- return [config, setConfig]
366
- }
367
-
368
- /**
369
- * @template GC GroupConfig
370
- * @template GCPT GroupConfigPropertyType
371
- * @param key
372
- * @param dpKey
373
- */
374
- export function useGroupConfigFeature<GC, GCPT extends PropertyValueTypes<GC>>
375
- (key: keyof GC, dpKey?: string): [GCPT | undefined, (value: GCPT, dpValue: any, extraDps?: any, extraConfig?: any) => Promise<Result<any>>] {
376
- const [groupConfig, setGroupConfig] = useGroupConfig<GC>()
377
- const setGroupConfigFeature = async (value: GCPT, dpValue: any, extraDps?: any, extraConfig?: any) => {
378
- const sendDpKey = !!dpKey ? dpKey : GlobalParams.dpSchemaMap[snakeCase(key as string)]?.dp
379
-
380
- const sendDps = {}
381
- if (sendDpKey) {
382
- sendDps[sendDpKey] = dpValue
383
- }
384
-
385
- return await setGroupConfig(
386
- {
387
- ...sendDps,
388
- ...(extraDps || {}),
389
- },
390
- {
391
- ...(extraConfig || {}),
392
- [key]: value,
393
- },
394
- )
395
- }
396
- return [groupConfig[key] as (GCPT | undefined), setGroupConfigFeature]
397
- }
398
-
399
- export function useFeatureHook<GC, T extends PropertyValueTypes<GC>>(
400
- featureKey: keyof GC,
401
- defValue: T,
402
- valueMapToDpValue?: (v: T) => any,
403
- getExtraDps?: (v: T) => any,
404
- getExtraConfig?: (v: T) => any,
405
- ): [T, (value: T) => Promise<Result<any>>] {
406
- const [featureHook, setFH] = useGroupConfigFeature<GC, T>(featureKey)
407
- const setFeatureHook = useCallback(async (value: T, extraDps?: any, extraConfig?: any) => {
408
- const dpValue = valueMapToDpValue ? valueMapToDpValue(value) : value
409
- const defDps = getExtraDps ? getExtraDps(value) : undefined
410
- const dps = {...(defDps || {}), ...(extraDps || {})}
411
- const defConfig = getExtraConfig ? getExtraConfig(value) : undefined
412
- const config = {...(defConfig || {}), ...(extraConfig || {})}
413
- return await setFH(value, dpValue, dps, config)
414
- }, [setFH])
415
- return [featureHook ?? defValue, setFeatureHook]
416
- }
417
-
418
- export const useFanMaxSpeed = () => {
419
- const { productId } = useDeviceInfo()
420
- return fanProductList.includes(productId) ? 20 : 3
421
- }
422
-
423
- export const isUVCFanDevice = () => {
424
- const { productId } = useDeviceInfo()
425
- return fanProductList.includes(productId)
426
- }
427
-
428
- export const ldvModules = nativePropsSlice.reducer
429
-
430
- export const {
431
- setNativeProps,
432
- setGroupNativeProps,
433
- setDps,
434
- setGroupDps,
435
- setTimeSchedule,
436
- setMoods,
437
- setFlags,
438
- setFlagMode,
439
- setGroupDevices,
440
- setSystemTimeFormat,
441
- setTimeZone,
442
- setEnergieverbrauch,
443
- } = nativePropsSlice.actions
444
-
445
- export {
446
- asyncSetDps,
447
- simpleSetDps,
448
- simpleSetDp,
449
- useDeviceId,
450
- useDeviceInfo,
451
- useDp,
452
- useGroupId,
453
- useGroupDp,
454
- useScaledDp,
455
- useDps,
456
- useFamilyName,
457
- useRole,
458
- useTimeSchedule,
459
- useMoods,
460
- useFlags,
461
- useFlagMode,
462
- useGroupDevices,
463
- useSystemTimeFormate,
464
- useTimeZone,
465
- useTimeZoneCity,
466
- useEnergieverbrauch,
467
- }
1
+ import { createSlice, PayloadAction } from '@reduxjs/toolkit'
2
+ import {useSelector} from '../index'
3
+ import { Dispatch, useCallback } from 'react'
4
+ import { DpsResult, Result } from './Result'
5
+ import { DevInfo, DpValue } from 'tuya-panel-kit'
6
+ import { NativeApi } from '../../api/native'
7
+ import { useDispatch } from 'react-redux'
8
+ import { GlobalParams } from '../GlobalParams'
9
+ import { isNumber, snakeCase } from 'lodash'
10
+ import { PropertyValueTypes } from '../../utils/TypeUtils'
11
+ import { DeviceInfo as GroupDeviceInfo } from '../../api/native'
12
+
13
+ export interface NativeProps {
14
+ familyName: string
15
+ role: 0 | 1 | 2 | 3
16
+ deviceInfo: DeviceInfo
17
+ uaGroupInfo: UAGroupInfo
18
+ timeSchedule?: boolean
19
+ energieverbrauch?: object
20
+ moods: any[]
21
+ flags: any[]
22
+ flagModeState: FlagModeState
23
+ is24HourClock: boolean
24
+ timeZone: string
25
+ }
26
+
27
+ interface FlagModeState {
28
+ flagMode: boolean
29
+ flagId?: number
30
+ }
31
+
32
+ export interface DeviceInfo {
33
+ devId: string
34
+ pId: string
35
+ dps: any
36
+ }
37
+
38
+ export interface UAGroupInfo {
39
+ tyGroupId: number
40
+ pId: string
41
+ dps: any
42
+ config: any
43
+ groupDevices: GroupDeviceInfo[]
44
+ }
45
+
46
+ const initialState: NativeProps = {
47
+ familyName: '',
48
+ role: 2,
49
+ deviceInfo: {
50
+ devId: '',
51
+ pId: '',
52
+ dps: {},
53
+ },
54
+ uaGroupInfo: {
55
+ tyGroupId: -1,
56
+ pId: '',
57
+ dps: {},
58
+ config: {},
59
+ groupDevices: []
60
+ },
61
+ timeSchedule: false,
62
+ energieverbrauch: {},
63
+ moods: [],
64
+ flags: [],
65
+ flagModeState:{
66
+ flagMode: false,
67
+ flagId: undefined
68
+ },
69
+ is24HourClock: true,
70
+ timeZone: 'Europe/Berlin'
71
+ }
72
+
73
+ // energy generation
74
+ const productList = [
75
+ 'rcqe1i17x0vrvws7',
76
+ ]
77
+
78
+ const fanProductList = [
79
+ 'urcqn70htlshvigb',
80
+ ]
81
+
82
+ const nativePropsSlice = createSlice({
83
+ name: 'ldvModules',
84
+ initialState: initialState,
85
+ reducers: {
86
+ setNativeProps: (state, action: PayloadAction<NativeProps>) => {
87
+ state.deviceInfo.devId = action.payload.deviceInfo.devId
88
+ if (!!action.payload.familyName) {
89
+ state.familyName = action.payload.familyName
90
+ }
91
+ if (isNumber(action.payload.role)) {
92
+ state.role = action.payload.role
93
+ }
94
+ if (!!action.payload.deviceInfo.pId) {
95
+ state.deviceInfo.pId = action.payload.deviceInfo.pId
96
+ }
97
+ state.deviceInfo.dps = { ...state.deviceInfo.dps, ...action.payload.deviceInfo.dps }
98
+ },
99
+ setGroupNativeProps: (state, action: PayloadAction<NativeProps>) => {
100
+ state.uaGroupInfo.tyGroupId = action.payload.uaGroupInfo.tyGroupId
101
+ if (!!action.payload.familyName) {
102
+ state.familyName = action.payload.familyName
103
+ }
104
+ if (isNumber(action.payload.role)) {
105
+ state.role = action.payload.role
106
+ }
107
+ if (!!action.payload.uaGroupInfo.pId) {
108
+ state.uaGroupInfo.pId = action.payload.uaGroupInfo.pId
109
+ }
110
+ state.uaGroupInfo.dps = { ...state.uaGroupInfo.dps, ...action.payload.uaGroupInfo.dps }
111
+ state.uaGroupInfo.config = action.payload.uaGroupInfo.config
112
+ },
113
+ setGroupConfig: (state, action: PayloadAction<any>) => {
114
+ state.uaGroupInfo.config = { ...state.uaGroupInfo.config, ...action.payload }
115
+ },
116
+ setDps(state, action: PayloadAction<any>) {
117
+ const dpKeys = Object.keys(action.payload)
118
+ dpKeys.forEach(dp => {
119
+ state.deviceInfo.dps[dp] = action.payload[dp]
120
+ })
121
+ },
122
+ setGroupDps(state, action: PayloadAction<any>) {
123
+ const dpKeys = Object.keys(action.payload)
124
+ dpKeys.forEach(dp => {
125
+ state.uaGroupInfo.dps[dp] = action.payload[dp]
126
+ })
127
+ },
128
+ setTimeSchedule(state, action: PayloadAction<any>) {
129
+ state.timeSchedule = action.payload
130
+ },
131
+ setEnergieverbrauch(state, action: PayloadAction<any>) {
132
+ state.energieverbrauch = action.payload
133
+ },
134
+ setMoods(state, action: PayloadAction<any>) {
135
+ state.moods = action.payload
136
+ },
137
+ setFlags(state, action: PayloadAction<any>) {
138
+ state.flags = action.payload
139
+ },
140
+ setFlagMode(state, action: PayloadAction<any>) {
141
+ state.flagModeState = action.payload
142
+ },
143
+ setGroupDevices(state, action: PayloadAction<any>) {
144
+ state.uaGroupInfo.groupDevices = action.payload
145
+ },
146
+ setSystemTimeFormat(state, action: PayloadAction<any>) {
147
+ state.is24HourClock = action.payload
148
+ },
149
+ setTimeZone(state, action: PayloadAction<any>) {
150
+ state.timeZone = action.payload
151
+ }
152
+ },
153
+ })
154
+
155
+ type AsyncBlockType<T> = () => Promise<DpsResult<T>>
156
+ type SyncBlockType<T> = () => DpsResult<T>
157
+
158
+ function asyncSetDps<T>(dispatch: Dispatch<any>, block: AsyncBlockType<T> | SyncBlockType<T>): Promise<Result<T>> {
159
+ return new Promise<Result<T>>(async (res, _rej) => {
160
+ const dpsResult = await block()
161
+ if (dpsResult.result.success) {
162
+ // 只在结果成功时才改变 redux 数据
163
+ dispatch(setDps(dpsResult.dps))
164
+ }
165
+ res(dpsResult.result)
166
+ })
167
+ }
168
+
169
+ function simpleSetDps<T>(dispatch: Dispatch<any>): (deviceId: string, dps: any) => Promise<Result<T>> {
170
+ return (deviceId, dps) => {
171
+ return asyncSetDps(dispatch, async () => {
172
+ console.log('发送dp数据:', deviceId, JSON.stringify(dps))
173
+ const res = await NativeApi.setDps(deviceId, dps)
174
+ if (res.result) {
175
+ return {
176
+ result: { success: true },
177
+ dps: dps,
178
+ }
179
+ }
180
+ return {
181
+ result: { success: false, msg: res.msg },
182
+ }
183
+ })
184
+ }
185
+ }
186
+
187
+ function simpleSetDp<T>(dispatch: Dispatch<any>): (deviceId: string, dp: string, value: any) => Promise<Result<T>> {
188
+ return (deviceId, dp, value) => {
189
+ const fun = simpleSetDps<T>(dispatch)
190
+ return fun(deviceId, { [dp]: value })
191
+ }
192
+ }
193
+
194
+ const useDeviceId = () => {
195
+ return useSelector(store => store.ldvModules.deviceInfo.devId)
196
+ }
197
+
198
+ const useGroupId = () => {
199
+ return useSelector(store => store.ldvModules.uaGroupInfo.tyGroupId)
200
+ }
201
+
202
+ const useFamilyName: () => string = () => {
203
+ return useSelector(store => store.ldvModules.familyName)
204
+ }
205
+
206
+ const useRole: () => 0 | 1 | 2 | 3 = () => {
207
+ return useSelector(store => store.ldvModules.role)
208
+ }
209
+
210
+ function useDp<T, R extends any>(dp: string): [T, (value: T) => Promise<Result<R>>] {
211
+ const deviceId = useDeviceId()
212
+ const dispatch = useDispatch()
213
+ const dpValue = useSelector(store => store.ldvModules.deviceInfo.dps[dp])
214
+ const setDp = useCallback((dpValue: T) => {
215
+ return simpleSetDp<R>(dispatch)(deviceId, dp, dpValue)
216
+ }, [])
217
+ return [dpValue, setDp]
218
+ }
219
+
220
+ function useGroupDp<T>(dp: string): [T] {
221
+ const dpValue = useSelector(store => store.ldvModules.uaGroupInfo.dps[dp])
222
+ return [dpValue]
223
+ }
224
+
225
+ function useScaledDp<R extends any>(dp: string, scaled: number = 10): [number, (value: number) => Promise<Result<R>>] {
226
+ const [sd, setSd] = useDp<number, R>(dp)
227
+ const setScaledDpValue = useCallback(async (value: number) => {
228
+ return await setSd(value * scaled)
229
+ }, [setSd])
230
+ return [sd / scaled, setScaledDpValue]
231
+ }
232
+
233
+ function useDps<R>(): [any, (dps: any) => Promise<Result<R>>] {
234
+ const deviceId = useDeviceId()
235
+ const dispatch = useDispatch()
236
+ const dps = useSelector(store => store.ldvModules.deviceInfo.dps)
237
+ const setDps = useCallback((dps: any) => {
238
+ return simpleSetDps<R>(dispatch)(deviceId, dps)
239
+ }, [])
240
+ return [dps, setDps]
241
+ }
242
+
243
+ interface DpState {
244
+ switch: boolean;
245
+
246
+ [dpCode: string]: DpValue;
247
+ }
248
+
249
+ const useDeviceInfo: () => DevInfo<DpState> = () => {
250
+ return useSelector(state => state.devInfo)
251
+ }
252
+
253
+ const useTimeSchedule = (): [v: boolean | undefined, f: any] => {
254
+ const dps = useSelector(store => store.ldvModules.timeSchedule)
255
+ const dispatch = useDispatch()
256
+ const setTimeScheduleFn = (value: boolean) => {
257
+ dispatch(setTimeSchedule(value))
258
+ }
259
+ return [dps, setTimeScheduleFn]
260
+ }
261
+
262
+ const useMoods = (): [any[], (v: any[]) => void] => {
263
+ const dps = useSelector(store => store.ldvModules.moods)
264
+ const dispatch = useDispatch()
265
+ const setMoodsFn = (value: any[]) => {
266
+ dispatch(setMoods(value))
267
+ }
268
+ return [dps, setMoodsFn]
269
+ }
270
+
271
+ const useFlags = (): [any[], (v: any[]) => void] => {
272
+ const dps = useSelector(store => store.ldvModules.flags)
273
+ const dispatch = useDispatch()
274
+ const setFlagsFn = (value: any[]) => {
275
+ dispatch(setFlags(value))
276
+ }
277
+ return [dps, setFlagsFn]
278
+ }
279
+
280
+ const useFlagMode = (): [FlagModeState, (v: FlagModeState) => void] =>{
281
+ const dps = useSelector(store => store.ldvModules.flagModeState)
282
+ const dispatch = useDispatch()
283
+ const setFlagModeFn = (value: FlagModeState) => {
284
+ dispatch(setFlagMode(value))
285
+ }
286
+ return [dps, setFlagModeFn]
287
+ }
288
+
289
+ const useGroupDevices = (): [GroupDeviceInfo[], (v: GroupDeviceInfo[]) => void] => {
290
+ const dps = useSelector(store => store.ldvModules.uaGroupInfo.groupDevices)
291
+ const dispatch = useDispatch()
292
+ const setGroupDevicesFn = (value: GroupDeviceInfo[]) => {
293
+ dispatch(setGroupDevices(value))
294
+ }
295
+ return [dps, setGroupDevicesFn]
296
+ }
297
+
298
+ const useSystemTimeFormate = (): boolean => {
299
+ return useSelector(store => store.ldvModules.is24HourClock)
300
+ }
301
+
302
+ const useTimeZone = (): string => {
303
+ return useSelector(store => store.ldvModules.timeZone)
304
+ }
305
+
306
+ const useTimeZoneCity = (): string => {
307
+ return useSelector(store => store.ldvModules.timeZone).split('/')[1]
308
+ }
309
+
310
+ const useEnergieverbrauch = () => {
311
+ const dps = useSelector(store => store.ldvModules.energieverbrauch)
312
+ const dispatch = useDispatch()
313
+ const setEnergieverbrauchFn = (value) => {
314
+ dispatch(setEnergieverbrauch(value))
315
+ }
316
+ return [dps, setEnergieverbrauchFn]
317
+ }
318
+
319
+ export const useEngergyGeneration = () => {
320
+ const { productId } = useDeviceInfo()
321
+ return productList.some(val => val === productId)
322
+ }
323
+
324
+ export function useUAGroupInfo(): UAGroupInfo {
325
+ return useSelector(state => state.ldvModules.uaGroupInfo)
326
+ }
327
+
328
+ export function putGroupTYDps(tyGroupId: number, dps: Record<string, any>) {
329
+ return NativeApi.putGroupTYDps(tyGroupId, JSON.stringify(dps))
330
+ }
331
+
332
+ export function useGroupEzvizConfig<GC, T extends PropertyValueTypes<GC>>(featureKey: keyof GC, defConfig?: T): [T, (newConfig: T) => Promise<Result<any>>] {
333
+ const tyGroupId = useUAGroupInfo().tyGroupId
334
+ const config = useUAGroupInfo().config || {}
335
+ const dispatch = useDispatch()
336
+ const { setGroupConfig } = nativePropsSlice.actions
337
+ const setConfig = useCallback(async (newConfig: T) => {
338
+ const configs = {
339
+ [featureKey]: newConfig
340
+ }
341
+ const res = await NativeApi.putGroupEzvizConfig(tyGroupId, JSON.stringify(configs))
342
+ if (res.success) {
343
+ // 只在结果成功时才改变 redux 数据
344
+ dispatch(setGroupConfig(configs))
345
+ }
346
+ return res
347
+ }, [])
348
+ return [config[featureKey] ?? defConfig, setConfig]
349
+ }
350
+
351
+ export function useGroupConfig<T>(): [T, (dps: any, newConfig: T) => Promise<Result<any>>] {
352
+ const tyGroupId = useUAGroupInfo().tyGroupId
353
+ const config = useUAGroupInfo().config || {}
354
+ const dispatch = useDispatch()
355
+ const { setGroupConfig } = nativePropsSlice.actions
356
+ const setConfig = useCallback(async (dps: any, newConfig: T) => {
357
+ // 发送控制命令
358
+ const res = await NativeApi.groupControl(tyGroupId, JSON.stringify(dps), JSON.stringify(newConfig))
359
+ if (res.success) {
360
+ // 只在结果成功时才改变 redux 数据
361
+ dispatch(setGroupConfig(newConfig))
362
+ }
363
+ return res
364
+ }, [])
365
+ return [config, setConfig]
366
+ }
367
+
368
+ /**
369
+ * @template GC GroupConfig
370
+ * @template GCPT GroupConfigPropertyType
371
+ * @param key
372
+ * @param dpKey
373
+ */
374
+ export function useGroupConfigFeature<GC, GCPT extends PropertyValueTypes<GC>>
375
+ (key: keyof GC, dpKey?: string): [GCPT | undefined, (value: GCPT, dpValue: any, extraDps?: any, extraConfig?: any) => Promise<Result<any>>] {
376
+ const [groupConfig, setGroupConfig] = useGroupConfig<GC>()
377
+ const setGroupConfigFeature = async (value: GCPT, dpValue: any, extraDps?: any, extraConfig?: any) => {
378
+ const sendDpKey = !!dpKey ? dpKey : GlobalParams.dpSchemaMap[snakeCase(key as string)]?.dp
379
+
380
+ const sendDps = {}
381
+ if (sendDpKey) {
382
+ sendDps[sendDpKey] = dpValue
383
+ }
384
+
385
+ return await setGroupConfig(
386
+ {
387
+ ...sendDps,
388
+ ...(extraDps || {}),
389
+ },
390
+ {
391
+ ...(extraConfig || {}),
392
+ [key]: value,
393
+ },
394
+ )
395
+ }
396
+ return [groupConfig[key] as (GCPT | undefined), setGroupConfigFeature]
397
+ }
398
+
399
+ export function useFeatureHook<GC, T extends PropertyValueTypes<GC>>(
400
+ featureKey: keyof GC,
401
+ defValue: T,
402
+ valueMapToDpValue?: (v: T) => any,
403
+ getExtraDps?: (v: T) => any,
404
+ getExtraConfig?: (v: T) => any,
405
+ ): [T, (value: T) => Promise<Result<any>>] {
406
+ const [featureHook, setFH] = useGroupConfigFeature<GC, T>(featureKey)
407
+ const setFeatureHook = useCallback(async (value: T, extraDps?: any, extraConfig?: any) => {
408
+ const dpValue = valueMapToDpValue ? valueMapToDpValue(value) : value
409
+ const defDps = getExtraDps ? getExtraDps(value) : undefined
410
+ const dps = {...(defDps || {}), ...(extraDps || {})}
411
+ const defConfig = getExtraConfig ? getExtraConfig(value) : undefined
412
+ const config = {...(defConfig || {}), ...(extraConfig || {})}
413
+ return await setFH(value, dpValue, dps, config)
414
+ }, [setFH])
415
+ return [featureHook ?? defValue, setFeatureHook]
416
+ }
417
+
418
+ export const useFanMaxSpeed = () => {
419
+ const { productId } = useDeviceInfo()
420
+ return fanProductList.includes(productId) ? 20 : 3
421
+ }
422
+
423
+ export const isUVCFanDevice = () => {
424
+ const { productId } = useDeviceInfo()
425
+ return fanProductList.includes(productId)
426
+ }
427
+
428
+ export const ldvModules = nativePropsSlice.reducer
429
+
430
+ export const {
431
+ setNativeProps,
432
+ setGroupNativeProps,
433
+ setDps,
434
+ setGroupDps,
435
+ setTimeSchedule,
436
+ setMoods,
437
+ setFlags,
438
+ setFlagMode,
439
+ setGroupDevices,
440
+ setSystemTimeFormat,
441
+ setTimeZone,
442
+ setEnergieverbrauch,
443
+ } = nativePropsSlice.actions
444
+
445
+ export {
446
+ asyncSetDps,
447
+ simpleSetDps,
448
+ simpleSetDp,
449
+ useDeviceId,
450
+ useDeviceInfo,
451
+ useDp,
452
+ useGroupId,
453
+ useGroupDp,
454
+ useScaledDp,
455
+ useDps,
456
+ useFamilyName,
457
+ useRole,
458
+ useTimeSchedule,
459
+ useMoods,
460
+ useFlags,
461
+ useFlagMode,
462
+ useGroupDevices,
463
+ useSystemTimeFormate,
464
+ useTimeZone,
465
+ useTimeZoneCity,
466
+ useEnergieverbrauch,
467
+ }