-
Notifications
You must be signed in to change notification settings - Fork 1
/
driver.mjs
120 lines (102 loc) · 4.86 KB
/
driver.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
async function saveLevel(purifier, nextLevel, hysteresisLevel) {
let expectedStackLength = hysteresisLevel + 1;
let hysteresisStack = purifier.hysteresisStack;
hysteresisStack[hysteresisStack.length] = nextLevel;
let currentStackLength = hysteresisStack.length;
if (currentStackLength > expectedStackLength) {
purifier.hysteresisStack = hysteresisStack.slice(currentStackLength - expectedStackLength, currentStackLength);
}
}
export async function hysteresis(nextLevel, purifier, config) {
purifier.state.advancedHysteresisUp = null;
purifier.state.advancedHysteresisDown = null;
await saveLevel(purifier, nextLevel);
let hysteresisStack = purifier.hysteresisStack;
let currentStackLength = hysteresisStack.length;
let expectedStackLength = config.hysteresisLevel + 1;
if (currentStackLength < expectedStackLength) {
return hysteresisStack[currentStackLength - 1];
}
let currentLevelDifference = hysteresisStack[currentStackLength - 2] - hysteresisStack[currentStackLength - 1];
let absCurrentLevelDifference = Math.abs(currentLevelDifference);
let testForHysteresis = true;
for (let i = config.hysteresisLevel; i > 1; --i) {
let position = currentStackLength - i;
testForHysteresis = (hysteresisStack[position] === hysteresisStack[position - 1]) && testForHysteresis;
if (!testForHysteresis) {
break;
}
}
purifier.state.hysteresis = testForHysteresis;
if (testForHysteresis && absCurrentLevelDifference < 2) {
return hysteresisStack[currentStackLength - 2];
}
if (testForHysteresis && config.advancedHysteresis && absCurrentLevelDifference > 3) {
let levelCorrection = Math.floor(absCurrentLevelDifference / 2);
let lastLevel = hysteresisStack[currentStackLength - 2];
if (absCurrentLevelDifference > currentLevelDifference) {
purifier.state.advancedHysteresisUp = (lastLevel + levelCorrection);
return (lastLevel + levelCorrection);
} else {
purifier.state.advancedHysteresisDown = (lastLevel - levelCorrection);
return (lastLevel - levelCorrection);
}
}
return hysteresisStack[currentStackLength - 1];
}
export async function determineNextSpeedLevel(purifier, config, dayLevels, nightLevels, isNight) {
let nextLevel = 0;
if (isNight && config.enableNightMode) {
purifier.state.nightMode = isNight;
for (let key in nightLevels) {
if (purifier.pm25 >= nightLevels[key].pm25) {
nextLevel = nightLevels[key].level;
break;
}
}
if (config.nightEnableCoolingDown && (purifier.temperature >= config.nightCoolingDownThreshold)) {
let temperatureDifference = purifier.temperature - config.nightCoolingDownThreshold;
let speed = Math.floor(1 + (temperatureDifference / config.nightTempBetweenLevels));
nextLevel += speed;
purifier.state.nightEnableCoolingDownSpeed = speed;
}
} else {
for (let key in dayLevels) {
if (purifier.pm25 >= dayLevels[key].pm25) {
nextLevel = dayLevels[key].level;
break;
}
}
if (config.dayEnableCoolingDown && (purifier.temperature >= config.dayCoolingDownThreshold)) {
let temperatureDifference = purifier.temperature - config.dayCoolingDownThreshold;
let speed = Math.floor(1 + (temperatureDifference / config.dayTempBetweenLevels));
nextLevel += speed;
if (speed > config.preventHighTemperatureMultiplier) {
await purifier.device.buzzer(1);
await purifier.device.buzzer(0);
purifier.state.preventHighTemperature = config.preventHighTemperature;
purifier.state.dayEnableCoolingDownSpeed = speed;
} else {
purifier.state.dayEnableCoolingDownSpeed = speed;
}
}
}
nextLevel = await hysteresis(nextLevel, purifier, config);
if (config.unconditionalBoost) {
nextLevel += config.unconditionalBoostLevel;
purifier.state.unconditionalBoostLevel = config.unconditionalBoostLevel;
}
if (config.preventLowHumidity && (purifier.humidity <= config.lowHumidityThreshold) && nextLevel >= 1) {
nextLevel -= 1;
purifier.state.preventLowHumidity = config.preventLowHumidity;
}
if (config.preventLowTemperature && (purifier.temperature <= config.preventLowTemperatureThreshold)) {
nextLevel = config.preventLowTemperatureSpeed;
purifier.state.preventLowTemperature = config.preventLowTemperature;
}
if (config.preventLowHumidity && (purifier.humidity <= config.criticalHumidityThreshold)) {
nextLevel = 0;
purifier.state.criticalHumidityThreshold = config.preventLowHumidity;
}
return nextLevel;
}