@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.
@@ -1,9 +1,15 @@
1
1
  import { TCycleData, TCycleNode } from '../../types';
2
2
  declare class CycleTimerParser {
3
+ defaultValue: {
4
+ version: number;
5
+ length: number;
6
+ nodes: never[];
7
+ };
3
8
  parser(dpValue: string): TCycleData;
4
9
  formatter(data: TCycleData): string;
5
10
  }
6
11
  export declare const cycleParser: {
12
+ defaultValue: TCycleData;
7
13
  parser: (dpValue: string) => {
8
14
  version: number;
9
15
  length: number;
@@ -10,79 +10,85 @@ const {
10
10
  } = scheduleDpCodes;
11
11
  const LENGTH = 16;
12
12
  class CycleTimerParser {
13
+ defaultValue = (() => ({
14
+ version: 0,
15
+ length: LENGTH,
16
+ nodes: []
17
+ }))();
13
18
  parser(dpValue) {
14
- ScheduleLogger.debug(`CycleTimerParser dpValue: ${dpValue}`);
15
- if (!dpValue) {
16
- ScheduleLogger.warn(`无法解析数据, 请检查数据是否合规1【${CYCLE_TIMING}】: ${dpValue}`);
17
- return {
18
- version: 0,
19
- length: LENGTH,
20
- nodes: []
21
- };
22
- }
23
- if (dpValue.length <= 4 || (dpValue.length - 4) % 32 !== 0) {
24
- ScheduleLogger.warn(`节点数量不匹配, 请检查数据是否合规【${CYCLE_TIMING}】: ${dpValue}`);
25
- return {
26
- version: 0,
27
- length: LENGTH,
28
- nodes: []
29
- };
30
- }
31
- const step = generateDpStrStep(dpValue);
32
- const version = step().value;
33
- const length = step().value;
34
- // 节点数
35
- const count = (dpValue.length - 4) / length / 2;
36
- const nodes = [];
37
- for (let i = 0; i < count; i++) {
38
- const powerNum = step().value;
39
- const powerInfo = numToBinStrPad8(powerNum);
40
- const powerBits = powerInfo.split('');
41
- const onOff = !!Number(powerBits[powerBits.length - 1]);
42
- // 通道号
43
- const channel = parseInt(powerBits.slice(1, powerBits.length - 1).join(''), 2);
44
- const repeatInt = step(2).value; // 单次定时任务bit0-bit6全为0,bit0-bit6某位为1时,表示对应周几周期定时 bit0 => 周日
45
- const loops = padEnd(repeatInt.toString(2).split('').reverse().join(''), 7, '0');
46
- const startTime = step(4).value;
47
- const endTime = step(4).value;
48
- const startTimeDuration = step(4).value; // 开始时间持续时间,单位:s
49
- const endTimeDuration = step(4).value; // 结束时间持续时间,单位:s
19
+ try {
20
+ ScheduleLogger.debug(`CycleTimerParser dpValue: ${dpValue}`);
21
+ if (!dpValue) {
22
+ ScheduleLogger.warn(`无法解析数据, 请检查数据是否合规1【${CYCLE_TIMING}】: ${dpValue}`);
23
+ return this.defaultValue;
24
+ }
25
+ if (dpValue.length <= 4 || (dpValue.length - 4) % 32 !== 0) {
26
+ ScheduleLogger.warn(`节点数量不匹配, 请检查数据是否合规【${CYCLE_TIMING}】: ${dpValue}`);
27
+ return {
28
+ version: 0,
29
+ length: LENGTH,
30
+ nodes: []
31
+ };
32
+ }
33
+ const step = generateDpStrStep(dpValue);
34
+ const version = step().value;
35
+ const length = step().value;
36
+ // 节点数
37
+ const count = (dpValue.length - 4) / length / 2;
38
+ const nodes = [];
39
+ for (let i = 0; i < count; i++) {
40
+ const powerNum = step().value;
41
+ const powerInfo = numToBinStrPad8(powerNum);
42
+ const powerBits = powerInfo.split('');
43
+ const onOff = !!Number(powerBits[powerBits.length - 1]);
44
+ // 通道号
45
+ const channel = parseInt(powerBits.slice(1, powerBits.length - 1).join(''), 2);
46
+ const repeatInt = step(2).value; // 单次定时任务bit0-bit6全为0,bit0-bit6某位为1时,表示对应周几周期定时 bit0 => 周日
47
+ const loops = padEnd(repeatInt.toString(2).split('').reverse().join(''), 7, '0');
48
+ const startTime = step(4).value;
49
+ const endTime = step(4).value;
50
+ const startTimeDuration = step(4).value; // 开始时间持续时间,单位:s
51
+ const endTimeDuration = step(4).value; // 结束时间持续时间,单位:s
50
52
 
51
- const hue = step(4).value;
52
- const saturation = step().value;
53
- const {
54
- value
55
- } = step();
56
- const brightness = step().value;
57
- const temperatureStep = step();
58
- const temperature = temperatureStep.value;
59
- const node = {
60
- onOff,
61
- channel,
62
- loops,
63
- startTime,
64
- endTime,
65
- startTimeDuration,
66
- endTimeDuration,
67
- index: i,
68
- color: {
69
- hue,
70
- saturation,
71
- value,
72
- brightness,
73
- temperature
53
+ const hue = step(4).value;
54
+ const saturation = step().value;
55
+ const {
56
+ value
57
+ } = step();
58
+ const brightness = step().value;
59
+ const temperatureStep = step();
60
+ const temperature = temperatureStep.value;
61
+ const node = {
62
+ onOff,
63
+ channel,
64
+ loops,
65
+ startTime,
66
+ endTime,
67
+ startTimeDuration,
68
+ endTimeDuration,
69
+ index: i,
70
+ color: {
71
+ hue,
72
+ saturation,
73
+ value,
74
+ brightness,
75
+ temperature
76
+ }
77
+ };
78
+ nodes.push(node);
79
+ if (temperatureStep.done) {
80
+ break;
74
81
  }
75
- };
76
- nodes.push(node);
77
- if (temperatureStep.done) {
78
- break;
79
82
  }
83
+ return {
84
+ version,
85
+ length,
86
+ nodes
87
+ };
88
+ } catch (error) {
89
+ ScheduleLogger.error('CycleTimerParser parser error', error);
90
+ return this.defaultValue;
80
91
  }
81
- return {
82
- version,
83
- length,
84
- nodes
85
- };
86
92
  }
87
93
  formatter(data) {
88
94
  const {
@@ -1,9 +1,15 @@
1
1
  import { TRandomData, TRandomNode } from '../../types';
2
2
  declare class RandomTimerParser {
3
+ defaultValue: {
4
+ version: number;
5
+ length: number;
6
+ nodes: never[];
7
+ };
3
8
  parser(dpValue: string): TRandomData;
4
9
  formatter(data: TRandomData): string;
5
10
  }
6
11
  export declare const randomParser: {
12
+ defaultValue: TRandomData;
7
13
  parser: (dpValue: string) => {
8
14
  version: number;
9
15
  length: number;
@@ -11,114 +11,125 @@ const {
11
11
  } = scheduleDpCodes;
12
12
  const LENGTH = 12;
13
13
  class RandomTimerParser {
14
+ defaultValue = {
15
+ version: 0,
16
+ length: 12,
17
+ nodes: []
18
+ };
14
19
  parser(dpValue) {
15
- ScheduleLogger.debug(`RandomTimerParser dpValue: ${dpValue}`);
16
- if (!dpValue) {
17
- ScheduleLogger.warn(`无法解析数据, 请检查数据是否合规1【${RANDOM_TIMING}】: ${dpValue}`);
18
- return {
19
- version: 0,
20
- length: LENGTH,
21
- nodes: []
22
- };
23
- }
24
- const step = generateDpStrStep(dpValue);
25
- const version = step().value;
26
- const length = step().value;
27
- if (!dpValue || (dpValue.length - 4) % 24 !== 0) {
28
- console.log('decodeRandomTask 数据有问题,无法解析');
29
- // 返回默认值
20
+ try {
21
+ ScheduleLogger.debug(`RandomTimerParser dpValue: ${dpValue}`);
22
+ if (!dpValue) {
23
+ ScheduleLogger.warn(`无法解析数据, 请检查数据是否合规1【${RANDOM_TIMING}】: ${dpValue}`);
24
+ return {
25
+ version: 0,
26
+ length: LENGTH,
27
+ nodes: []
28
+ };
29
+ }
30
+ const step = generateDpStrStep(dpValue);
31
+ const version = step().value;
32
+ const length = step().value;
33
+ if (!dpValue || (dpValue.length - 4) % 24 !== 0) {
34
+ console.log('decodeRandomTask 数据有问题,无法解析');
35
+ // 返回默认值
36
+ return this.defaultValue;
37
+ }
38
+ const nodes = [];
39
+ // 节点数
40
+ const count = (dpValue.length - 4) / LENGTH / 2;
41
+ for (let i = 0; i < count; i++) {
42
+ const powerNum = step().value;
43
+ const powerInfo = numToBinStrPad8(powerNum);
44
+ const powerBits = powerInfo.split('');
45
+ const onOff = !!Number(powerBits[powerBits.length - 1]);
46
+ // 通道号
47
+ const channel = parseInt(powerBits.slice(1, powerBits.length - 1).join(''), 2);
48
+ const repeatInt = step(2).value; // 单次定时任务bit0-bit6全为0,bit0-bit6某位为1时,表示对应周几周期定时 bit0 => 周日
49
+ const loops = padEnd(repeatInt.toString(2).split('').reverse().join(''), 7, '0');
50
+ const startTime = step(4).value;
51
+ const endTime = step(4).value;
52
+ const hue = step(4).value;
53
+ const saturation = step().value;
54
+ const {
55
+ value
56
+ } = step();
57
+ const brightness = step().value;
58
+ const temperatureStep = step();
59
+ const temperature = temperatureStep.value;
60
+ const node = {
61
+ onOff,
62
+ channel,
63
+ loops,
64
+ startTime,
65
+ endTime,
66
+ index: i,
67
+ color: {
68
+ hue,
69
+ saturation,
70
+ value,
71
+ brightness,
72
+ temperature
73
+ }
74
+ };
75
+ nodes.push(node);
76
+ if (temperatureStep.done) {
77
+ break;
78
+ }
79
+ }
30
80
  return {
31
- version: 0,
32
- length: 12,
33
- nodes: []
81
+ version,
82
+ length,
83
+ nodes
34
84
  };
85
+ } catch (error) {
86
+ ScheduleLogger.error('RandomTimerParser parser error', error);
87
+ return this.defaultValue;
35
88
  }
36
- const nodes = [];
37
- // 节点数
38
- const count = (dpValue.length - 4) / LENGTH / 2;
39
- for (let i = 0; i < count; i++) {
40
- const powerNum = step().value;
41
- const powerInfo = numToBinStrPad8(powerNum);
42
- const powerBits = powerInfo.split('');
43
- const onOff = !!Number(powerBits[powerBits.length - 1]);
44
- // 通道号
45
- const channel = parseInt(powerBits.slice(1, powerBits.length - 1).join(''), 2);
46
- const repeatInt = step(2).value; // 单次定时任务bit0-bit6全为0,bit0-bit6某位为1时,表示对应周几周期定时 bit0 => 周日
47
- const loops = padEnd(repeatInt.toString(2).split('').reverse().join(''), 7, '0');
48
- const startTime = step(4).value;
49
- const endTime = step(4).value;
50
- const hue = step(4).value;
51
- const saturation = step().value;
89
+ }
90
+ formatter(data) {
91
+ try {
52
92
  const {
53
- value
54
- } = step();
55
- const brightness = step().value;
56
- const temperatureStep = step();
57
- const temperature = temperatureStep.value;
58
- const node = {
59
- onOff,
60
- channel,
61
- loops,
62
- startTime,
63
- endTime,
64
- index: i,
65
- color: {
93
+ version,
94
+ nodes
95
+ } = data;
96
+ const versionStr = numToHexPad2(version);
97
+ const lengthStr = numToHexPad2(LENGTH);
98
+ const nodesStr = nodes.map(_ref => {
99
+ let {
100
+ onOff,
101
+ channel,
102
+ loops,
103
+ startTime,
104
+ endTime,
105
+ color
106
+ } = _ref;
107
+ const channelStr = numToBinStrPad(channel || 1, 7);
108
+ const powerChannel = parseInt(`${channelStr}${onOff ? 1 : 0}`, 2);
109
+ const powerChannelStr = numToHexPad2(powerChannel);
110
+ const weeksValue = padStart([...loops.split('')].reverse().join(''), 8, '0');
111
+ const weeksStr = numToHexPad2(parseInt(weeksValue, 2));
112
+ const startTimeStr = numToHexPad4(startTime);
113
+ const endTimeStr = numToHexPad4(endTime);
114
+ const {
66
115
  hue,
67
116
  saturation,
68
117
  value,
69
118
  brightness,
70
119
  temperature
71
- }
72
- };
73
- nodes.push(node);
74
- if (temperatureStep.done) {
75
- break;
76
- }
120
+ } = color;
121
+ const hueStr = numToHexPad4(hue);
122
+ const saturationStr = numToHexPad2(saturation);
123
+ const valueStr = numToHexPad2(value);
124
+ const brightnessStr = numToHexPad2(brightness);
125
+ const temperatureStr = numToHexPad2(temperature);
126
+ return `${powerChannelStr}${weeksStr}${startTimeStr}${endTimeStr}${hueStr}${saturationStr}${valueStr}${brightnessStr}${temperatureStr}`;
127
+ }).join('');
128
+ return `${versionStr}${lengthStr}${nodesStr}`;
129
+ } catch (error) {
130
+ ScheduleLogger.error('RandomTimerParser formatter error', error);
131
+ return '';
77
132
  }
78
- return {
79
- version,
80
- length,
81
- nodes
82
- };
83
- }
84
- formatter(data) {
85
- const {
86
- version,
87
- nodes
88
- } = data;
89
- const versionStr = numToHexPad2(version);
90
- const lengthStr = numToHexPad2(LENGTH);
91
- const nodesStr = nodes.map(_ref => {
92
- let {
93
- onOff,
94
- channel,
95
- loops,
96
- startTime,
97
- endTime,
98
- color
99
- } = _ref;
100
- const channelStr = numToBinStrPad(channel || 1, 7);
101
- const powerChannel = parseInt(`${channelStr}${onOff ? 1 : 0}`, 2);
102
- const powerChannelStr = numToHexPad2(powerChannel);
103
- const weeksValue = padStart([...loops.split('')].reverse().join(''), 8, '0');
104
- const weeksStr = numToHexPad2(parseInt(weeksValue, 2));
105
- const startTimeStr = numToHexPad4(startTime);
106
- const endTimeStr = numToHexPad4(endTime);
107
- const {
108
- hue,
109
- saturation,
110
- value,
111
- brightness,
112
- temperature
113
- } = color;
114
- const hueStr = numToHexPad4(hue);
115
- const saturationStr = numToHexPad2(saturation);
116
- const valueStr = numToHexPad2(value);
117
- const brightnessStr = numToHexPad2(brightness);
118
- const temperatureStr = numToHexPad2(temperature);
119
- return `${powerChannelStr}${weeksStr}${startTimeStr}${endTimeStr}${hueStr}${saturationStr}${valueStr}${brightnessStr}${temperatureStr}`;
120
- }).join('');
121
- return `${versionStr}${lengthStr}${nodesStr}`;
122
133
  }
123
134
  }
124
135
  export const randomParser = new RandomTimerParser();
@@ -3,38 +3,7 @@ export default class RhythmFormatter {
3
3
  uuid: string;
4
4
  defaultValue: any;
5
5
  constructor(uuid?: "rhythm_mode", defaultValue?: null);
6
- parser(dpValue?: string): {
7
- version: number;
8
- power: boolean;
9
- mode: number;
10
- weeks: number[];
11
- number: number;
12
- rhythms: {
13
- key: number;
14
- version: number;
15
- power: boolean;
16
- mode: number;
17
- weeks: number[];
18
- number: number;
19
- rhythms: {
20
- power: boolean;
21
- hour: number;
22
- minute: number;
23
- hue: number;
24
- saturation: number;
25
- value: number;
26
- brightness: number;
27
- temperature: number;
28
- }[];
29
- }[];
30
- } | {
31
- version: any;
32
- power: boolean;
33
- mode: any;
34
- weeks: number[];
35
- number: any;
36
- rhythms: never[];
37
- };
6
+ parser(dpValue?: string): any;
38
7
  formatter(data: TRhythmData): string;
39
8
  }
40
9
  export declare const rhythmParser: RhythmFormatter;
@@ -35,43 +35,48 @@ export default class RhythmFormatter {
35
35
  rhythms: defaultValue
36
36
  };
37
37
  }
38
- const step = generateDpStrStep(dpValue);
39
- const version = step().value;
40
- const power = !!step().value;
41
- const mode = step().value;
42
- const weekNum = step().value;
43
- const weeks = padStart(Number(weekNum).toString(2), 8, '0').split('').reverse().map(v => parseInt(v, 10));
44
- const number = step().value;
45
- const rhythms = [];
46
- for (let i = 0; i < number; i++) {
47
- const valid = !!step().value;
48
- const hour = step().value;
49
- const minute = step().value;
50
- const hue1 = step().value;
51
- const hue2 = step().value;
52
- const saturation = step().value;
53
- const v = step().value;
54
- const brightness = step().value;
55
- const temperature = step().value;
56
- rhythms.push({
57
- power: valid,
58
- hour,
59
- minute,
60
- hue: hue1 * 100 + hue2,
61
- saturation,
62
- value: v,
63
- brightness,
64
- temperature
65
- });
38
+ try {
39
+ const step = generateDpStrStep(dpValue);
40
+ const version = step().value;
41
+ const power = !!step().value;
42
+ const mode = step().value;
43
+ const weekNum = step().value;
44
+ const weeks = padStart(Number(weekNum).toString(2), 8, '0').split('').reverse().map(v => parseInt(v, 10));
45
+ const number = step().value;
46
+ const rhythms = [];
47
+ for (let i = 0; i < number; i++) {
48
+ const valid = !!step().value;
49
+ const hour = step().value;
50
+ const minute = step().value;
51
+ const hue1 = step().value;
52
+ const hue2 = step().value;
53
+ const saturation = step().value;
54
+ const v = step().value;
55
+ const brightness = step().value;
56
+ const temperature = step().value;
57
+ rhythms.push({
58
+ power: valid,
59
+ hour,
60
+ minute,
61
+ hue: hue1 * 100 + hue2,
62
+ saturation,
63
+ value: v,
64
+ brightness,
65
+ temperature
66
+ });
67
+ }
68
+ return {
69
+ version,
70
+ power,
71
+ mode,
72
+ weeks,
73
+ number,
74
+ rhythms
75
+ };
76
+ } catch (err) {
77
+ ScheduleLogger.error('rhythmParser parser error', err);
78
+ return this.defaultValue;
66
79
  }
67
- return {
68
- version,
69
- power,
70
- mode,
71
- weeks,
72
- number,
73
- rhythms
74
- };
75
80
  }
76
81
  formatter(data) {
77
82
  const support = getSupportIns();