@ledvance/base 1.0.35 → 1.1.0

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.0.35",
7
+ "version": "1.1.0",
8
8
  "scripts": {},
9
9
  "dependencies": {
10
10
  "@reduxjs/toolkit": "^1.8.6",
@@ -5,14 +5,31 @@ import {DevInfo, DpValue, Theme, TYSdk} from 'tuya-panel-kit'
5
5
  import {actions, store} from './models'
6
6
  import {addListener} from 'api/nativeEventEmitter'
7
7
  import {NativeApi} from 'api/native'
8
- import {setNativeProps} from './models/modules/NativePropsSlice'
8
+ import {
9
+ DeviceInfo,
10
+ NativeProps,
11
+ setGroupNativeProps,
12
+ setNativeProps,
13
+ UAGroupInfo,
14
+ } from './models/modules/NativePropsSlice'
9
15
  import {DpSchema, GlobalParams} from './models/GlobalParams'
10
16
  import Connect from './components/connect'
11
17
 
12
18
  interface Props {
13
- devInfo: DevInfo;
14
- preload?: boolean;
15
- ldvDevInfo: any;
19
+ devInfo: DevInfo
20
+ preload?: boolean
21
+ ldvDevInfo: LdvDevInfo
22
+ uaGroupInfo: UAGroupInfoProps
23
+ }
24
+
25
+ interface LdvDevInfo extends DeviceInfo {
26
+ schema: any
27
+ familyName: string
28
+ }
29
+
30
+ interface UAGroupInfoProps extends UAGroupInfo {
31
+ schema: any
32
+ familyName: string
16
33
  }
17
34
 
18
35
  const TYEvent = TYSdk.event
@@ -91,16 +108,66 @@ const composeLayout = (component: React.ComponentType) => {
91
108
  TYDevice.getDeviceInfo().then(data => dispatch(actions.common.devInfoChange(data)))
92
109
  }
93
110
 
94
- this.initReduxNativeProps(props.ldvDevInfo)
111
+ if (!!props.ldvDevInfo) {
112
+ this.initReduxDeviceNativeProps(props.ldvDevInfo)
113
+ } else {
114
+ this.initReduxGroupNativeProps(props.uaGroupInfo)
115
+ }
95
116
  }
96
117
 
97
- initReduxNativeProps(ldvDevInfo) {
118
+ initReduxDeviceNativeProps(ldvDevInfo: LdvDevInfo) {
98
119
  console.log('原生数据:', JSON.stringify(ldvDevInfo))
99
120
  NativeApi.log('原生数据: ' + JSON.stringify(ldvDevInfo))
121
+ const dps = this.setDpSchemaMap(ldvDevInfo.schema)
122
+ const nativeProps: NativeProps = {
123
+ deviceInfo: {
124
+ devId: ldvDevInfo.devId,
125
+ dps: {...dps, ...JSON.parse(ldvDevInfo.dps)},
126
+ pId: ldvDevInfo.pId,
127
+ },
128
+ uaGroupInfo: {
129
+ tyGroupId: -1,
130
+ dps: {},
131
+ pId: '',
132
+ },
133
+ familyName: ldvDevInfo.familyName,
134
+ timeSchedule: Symbol(),
135
+ }
136
+ NativeApi.showObj(nativeProps)
137
+ console.log('Redux 初始数据:', JSON.stringify(nativeProps))
138
+ NativeApi.log('Redux 初始数据:' + JSON.stringify(nativeProps))
139
+ dispatch(setNativeProps(nativeProps))
140
+ }
141
+
142
+ initReduxGroupNativeProps(uaGroupInfo: UAGroupInfoProps) {
143
+ console.log('原生数据:', JSON.stringify(uaGroupInfo))
144
+ NativeApi.log('原生数据: ' + JSON.stringify(uaGroupInfo))
145
+ const dps = this.setDpSchemaMap(uaGroupInfo.schema)
146
+ const nativeProps: NativeProps = {
147
+ deviceInfo: {
148
+ devId: '',
149
+ dps: {},
150
+ pId: '',
151
+ },
152
+ uaGroupInfo: {
153
+ tyGroupId: uaGroupInfo.tyGroupId,
154
+ dps: {...dps, ...JSON.parse(uaGroupInfo.dps)},
155
+ pId: uaGroupInfo.pId,
156
+ },
157
+ familyName: uaGroupInfo.familyName,
158
+ timeSchedule: Symbol(),
159
+ }
160
+ NativeApi.showObj(nativeProps)
161
+ console.log('Redux 初始数据:', JSON.stringify(nativeProps))
162
+ NativeApi.log('Redux 初始数据:' + JSON.stringify(nativeProps))
163
+ dispatch(setGroupNativeProps(nativeProps))
164
+ }
165
+
166
+ setDpSchemaMap(schema: string): any {
100
167
  // 处理物模型协议和dps
101
168
  const dpSchemaMap: Record<string, DpSchema> = {}
102
169
  const dps = {}
103
- JSON.parse(this.props.ldvDevInfo.schema || [])
170
+ JSON.parse(schema || '[]')
104
171
  .forEach(schemaItem => {
105
172
  dpSchemaMap[schemaItem.code] = {
106
173
  name: schemaItem.name,
@@ -111,17 +178,8 @@ const composeLayout = (component: React.ComponentType) => {
111
178
  }
112
179
  dps[schemaItem.id] = null
113
180
  })
114
- const nativeProps = {
115
- devId: ldvDevInfo.devId,
116
- pId: ldvDevInfo.pId,
117
- familyName: ldvDevInfo.familyName,
118
- dps: {...dps, ...JSON.parse(ldvDevInfo.dps)},
119
- }
120
181
  GlobalParams.dpSchemaMap = dpSchemaMap
121
- NativeApi.showObj(nativeProps)
122
- console.log('Redux 初始数据:', JSON.stringify(nativeProps))
123
- NativeApi.log('Redux 初始数据:' + JSON.stringify(nativeProps))
124
- dispatch(setNativeProps(nativeProps))
182
+ return dps
125
183
  }
126
184
 
127
185
  render() {
@@ -7,26 +7,44 @@ import {NativeApi} from '../../api/native'
7
7
  import {useDispatch} from 'react-redux'
8
8
 
9
9
  export interface NativeProps {
10
- devId: string,
11
- pId: string,
12
- dps: any,
13
- familyName: string,
14
- timeSchedule: symbol;
10
+ familyName: string
11
+ deviceInfo: DeviceInfo
12
+ uaGroupInfo: UAGroupInfo
13
+ timeSchedule: symbol
15
14
  energieverbrauch?: object
16
15
  }
17
16
 
17
+ export interface DeviceInfo {
18
+ devId: string
19
+ pId: string
20
+ dps: any
21
+ }
22
+
23
+ export interface UAGroupInfo {
24
+ tyGroupId: number
25
+ pId: string
26
+ dps: any
27
+ }
28
+
18
29
  const initialState: NativeProps = {
19
- devId: '',
20
- pId: '',
21
- dps: {},
22
30
  familyName: '',
31
+ deviceInfo: {
32
+ devId: '',
33
+ pId: '',
34
+ dps: {},
35
+ },
36
+ uaGroupInfo: {
37
+ tyGroupId: -1,
38
+ pId: '',
39
+ dps: {},
40
+ },
23
41
  timeSchedule: Symbol(),
24
- energieverbrauch: {}
42
+ energieverbrauch: {},
25
43
  }
26
44
 
27
45
  // energy generation
28
46
  const productList = [
29
- 'rcqe1i17x0vrvws7'
47
+ 'rcqe1i17x0vrvws7',
30
48
  ]
31
49
 
32
50
  const nativePropsSlice = createSlice({
@@ -34,19 +52,29 @@ const nativePropsSlice = createSlice({
34
52
  initialState: initialState,
35
53
  reducers: {
36
54
  setNativeProps: (state, action: PayloadAction<NativeProps>) => {
37
- state.devId = action.payload.devId
55
+ state.deviceInfo.devId = action.payload.deviceInfo.devId
56
+ if (!!action.payload.familyName) {
57
+ state.familyName = action.payload.familyName
58
+ }
59
+ if (!!action.payload.deviceInfo.devId) {
60
+ state.deviceInfo.pId = action.payload.deviceInfo.pId
61
+ }
62
+ state.deviceInfo.dps = {...state.deviceInfo.dps, ...action.payload.deviceInfo.dps}
63
+ },
64
+ setGroupNativeProps: (state, action: PayloadAction<NativeProps>) => {
65
+ state.uaGroupInfo.tyGroupId = action.payload.uaGroupInfo.tyGroupId
38
66
  if (!!action.payload.familyName) {
39
67
  state.familyName = action.payload.familyName
40
68
  }
41
- if (!!action.payload.pId) {
42
- state.pId = action.payload.pId
69
+ if (!!action.payload.uaGroupInfo.tyGroupId) {
70
+ state.uaGroupInfo.pId = action.payload.uaGroupInfo.pId
43
71
  }
44
- state.dps = {...state.dps, ...action.payload.dps}
72
+ state.uaGroupInfo.dps = {...state.uaGroupInfo.dps, ...action.payload.uaGroupInfo.dps}
45
73
  },
46
74
  setDps(state, action: PayloadAction<any>) {
47
75
  const dpKeys = Object.keys(action.payload)
48
76
  dpKeys.forEach(dp => {
49
- state.dps[dp] = action.payload[dp]
77
+ state.deviceInfo.dps[dp] = action.payload[dp]
50
78
  })
51
79
  },
52
80
  setTimeSchedule(state, action: PayloadAction<any>) {
@@ -98,7 +126,7 @@ function simpleSetDp<T>(dispatch: Dispatch<any>): (deviceId: string, dp: string,
98
126
  }
99
127
 
100
128
  const useDeviceId = () => {
101
- return useSelector(store => store.ldvModules.devId)
129
+ return useSelector(store => store.ldvModules.deviceInfo.devId)
102
130
  }
103
131
 
104
132
  const useFamilyName: () => string = () => {
@@ -108,7 +136,7 @@ const useFamilyName: () => string = () => {
108
136
  function useDp<T, R>(dp: string): [T, (value: T) => Promise<Result<R>>] {
109
137
  const deviceId = useDeviceId()
110
138
  const dispatch = useDispatch()
111
- const dpValue = useSelector(store => store.ldvModules.dps[dp])
139
+ const dpValue = useSelector(store => store.ldvModules.deviceInfo.dps[dp])
112
140
  const setDp = useCallback((dpValue: T) => {
113
141
  return simpleSetDp<R>(dispatch)(deviceId, dp, dpValue)
114
142
  }, [])
@@ -118,7 +146,7 @@ function useDp<T, R>(dp: string): [T, (value: T) => Promise<Result<R>>] {
118
146
  function useDps<R>(): [any, (dps: any) => Promise<Result<R>>] {
119
147
  const deviceId = useDeviceId()
120
148
  const dispatch = useDispatch()
121
- const dps = useSelector(store => store.ldvModules.dps)
149
+ const dps = useSelector(store => store.ldvModules.deviceInfo.dps)
122
150
  const setDps = useCallback((dps: any) => {
123
151
  return simpleSetDps<R>(dispatch)(deviceId, dps)
124
152
  }, [])
@@ -135,10 +163,10 @@ const useDeviceInfo: () => DevInfo<DpState> = () => {
135
163
  return useSelector(state => state.devInfo)
136
164
  }
137
165
 
138
- const useTimeSchedule = (): [v:Symbol, f:any] => {
166
+ const useTimeSchedule = (): [v: Symbol, f: any] => {
139
167
  const dps = useSelector(store => store.ldvModules.timeSchedule)
140
168
  const dispatch = useDispatch()
141
- const setTimeScheduleFn = (value: Symbol) =>{
169
+ const setTimeScheduleFn = (value: Symbol) => {
142
170
  dispatch(setTimeSchedule(value))
143
171
  }
144
172
  return [dps, setTimeScheduleFn]
@@ -147,20 +175,26 @@ const useTimeSchedule = (): [v:Symbol, f:any] => {
147
175
  const useEnergieverbrauch = () => {
148
176
  const dps = useSelector(store => store.ldvModules.energieverbrauch)
149
177
  const dispatch = useDispatch()
150
- const setEnergieverbrauchFn = (value) =>{
178
+ const setEnergieverbrauchFn = (value) => {
151
179
  dispatch(setEnergieverbrauch(value))
152
180
  }
153
181
  return [dps, setEnergieverbrauchFn]
154
182
  }
155
183
 
156
- export const useEngergyGeneration = () =>{
157
- const { productId } = useDeviceInfo()
184
+ export const useEngergyGeneration = () => {
185
+ const {productId} = useDeviceInfo()
158
186
  return productList.some(val => val === productId)
159
187
  }
160
188
 
161
189
  export const ldvModules = nativePropsSlice.reducer
162
190
 
163
- export const {setNativeProps, setDps, setTimeSchedule, setEnergieverbrauch} = nativePropsSlice.actions
191
+ export const {
192
+ setNativeProps,
193
+ setGroupNativeProps,
194
+ setDps,
195
+ setTimeSchedule,
196
+ setEnergieverbrauch,
197
+ } = nativePropsSlice.actions
164
198
 
165
199
  export {
166
200
  simpleSetDps,
@@ -171,5 +205,5 @@ export {
171
205
  useDps,
172
206
  useFamilyName,
173
207
  useTimeSchedule,
174
- useEnergieverbrauch
208
+ useEnergieverbrauch,
175
209
  }