24/01/2011, 13:00:41
Le lundi 24 janvier 2011, Frédéric a écrit :
> J'essaierai de pondre un petit exemple en Python...
Voili (non testé). C'est basique ; il est possible de lire plusieurs valeurs
d'un coup, mais il faut ensuite parser plus finement le résultat. Je te
laisse te reporter aux diverses doc des modules python utilisés.
-----------------------------8<-----------------------------------
#/bin/sh python
# -*- coding: utf-8 -*-
import socket
import xml.etree.ElementTree
import MySQLdb
import MySQLdb.cursors
HOST = "localhost"
PORT = 1028 # defined in the <xmlserver> 'port' attribute of the <services>
section
EOF = 0x04
MYSQL_USER = "mysql_user"
MYSQL_PASSWD = "mysql_passwd"
MYSQL_DB = "mysql_db"
# Open a socket to communicate with linknx
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((HOST, PORT))
# Send a read command
sock.send("<read><object id=\"temp_bureau\"/></read>" + EOF)
# Read answer ("<read status='success'>20.2</read>")
ans = sock.read()
# Decode answer(xml)
elem = xml.etree.ElementTree.XML(ans)
status = elem.attrib.get("status")
if status == "success":
value = float(elem.text)
# Store in MySQL database
conn = MySQLdb.Connect(host=HOST, user=MYSQL_USER,
passwd=MYSQL_PASSWD, db=MYSQL_DB)
cursor = conn.cursor(cursorclass=MySQLdb.cursors.DictCursor)
request = "INSERT INTO %(db)s temp_bureau VALUES %(temp_bureau)f"
d = {'db': MYSQL_DB, 'temp_bureau': value}
cursor.execute(request % d)
conn.commit()
-----------------------------8<-----------------------------------
--
Frédéric
> J'essaierai de pondre un petit exemple en Python...
Voili (non testé). C'est basique ; il est possible de lire plusieurs valeurs
d'un coup, mais il faut ensuite parser plus finement le résultat. Je te
laisse te reporter aux diverses doc des modules python utilisés.
-----------------------------8<-----------------------------------
#/bin/sh python
# -*- coding: utf-8 -*-
import socket
import xml.etree.ElementTree
import MySQLdb
import MySQLdb.cursors
HOST = "localhost"
PORT = 1028 # defined in the <xmlserver> 'port' attribute of the <services>
section
EOF = 0x04
MYSQL_USER = "mysql_user"
MYSQL_PASSWD = "mysql_passwd"
MYSQL_DB = "mysql_db"
# Open a socket to communicate with linknx
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((HOST, PORT))
# Send a read command
sock.send("<read><object id=\"temp_bureau\"/></read>" + EOF)
# Read answer ("<read status='success'>20.2</read>")
ans = sock.read()
# Decode answer(xml)
elem = xml.etree.ElementTree.XML(ans)
status = elem.attrib.get("status")
if status == "success":
value = float(elem.text)
# Store in MySQL database
conn = MySQLdb.Connect(host=HOST, user=MYSQL_USER,
passwd=MYSQL_PASSWD, db=MYSQL_DB)
cursor = conn.cursor(cursorclass=MySQLdb.cursors.DictCursor)
request = "INSERT INTO %(db)s temp_bureau VALUES %(temp_bureau)f"
d = {'db': MYSQL_DB, 'temp_bureau': value}
cursor.execute(request % d)
conn.commit()
-----------------------------8<-----------------------------------
--
Frédéric