var getBase64 = RawType.valueOf(executeCommandLine(Duration.ofSeconds(10), base64, convertName))
getBase64 = "data:image/png;base64," + getBase64
spdImage.postUpdate(getBase64)
ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'Testswitch-1' failed: Invalid data URI syntax for argument
The post doesn´t say how to get the image from an external source, convert it into base64
and then store it into the item.
The linked post shows a static way that needs interaction by the user.
I wasn´t able to get it working with that one but with this monster of a rule.
Thanks rossko57!
Final rule for the speedtest image:
import java.io.ByteArrayOutputStream
import java.net.URL
rule "Speedtest Image Update"
when
Item spdURL changed
then
val speedImage = spdURL.state + ".png"
var userImageDataBytes = newByteArrayOfSize(0)
try {
// use the built in java URL class - pass it the url to download
val url = new URL(speedImage)
// create an output stream - we'll use it for building up our downloaded bytes
val byteStreamOutput = new ByteArrayOutputStream()
// open the url as a stream - aka - start getting stuff
val inputStream = url.openStream()
// n is a variable for tracking how much data we have read off the inputStream per loop (and how much we write to the output stream)
var n = 0
// buffer is another byte array. basically we're using it as a fixed size copy byte array
var buffer = newByteArrayOfSize(1024)
do {
// read from input stream (the data at the url) into buffer - and place how many bytes were read into n
n = inputStream.read(buffer)
if (n > 0) {
// if we read more than 0 bytes - copy them from buffer into our output stream
byteStreamOutput.write(buffer, 0, n)
}
} while (n > 0) // keep doing this until we don't have anything to read.
userImageDataBytes = byteStreamOutput.toByteArray() // assemble all the bytes we wrote into an actual byte array
} catch(Throwable t) {
logError(ruleId, "Es ist ein Problem aufgetreten: " + t.toString)
}
logInfo(ruleId, "Bild Daten erhalten")
// make a new RawType with the byte array and the mime type - the open hab type Image needs a raw type
// my images are all jpeg - so this mime type is right for me
val rawType = new RawType(userImageDataBytes, "image/jpeg")
// entry.getValue is the "ImageItem" from the map. In my case - this is BrelandImage
if (rawType.toString != spdImage.state.toString) { // if this raw type isn't the same as what's already there (the toString is a kind of accurate way to test - the raw type to string is bascially mime + size - which is a bad approximation for equality - but good enough for me
spdImage.postUpdate(rawType) // update it!
logInfo(ruleId, "Bild aktualisiert")
} else {
logInfo(ruleId, "Bilder waren identisch")
}
end