Skip to content

Instantly share code, notes, and snippets.

@hacki11
Last active October 29, 2020 17:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hacki11/70665ef90312eadad492459ce6102cf8 to your computer and use it in GitHub Desktop.
Save hacki11/70665ef90312eadad492459ce6102cf8 to your computer and use it in GitHub Desktop.
Fetches widget configuration from a userdata point and renders it (Proof of concept)
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: deep-purple; icon-glyph: magic;
// First draft of fetching values from iobroker datapoints with iobroker.web adapter
/*
Proof of concept
Add a native widget json configuration to a new user data point
0_userdata.0.ioswidget
"native": {
"widget": {
"name": "energy",
"elements": [
{
"summary": "Gasverbrauch",
"value": "sourceanalytix.0.mqtt__0__gas__value.currentYear.consumed.01_currentDay"
},
{
"summary": "Stromverbrauch",
"value": "sourceanalytix.0.mqtt__0__power__counter.currentYear.consumed.01_currentDay"
}
]
}
},
*/
let configDataPoint = "0_userdata.0.ioswidget"
let host = args.widgetParameter;
if (!host)
host = "http://192.168.0.100:8087"
console.log(host)
var widgetConfig = await downloadConfig()
let widget = await createWidget(widgetConfig)
Script.setWidget(widget)
widget.presentSmall()
Script.complete()
async function downloadConfig() {
try{
let req = new Request(host + "/get/" + configDataPoint)
let value = JSON.parse(await req.loadString()).native.widget
console.log(JSON.stringify(value))
return value;
} catch(err) {
console.error(err)
return "{}"
}
}
async function createWidget(config){
const list = new ListWidget()
console.log("create widget: " + JSON.stringify(config))
for(let element of config.elements) {
await createElement(list, element)
list.addSpacer()
}
return list
}
async function createElement(widget, element) {
try {
console.log("Create element:" + element.summary)
var data = await get(element.value)
const summary = widget.addText(element.summary)
summary.font = Font.boldSystemFont(10)
summary.Color = Color.white()
console.log("create line: " + data)
const stack = widget.addStack()
const valueEntry = stack.addText(parseValue(data.val))
valueEntry.font = Font.systemFont(28)
valueEntry.Color = Color.gray()
stack.addSpacer(4)
const unitEntry = stack.addText(data.common.unit)
unitEntry.font = Font.systemFont(14)
unitEntry.Color = Color.gray()
stack.centerAlignContent()
} catch(err) {
console.error(err)
}
}
function parseValue(value) {
try {
console.log("parsing value: " + value)
const number = Number.parseFloat(value)
console.log("parsed " + value + " to " + number)
return number.toFixed(1)
} catch(err) {
console.error(err)
return "--"
}
}
async function get(point) {
try {
let req = new Request(host + "/get/" + point)
let value = await req.loadString()
let json = JSON.parse(value)
console.log("response: " + value)
return json
} catch(err) {
console.error(err)
return "n/a"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment