-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathutils.js
More file actions
136 lines (113 loc) · 3.53 KB
/
utils.js
File metadata and controls
136 lines (113 loc) · 3.53 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import {
TEMPERATURE_OVERFLOW_THRESHOLD,
MIN_CELSIUS_TEMP,
MAX_CELSIUS_TEMP,
DEGREE_SYMBOL,
} from "../constants/temperature";
import { WorkflowItemTypes } from "../constants/enums";
import {
localStorageKey,
defaultTemperatureArray,
defaultGlobalFanOnTimeInSeconds,
defaultWorkflows,
} from "../constants/constants";
import { aSuperSpecialAutoThemeSettingsId } from "../constants/themeIds";
export function convertToUInt8BLE(val) {
const buffer = new ArrayBuffer(1);
const dataView = new DataView(buffer);
dataView.setUint8(0, val % 256);
return buffer;
}
export function convertBLEtoUint16(bleBuf) {
return bleBuf.getUint8(0) + bleBuf.getUint8(1) * 256;
}
export function convertToUInt16BLE(val) {
const buffer = new ArrayBuffer(2);
const dataView = new DataView(buffer);
dataView.setUint8(0, val % 256);
dataView.setUint8(1, Math.floor(val / 256));
return buffer;
}
export function convertToUInt32BLE(val) {
const buffer = new ArrayBuffer(4);
const dataView = new DataView(buffer);
dataView.setUint8(0, val & 255);
let tempVal = val >> 8;
dataView.setUint8(1, tempVal & 255);
tempVal = tempVal >> 8;
dataView.setUint8(2, tempVal & 255);
tempVal = tempVal >> 8;
dataView.setUint8(3, tempVal & 255);
return buffer;
}
export function convertToggleCharacteristicToBool(value, mask) {
if ((value & mask) === 0) {
return false;
}
return true;
}
export function convertCurrentTemperatureCharacteristicToCelcius(value) {
const result = Math.round(convertBLEtoUint16(value) / 10);
return result < TEMPERATURE_OVERFLOW_THRESHOLD ? result : MIN_CELSIUS_TEMP;
}
export function convertToFahrenheitFromCelsius(celsius) {
return Math.round(celsius * 1.8 + 32);
}
export function convertToCelsiusFromFahrenheit(fahrenheit) {
return Math.round((fahrenheit - 32) * (5 / 9));
}
export function getDisplayTemperature(temperature, isF) {
const temperatureAbbreviation = isF ? "F" : "C";
const normalizedTemperature = isF
? convertToFahrenheitFromCelsius(temperature)
: temperature;
return `${normalizedTemperature}${DEGREE_SYMBOL}${temperatureAbbreviation}`;
}
export function isValueInValidVolcanoCelciusRange(value) {
if (isNaN(value)) {
return false;
}
return !(value > MAX_CELSIUS_TEMP || value < MIN_CELSIUS_TEMP);
}
export function ReadConfigFromLocalStorage() {
let config = JSON.parse(window.localStorage.getItem(localStorageKey));
const defaultConfig = {
temperatureControlValues: defaultTemperatureArray,
currentTheme: aSuperSpecialAutoThemeSettingsId,
workflows: {
items: defaultWorkflows,
[WorkflowItemTypes.FAN_ON_GLOBAL]: defaultGlobalFanOnTimeInSeconds,
},
onConnectTurnHeatOn: false,
showCurrentWorkflowDetails: false,
};
if (!config) {
window.localStorage.setItem(localStorageKey, JSON.stringify(defaultConfig));
config = defaultConfig;
} else {
config = { ...defaultConfig, ...config };
if (!config.workflows.items) {
config.workflows = {
items: config.workflows,
[WorkflowItemTypes.FAN_ON_GLOBAL]: defaultGlobalFanOnTimeInSeconds,
};
}
}
return config;
}
export function WriteNewConfigToLocalStorage(config) {
const comparer = function (a, b) {
return a - b;
};
const sortedTemperatureControlValues = [
...config.temperatureControlValues,
].sort(comparer);
const sortedTemperatureConfig = {
...config,
temperatureControlValues: sortedTemperatureControlValues,
};
window.localStorage.setItem(
localStorageKey,
JSON.stringify(sortedTemperatureConfig)
);
}