Created
October 22, 2020 09:10
-
-
Save lukasklein/f857b77712b741efb2bbacde5f60fecd to your computer and use it in GitHub Desktop.
This file contains 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
const widget = new ListWidget(); | |
await createWidget(); | |
if(!config.runsInWidget) { | |
await widget.presentSmall(); | |
} | |
Script.setWidget(widget); | |
Script.complete(); | |
async function createWidget() { | |
widget.backgroundColor = Color.white(); | |
widget.setPadding(10,10,10,10); | |
const logoReq = new Request('https://app.datacake.de/apple-icon.png'); | |
const logoImg = await logoReq.loadImage(); | |
const wLogo = widget.addImage(logoImg); | |
wLogo.imageSize = new Size(25,25); | |
wLogo.rightAlignImage(); | |
widget.addSpacer(); | |
let row = widget.addStack(); | |
row.layoutHorizontally(); | |
row.centerAlignContent(); | |
const iconReq = new Request('https://img.icons8.com/cotton/344/solar-panel.png'); | |
const icon = await iconReq.loadImage(); | |
const iconImg = row.addImage(icon); | |
iconImg.imageSize = new Size(40, 40); | |
row.addSpacer(10); | |
let column = row.addStack(); | |
column.layoutVertically(); | |
column.centerAlignContent(); | |
column.addSpacer(6); | |
const labelText = column.addText('CURRENT'); | |
labelText.font = Font.mediumRoundedSystemFont(12); | |
labelText.textColor = Color.darkGray(); | |
labelText.rightAlignText(); | |
const data = await fetchPVInformation(); | |
const powerRow = column.addStack(); | |
powerRow.layoutHorizontally(); | |
powerRow.bottomAlignContent(); | |
const powerText = powerRow.addText(`${(data.data.device.currentMeasurement.value / 1000).toFixed(2)}`); | |
powerText.font = Font.mediumRoundedSystemFont(22); | |
powerText.textColor = Color.darkGray(); | |
const unitText = powerRow.addText('kW'); | |
unitText.font = Font.mediumRoundedSystemFont(22); | |
unitText.textColor = Color.gray(); | |
widget.addSpacer(45); | |
} | |
async function fetchPVInformation() { | |
const url = 'https://api.datacake.co/graphql/'; | |
const request = new Request(url); | |
request.method = 'POST'; | |
request.headers = { | |
Authorization: 'Token <YourToken>', | |
'Content-Type': 'application/json', | |
}; | |
request.body = JSON.stringify({ | |
query: `query readData { | |
device(deviceId:"<YourDevice>") { | |
id | |
currentMeasurement(fieldName:"<YourField>") { | |
id | |
value | |
} | |
} | |
}` | |
}); | |
const result = await request.loadJSON(); | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment