Last active
April 17, 2023 19:59
-
-
Save marco79cgn/6286a1141543305262ff921af5768691 to your computer and use it in GitHub Desktop.
Scriptable App - Solar Manager App
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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