Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // put your zipcode here
- const zip = "94469";
- // put your amount of future GSI values here
- // value 0 will show GSI for current hour only
- // max value should be 4 for visibility reasons
- const additionalHours = 2;
- // progress bar settings
- const width = 200;
- const height = 3;
- // get GSI-values forecast
- let forecast = await getGsiForecast();
- async function createWidget() {
- let listWidget = new ListWidget();
- listWidget.backgroundColor = new Color("#0000");
- let correntlyHeading = listWidget.addText("Corrently");
- let gsiHeading = listWidget.addText("GrünstromIndex");
- let zipHeading = listWidget.addText(zip + "\n");
- correntlyHeading.centerAlignText();
- correntlyHeading.textColor = new Color("feb133");
- correntlyHeading.font = Font.systemFont(11);
- gsiHeading.centerAlignText();
- gsiHeading.textColor = new Color("#0d6d37");
- gsiHeading.font = Font.boldSystemFont(10);
- zipHeading.centerAlignText();
- zipHeading.font = Font.systemFont(9);
- for (let i = 0; i < getRelevantGsiValues().length; i++) {
- let gsiValue = listWidget.addText(getRelevantGsiValues()[i].hour + ": " + getRelevantGsiValues()[i].gsi);
- gsiValue.leftAlignText();
- gsiValue.color = new Color("#ffff");
- gsiValue.font = Font.boldSystemFont(8);
- let gsiProgressBar = listWidget.addImage(createProgressBar(getRelevantGsiValues()[i].gsi));
- gsiProgressBar.leftAlignImage();
- }
- return listWidget;
- }
- async function getGsiForecast() {
- let url = "https://api.corrently.io/core/gsi?zipcode=" + zip + "&key=13374711";
- let req = new Request(url);
- let res = await req.loadJSON();
- return res.forecast;
- }
- function getStartHourIndex(forecast) {
- let currHour = new Date().getHours();
- for (let i = 0; i < forecast.length - 1; i++) {
- let gsiHour = new Date(forecast[i].timeStamp).getHours();
- if (currHour === gsiHour) {
- return i;
- }
- }
- }
- function getRelevantGsiValues() {
- let gsiValuesArr = [];
- let startIndex = getStartHourIndex(forecast);
- for (let i = startIndex; i < startIndex + additionalHours + 1; i++) {
- gsiValuesArr.push({
- hour: new Date(forecast[i].timeStamp).getHours().toString() + ":00",
- gsi: forecast[i].gsi,
- });
- }
- return gsiValuesArr;
- }
- function createProgressBar(gsi) {
- const context = new DrawContext();
- const pathFrame = new Path();
- const pathProgress = new Path();
- pathFrame.addRoundedRect(new Rect(0, 0, width, height), 3, 2);
- pathProgress.addRoundedRect(new Rect(0, 0, (width * gsi) / 100, height), 3, 2);
- context.size = new Size(width, height);
- context.opaque = false;
- context.respectScreenScale = true;
- context.setFillColor(new Color("#48484b"));
- context.addPath(pathFrame);
- context.fillPath();
- // set specific GSI colors
- if (gsi < 45) {
- context.setFillColor(new Color("#f00"));
- } else if (gsi < 56) {
- context.setFillColor(new Color("#ffd60a"));
- } else {
- context.setFillColor(new Color("#0d6d37"));
- }
- context.addPath(pathProgress);
- context.fillPath();
- return context.getImage();
- }
- // set widget
- let widget = await createWidget();
- if (config.runsInWidget) {
- Script.setWidget(widget);
- } else {
- widget.presentMedium();
- }
- Script.complete();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement