@ray-js/ipc-player-integration 0.0.1-beta-16 → 0.0.1-beta-18

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.
@@ -2,5 +2,19 @@ type Options<T> = {
2
2
  devId: string;
3
3
  dpCodes: T[];
4
4
  };
5
- export declare function useDpState<T extends string>(options: Options<T>): [Record<T, number | string | boolean>];
5
+ type Schema = {
6
+ id: number;
7
+ code: string;
8
+ mode: string;
9
+ property: {
10
+ type: 'value' | 'enum' | 'bool';
11
+ unit?: string;
12
+ min?: number;
13
+ max?: number;
14
+ scale?: number;
15
+ step?: number;
16
+ range?: Array<unknown>;
17
+ };
18
+ };
19
+ export declare function useDpState<T extends string>(options: Options<T>): [Record<T, number | string | boolean>, Record<string, Schema>];
6
20
  export {};
@@ -14,6 +14,7 @@ export function useDpState(options) {
14
14
  dpCodes
15
15
  } = options;
16
16
  const codeMapToIdRef = useRef();
17
+ const dpCodeSchemaMapsRef = useRef();
17
18
  const [values, setValues] = useState(() => {
18
19
  return getInitData(options.dpCodes);
19
20
  });
@@ -42,38 +43,44 @@ export function useDpState(options) {
42
43
  }
43
44
  }, [devId]);
44
45
  useEffect(() => {
45
- getDeviceInfo({
46
- deviceId: devId,
47
- success: res => {
48
- const {
49
- schema
50
- } = res;
51
- const IdMapToCode = {};
52
- let count = 0;
53
- for (const schemaItem of schema) {
54
- // @ts-ignore
55
- if (dpCodes.includes(schemaItem.code)) {
46
+ if (devId) {
47
+ getDeviceInfo({
48
+ deviceId: devId,
49
+ success: res => {
50
+ const {
51
+ schema
52
+ } = res;
53
+ const IdMapToCode = {};
54
+ const dpCodeSchemaMaps = {};
55
+ let count = 0;
56
+ for (const schemaItem of schema) {
56
57
  // @ts-ignore
57
- IdMapToCode[schemaItem.id] = schemaItem.code;
58
- count += 1;
58
+ if (dpCodes.includes(schemaItem.code)) {
59
+ // @ts-ignore
60
+ IdMapToCode[schemaItem.id] = schemaItem.code;
61
+ // @ts-ignore
62
+ dpCodeSchemaMaps[schemaItem.code] = schemaItem;
63
+ count += 1;
64
+ }
65
+ if (count >= dpCodes.length) break;
59
66
  }
60
- if (count >= dpCodes.length) break;
67
+ codeMapToIdRef.current = IdMapToCode;
68
+ dpCodeSchemaMapsRef.current = dpCodeSchemaMaps;
69
+ const initValue = options.dpCodes.reduce((ret, key) => {
70
+ return _objectSpread(_objectSpread({}, ret), {}, {
71
+ [key]: res.dpCodes[key]
72
+ });
73
+ }, {});
74
+ setValues(initValue);
61
75
  }
62
- codeMapToIdRef.current = IdMapToCode;
63
- const initValue = options.dpCodes.reduce((ret, key) => {
64
- return _objectSpread(_objectSpread({}, ret), {}, {
65
- [key]: res.dpCodes[key]
66
- });
67
- }, {});
68
- setValues(initValue);
69
- }
70
- });
71
- }, [options.devId]);
76
+ });
77
+ }
78
+ }, [devId]);
72
79
  useEffect(() => {
73
80
  onDpDataChange(listenDpChange);
74
81
  return () => {
75
82
  offDpDataChange(listenDpChange);
76
83
  };
77
84
  }, [listenDpChange]);
78
- return [values];
85
+ return [values, dpCodeSchemaMapsRef.current];
79
86
  }
@@ -1,15 +1,20 @@
1
1
  import { useEffect, useState } from 'react';
2
2
  import { useDpState } from '../useDpState';
3
3
  import { SENSOR_HUMIDITY } from '../../utils/content/dpCode';
4
+ import { showMathPowValue } from '../../utils/device';
4
5
  export const useHumidity = devId => {
5
6
  const [humidity, setHumidity] = useState('');
6
- const [dpState] = useDpState({
7
+ const [dpState, schemaObj] = useDpState({
7
8
  devId,
8
9
  dpCodes: [SENSOR_HUMIDITY]
9
10
  });
10
11
  useEffect(() => {
11
12
  if (dpState[SENSOR_HUMIDITY] !== undefined) {
12
- setHumidity(dpState[SENSOR_HUMIDITY] + '%');
13
+ var _schema$property;
14
+ const schema = schemaObj && schemaObj[SENSOR_HUMIDITY];
15
+ const scale = (schema === null || schema === void 0 || (_schema$property = schema.property) === null || _schema$property === void 0 ? void 0 : _schema$property.scale) || 0;
16
+ const value = showMathPowValue(dpState[SENSOR_HUMIDITY], scale);
17
+ setHumidity(value + '%');
13
18
  }
14
19
  }, [dpState]);
15
20
  return humidity;
@@ -1,11 +1,12 @@
1
1
  import { useEffect, useState } from 'react';
2
2
  import { TEMP_REPORT_F, TEMP_UNIT_SELECT, SENSOR_TEMPERATURE } from '../../utils/content/dpCode';
3
3
  import { useDpState } from '../useDpState';
4
+ import { showMathPowValue } from '../../utils/device';
4
5
  export const useTemperature = devId => {
5
6
  const [tempUnit, setTempUnit] = useState('');
6
7
  const [tempC, setTempC] = useState('');
7
8
  const [tempF, setTempF] = useState('');
8
- const [dpState] = useDpState({
9
+ const [dpState, schemaObj] = useDpState({
9
10
  devId,
10
11
  dpCodes: [TEMP_UNIT_SELECT, SENSOR_TEMPERATURE, TEMP_REPORT_F]
11
12
  });
@@ -15,10 +16,18 @@ export const useTemperature = devId => {
15
16
  setTempUnit(dpState[TEMP_UNIT_SELECT] + '');
16
17
  }
17
18
  if (dpState[SENSOR_TEMPERATURE] !== undefined) {
18
- setTempC(dpState[SENSOR_TEMPERATURE] + '°C');
19
+ var _schema$property;
20
+ const schema = schemaObj && schemaObj[SENSOR_TEMPERATURE];
21
+ const scale = (schema === null || schema === void 0 || (_schema$property = schema.property) === null || _schema$property === void 0 ? void 0 : _schema$property.scale) || 0;
22
+ const value = showMathPowValue(dpState[SENSOR_TEMPERATURE], scale);
23
+ setTempC(value + '°C');
19
24
  }
20
25
  if (dpState[TEMP_REPORT_F] !== undefined) {
21
- setTempF(dpState[TEMP_REPORT_F] + '°F');
26
+ var _schema$property2;
27
+ const schema = schemaObj && schemaObj[TEMP_REPORT_F];
28
+ const scale = (schema === null || schema === void 0 || (_schema$property2 = schema.property) === null || _schema$property2 === void 0 ? void 0 : _schema$property2.scale) || 0;
29
+ const value = showMathPowValue(dpState[TEMP_REPORT_F], scale);
30
+ setTempF(value + '°F');
22
31
  }
23
32
  }, [dpState]);
24
33
  return {
@@ -15,18 +15,19 @@ export const TempHumidity = props => {
15
15
  } = useTemperature(devId);
16
16
  const humidity = useHumidity(devId);
17
17
  const tempRender = () => {
18
- if (tempUnit === '1') {
19
- return tempF && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Text, {
18
+ if (tempUnit === '1' && tempF) {
19
+ return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Text, {
20
20
  className: "ipc-player-plugin-tempHumidity-text-icon icon-panel icon-panel-temperature"
21
21
  }), /*#__PURE__*/React.createElement(Text, {
22
22
  className: "tempHumidity"
23
23
  }, tempF));
24
24
  }
25
- return tempC && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Text, {
26
- className: "ipc-player-plugin-tempHumidity-text-icon icon-panel icon-panel-temperature"
27
- }), /*#__PURE__*/React.createElement(Text, null, tempC), /*#__PURE__*/React.createElement(Text, {
28
- className: "ipc-player-plugin-tempHumidity-divider"
29
- }, "/"));
25
+ if (tempC) {
26
+ return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Text, {
27
+ className: "ipc-player-plugin-tempHumidity-text-icon icon-panel icon-panel-temperature"
28
+ }), /*#__PURE__*/React.createElement(Text, null, tempC));
29
+ }
30
+ return /*#__PURE__*/React.createElement(React.Fragment, null);
30
31
  };
31
32
  const humRender = () => {
32
33
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Text, {
@@ -35,13 +36,15 @@ export const TempHumidity = props => {
35
36
  className: "tempHumidity"
36
37
  }, `${humidity}`));
37
38
  };
38
- return /*#__PURE__*/React.createElement(React.Fragment, null, (tempC || humidity) && /*#__PURE__*/React.createElement(View, {
39
+ return /*#__PURE__*/React.createElement(React.Fragment, null, (tempC || tempF || humidity) && /*#__PURE__*/React.createElement(View, {
39
40
  className: clsx('ipc-player-plugin-tempHumidity', className)
40
41
  }, (() => {
41
- if (tempC && humidity) {
42
- return /*#__PURE__*/React.createElement(React.Fragment, null, tempRender(), humRender());
42
+ if ((tempC || tempUnit === '1' && tempF) && humidity) {
43
+ return /*#__PURE__*/React.createElement(React.Fragment, null, tempRender(), /*#__PURE__*/React.createElement(Text, {
44
+ className: "ipc-player-plugin-tempHumidity-divider"
45
+ }, "/"), humRender());
43
46
  }
44
- if (tempC) {
47
+ if (tempC || tempUnit === '1' && tempF) {
45
48
  return /*#__PURE__*/React.createElement(React.Fragment, null, tempRender());
46
49
  }
47
50
  if (humidity) {
@@ -9,5 +9,5 @@ export interface IDpCode {
9
9
  time: number;
10
10
  type: string;
11
11
  }
12
- export declare function getDpCodeObj(devId: string, dpCode: string): Promise<IDpCode | undefined>;
13
12
  export declare function getDpIdsByCodes<T extends string>(devId: string, dpIds: T[]): Promise<Record<T, string>>;
13
+ export declare const showMathPowValue: (value: any, scale: any) => string | 0;
@@ -1,4 +1,3 @@
1
- import { getDpsInfos } from '@ray-js/ray';
2
1
  export function getDpValue(options) {
3
2
  return new Promise((resolve, reject) => {
4
3
  ty.device.getDeviceInfo({
@@ -21,30 +20,6 @@ export function getDpValue(options) {
21
20
  });
22
21
  });
23
22
  }
24
- export function getDpCodeObj(devId, dpCode) {
25
- return new Promise((resolve, reject) => {
26
- getDpsInfos({
27
- devId: devId,
28
- gwId: devId
29
- }).then(res => {
30
- let result;
31
- for (let i = 0; i < res.length; i++) {
32
- const ele = res[i];
33
- const {
34
- code,
35
- value
36
- } = ele;
37
- if (code === dpCode) {
38
- result = ele;
39
- break;
40
- }
41
- }
42
- resolve(result);
43
- }).catch(error => {
44
- reject(error);
45
- });
46
- });
47
- }
48
23
  export function getDpIdsByCodes(devId) {
49
24
  return new Promise((resolve, reject) => {
50
25
  ty.device.getDeviceInfo({
@@ -65,4 +40,16 @@ export function getDpIdsByCodes(devId) {
65
40
  }
66
41
  });
67
42
  });
68
- }
43
+ }
44
+ export const showMathPowValue = (value, scale) => {
45
+ if (isNaN(Number(value))) {
46
+ return 0;
47
+ }
48
+ let v = '';
49
+ if (scale === 0) {
50
+ v = (Number(value) / Math.pow(10, scale)).toFixed(0);
51
+ } else {
52
+ v = (Number(value) / Math.pow(10, scale)).toFixed(1);
53
+ }
54
+ return v;
55
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ray-js/ipc-player-integration",
3
- "version": "0.0.1-beta-16",
3
+ "version": "0.0.1-beta-18",
4
4
  "description": "IPC 播放器功能集成",
5
5
  "main": "lib/index",
6
6
  "files": [