#!/usr/bin/python # -*- coding: utf-8 -*- import threading import serial import string import datetime from bottle import Bottle, run, template, static_file ################################################################################ class TeleInfo(object): """ Informations en cours issues du module Arduino de Téléinformation """ def __init__(self): self._index = 0 self._pa = -1 self._ii = -1 pass def _get_index(self): return(self._index) def _set_index(self, newIndex): self._index = newIndex index = property( fget = _get_index, fset = _set_index, doc = "index, en W/h, affiché par le compteur." ) def _get_kWh(self): return(int(float(self._index) / 1000.0)) kWh = property( fget = _get_kWh, doc = "index, en kW/h, affiché par le compteur." ) def _get_pa(self): return(self._pa) def _set_pa(self, newPa): self._pa = newPa pa = property( fget = _get_pa, fset = _set_pa, doc = "Puissance apparente courante, en 'VoltAmpères'" ) def _get_ii(self): return(self._ii) def _set_ii(self, newIi): self._ii = newIi ii = property( fget = _get_ii, fset = _set_ii, doc = "Intensité instantannée, en 'Ampères'" ) def ml(self): """représentation xml des valeurs courantes du compteur""" return(''.format(self._index, self._pa)) def json(self): """représentation json des valeurs courantes du compteur""" return '{{ "index": {}, "pa": {} }}'.format(self._index, self._pa) ################################################################################ class readFromSerial(threading.Thread): """Lecture du port USB sur lequel est branché la téléInformation. La lecture se fait dans un thread différent. """ def __init__(self, ti): threading.Thread.__init__(self) self.setDaemon(True) self._ti = ti self._ser = serial.Serial( port='/dev/ttyACM0', baudrate=1200, parity=serial.PARITY_EVEN, stopbits=serial.STOPBITS_ONE, bytesize=serial.SEVENBITS) def __del__(self): self._ser.close() def run(self): while True: tiLine = self._ser.readline() if (string.find(tiLine, 'BASE ') == 0): if (tiLine[5:14].isdigit() == True): self._ti.index = int(tiLine[5:14]) if (string.find(tiLine, 'IINST ') == 0): if (tiLine[6:8].isdigit() == True): self._ti.ii = int(tiLine[6:9]) if (string.find(tiLine, 'PAPP ') == 0): if (tiLine[5:10].isdigit() == True): self._ti.pa = int(tiLine[5:10]) ################################################################################ # Instancie un site web webApp = Bottle() ################################################################################ @webApp.route('/edf') def edf(): return static_file('web01.html', root='/home/pi/Python') @webApp.route('/edf/json', method='ANY') def edfjson(): dict = {'index':ti.index, 'kWh':ti.kWh, 'pa':ti.pa, 'ii':ti.ii } return dict @webApp.route('/edf/graph') def edfgraph(): return static_file('web02.html', root='/home/pi/Python') ################################################################################ ti = TeleInfo() # Instancie & démarre les threads de lecture de la TéléInfo tiReader = readFromSerial(ti) tiReader.start() # Lance le site web run(webApp, host='0.0.0.0', port=8080, server='cherrypy')