forked from benlcollins/nestThermostatLogger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDevices.js
124 lines (108 loc) · 3.94 KB
/
Devices.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
/**
* list devices to get thermostat IDs
*/
function listDevices() {
// get the device data
const deviceArray = getDevices().map(device => [
device.name,
device.type,
device.traits["sdm.devices.traits.Settings"].temperatureScale,
device.parentRelations[0].displayName
]);
// prep a new sheet or reset the existing one
var sheet = getSheetByName(DEVICE_SHEET_NAME);
if (sheet == null) {
sheet = SpreadsheetApp.getActiveSpreadsheet().insertSheet(DEVICE_SHEET_NAME);
const deviceSheetHeaders = ["ID", "Type", "Temp Scale", "Location"];
sheet.getRange(1, 1, 1, 4)
.setValues([deviceSheetHeaders]);
} else {
sheet.getRange(2, 1, sheet.getMaxRows(), sheet.getMaxColumns())
.clearContent();
}
// output the data
sheet.getRange(2, 1, deviceArray.length, 4)
.setValues(deviceArray);
}
/**
* function to make request to google smart api
*/
function makeRequest(endpoint) {
const smartService = getSmartService();
const access_token = smartService.getAccessToken();
const url = `https://smartdevicemanagement.googleapis.com/v1${endpoint}`;
// setup the headers for the call
const headers = {
'Authorization': 'Bearer ' + access_token,
'Content-Type': 'application/json'
}
// set up params
const params = {
'headers': headers,
'method': 'get',
'muteHttpExceptions': true
}
// try calling API
const response = UrlFetchApp.fetch(url, params);
const statusCode = response.getResponseCode();
const responseBody = JSON.parse(response.getContentText());
if (statusCode != 200) {
throw responseBody.error.message;
}
return responseBody;
}
function getDevices() {
return makeRequest('/enterprises/' + PROJECT_ID + '/devices').devices;
}
/**
* function to make request to google smart api
*/
function logThermostatDataAllDevices() {
let dataArray = [];
const weatherDataArray = retrieveWeather();
const devices = getDevices();
const now = new Date();
devices.forEach(device => {
if (device['type'] === 'sdm.devices.types.THERMOSTAT') {
dataArray.push([now, ...getThermostatData(device), ...weatherDataArray]);
}
});
const sheet = getSheetByName(LOG_SHEET_NAME);
sheet.getRange(sheet.getLastRow()+1,1,dataArray.length,dataArray[0].length).setValues(dataArray);
}
function getThermostatData(device) {
const humidity = device.traits['sdm.devices.traits.Humidity']['ambientHumidityPercent'];
const connectivity = device.traits['sdm.devices.traits.Connectivity']['status'];
const fan = device.traits['sdm.devices.traits.Fan']['timerMode'];
const mode = device.traits['sdm.devices.traits.ThermostatMode']['mode'];
const thermostatEcoMode = device.traits['sdm.devices.traits.ThermostatEco']['mode'];
const thermostatEcoHeatCelcius = device.traits['sdm.devices.traits.ThermostatEco']['heatCelsius'];
const thermostatEcoCoolCelcius = device.traits['sdm.devices.traits.ThermostatEco']['coolCelsius'];
const thermostatHvac = device.traits['sdm.devices.traits.ThermostatHvac']['status'];
const tempCelcius = device.traits['sdm.devices.traits.Temperature']['ambientTemperatureCelsius'];
const targetHeatCelcius = device.traits['sdm.devices.traits.ThermostatTemperatureSetpoint']['heatCelsius'];
const targetCoolCelcius = device.traits['sdm.devices.traits.ThermostatTemperatureSetpoint']['coolCelsius'];
return [
getDeviceLocation(device['name']),
humidity,
connectivity,
fan,
mode,
thermostatEcoMode,
thermostatEcoHeatCelcius,
thermostatEcoCoolCelcius,
thermostatHvac,
tempCelcius,
targetHeatCelcius,
targetCoolCelcius,
];
}
function getDeviceLocation(deviceName) {
const sheet = getSheetByName(DEVICE_SHEET_NAME);
const deviceNameLocation = sheet.createTextFinder(deviceName).findNext();
if (!deviceNameLocation) {
return 'Unknown';
}
const deviceLocationLocation = sheet.getRange(deviceNameLocation.getRow(), 4);
return deviceLocationLocation.getValue();
}