Advertisement
Guest User

GSI-Scriptable

a guest
Jun 21st, 2021
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. // put your zipcode here
  2. const zip = "94469";
  3. // put your amount of future GSI values here
  4. // value 0 will show GSI for current hour only
  5. // max value should be 4 for visibility reasons
  6. const additionalHours = 2;
  7.  
  8. // progress bar settings
  9. const width = 200;
  10. const height = 3;
  11.  
  12. // get GSI-values forecast
  13. let forecast = await getGsiForecast();
  14.  
  15. async function createWidget() {
  16. let listWidget = new ListWidget();
  17. listWidget.backgroundColor = new Color("#0000");
  18.  
  19. let correntlyHeading = listWidget.addText("Corrently");
  20. let gsiHeading = listWidget.addText("GrünstromIndex");
  21. let zipHeading = listWidget.addText(zip + "\n");
  22.  
  23. correntlyHeading.centerAlignText();
  24. correntlyHeading.textColor = new Color("feb133");
  25. correntlyHeading.font = Font.systemFont(11);
  26.  
  27. gsiHeading.centerAlignText();
  28. gsiHeading.textColor = new Color("#0d6d37");
  29. gsiHeading.font = Font.boldSystemFont(10);
  30.  
  31. zipHeading.centerAlignText();
  32. zipHeading.font = Font.systemFont(9);
  33.  
  34. for (let i = 0; i < getRelevantGsiValues().length; i++) {
  35. let gsiValue = listWidget.addText(getRelevantGsiValues()[i].hour + ": " + getRelevantGsiValues()[i].gsi);
  36. gsiValue.leftAlignText();
  37. gsiValue.color = new Color("#ffff");
  38. gsiValue.font = Font.boldSystemFont(8);
  39.  
  40. let gsiProgressBar = listWidget.addImage(createProgressBar(getRelevantGsiValues()[i].gsi));
  41. gsiProgressBar.leftAlignImage();
  42. }
  43.  
  44. return listWidget;
  45. }
  46.  
  47. async function getGsiForecast() {
  48. let url = "https://api.corrently.io/core/gsi?zipcode=" + zip + "&key=13374711";
  49. let req = new Request(url);
  50. let res = await req.loadJSON();
  51. return res.forecast;
  52. }
  53.  
  54. function getStartHourIndex(forecast) {
  55. let currHour = new Date().getHours();
  56.  
  57. for (let i = 0; i < forecast.length - 1; i++) {
  58. let gsiHour = new Date(forecast[i].timeStamp).getHours();
  59. if (currHour === gsiHour) {
  60. return i;
  61. }
  62. }
  63. }
  64.  
  65. function getRelevantGsiValues() {
  66. let gsiValuesArr = [];
  67. let startIndex = getStartHourIndex(forecast);
  68.  
  69. for (let i = startIndex; i < startIndex + additionalHours + 1; i++) {
  70. gsiValuesArr.push({
  71. hour: new Date(forecast[i].timeStamp).getHours().toString() + ":00",
  72. gsi: forecast[i].gsi,
  73. });
  74. }
  75.  
  76. return gsiValuesArr;
  77. }
  78.  
  79. function createProgressBar(gsi) {
  80. const context = new DrawContext();
  81. const pathFrame = new Path();
  82. const pathProgress = new Path();
  83.  
  84. pathFrame.addRoundedRect(new Rect(0, 0, width, height), 3, 2);
  85. pathProgress.addRoundedRect(new Rect(0, 0, (width * gsi) / 100, height), 3, 2);
  86.  
  87. context.size = new Size(width, height);
  88. context.opaque = false;
  89. context.respectScreenScale = true;
  90. context.setFillColor(new Color("#48484b"));
  91. context.addPath(pathFrame);
  92. context.fillPath();
  93.  
  94. // set specific GSI colors
  95. if (gsi < 45) {
  96. context.setFillColor(new Color("#f00"));
  97. } else if (gsi < 56) {
  98. context.setFillColor(new Color("#ffd60a"));
  99. } else {
  100. context.setFillColor(new Color("#0d6d37"));
  101. }
  102.  
  103. context.addPath(pathProgress);
  104. context.fillPath();
  105.  
  106. return context.getImage();
  107. }
  108.  
  109. // set widget
  110. let widget = await createWidget();
  111.  
  112. if (config.runsInWidget) {
  113. Script.setWidget(widget);
  114. } else {
  115. widget.presentMedium();
  116. }
  117.  
  118. Script.complete();
  119.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement