-
Notifications
You must be signed in to change notification settings - Fork 0
/
smartthings.js
35 lines (32 loc) · 1.23 KB
/
smartthings.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
const request = require('request-promise-native')
const smartthingsBaseUrl = 'https://api.smartthings.com/v1'
const humidityDevId = 'a769b1ba-5888-41da-b431-2f80a7d2ac3f'
/**
* Fetch smartthings humidity sensor reading
* @returns {Promise<{temp: number, humidity: number>} object containing temperature and humidity percentage
*/
async function getTemperatureAndHumidity () {
const smartthingsAccessToken = process.env.SMARTTHINGS_ACCESS_TOKEN
if (!smartthingsAccessToken) {
throw new Error('environment variable SMARTTHINGS_ACCESS_TOKEN not defined')
}
const options = {
uri: `${smartthingsBaseUrl}/devices/${humidityDevId}/status`,
auth: { bearer: smartthingsAccessToken },
json: true
}
return request(options)
.then((response) => {
console.log('getTemperatureAndHumidity(): response\n' + JSON.stringify(response))
return {
temp: response.components.main.temperatureMeasurement.temperature.value,
humidity: response.components.main.relativeHumidityMeasurement.humidity.value
}
})
.catch((err) => {
console.log('getTemperatureAndHumidity(): error fetching humidity sensor reading from smartthings api: ' + err)
})
}
module.exports = {
getTemperatureAndHumidity
}