How can I pass information from a python script to a skin?
#1
Question 
Hello all,

I am trying to add a window on the home page that would display the real temperature outside my home read from my weatherstation.

Using an application called open2300, I am able to read this temperature outside of XBMC. This data is then available to XBMC either in a file or via HTTP (XML, JSON, whatever).

I created an addon that reads this file/url, and this works fine. Now I am struggling to have the skinning engine use this data. I was thinking about running the addon at startup and then store the temperature inside some XBMC global setting. (For the Java programmers out there, this would be the equivalent of System.setProperty("temp","5°C"), which make the "temp" property available to anuthing in the JVM)

I have found that addons can done this with the following label:
Code:
<label>$ADDON[script.cdartmanager 32067]</label>
However in my case, I am not running within the context of an addon.

Any idea? (or any suggestion for a better approach to this problem)?

Thank you.

V.
Reply
#2
Lightbulb 
I found inspiration from existing skin/addon (NextAired and Transparency, actually).

To "put" information from the script:
Code:
self.WINDOW = xbmcgui.Window( 10000 )
self.WINDOW.setProperty( 'MyProperty' , 'hello' )

To read the information from the skin:
Code:
<label>$INFO[Window(Home).Property(MyProperty)]</label>

Issue closed!

V.
Reply
#3
Hey, done!

This is what I managed to do.

Image
When XBMC starts, it initiates a thread that will poll the weather every 30 minutes.

Unfortunately the wife does not realize how uber cool this is Rofl

V.
Reply
#4
V-Turn Wrote:Hey, done!

This is what I managed to do.

Image
When XBMC starts, it initiates a thread that will poll the weather every 30 minutes.

Unfortunately the wife does not realize how uber cool this is Rofl

V.

Looks great, and awesome idea. No more running to the window to check the temp meter. Makes me wish I had a weather station. Smile If it can't be controlled from the sofa, it's not worth having. Wink

Are you going to release it to the public so that others can use it (i.e. me, if I ever bought a station)?
Reply
#5
nice job
Reply
#6
filigran Wrote:Are you going to release it to the public so that others can use it (i.e. me, if I ever bought a station)?

I'm not going to package this as an addon (no time for this), but I can share the source files.

The data is put in a mysql database using open2300's mysql2300 (nothing to do with XBMC)

I have a PHP page that provides the data in text format to XBMC via HTTP (I already have a webserver for my weather reports, so I used this one instead of messing with mysql in XBMC's python)
PHP Code:
<?php
    define
('MYSQL_HOST''localhost');
    
define('MYSQL_USER''open2300');
    
define('MYSQL_PASS''**********');
    
define('MYSQL_DATABASE''open2300');
    
$sql "SELECT * FROM weather where timestamp = (select max(timestamp) from weather)";

$mysqlCnx = @mysql_connect(MYSQL_HOSTMYSQL_USERMYSQL_PASS) or die('Grrrr');
@
mysql_select_db(MYSQL_DATABASE) or die('Grrrr');
$mysqlQuery = @mysql_query($sql$mysqlCnx) or die('Grrrr');
$row mysql_fetch_array($mysqlQuery,  MYSQL_ASSOC);
echo 
'date='.$row['rec_date']."\n";
echo 
'time='.$row['rec_time']."\n";
echo 
't_int='.$row['temp_in']."\n";
echo 
'h_int='.$row['rel_hum_in']."\n";
echo 
't_ext='.$row['temp_out']."\n";
echo 
'h_ext='.$row['rel_hum_out']."\n";
echo 
'tendency='.strtolower($row['tendency'])."\n";
echo 
'forecast='.strtolower($row['forecast'])."\n";

$sql "SELECT min(temp_out) as t_min_24h, max(temp_out) as t_max_24h FROM weather where timestamp>" .date("YmdHis",time()-3600*24);
$mysqlQuery = @mysql_query($sql$mysqlCnx) or die('Pb de requête');
$row mysql_fetch_array($mysqlQuery,  MYSQL_ASSOC);
echo 
't_min_24h='.$row['t_min_24h']."\n";
echo 
't_max_24h='.$row['t_max_24h']."\n";

$sql "SELECT min(temp_out) as t_min_7j, max(temp_out) as t_max_7j FROM weather where timestamp>" .date("YmdHis",time()-3600*24*7);
$mysqlQuery = @mysql_query($sql$mysqlCnx) or die('Pb de requête');
$row mysql_fetch_array($mysqlQuery,  MYSQL_ASSOC);
echo 
't_min_7j='.$row['t_min_7j']."\n";
echo 
't_max_7j='.$row['t_max_7j']."\n";

$sql "SELECT min(temp_out) as t_min_30j, max(temp_out) as t_max_30j FROM weather where timestamp>" .date("YmdHis",time()-3600*24*30);
$mysqlQuery = @mysql_query($sql$mysqlCnx) or die('Pb de requête');
$row mysql_fetch_array($mysqlQuery,  MYSQL_ASSOC);
echo 
't_min_30j='.$row['t_min_30j']."\n";
echo 
't_max_30j='.$row['t_max_30j']."\n";
?>

Then a script in XBMC that runs at startup (using autoexec.py) and calls the proper URL every 30 minutes then parse it to get the weather data
Code:
# -*- coding: utf-8 -*-

import urllib
import os
import sys
from traceback import print_exc
import re
import socket
import xbmc
import xbmcgui
import xbmcplugin
import time
import threading

CACHE_PATH = xbmc.translatePath("special://profile/temp/")
if not os.path.exists(CACHE_PATH): os.makedirs(CACHE_PATH)

class MeteoThread (threading.Thread):
  def __init__(self, threadID, sleeptime):
    self.threadID = threadID
    self.sleeptime = sleeptime
    threading.Thread.__init__(self)
  def run(self):
    while 1==1:
      print "[MeteoThread] fetching data"
      counter = int( xbmcgui.Window( 10000 ).getProperty( 'Meteo.counter' ) )
      counter += 1
      __myurlopener__ = urllib.FancyURLopener()
      __myurlopener__.retrieve("http://localhost/getdata.php",CACHE_PATH+"data.txt")
      filename = CACHE_PATH+"data.txt"
      file = open(filename, "r")
      lines = file.readlines()
      file.close()
      for line in lines:
        data = line.split('=')
        xbmcgui.Window( 10000 ).setProperty( 'Meteo.'+data[0], data[1].strip() )
      xbmcgui.Window( 10000 ).setProperty( 'Meteo.fetched', 'true' )
      xbmcgui.Window( 10000 ).setProperty( 'Meteo.counter' , str(counter) )
      print "[MeteoThread] fetching data complete (set#"+str(counter)+"), now sleeping "+str(self.sleeptime)+" seconds"
      time.sleep(self.sleeptime)

print "[Meteo] starting..."
HomeWindow = xbmcgui.Window( 10000 )
HomeWindow.setProperty( 'Meteo.counter' , str(0) )
pollingthread = MeteoThread(0, 60*30)
pollingthread.start()
print "[Meteo] started"
This is my very first try at Python, so it's probably ugly...

Then finally in the skin I add the corresponding stuff (see next post)
Reply
#7
Code:
        <!-- meteo-->
        <control type="group">
            <posx>826</posx>
            <posy>380</posy>
            <visible>substring(Window(Home).Property(Meteo.fetched),true)</visible>
            <control type="group">
                <control type="image">
                    <description>background top</description>
                    <posx>0</posx>
                    <posy>0</posy>
                    <width>512</width>
                    <height>64</height>
                    <colordiffuse>CCFFFFFF</colordiffuse>
                    <texture>DialogContextTop.png</texture>
                </control>
                <control type="image">
                    <posx>0</posx>
                    <posy>64</posy>
                    <description>background</description>
                    <!--<posy>64</posy>-->
                    <width>512</width>
                    <height>200</height>
                    <colordiffuse>CCFFFFFF</colordiffuse>
                    <!--<texture>ContentPanel.png</texture>-->
                    <texture>DialogContextMiddle.png</texture>
                </control>
                <control type="image">
                    <description>background bottom</description>
                    <posx>0</posx>
                    <posy>264</posy>
                    <width>512</width>
                    <height>64</height>
                    <colordiffuse>CCFFFFFF</colordiffuse>
                    <texture>DialogContextBottom.png</texture>
                </control>
            
                <control type="rss">
                    <description>RSS feed meteo</description>
                    <posx>80</posx>
                    <posy>250</posy>
                    <height>40</height>
                    <width>340</width>
                    <font>font12</font>
                    <urlset>2</urlset>
                    <textcolor>blue</textcolor>
                    <titlecolor>blue</titlecolor>
                    <headlinecolor>white</headlinecolor>
                </control>
                <!--<control type="label">
                    <description>RSS feed meteo</description>
                    <posx>80</posx>
                    <posy>250</posy>
                    <height>40</height>
                    <width>340</width>
                    <font>font12</font>
                    <urlset>1</urlset>
                    <textcolor>blue</textcolor>
                    <titlecolor>blue</titlecolor>
                    <label>bla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla bla</label>
                    <headlinecolor>white</headlinecolor>
                </control>-->
            
            
            
            <control type="label">
                <description>int temp value</description>
                <posx>100</posx>
                <posy>40</posy>
                <width>150</width>
                <height>60</height>
                <textcolor>white</textcolor>
                <shadowcolor>black</shadowcolor>
                <font>font50caps_title</font>
                <label>$INFO[Window(Home).Property(Meteo.t_int)]°</label>
                <aligny>center</aligny>
                <align>center</align>
            </control>
            <control type="label">
                <description>int temp label</description>
                <posx>60</posx>
                <posy>40</posy>
                <width>40</width>
                <height>60</height>
                <font>font12_title</font>
                <label>int.</label>
                <align>center</align>
                <aligny>center</aligny>
                <angle>90</angle>
                <textcolor>AADDDDDD</textcolor>
            </control>

            <control type="label">
                <description>ext temp value</description>
                <posx>100</posx>
                <posy>100</posy>
                <width>150</width>
                <height>60</height>
                <textcolor>white</textcolor>
                <shadowcolor>black</shadowcolor>
                <font>font50caps_title</font>
                <label>$INFO[Window(Home).Property(Meteo.t_ext)]°</label>
                <aligny>center</aligny>
                <align>center</align>
            </control>
            <control type="label">
                <description>ext temp label</description>
                <posx>60</posx>
                <posy>100</posy>
                <width>40</width>
                <height>60</height>
                <textcolor>AADDDDDD</textcolor>
                <font>font12_title</font>
                <label>ext.</label>
                <align>center</align>
                <aligny>center</aligny>
                <angle>90</angle>
            </control>
            

            
            
            <control type="image">
                <posx>250</posx>
                <posy>55</posy>
                <width>128</width>
                <height>128</height>
                <!--<texture>sunny.png</texture>-->
                <texture>$INFO[Window(Home).Property(Meteo.forecast)].png</texture>
                <!--<texture>cloudy.png</texture>-->
                <!--<colordiffuse>BBFF00FF</colordiffuse>-->
                </control>
            <control type="image">
                <posx>380</posx>
                <posy>58</posy>
                <width>46</width>
                <height>99</height>
                <!--<texture>falling.png</texture>-->
                <texture>$INFO[Window(Home).Property(Meteo.tendency)].png</texture>
                <!--<texture>steady.png</texture>-->
                <colordiffuse>BBFFFFFF</colordiffuse>
                </control>                
    
            <control type="group">
                <description>min/max table</description>
                <posx>100</posx>
                <posy>160</posy>
                <control type="label">
                    <description>max label</description>
                    <posx>0</posx>
                    <posy>30</posy>
                    <width>60</width>
                    <height>20</height>
                    <font>font12_title</font>
                    <label>max</label>
                    <align>center</align>
                    <aligny>center</aligny>
                    <textcolor>88FF7777</textcolor>
                </control>            
                <control type="label">
                    <description>min label</description>
                    <posx>0</posx>
                    <posy>60</posy>
                    <width>60</width>
                    <height>20</height>
                    <font>font12_title</font>
                    <label>min</label>
                    <align>center</align>
                    <aligny>center</aligny>
                    <textcolor>886699FF</textcolor>
                </control>    
                
                <control type="label">
                    <description>24h label</description>
                    <posx>60</posx>
                    <posy>0</posy>
                    <width>80</width>
                    <height>20</height>
                    <textcolor>AADDDDDD</textcolor>
                    <font>font12_title</font>
                    <label>24h</label>
                    <align>center</align>
                    <aligny>center</aligny>
                </control>                    
    
                <control type="label">
                    <description>24h max</description>
                    <posx>60</posx>
                    <posy>30</posy>
                    <width>80</width>
                    <height>20</height>
                    <textcolor>white</textcolor>
                    <font>font24_title</font>
                    <label>$INFO[Window(Home).Property(Meteo.t_max_24h)]°</label>
                    <align>center</align>
                    <aligny>center</aligny>
                </control>    
                <control type="label">
                    <description>24h min</description>
                    <posx>60</posx>
                    <posy>60</posy>
                    <width>80</width>
                    <height>20</height>
                    <textcolor>white</textcolor>
                    <font>font24_title</font>
                    <label>$INFO[Window(Home).Property(Meteo.t_min_24h)]°</label>
                    <align>center</align>
                    <aligny>center</aligny>
                </control>    
                
                
                <control type="label">
                    <description>7j label</description>
                    <posx>150</posx>
                    <posy>0</posy>
                    <width>80</width>
                    <height>20</height>
                    <textcolor>AADDDDDD</textcolor>
                    <font>font12_title</font>
                    <label>7j</label>
                    <align>center</align>
                    <aligny>center</aligny>
                </control>
                <control type="label">
                    <description>7j max</description>
                    <posx>150</posx>
                    <posy>30</posy>
                    <width>80</width>
                    <height>20</height>
                    <textcolor>white</textcolor>
                    <font>font24_title</font>
                    <label>$INFO[Window(Home).Property(Meteo.t_max_7j)]°</label>
                    <align>center</align>
                    <aligny>center</aligny>
                </control>    
                <control type="label">
                    <description>7j min</description>
                    <posx>150</posx>
                    <posy>60</posy>
                    <width>80</width>
                    <height>20</height>
                    <textcolor>white</textcolor>
                    <font>font24_title</font>
                    <label>$INFO[Window(Home).Property(Meteo.t_min_7j)]°</label>
                    <align>center</align>
                    <aligny>center</aligny>
                </control>    
                
                <control type="label">
                    <description>30j label</description>
                    <posx>240</posx>
                    <posy>0</posy>
                    <width>80</width>
                    <height>20</height>
                    <textcolor>AADDDDDD</textcolor>
                    <font>font12_title</font>
                    <label>30j</label>
                    <align>center</align>
                    <aligny>center</aligny>
                </control>    
                <control type="label">
                    <description>30j max</description>
                    <posx>240</posx>
                    <posy>30</posy>
                    <width>80</width>
                    <height>20</height>
                    <textcolor>white</textcolor>
                    <font>font24_title</font>
                    <label>$INFO[Window(Home).Property(Meteo.t_max_30j)]°</label>
                    <align>center</align>
                    <aligny>center</aligny>
                </control>    
                <control type="label">
                    <description>30j min</description>
                    <posx>240</posx>
                    <posy>60</posy>
                    <width>80</width>
                    <height>20</height>
                    <textcolor>white</textcolor>
                    <backgroundcolor>FFFF0000</backgroundcolor>
                    <font>font24_title</font>
                    <label>$INFO[Window(Home).Property(Meteo.t_min_30j)]°</label>
                    <align>center</align>
                    <aligny>center</aligny>
                </control>    
            </control>    
        </control>
    </control>
Reply
#8
Nice idea, thanks.
I made a "weather widget" for builtin functions out of it.
ImageImage
freaksworth is not connected to or in any other way affiliated with XBMC, Team XBMC, or the XBMC Foundation.
Reply
#9
V-Turn Wrote:I'm not going to package this as an addon (no time for this), but I can share the source files.

...[snip]...

This is my very first try at Python, so it's probably ugly...

Then finally in the skin I add the corresponding stuff (see next post)

Great, will save that for possible future use. Smile

I'm no good at python either, been at it a few times but can never think of anything I want to code, so to me it looks great. Hey - if it works, it's all good. Smile
Reply
#10
V-Turn Wrote:Hey, done!

This is what I managed to do.

Image
When XBMC starts, it initiates a thread that will poll the weather every 30 minutes.

Unfortunately the wife does not realize how uber cool this is Rofl

V.

They rarely do. They rarely do. Sad
Reply

Logout Mark Read Team Forum Stats Members Help
How can I pass information from a python script to a skin?0