Created
July 17, 2021 15:39
-
-
Save Basicprogrammer10/2d2c02e0d3ccb1fa02500661345283ee to your computer and use it in GitHub Desktop.
Widget for my Water Temp System
This file contains 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
// Connor Slade - 7/14/2021 | |
let server = "https://water.connorcode.com"; | |
// Weather Info Stuff | |
let weatherConfig = { | |
showAirTemp: false, // Make true if you want to show air temp too | |
server: "https://api.openweathermap.org/data/2.5/weather", | |
apiKey: "", // Put ya api key here | |
zip: "18464,US" | |
} | |
// Get Request | |
async function getRequest(url) { | |
let req = new Request(url); | |
let json = await req.loadJSON(); | |
return json; | |
} | |
// Add text to a widget | |
function addWidgetText(widget, str, color, font) { | |
let text = widget.addText(str); | |
text.textColor = new Color(color); | |
text.font = Font.boldSystemFont(font); | |
widget.addSpacer(6); | |
} | |
// Make a gradient | |
function makeGradient(colors, pos) { | |
let gradient = new LinearGradient(); | |
gradient.locations = pos; | |
gradient.colors = colors; | |
return gradient; | |
} | |
function roundAbs(num) { | |
return Math.round(Math.abs(num)); | |
} | |
// Main Function | |
async function main(server, wc) { | |
// Get Data from Apis | |
let temp = await getRequest(`${server}/api/temp`); | |
let stats = await getRequest(`${server}/api/stats`); | |
if (wc.showAirTemp) weather = await getRequest(`${wc.server}?zip=${wc.zip}&units=imperial&appid=${wc.apiKey}`); | |
// Calculate Values | |
let avg = Math.round(stats.mean * 10) / 10; | |
let tmp = Math.round(temp["temp"] * 100) / 100; | |
let dev = roundAbs((avg - tmp) * 10) / 10; | |
// Create Widget | |
widget.backgroundGradient = makeGradient([new Color("007fffff"), new Color("0825fcff")], [0.25, 1]); | |
addWidgetText(widget, `${tmp}°F`, "#ffffff", 30); | |
addWidgetText(widget, `AVG: ${avg} DEV: ${dev}`, "#ffffff", 11); | |
if (wc.showAirTemp) addWidgetText(widget, `💨 ${weather.main.temp}°F`, "ffffff", 15); | |
} | |
// Run Main Function and Exit Script | |
let widget = new ListWidget(); | |
await main(server, weatherConfig); | |
Script.setWidget(widget); | |
widget.presentSmall(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment