@ray-js/lamp-schedule-core 1.0.0-beta-1 → 1.0.0-beta-3

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,6 +2,8 @@ import "core-js/modules/esnext.iterator.constructor.js";
2
2
  import "core-js/modules/esnext.iterator.for-each.js";
3
3
  import "core-js/modules/esnext.iterator.map.js";
4
4
  import _padStart from 'lodash/padStart';
5
+ import { scheduleLogger as ScheduleLogger } from '../utils/ScheduleLogger';
6
+
5
7
  // 补充工具函数
6
8
  const numToHexString = function () {
7
9
  let value = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
@@ -15,7 +17,10 @@ export class TimerReportFormatter {
15
17
  }
16
18
  parser() {
17
19
  let val = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
18
- if (val) {
20
+ if (!val) {
21
+ return this.defaultValue;
22
+ }
23
+ try {
19
24
  const arr = [];
20
25
  const strArr = val.match(/[a-z0-9]{2}/g);
21
26
  strArr.forEach(item => {
@@ -26,18 +31,25 @@ export class TimerReportFormatter {
26
31
  });
27
32
  });
28
33
  return arr;
34
+ } catch (error) {
35
+ ScheduleLogger.error('TimerReportFormatter parser error', error);
36
+ return this.defaultValue;
29
37
  }
30
- return this.defaultValue;
31
38
  }
32
39
  formatter(tasks) {
33
- return tasks.map(task => {
34
- // 组装ABBBBBBB
35
- const binStr = (task.status ? '1' : '0') +
36
- // A位
37
- (+task.id).toString(2).padStart(7, '0'); // BBBBBBB
38
- const hexStr = numToHexString(parseInt(binStr, 2));
39
- return hexStr;
40
- }).join('');
40
+ try {
41
+ return tasks.map(task => {
42
+ // 组装ABBBBBBB
43
+ const binStr = (task.status ? '1' : '0') +
44
+ // A位
45
+ (+task.id).toString(2).padStart(7, '0'); // BBBBBBB
46
+ const hexStr = numToHexString(parseInt(binStr, 2));
47
+ return hexStr;
48
+ }).join('');
49
+ } catch (error) {
50
+ ScheduleLogger.error('TimerReportFormatter formatter error', error);
51
+ return '';
52
+ }
41
53
  }
42
54
  }
43
55
  export const timerReportParser = new TimerReportFormatter();
@@ -17,90 +17,100 @@ export class WakeUp {
17
17
  nodes: []
18
18
  };
19
19
  parser(dpStr) {
20
- var _dpStr$slice;
21
20
  scheduleLogger.debug('WakeUp parser dpStr: ', dpStr);
22
21
  if (!dpStr) {
23
22
  return this.defaultValue;
24
23
  }
25
- const step = generateDpStrStep(dpStr);
26
- const version = step(2).value;
27
- let number = step(2).value;
28
- const nodeLen = (_dpStr$slice = dpStr.slice(4)) === null || _dpStr$slice === void 0 ? void 0 : _dpStr$slice.length;
29
- if (nodeLen % 24 !== 0) {
30
- scheduleLogger.error('wakeup dpStr error nodeLen:', nodeLen);
31
- }
32
- const nodes = [];
33
- number = Math.min(number, 4);
34
- // 解析循环节点
35
- for (let i = 0; i < number; i++) {
36
- const onOff = !!step(2).value;
37
- const repeatInt = step(2).value; // 单次定时任务bit0-bit6全为0,bit0-bit6某位为1时,表示对应周几周期定时 bit0 => 周日
38
- const loops = padEnd(repeatInt.toString(2).split('').reverse().join(''), 7, '0');
39
- const stepNum = step(2).value;
40
- const hour = step(2).value;
41
- const minute = step(2).value;
24
+ try {
25
+ var _dpStr$slice;
26
+ const step = generateDpStrStep(dpStr);
27
+ const version = step(2).value;
28
+ let number = step(2).value;
29
+ const nodeLen = (_dpStr$slice = dpStr.slice(4)) === null || _dpStr$slice === void 0 ? void 0 : _dpStr$slice.length;
30
+ if (nodeLen % 24 !== 0) {
31
+ scheduleLogger.error('wakeup dpStr error nodeLen:', nodeLen);
32
+ }
33
+ const nodes = [];
34
+ number = Math.min(number, 4);
35
+ // 解析循环节点
36
+ for (let i = 0; i < number; i++) {
37
+ const onOff = !!step(2).value;
38
+ const repeatInt = step(2).value; // 单次定时任务bit0-bit6全为0,bit0-bit6某位为1时,表示对应周几周期定时 bit0 => 周日
39
+ const loops = padEnd(repeatInt.toString(2).split('').reverse().join(''), 7, '0');
40
+ const stepNum = step(2).value;
41
+ const hour = step(2).value;
42
+ const minute = step(2).value;
42
43
 
43
- // 解析色相值(组合百位和十个位)
44
- const hundreds = step(2).value;
45
- const tensOnes = step(2).value;
46
- const saturation = step(2).value;
47
- const val = step(2).value;
48
- const brightness = step(2).value;
49
- const temperature = step(2).value;
50
- const durationStep = step(2);
51
- const duration = durationStep.value;
52
- nodes.push({
53
- onOff,
54
- loops,
55
- step: stepNum,
56
- hour,
57
- minute,
58
- hue: Math.round(hundreds * 100 + tensOnes),
59
- // 0-360
60
- saturation,
61
- value: val,
62
- brightness,
63
- temperature,
64
- duration,
65
- index: i
66
- });
67
- if (durationStep.done) {
68
- break;
44
+ // 解析色相值(组合百位和十个位)
45
+ const hundreds = step(2).value;
46
+ const tensOnes = step(2).value;
47
+ const saturation = step(2).value;
48
+ const val = step(2).value;
49
+ const brightness = step(2).value;
50
+ const temperature = step(2).value;
51
+ const durationStep = step(2);
52
+ const duration = durationStep.value;
53
+ nodes.push({
54
+ onOff,
55
+ loops,
56
+ step: stepNum,
57
+ hour,
58
+ minute,
59
+ hue: Math.round(hundreds * 100 + tensOnes),
60
+ // 0-360
61
+ saturation,
62
+ value: val,
63
+ brightness,
64
+ temperature,
65
+ duration,
66
+ index: i
67
+ });
68
+ if (durationStep.done) {
69
+ break;
70
+ }
69
71
  }
72
+ return {
73
+ version,
74
+ number,
75
+ nodes
76
+ };
77
+ } catch (error) {
78
+ scheduleLogger.error('wakeup parser error', error);
79
+ return this.defaultValue;
70
80
  }
71
- return {
72
- version,
73
- number,
74
- nodes
75
- };
76
81
  }
77
82
  formatter(data) {
78
- var _data$nodes, _data$nodes2;
79
- const result = [numToHexString(data.version), numToHexString((_data$nodes = data.nodes) === null || _data$nodes === void 0 ? void 0 : _data$nodes.length)];
80
- scheduleLogger.debug('wakeup formatter data', data);
81
- data === null || data === void 0 || (_data$nodes2 = data.nodes) === null || _data$nodes2 === void 0 || _data$nodes2.forEach(node => {
82
- // 拆分色相值为百位和十个位
83
- const hueHundreds = Math.floor(node.hue / 100);
84
- const hueTensOnes = node.hue % 100;
85
- const {
86
- saturation,
87
- value,
88
- brightness,
89
- temperature
90
- } = node;
91
- let saturation1 = node.saturation;
92
- let value1 = node.value;
93
- let brightness1 = node.brightness;
94
- let temperature1 = node.temperature;
95
- if (saturation === 0 && value === 0 && brightness === 0 && temperature === 0) {
96
- saturation1 = defaultColor.saturation;
97
- value1 = defaultColor.value;
98
- brightness1 = defaultColor.brightness;
99
- temperature1 = defaultColor.temperature;
100
- }
101
- result.push(numToHexString(node.onOff ? 1 : 0), numToHexString(parseInt(node.loops.split('').reverse().join(''), 2), 2), numToHexString(node.step), numToHexString(node.hour), numToHexString(node.minute), numToHexString(hueHundreds), numToHexString(hueTensOnes), numToHexString(saturation1), numToHexString(value1), numToHexString(brightness1), numToHexString(temperature1), numToHexString(node.duration));
102
- });
103
- return result.join('');
83
+ try {
84
+ var _data$nodes, _data$nodes2;
85
+ const result = [numToHexString(data.version), numToHexString((_data$nodes = data.nodes) === null || _data$nodes === void 0 ? void 0 : _data$nodes.length)];
86
+ scheduleLogger.debug('wakeup formatter data', data);
87
+ data === null || data === void 0 || (_data$nodes2 = data.nodes) === null || _data$nodes2 === void 0 || _data$nodes2.forEach(node => {
88
+ // 拆分色相值为百位和十个位
89
+ const hueHundreds = Math.floor(node.hue / 100);
90
+ const hueTensOnes = node.hue % 100;
91
+ const {
92
+ saturation,
93
+ value,
94
+ brightness,
95
+ temperature
96
+ } = node;
97
+ let saturation1 = node.saturation;
98
+ let value1 = node.value;
99
+ let brightness1 = node.brightness;
100
+ let temperature1 = node.temperature;
101
+ if (saturation === 0 && value === 0 && brightness === 0 && temperature === 0) {
102
+ saturation1 = defaultColor.saturation;
103
+ value1 = defaultColor.value;
104
+ brightness1 = defaultColor.brightness;
105
+ temperature1 = defaultColor.temperature;
106
+ }
107
+ result.push(numToHexString(node.onOff ? 1 : 0), numToHexString(parseInt(node.loops.split('').reverse().join(''), 2), 2), numToHexString(node.step), numToHexString(node.hour), numToHexString(node.minute), numToHexString(hueHundreds), numToHexString(hueTensOnes), numToHexString(saturation1), numToHexString(value1), numToHexString(brightness1), numToHexString(temperature1), numToHexString(node.duration));
108
+ });
109
+ return result.join('');
110
+ } catch (error) {
111
+ scheduleLogger.error('wakeup formatter error', error);
112
+ return '';
113
+ }
104
114
  }
105
115
  }
106
116
  export const wakeupParser = new WakeUp();
@@ -10,12 +10,21 @@ type TDpRes<T> = {
10
10
  };
11
11
  export declare function useBaseLightDp<T>(dpCode: string, defaultDpValue?: T): TDpRes<T>;
12
12
  export declare const getDpDataByMesh: (dpCodes?: string[]) => void;
13
+ export declare const fetchDpData: (dpCodes?: string[]) => void;
13
14
  /**
14
- * 主动拉取 dp 数据
15
+ * 主动拉取 dp 数据, 仅支持 sigmesh 设备
15
16
  * @param dpCodes dpCode 数组
16
17
  * @returns refresh 刷新 dp 数据
17
18
  */
18
19
  export declare const useFetchDpDataByMesh: (dpCodes: string[]) => {
19
20
  refresh: () => void;
20
21
  };
22
+ /**
23
+ * 主动拉取 dp 数据, 不限制协议
24
+ * @param dpCodes dpCode 数组
25
+ * @returns refresh 刷新 dp 数据
26
+ */
27
+ export declare const useFetchDpData: (dpCodes: string[]) => {
28
+ refresh: () => void;
29
+ };
21
30
  export {};
@@ -134,8 +134,48 @@ export const getDpDataByMesh = function () {
134
134
  scheduleLogger.error('getDpDataByMesh 调用失败:', error);
135
135
  }
136
136
  };
137
+
138
+ // 查询设备的 DP
139
+ export const fetchDpData = function () {
140
+ let dpCodes = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
141
+ if (!Array.isArray(dpCodes)) {
142
+ scheduleLogger.error('fetchDpData: 参数dpCodes必须是数组');
143
+ return;
144
+ }
145
+ if (dpCodes.length >= 5) {
146
+ scheduleLogger.warn('fetchDpData: 单次查询 dp 数过多,dp 可能响应不及时,请分次查询');
147
+ }
148
+ scheduleLogger.debug('fetchDpData 开始查询DP参数 dpCodes:', dpCodes);
149
+ const deviceData = devIdOrGroupIdCache.get();
150
+ const deviceId = deviceData === null || deviceData === void 0 ? void 0 : deviceData.devId;
151
+ if (!deviceId) {
152
+ scheduleLogger.error('fetchDpData: 未能获取到deviceId');
153
+ return;
154
+ }
155
+ const dpIds = dpCodes.map(item => getDpIdByDpCode(item)).filter(i => !!i).map(String); // 简化字符串转换
156
+
157
+ if (dpIds.length === 0) {
158
+ scheduleLogger.warn('fetchDpData', dpCodes, '未匹配到 dp');
159
+ return;
160
+ }
161
+ scheduleLogger.debug('fetchDpData 查询DP转换后参数 dpIds:', dpIds);
162
+ try {
163
+ queryDps({
164
+ deviceId,
165
+ dpIds,
166
+ success: e => {
167
+ scheduleLogger.debug('fetchDpData 查询DP参数 success:', e);
168
+ },
169
+ fail: err => {
170
+ scheduleLogger.warn('fetchDpData 查询DP参数 fail:', err);
171
+ }
172
+ });
173
+ } catch (error) {
174
+ scheduleLogger.error('fetchDpData 调用失败:', error);
175
+ }
176
+ };
137
177
  /**
138
- * 主动拉取 dp 数据
178
+ * 主动拉取 dp 数据, 仅支持 sigmesh 设备
139
179
  * @param dpCodes dpCode 数组
140
180
  * @returns refresh 刷新 dp 数据
141
181
  */
@@ -148,7 +188,7 @@ export const useFetchDpDataByMesh = dpCodes => {
148
188
  return;
149
189
  }
150
190
  scheduleLogger.debug('getDpDataByMeshFn get:', dpCodes);
151
- getDpDataByMesh(dpCodes);
191
+ fetchDpData(dpCodes);
152
192
  };
153
193
  useEffect(() => {
154
194
  const isSigMeshDevice = support === null || support === void 0 ? void 0 : support.isSigMeshDevice();
@@ -162,4 +202,23 @@ export const useFetchDpDataByMesh = dpCodes => {
162
202
  return {
163
203
  refresh: getDpDataByMeshFn
164
204
  };
205
+ };
206
+
207
+ /**
208
+ * 主动拉取 dp 数据, 不限制协议
209
+ * @param dpCodes dpCode 数组
210
+ * @returns refresh 刷新 dp 数据
211
+ */
212
+ export const useFetchDpData = dpCodes => {
213
+ const getDpDataFn = () => {
214
+ scheduleLogger.debug('getDpDataFn get:', dpCodes);
215
+ fetchDpData(dpCodes);
216
+ };
217
+ useEffect(() => {
218
+ scheduleLogger.debug('useFetchDpData get:', dpCodes);
219
+ getDpDataFn();
220
+ }, [dpCodes.join(',')]);
221
+ return {
222
+ refresh: getDpDataFn
223
+ };
165
224
  };
@@ -1,4 +1,4 @@
1
- import { useBaseLightDp, useFetchDpDataByMesh } from './useBaseLightDp';
1
+ import { useBaseLightDp, useFetchDpData } from './useBaseLightDp';
2
2
  import { scheduleDpCodes } from '../config/dpCodes';
3
3
  export function useCountdownDp() {
4
4
  let dpCode = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : scheduleDpCodes.COUNTDOWN;
@@ -21,7 +21,7 @@ export const useCountdownDpPull = function () {
21
21
  let countdownCode = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : scheduleDpCodes.COUNTDOWN;
22
22
  const {
23
23
  refresh
24
- } = useFetchDpDataByMesh([countdownCode]);
24
+ } = useFetchDpData([countdownCode]);
25
25
  return {
26
26
  refresh
27
27
  };
@@ -1,6 +1,6 @@
1
1
  /* eslint-disable no-param-reassign */
2
2
  import { useMemo } from 'react';
3
- import { useBaseLightDp, useFetchDpDataByMesh } from './useBaseLightDp';
3
+ import { useBaseLightDp, useFetchDpData } from './useBaseLightDp';
4
4
  import { scheduleDpCodes } from '../config/dpCodes';
5
5
  import { scheduleLogger as ScheduleLogger } from '../utils/ScheduleLogger';
6
6
  import { timerReportParser } from '../dpParser';
@@ -41,7 +41,7 @@ export const useTimerReportDpPull = function () {
41
41
  ScheduleLogger.debug('useTimerReportDpPull 执行');
42
42
  const {
43
43
  refresh
44
- } = useFetchDpDataByMesh([timerReport]);
44
+ } = useFetchDpData([timerReport]);
45
45
  return {
46
46
  refresh
47
47
  };
package/lib/index.d.ts CHANGED
@@ -19,7 +19,7 @@ export { useWakeUpDp } from './hooks/useWakeUpDp';
19
19
  export { useRandomDp } from './hooks/useRandomDp';
20
20
  export { useCycleDp } from './hooks/useCycleDp';
21
21
  export { useWakeUpSupport } from './hooks/useWakeUpSupport';
22
- export { useBaseLightDp, useFetchDpDataByMesh, getDpDataByMesh } from './hooks/useBaseLightDp';
22
+ export { useBaseLightDp, useFetchDpDataByMesh, getDpDataByMesh, useFetchDpData } from './hooks/useBaseLightDp';
23
23
  export { useTimerReportDp, useTimerReportDpPull } from './hooks/useTimerReportDp';
24
24
  /** Conflict 冲突判断 */
25
25
  export { transScheduleListToConflictList } from './conflict/transform';
package/lib/index.js CHANGED
@@ -24,7 +24,7 @@ export { useWakeUpDp } from './hooks/useWakeUpDp';
24
24
  export { useRandomDp } from './hooks/useRandomDp';
25
25
  export { useCycleDp } from './hooks/useCycleDp';
26
26
  export { useWakeUpSupport } from './hooks/useWakeUpSupport';
27
- export { useBaseLightDp, useFetchDpDataByMesh, getDpDataByMesh } from './hooks/useBaseLightDp';
27
+ export { useBaseLightDp, useFetchDpDataByMesh, getDpDataByMesh, useFetchDpData } from './hooks/useBaseLightDp';
28
28
  export { useTimerReportDp, useTimerReportDpPull } from './hooks/useTimerReportDp';
29
29
 
30
30
  /** Conflict 冲突判断 */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ray-js/lamp-schedule-core",
3
- "version": "1.0.0-beta-1",
3
+ "version": "1.0.0-beta-3",
4
4
  "description": "照明计划模块核心能力",
5
5
  "main": "./lib/index.js",
6
6
  "files": [