I’m trying to get a JPEG from an URL into an item type image and I’m not able to find the correct way to perform this.
I was thinking that this is quite standard but looks like it is not.
I receive for example the following if I try to postUpdate the URL into the image item.
Cannot convert 'http://<picture-url>' to a state type which item 'pic_jpeg' accepts: [RawType, UnDefType]
Looks like I have to convert the data in base64 or so before but any idea how to perform this?
import java.net.URL
rule "test1"
when
// Time cron '* * * * * *'
then
val imageUrl = "https://www.openhab.org/demo/openhab-logo-square.png"
val url = new URL(imageUrl)
val connection = url.openConnection()
val contentType = connection.getContentType()
val bytes = connection.getInputStream().readAllBytes()
val image = new RawType(bytes, contentType)
TestImage.postUpdate(image)
end
Yes this might work. The only issue might be that I need to authenticate with the server. It is a basic authentication and it is not working if I just write user:password inside the URL.
import java.net.URL
import java.util.Base64
rule "test1"
when
// Time cron '* * * * * *'
then
val imageUrl = "https://www.openhab.org/demo/openhab-logo-square.png"
val username = "username"
val password = "12345"
val url = new URL(imageUrl)
val connection = url.openConnection()
val userpass = username + ":" + password
val basicAuth = "Basic " + new String(Base64.getEncoder().encode(userpass.getBytes()))
connection.setRequestProperty("Authorization", basicAuth)
val contentType = connection.getContentType()
val bytes = connection.getInputStream().readAllBytes()
val image = new RawType(bytes, contentType)
TestImage.postUpdate(image)
end