-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhome-screen.js
125 lines (104 loc) · 3.03 KB
/
home-screen.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
125
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: green; icon-glyph: magic;
// URL of the getOutputData endpoint of APsystems EZ1-M local API
const url = "http://192.168.178.58:8050/getOutputData";
// max. configured output power of APsystems EZ1-M
const maxPower = 800;
const thresholds = {
orange: 24,
green: 74
};
const widget = await createWidget();
if (config.runsInWidget) {
Script.setWidget(widget);
} else {
widget.presentSmall();
}
Script.complete();
async function createWidget() {
const listwidget = new ListWidget();
const heading = listwidget.addText("☀️ Balkon");
heading.centerAlignText();
heading.font = Font.lightSystemFont(20);
const power = await getCurrentPower();
const powerPercentage = power ? power / maxPower * 100 : 0;
const powerLabel = power ? `${power} W` : "error";
listwidget.addSpacer(5);
const diagramStack = listwidget.addStack();
diagramStack.addSpacer();
diagramStack.addImage(getDiagram(powerPercentage, powerLabel));
diagramStack.addSpacer();
return listwidget;
}
async function getCurrentPower() {
let power;
try {
const request = new Request(url);
request.timeoutInterval = 5;
const response = await request.loadJSON();
power = response.data?.p1 + response.data?.p2;
} catch (error) {
console.log(error);
power = undefined;
}
return power;
}
function getDiagram(percentage, label) {
function drawArc(ctr, rad, w, deg, color) {
const bgx = ctr.x - rad;
const bgy = ctr.y - rad;
const bgd = 2 * rad;
const bgr = new Rect(bgx, bgy, bgd, bgd);
canvas.setFillColor(color);
canvas.setStrokeColor(Color.gray());
canvas.setLineWidth(w);
canvas.strokeEllipse(bgr);
for (t = 0; t < deg; t++) {
const rect_x = ctr.x + rad * sinDeg(t) - w / 2;
const rect_y = ctr.y - rad * cosDeg(t) - w / 2;
const rect_r = new Rect(rect_x, rect_y, w, w);
canvas.fillEllipse(rect_r);
}
}
function sinDeg(deg) {
return Math.sin((deg * Math.PI) / 180);
}
function cosDeg(deg) {
return Math.cos((deg * Math.PI) / 180);
}
const canvas = new DrawContext();
const canvSize = 200;
const canvTextSize = 30;
const canvWidth = 15;
const canvRadius = 85;
canvas.opaque = false;
canvas.size = new Size(canvSize, canvSize);
canvas.respectScreenScale = true;
let color;
if (percentage > thresholds.green) {
color = Color.green();
} else if (percentage > thresholds.orange) {
color = Color.orange();
} else {
color = Color.red();
}
drawArc(
new Point(canvSize / 2, canvSize / 2),
canvRadius,
canvWidth,
Math.floor(percentage * 3.6),
color
);
const canvTextRect = new Rect(
0,
100 - canvTextSize / 2,
canvSize,
canvTextSize * 1.4 // X-height "* 1.4" so e.g. commas aren't cut off
);
canvas.setTextAlignedCenter();
canvas.setTextColor(Color.gray());
canvas.setFont(Font.boldSystemFont(canvTextSize));
canvas.drawTextInRect(label, canvTextRect);
return canvas.getImage();
}