Skip to content

Instantly share code, notes, and snippets.

@marco79cgn
Last active April 17, 2023 19:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marco79cgn/6286a1141543305262ff921af5768691 to your computer and use it in GitHub Desktop.
Save marco79cgn/6286a1141543305262ff921af5768691 to your computer and use it in GitHub Desktop.
Scriptable App - Solar Manager App
/// Widget for Solar Manager Data
// read username:password from widget parameter, not hardcoded in the script
const token = args.widgetParameter
let smID = "SolarManagerID"
const gatewayData = await fetchGatewayProductionAndConsumption()
// Erstellen Sie das Widget
let widget = new ListWidget();
widget.addText("Ertrag: " + gatewayData.production + " kWh");
widget.addText("Verbrauch: " + gatewayData.consumption + " kWh");
if (config.runsInWidget) {
// Platzieren Sie das Widget auf dem Startbildschirm des iPhones
Script.setWidget(widget);
} else {
// Vorschau anzeigen
widget.presentSmall();
}
Script.complete();
// fetches production and consumption of the gateway
async function fetchGatewayProductionAndConsumption() {
let req = new Request('https://cloud.solar-manager.ch/v1/consumption/gateway/' + smID + '?period=day')
req.headers = {
"Authorization": getAuthHeader()
}
const apiResult = await req.loadJSON()
const data = apiResult['data'][0]
let consumption = data['consumption']/1000
let production = data['production']/1000
production = Math.round(production * 10) / 10; // Runden auf eine Nachkommastelle
consumption = Math.round(consumption * 10) / 10; // Runden auf eine Nachkommastelle
return {
"production": production,
"consumption": consumption
}
}
// creates the Authorization header for http api requests
function getAuthHeader()
{
var base64Credentials
if (token != null && token.length > 0) {
base64Credentials = btoa(token);
} else {
console.log("No valid credentials provided.")
// TODO: Remove for production use
let username = "username"
let password = "password"
base64Credentials = btoa("$username:$password")
}
return "Basic " + base64Credentials;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment