-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolarman-widget.js
182 lines (155 loc) · 5.31 KB
/
solarman-widget.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: deep-gray; icon-glyph: magic;
const sjcl = importModule("lib/sjcl");
// Source at https://github.com/toto/solarman-widget-scriptable
// License BSD-License: See LICENSE file for details
// Use with https://scriptable.app
// -- Configuration --
const API_HOST = "globalhomeappapi.solarmanpv.com";
// Before 15.04.2023 the API endpoint was "api4home.solarmanpv.com"
const USERNAME = "[email protected]";
const PASSWORD = "yourPassword";
// -------------------
function query(params) {
let result = [];
for (const key in params) {
if (Object.hasOwnProperty.call(params, key)) {
const value = params[key];
result.push(`${key}=${value}`);
}
}
return result.join("&");
}
async function getToken(username, password) {
const hash = sjcl.hash.sha256.hash(password);
const hashedPassword = sjcl.codec.hex.fromBits(hash);
console.log(`Hash PW ${hashedPassword}`);
const params = query({
system: "SOLARMAN",
grant_type: "password",
username: username,
password: hashedPassword,
client_id: "test",
clear_text_pwd: password,
identity_type: "2",
});
console.log(`Params: ${params}`);
const request = new Request(
`https://${API_HOST}/oauth-s/oauth/token?${params}`
);
request.method = "POST";
console.log(`Start request ${request}`);
request.body = params.toString();
const data = await request.loadJSON();
return data;
}
async function stationSearch(token, page = 1) {
const body = {
region: {
nationId: null,
level1: null,
level2: null,
level3: null,
level4: null,
level5: null,
},
};
const request = new Request(
`https://${API_HOST}/maintain-s/operating/station/search?page=${page}&size=10&order.direction=ASC&order.property=name`
);
request.method = "POST";
request.body = JSON.stringify(body);
request.headers = {
Authorization: `bearer ${token}`,
"Content-Type": "application/json",
};
const data = await request.loadJSON();
return data;
}
function createMediumWidget(homeData) {
const widget = new ListWidget();
const gradient = new LinearGradient();
gradient.colors = [new Color("#0064A6"), new Color("#D6A94B")];
gradient.locations = [0, 1];
widget.backgroundGradient = gradient;
const homeName = widget.addText(`☀️⚡️ ${homeData.name}`);
homeName.textColor = Color.white();
homeName.font = Font.boldSystemFont(12);
widget.addSpacer();
const wattText = `${homeData.currentWatts.toFixed(homeData.currentWatts > 100 ? 0 : 1) }W`
const titleText = widget.addText(wattText);
titleText.textColor = Color.white();
titleText.font = Font.boldSystemFont(32);
const totalkWh = `${homeData.generationValue.toFixed(1) }kWh today`
const totalText = widget.addText(totalkWh);
totalText.textColor = Color.white();
totalText.font = Font.subheadline();
widget.addSpacer();
const updatedAtText = widget.addText(homeData.lastUpdatedLabel);
updatedAtText.textColor = Color.white();
updatedAtText.font = Font.caption2();
return widget;
}
function createCircularLockScreenWidget(homeData) {
const widget = new ListWidget();
widget.addAccessoryWidgetBackground = true
widget.addSpacer();
const wattText = `${homeData.currentWatts.toFixed(0)}W`
wattText.minimumScaleFactor = 0.75
const titleText = widget.addText(wattText);
titleText.textColor = Color.white();
titleText.centerAlignText()
titleText.font = Font.boldSystemFont(14);
const totalkWh = `${homeData.generationValue.toFixed(1) }kWh`
const totalText = widget.addText(totalkWh);
totalText.minimumScaleFactor = 0.75
totalText.centerAlignText()
totalText.textColor = Color.white();
totalText.font = Font.systemFont(12);
widget.addSpacer();
return widget;
}
function extractHomeData(home) {
const lastUpdated = new Date();
lastUpdated.setTime(home.lastUpdateTime * 1000.0);
const formatter = new DateFormatter();
if (new Date() - lastUpdated < 24 * 60 * 60 * 1000) {
formatter.useShortTimeStyle();
formatter.useNoDateStyle();
} else {
formatter.useNoTimeStyle();
formatter.useShortDateStyle();
}
return {
lastUpdated,
lastUpdatedLabel: `Updated ${formatter.string(lastUpdated, new Date())}`,
currentWatts: home.generationPower ?? 0,
generationValue: home.generationValue ?? 0,
percentOfCapacity: home.generationCapacity ?? 0,
name: home.name,
temperature: home.temperature,
weather: home.weather,
};
}
const token = await getToken(USERNAME, PASSWORD);
// console.log(`getToken Response: ${JSON.stringify(token)}`);
const searchResponse = await stationSearch(token.access_token);
// console.log(`searchResponse Response: ${JSON.stringify(searchResponse)}`);
const [firstHome] = searchResponse.data;
const homeData = extractHomeData(firstHome);
console.log(`Home Response: ${JSON.stringify(homeData)}`);
if (config.runsInAccessoryWidget) {
if (config.widgetFamily == 'accessoryCircular') {
let widget = createCircularLockScreenWidget(homeData);
Script.setWidget(widget);
Script.complete();
}
} else if (config.runsInWidget) {
let widget = createMediumWidget(homeData);
Script.setWidget(widget);
Script.complete();
} else {
console.log("Not running in widget or home screen");
console.log(`config = ${JSON.stringify(config)}`);
}