After hour of searching I figure this out, I’m really happy to share, explanation is below, I hope this would help someone:
- sudo apt-get install xvfb (googling if you do not know what is it)
- sudo apt-get install python-qt4
- copy this code:
#! /usr/bin/python
import sys
import time
import os
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
from shutil import copyfile
class Screenshot(QWebView):
def __init__(self):
self.app = QApplication(sys.argv)
QWebView.__init__(self)
self._loaded = False
self.loadFinished.connect(self._loadFinished)
def capture(self, url, output_file):
self.load(QUrl(url))
self.wait_load()
# set to webpage size
frame = self.page().mainFrame()
self.page().setViewportSize(frame.contentsSize())
# render image
image = QImage(self.page().viewportSize(), QImage.Format_ARGB32)
painter = QPainter(image)
frame.render(painter)
painter.end()
# print 'saving', output_file
image.save(output_file)
def wait_load(self, delay=0):
# process app events until page loaded
while not self._loaded:
self.app.processEvents()
time.sleep(delay)
self._loaded = False
def _loadFinished(self, result):
self._loaded = True
s = Screenshot()
filename='website.png'
s.capture('http://localhost:8080/weather?locationId=home&layout=example&iconset=colorful', filename)
copyfile('/home/pi/'+filename,'/etc/openhab2/html/'+filename)
os.remove('/home/pi/'+filename)
and save under /etc/openhab2/scripts. Then:
sudo chmod a+rwx /etc/openhab2/scripts/screenshot.py
to get rid of the error “permission denied” (just for sure)
You might want to know that s.capture is used for capturing image from your weather webpage
- Create the rule:
rule "Periodically update weather-data"
when
Time cron Time cron "0 */5 * * * ?" or
System started
then
val String results = executeCommandLine("sudo xvfb-run python /etc/openhab2/scripts/screenshot.py", 300000)
end
NOTE:
After the line:
val String results = executeCommandLine("sudo xvfb-run python /etc/openhab2/scripts/screenshot.py", 300000)
end
your website.png (the screenshot image from weather webpage) will be stored under the root directory (the place on the terminal interface when you power up your system), I’m using raspberry pi 3 so you can see my source directory is /home/pi. Change this to adapt your system. This is very important because the website.png is saved under the root directory by default due to the command executeCommandLine(“sudo xvfb-run python /etc/openhab2/scripts/screenshot.py”, 300000) is executed under the root directory.
And the lines:
copyfile('/home/pi/'+filename,'/etc/openhab2/html/'+filename)
os.remove('/home/pi/'+filename)
just merely cut the website.png from destination to source directory, you can change these lines if you know any command can directly cut a file to a desiring path. In my case, I couldn’t find out that command so I had to do this. It’s quite clumsy but it did work after all!
Furthermore, you might want to change the destination path to satisfy yourself.
Personally, I’d like to put website.png under /etc/openhab2/html
In sitemap:
Image url="http://localhost:8080/static/website.png" refresh=10000
The only problem remaining is the resolution of the image is too large, anyway to resize it down ? I do not want to use python code anymore to resize the image, you know, it’s quite complicated and slow to do this way
@opus I hope you can share yours and suggest how can I improve mine, thanks very much