-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather.js
296 lines (257 loc) · 8.8 KB
/
weather.js
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
const VENDOR_ID = 0x1941
const PRODUCT_ID = 0x8021
const INTERFACE = 0
const MAX_READ_TRIES = 3
const READ_BUFFER_SIZE = 32
const MESSAGE_SIZE = 8
const WS_ENTRY_SIZE = 0x14
const MAX_WS_ENTRY_COUNT = 3264
const WS_ALL_ADDRESS = 0
const WS_PERIOD_ADDRESS = 16
const WS_DATA_COUNT_ADDRESS = 27
const WS_CURRENT_POSITION_ADDRESS = 30
const WS_ALL_SIZE = 256
const WS_PERIOD_SIZE = 1
const WS_DATA_COUNT_SIZE = 2
const WS_CURRENT_POSITION_SIZE = 2
const directions = [
"N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE",
"S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"
]
const directionsDeg = [
0, 23, 45, 68, 90, 113, 135, 158,
180, 203, 225, 248, 270, 293, 315, 338
]
class WeatherStation {
/**
* @param {HIDDevice} device
*/
constructor(device) {
this.device = device
}
/**
* @param {number} address
* @param {number} size
* @returns {Promise<Array<number>>}
*/
async readData(address = WS_ALL_ADDRESS, size = WS_ALL_SIZE) {
const readSize = Math.min(size, READ_BUFFER_SIZE)
/** @type Array<number> */
const completeBuffer = []
for (let i = 0; i < size; i += readSize) {
const data = await this.readDataPart(address + i, readSize)
completeBuffer.push(...data)
}
return completeBuffer
}
/**
* @param {number} partAddress
* @param {number} readSize
* @returns {Promise<Array<number>>}
*/
async readDataPart(partAddress, readSize) {
let message = [
0b10100001,
(partAddress >> 8) & 0xff,
partAddress & 0xff,
READ_BUFFER_SIZE,
0b10100001,
(partAddress >> 8) & 0xff,
partAddress & 0xff,
READ_BUFFER_SIZE,
]
const dataPromise = new Promise((resolve) => {
let requiredBytes = READ_BUFFER_SIZE
/** @type Array<number> */
const buffer = []
/** @param {DataView} data */
function receiver({data}) {
requiredBytes -= data.byteLength
for (let j = 0; j < data.byteLength; j++) {
buffer.push(data.getUint8(j))
}
if (requiredBytes <= 0) {
resolve(buffer.slice(0, readSize))
}
}
this.device.oninputreport = receiver
})
await this.device.sendReport(0, new Uint8Array(message))
return await dataPromise
}
async close() {
if (!this.device) {
return
}
await this.device.close()
if ("forget" in HIDDevice.prototype) {
await this.device.forget()
}
console.log("forgot the device")
this.device = undefined
}
}
/**
* @param {Array<number>} data
* @param {Array<number>} data60Min
* @return {{
* windDirectionCompass: string,
* outsideTemperatureCelsius: number,
* outsideHumidityPercent: number,
* windSpeedKilometersPerHour: number,
* illuminationLux: number,
* uvLevel: number,
* windGustKilometersPerHour: number,
* windDirectionDegrees: number,
* insideHumidityPercent: number,
* airPressureHectoPascal: number,
* insideTemperatureCelsius: number,
* ageMinutes: number,
* rainMillimeter1h: number
* }}
*/
function interpretWeatherData(data, data60Min) {
return {
ageMinutes: data[0],
insideTemperatureCelsius: data[0x03] >= 0x80
? (data[0x02] + (data[0x03] << 8) ^ 0x7FFF) / 10
: (data[0x02] + (data[0x03] << 8) ^ 0x0000) / 10,
outsideTemperatureCelsius: data[0x06] >= 0x80
? (data[0x05] + (data[0x06] << 8) ^ 0x7FFF) / 10
: (data[0x05] + (data[0x06] << 8) ^ 0x0000) / 10,
insideHumidityPercent: data[0x01],
outsideHumidityPercent: data[0x04],
windSpeedKilometersPerHour: data[0x09] / 10 * 3.6,
windGustKilometersPerHour: data[0x0A] / 10 * 3.6,
windDirectionDegrees: directionsDeg[data[0x0C] < directionsDeg.length ? data[0x0C] : 0],
windDirectionCompass: directions[data[0x0C] < directions.length ? data[0x0C] : 0],
airPressureHectoPascal: (data[0x07] + (data[0x08] << 8)) / 10,
rainMillimeter1h: ((data[0x0D] + (data[0x0E] << 8)) - (data60Min[0x0D] + (data60Min[0x0E] << 8))) * 0.3,
uvLevel: data[19],
illuminationLux: Math.floor((data[16] + (data[17] << 8) + (data[18] << 16)) * 0.1)
}
}
/**
* @param {{
* windDirectionCompass: string,
* outsideTemperatureCelsius: number,
* outsideHumidityPercent: number,
* windSpeedKilometersPerHour: number,
* illuminationLux: number,
* uvLevel: number,
* windGustKilometersPerHour: number,
* windDirectionDegrees: number,
* insideHumidityPercent: number,
* airPressureHectoPascal: number,
* insideTemperatureCelsius: number,
* ageMinutes: number,
* rainMillimeter1h: number
* }} weatherData
*/
function updateUI(weatherData) {
document.getElementById('air-pressure').value = weatherData.airPressureHectoPascal
document.getElementById('illumination').value = weatherData.illuminationLux
document.getElementById('inside-humidity').value = weatherData.insideHumidityPercent
document.getElementById('outside-humidity').value = weatherData.outsideHumidityPercent
document.getElementById('inside-temperature').value = weatherData.insideTemperatureCelsius
document.getElementById('outside-temperature').value = weatherData.outsideTemperatureCelsius
document.getElementById('rain').value = weatherData.rainMillimeter1h
document.getElementById('uv-level').value = weatherData.uvLevel
document.getElementById('wind-speed').textContent = weatherData.windSpeedKilometersPerHour.toFixed(1)
document.querySelectorAll('polygon')
.forEach(p => p.classList.remove('current-direction'))
document.getElementById(`p${weatherData.windDirectionDegrees}`).classList.add('current-direction')
document.getElementById('windDescription').textContent =
`The current wind direction is ${weatherData.windDirectionCompass} with a speed of ${weatherData.windSpeedKilometersPerHour.toFixed(1)} km/h.`
}
/** @type WeatherStation */
let weatherStation
async function open(device) {
if (!device) {
document.getElementById('btn-connect').disabled = false
return
}
if (!device.opened) {
await device.open()
.catch(console.error)
}
if (!device.opened) {
return
}
document.getElementById('btn-connect').disabled = true
weatherStation = new WeatherStation(device)
document.getElementById('btn-request-data').disabled = false
document.getElementById('btn-disconnect').disabled = false
document.getElementById('chk-auto-refresh').disabled = false
}
async function connect() {
if (!navigator.hid) {
console.log("WebHID is not available")
return
}
const [device] = await navigator.hid.requestDevice({
filters: [{
vendorId: VENDOR_ID,
productId: PRODUCT_ID
}]
})
.catch(console.error)
await open(device)
}
function get60MinutesAgoAddress(lastAgeMinutes, readPeriodMinutes, dataCount, currentDataAddress) {
let position60Min = Math.round((60 - lastAgeMinutes) / readPeriodMinutes)
if (dataCount <= position60Min) {
position60Min = dataCount - 1
}
let address60Min = currentDataAddress - position60Min * WS_ENTRY_SIZE
if (address60Min < WS_ALL_ADDRESS) {
address60Min += MAX_WS_ENTRY_COUNT * WS_ENTRY_SIZE
}
return address60Min
}
async function requestData() {
const [readPeriodMinutes] = await weatherStation.readData(WS_PERIOD_ADDRESS, WS_PERIOD_SIZE)
const dataCount = await weatherStation.readData(WS_DATA_COUNT_ADDRESS, WS_DATA_COUNT_SIZE)
.then(([dc1, dc2]) => dc1 + dc2 * 256)
const currentDataAddress = await weatherStation.readData(WS_CURRENT_POSITION_ADDRESS, WS_CURRENT_POSITION_SIZE)
.then(address => address[0] + address[1] * 256)
const data = await weatherStation.readData(currentDataAddress, WS_ENTRY_SIZE)
const lastAgeMinutes = data[0]
const address60Min = get60MinutesAgoAddress(lastAgeMinutes, readPeriodMinutes, dataCount, currentDataAddress)
const data60Min = await weatherStation.readData(address60Min, WS_ENTRY_SIZE)
updateUI(interpretWeatherData(data, data60Min))
}
/** @type number */
let refreshInterval
async function changeAutoRefresh() {
if (document.getElementById('chk-auto-refresh').checked) {
await requestData()
refreshInterval = setInterval(() => requestData(), 30_000)
} else {
clearInterval(refreshInterval)
}
}
async function disconnect() {
await weatherStation.close()
weatherStation = undefined
document.getElementById('btn-connect').disabled = false
document.getElementById('btn-request-data').disabled = true
document.getElementById('btn-disconnect').disabled = true
document.getElementById('chk-auto-refresh').checked = false
document.getElementById('chk-auto-refresh').disabled = true
}
if (!navigator.hid) {
document.querySelector('.warn__hid-not-available').style.display = 'block'
document.querySelector('.controls').style.display = 'none'
} else {
navigator.hid.getDevices()
.then(devices => {
for (const device of devices) {
if (device.productId === PRODUCT_ID && device.vendorId === VENDOR_ID) {
return device
}
}
return null
})
.then(device => open(device))
}