-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThermostatControl.tsx
191 lines (181 loc) · 5.57 KB
/
ThermostatControl.tsx
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import styled from '@emotion/styled';
import { CurrentTemperature } from '../components/TemperaturDisplay';
import { HeatingClimateControlTransceiverChannel } from 'src/types/types';
import { useWebSocketContext } from '../hooks/useWebsocket';
import { Button } from '../components/Button';
import { MdiMinus } from '../components/icons/MdiMinus';
import { MdiPlus } from '../components/icons/MdiPlus';
import { ChannelName } from '../components/ChannelName';
import { MaterialSymbolsLightWindowOpen } from '../components/icons/MaterialSymbolsLightWindowOpen';
type ThermostatProps = {
channel: HeatingClimateControlTransceiverChannel;
};
const Container = styled.div`
position: relative;
width: 250px;
height: 100%;
padding: 10px;
`;
const getColor = (temperature: number) => {
if (temperature < 10) return '#00BFFF'; // DeepSkyBlue
if (temperature < 15) return '#8A2BE2'; // BlueViolet
if (temperature < 25) return '#32CD32'; // LimeGreen
return '#FF8C00'; // DarkOrange
};
const Dial = styled.div<{ temperature: number; targetTemperature: number }>`
width: 70%;
height: 0;
padding-bottom: 70%;
margin: 0 auto;
margin-bottom: 10px;
position: relative;
border-radius: 50%;
&::before {
content: '';
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 100%;
height: 100%;
border-radius: 50%;
background: conic-gradient(
from 220deg,
${(props) => getColor(props.temperature)}
${(props) => (props.temperature / 40) * 280}deg,
#d3d3d3 ${(props) => (props.temperature / 40) * 280}deg 280deg,
transparent 280deg 360deg
);
-webkit-mask: radial-gradient(
farthest-side,
transparent calc(100% - 10px),
black calc(100% - 10px)
);
mask: radial-gradient(
farthest-side,
transparent calc(100% - 10px),
black calc(100% - 10px)
);
}
&::after {
content: '';
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 100%;
height: 100%;
pointer-events: none;
border-radius: 50%;
background: conic-gradient(
from 220deg,
transparent 0deg ${(props) => (props.targetTemperature / 40) * 280}deg,
red ${(props) => (props.targetTemperature / 40) * 280}deg
${(props) => (props.targetTemperature / 40) * 280 + 2}deg,
transparent ${(props) => (props.targetTemperature / 40) * 280 + 2}deg
360deg
);
-webkit-mask: radial-gradient(
farthest-side,
transparent calc(100% - 10px),
black calc(100% - 10px)
);
mask: radial-gradient(
farthest-side,
transparent calc(100% - 10px),
black calc(100% - 10px)
);
}
`;
const Controls = styled.div`
display: flex;
justify-content: space-between;
align-items: center;
margin-top: -50px;
`;
export const ThermostatControl: React.FC<ThermostatProps> = ({ channel }) => {
const datapoints = channel.datapoints;
const targetTemperature = datapoints.SET_POINT_TEMPERATURE;
const currentTemperature = datapoints.ACTUAL_TEMPERATURE;
const humidity = datapoints.HUMIDITY;
const { setDataPoint } = useWebSocketContext();
const decreaseTemperature = () =>
setDataPoint(
channel.interfaceName,
channel.address,
'SET_POINT_TEMPERATURE',
targetTemperature - 0.5,
);
const increaseTemperature = () =>
setDataPoint(
channel.interfaceName,
channel.address,
'SET_POINT_TEMPERATURE',
targetTemperature + 0.5,
);
// const boostMode = () => setDataPoint(channel.interfaceName, channel.address, 'BOOST_MODE', true);
return (
<Container>
<ChannelName name={channel.name} maxWidth="250px" />
<Dial
temperature={currentTemperature}
targetTemperature={targetTemperature}
>
<CurrentTemperature currentTemperature={currentTemperature} />
</Dial>
<Controls>
<Button onClick={decreaseTemperature}>
<MdiMinus />
</Button>
<div
style={{
position: 'relative',
display: 'inline-block',
border: '1px solid lightgrey',
borderRadius: '5px',
padding: '5px',
}}
>
<div style={{ display: 'flex', alignItems: 'baseline' }}>
<span style={{ fontSize: '25px', fontWeight: 'bold' }}>
{targetTemperature.toFixed(1)}
</span>
<span style={{ fontSize: '12px', marginLeft: '4px' }}>°C</span>
</div>
</div>
<Button onClick={increaseTemperature}>
<MdiPlus />
</Button>
</Controls>
<div>
{humidity ? (
<div
style={{
position: 'absolute',
bottom: 165,
left: 110,
display: 'flex',
alignItems: 'center',
}}
>
<span
role="img"
aria-label="humidity"
style={{
fontSize: '15px',
}}
>
💧
</span>
<div style={{ color: '#00BFFF' }}>{humidity}%</div>
</div>
) : null}
<div style={{ marginTop: 4, position: 'absolute', top: 2, right: 10 }}>
{datapoints.WINDOW_STATE === 1 ? (
<MaterialSymbolsLightWindowOpen fontSize={23} color="#00BFFF" />
) : null}
</div>
</div>
</Container>
);
};