@ledvance/base 1.1.7 → 1.1.9

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/base",
5
5
  "pid": [],
6
6
  "uiid": "",
7
- "version": "1.1.7",
7
+ "version": "1.1.9",
8
8
  "scripts": {},
9
9
  "dependencies": {
10
10
  "@reduxjs/toolkit": "^1.8.6",
@@ -16,6 +16,7 @@ export declare class NativeApi {
16
16
  static toDeviceSettingsPage(deviceId: string): void;
17
17
  static putJson(deviceId: string, featureType: string, json: string): Promise<Result<any>>;
18
18
  static getJson(deviceId: string, featureType: string): Promise<Result<string>>;
19
+ static groupControl(dps: string, config: string): Promise<Result<any>>;
19
20
  }
20
21
  export declare const openDownloadFile: (filePath: string) => Promise<unknown>;
21
22
  export declare const queryDpIds: (dpIds: string, deviceId: string) => Promise<unknown>;
package/src/api/native.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { NativeModules, Platform } from 'react-native'
2
- import { NativeResult, Result } from '../models/modules/Result'
1
+ import {NativeModules, Platform} from 'react-native'
2
+ import {NativeResult, Result} from '../models/modules/Result'
3
3
 
4
4
  interface LDVDevicePanelManager {
5
5
  back: () => void
@@ -15,7 +15,7 @@ interface LDVDevicePanelManager {
15
15
  openDownloadFile: (filePath: string) => void
16
16
  formatNumber: (num: number, fixed: number) => string
17
17
  getTimeZone: () => string
18
-
18
+ groupControl: (dps: string, config: string) => Promise<NativeResult<any>>
19
19
  }
20
20
 
21
21
  const devicePanel: LDVDevicePanelManager = NativeModules.LDVDevicePanelManager
@@ -213,8 +213,16 @@ export class NativeApi {
213
213
  })
214
214
  })
215
215
  }
216
- }
217
216
 
217
+ static async groupControl(dps: string, config: string): Promise<Result<any>> {
218
+ const nativeResult = await devicePanel.groupControl(dps, config)
219
+ return {
220
+ success: nativeResult.result,
221
+ msg: nativeResult.msg,
222
+ data: nativeResult.data
223
+ }
224
+ }
225
+ }
218
226
 
219
227
  // 打开下载文件
220
228
  export const openDownloadFile = (filePath: string) => {
@@ -34,7 +34,16 @@ declare const useTimeSchedule: () => [v: symbol | undefined, f: any];
34
34
  declare const useEnergieverbrauch: () => (object | undefined)[];
35
35
  export declare const useEngergyGeneration: () => boolean;
36
36
  export declare function useUAGroupInfo(): UAGroupInfo;
37
- export declare function useGroupConfig<T>(): T;
37
+ export declare function useGroupConfig<T>(): [T, (dps: any, newConfig: T) => Promise<Result<any>>];
38
+ /**
39
+ * @template GC GroupConfig
40
+ * @template GCPT GroupConfigPropertyType
41
+ * @param key
42
+ * @param extraDps
43
+ */
44
+ export declare function useGroupConfigFeature<GC, GCPT extends {
45
+ [K in keyof GC]: GC[K];
46
+ }[keyof GC]>(key: keyof GC, extraDps?: any): [GCPT, (value: GCPT) => Promise<Result<any>>];
38
47
  export declare const ldvModules: import("@reduxjs/toolkit").Reducer<NativeProps, import("@reduxjs/toolkit").AnyAction>;
39
48
  export declare const setNativeProps: import("@reduxjs/toolkit").ActionCreatorWithPayload<NativeProps, string>, setGroupNativeProps: import("@reduxjs/toolkit").ActionCreatorWithPayload<NativeProps, string>, setDps: import("@reduxjs/toolkit").ActionCreatorWithPayload<any, string>, setTimeSchedule: import("@reduxjs/toolkit").ActionCreatorWithPayload<any, string>, setEnergieverbrauch: import("@reduxjs/toolkit").ActionCreatorWithPayload<any, string>;
40
49
  export { simpleSetDps, simpleSetDp, useDeviceId, useDeviceInfo, useDp, useDps, useFamilyName, useTimeSchedule, useEnergieverbrauch, };
@@ -5,6 +5,8 @@ import {DpsResult, Result} from './Result'
5
5
  import {DevInfo, DpValue} from 'tuya-panel-kit'
6
6
  import {NativeApi} from '../../api/native'
7
7
  import {useDispatch} from 'react-redux'
8
+ import {GlobalParams} from '../GlobalParams'
9
+ import {camelCase} from 'lodash'
8
10
 
9
11
  export interface NativeProps {
10
12
  familyName: string
@@ -74,6 +76,9 @@ const nativePropsSlice = createSlice({
74
76
  state.uaGroupInfo.dps = {...state.uaGroupInfo.dps, ...action.payload.uaGroupInfo.dps}
75
77
  state.uaGroupInfo.config = action.payload.uaGroupInfo.config
76
78
  },
79
+ setGroupConfig: (state, action: PayloadAction<any>) => {
80
+ state.uaGroupInfo.config = action.payload
81
+ },
77
82
  setDps(state, action: PayloadAction<any>) {
78
83
  const dpKeys = Object.keys(action.payload)
79
84
  dpKeys.forEach(dp => {
@@ -193,8 +198,44 @@ export function useUAGroupInfo(): UAGroupInfo {
193
198
  return useSelector(state => state.ldvModules.uaGroupInfo)
194
199
  }
195
200
 
196
- export function useGroupConfig<T>(): T {
197
- return useUAGroupInfo().config as T
201
+ export function useGroupConfig<T>(): [T, (dps: any, newConfig: T) => Promise<Result<any>>] {
202
+ const config = useUAGroupInfo().config as T
203
+ const dispatch = useDispatch()
204
+ const {setGroupConfig} = nativePropsSlice.actions
205
+ const setConfig = useCallback(async (dps: any, newConfig: T) => {
206
+ // 发送控制命令
207
+ const res = await NativeApi.groupControl(JSON.stringify(dps), JSON.stringify(newConfig))
208
+ if (res.success) {
209
+ // 只在结果成功时才改变 redux 数据
210
+ dispatch(setGroupConfig(newConfig))
211
+ }
212
+ return res
213
+ }, [])
214
+ return [config, setConfig]
215
+ }
216
+
217
+ /**
218
+ * @template GC GroupConfig
219
+ * @template GCPT GroupConfigPropertyType
220
+ * @param key
221
+ * @param extraDps
222
+ */
223
+ export function useGroupConfigFeature<GC, GCPT extends { [K in keyof GC]: GC[K] }[keyof GC]>
224
+ (key: keyof GC, extraDps: any = {}): [GCPT, (value: GCPT) => Promise<Result<any>>] {
225
+ const [groupConfig, setGroupConfig] = useGroupConfig<GC>()
226
+ const setGroupConfigFeature = async (value: GCPT) => {
227
+ return await setGroupConfig(
228
+ {
229
+ [GlobalParams.dpSchemaMap[camelCase(key as string)].dp]: value,
230
+ ...extraDps,
231
+ },
232
+ {
233
+ ...groupConfig,
234
+ [key]: value,
235
+ },
236
+ )
237
+ }
238
+ return [groupConfig[key] as GCPT, setGroupConfigFeature]
198
239
  }
199
240
 
200
241
  export const ldvModules = nativePropsSlice.reducer