Created
June 18, 2022 22:23
-
-
Save freddiefujiwara/185e3b34cf65036dc95791da7e7fb613 to your computer and use it in GitHub Desktop.
AirCondition.gs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const SPREADSHEET_ID = "1RbjK9i7aMsEUi5rdaPHk-BmOyV-muYJdWWx-i9nzbuM"; | |
const sheet = SpreadsheetApp.openById(SPREADSHEET_ID); | |
const conditions = sheet.getSheetByName("conditions"); | |
const status = sheet.getSheetByName("status"); | |
const conditionsLastRow = conditions.getLastRow(); | |
function doGet(e) { | |
const output = ContentService.createTextOutput(); | |
if (e.parameter.callback === undefined) { | |
output.setMimeType(ContentService.MimeType.TEXT); | |
if ("status" === e.parameter.s) { | |
status.getRange(1, 1).setValue(e.parameter.t); | |
output.setContent("OK"); | |
return; | |
} | |
conditions.getRange(conditionsLastRow + 1, 1).setValue(new Date()); // Datetime | |
conditions.getRange(conditionsLastRow + 1, 2).setValue(e.parameter.t || 0); // Temperature | |
conditions.getRange(conditionsLastRow + 1, 3).setValue(e.parameter.h || 0); // Humidity | |
output.setContent("OK"); | |
} else { | |
output.setMimeType(ContentService.MimeType.JAVASCRIPT); | |
const values = conditions.getDataRange().getValues(); | |
const headers = values.shift(); | |
const result = values.map((row) => { | |
let data = {}; | |
row.map((column, index) => { | |
if ("Datetime" === headers[index]) { | |
const dt = new Date(new Date(column).toLocaleString("en-US", { | |
timeZone: "Asia/Tokyo" | |
})); | |
data[headers[index]] = `${dt.toLocaleDateString()} ${dt.toLocaleTimeString()}`; | |
} else { | |
data[headers[index]] = column; | |
} | |
}); | |
return data; | |
}); | |
output.setContent(e.parameter.callback + "&&" + e.parameter.callback + "(" + JSON.stringify(result) + ");"); | |
} | |
return output; | |
} | |
function turnOnAC() { | |
const temperature = 1.0 * conditions.getRange(conditionsLastRow, 2).getValue(); | |
const stat = status.getRange(1, 1).getValue(); | |
if (temperature <= 23.3 && "off" == stat) { | |
UrlFetchApp.fetch("http://a.ze.gs/switchbot-ac/-d/02-202102231808-95275247/-a/25,5,1,on"); | |
status.getRange(1, 1).setValue("hot"); | |
} else if (temperature >= 24.3 && "hot" == stat) { | |
UrlFetchApp.fetch("http://a.ze.gs/switchbot-ac/-d/02-202102231808-95275247/-a/25,5,1,off"); | |
status.getRange(1, 1).setValue("off"); | |
} else if (temperature <= 25.9 && "cool" == stat) { | |
UrlFetchApp.fetch("http://a.ze.gs/switchbot-ac/-d/02-202102231808-95275247/-a/25,2,1,off"); | |
status.getRange(1, 1).setValue("off"); | |
} else if (temperature >= 26.9 && "off" == stat) { | |
UrlFetchApp.fetch("http://a.ze.gs/switchbot-ac/-d/02-202102231808-95275247/-a/25,2,1,on"); | |
status.getRange(1, 1).setValue("cool"); | |
} | |
Logger.log(temperature); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment