Note de ce sujet :
  • Moyenne : 0 (0 vote(s))
  • 1
  • 2
  • 3
  • 4
  • 5
Log avec linknx
#5
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


Messages dans ce sujet
Log avec linknx - par sprint95 - 24/01/2011, 08:41:13
Log avec linknx - par Frédéric - 24/01/2011, 08:59:26
Log avec linknx - par sprint95 - 24/01/2011, 10:07:35
Log avec linknx - par Frédéric - 24/01/2011, 11:30:53
Log avec linknx - par Frédéric - 24/01/2011, 13:00:41
Log avec linknx - par jef2000 - 24/01/2011, 13:25:18
Log avec linknx - par Frédéric - 24/01/2011, 13:31:54
Log avec linknx - par Sprint - 24/01/2011, 13:35:32
Log avec linknx - par sprint95 - 24/01/2011, 14:18:55

Atteindre :


Utilisateur(s) parcourant ce sujet : 1 visiteur(s)