<s> '''<STR_LIT>''' <EOL> import datetime <EOL> import time <EOL> from sbs . tools import Tools <EOL> from sbs . tools import GLOBALS <EOL> import json <EOL> from tornado import gen <EOL> import statistics <EOL> """<STR_LIT>""" <EOL> class SBSReader ( object ) : <EOL> sensor_buffer = { "<STR_LIT>" : [ ] , "<STR_LIT>" : [ ] , "<STR_LIT>" : [ ] , "<STR_LIT>" : [ ] , "<STR_LIT>" : [ ] } <EOL> def __init__ ( self , board ) : <EOL> super ( SBSReader , self ) . __init__ ( ) <EOL> self . board = board <EOL> @ gen . coroutine <EOL> def read_once ( self ) : <EOL> try : <EOL> values = { } <EOL> values [ '<STR_LIT>' ] = self . board . read_ultrasonic_ranger ( ) <EOL> values [ '<STR_LIT>' ] = self . board . read_dht ( ) <EOL> Tools . log ( str ( values ) ) <EOL> self . sensor_buffer [ "<STR_LIT>" ] . append ( int ( values [ '<STR_LIT>' ] [ <NUM_LIT:0> ] ) ) <EOL> self . sensor_buffer [ "<STR_LIT>" ] . append ( int ( values [ '<STR_LIT>' ] [ <NUM_LIT:1> ] ) ) <EOL> self . sensor_buffer [ "<STR_LIT>" ] . append ( values [ '<STR_LIT>' ] ) <EOL> except IOError , e : <EOL> Tools . log ( '<STR_LIT>' % str ( e ) , <NUM_LIT:1> ) <EOL> except Exception , e : <EOL> Tools . log ( '<STR_LIT>' % str ( e ) , <NUM_LIT:1> ) <EOL> pass <EOL> @ gen . coroutine <EOL> def read ( self ) : <EOL> try : <EOL> values = { } <EOL> values [ '<STR_LIT>' ] = self . board . read_flow_sensor ( ) <EOL> values [ '<STR_LIT>' ] = self . board . read_sound_sensor ( ) <EOL> Tools . log ( str ( values ) ) <EOL> self . sensor_buffer [ "<STR_LIT>" ] . append ( values [ '<STR_LIT>' ] ) <EOL> self . sensor_buffer [ "<STR_LIT>" ] . append ( values [ '<STR_LIT>' ] ) <EOL> except IOError , e : <EOL> Tools . log ( '<STR_LIT>' % str ( e ) , <NUM_LIT:1> ) <EOL> except Exception , e : <EOL> Tools . log ( '<STR_LIT>' % str ( e ) , <NUM_LIT:1> ) <EOL> pass <EOL> def get_sensor_data ( self ) : <EOL> Tools . log ( "<STR_LIT>" + str ( self . sensor_buffer ) ) <EOL> try : <EOL> sensor_data = { '<STR_LIT>' : { } , '<STR_LIT>' : { } } <EOL> sensor_data [ '<STR_LIT>' ] [ '<STR_LIT>' ] = sum ( self . sensor_buffer [ '<STR_LIT>' ] ) <EOL> sensor_data [ '<STR_LIT>' ] [ '<STR_LIT>' ] = statistics . median ( self . sensor_buffer [ '<STR_LIT>' ] ) <EOL> sensor_data [ '<STR_LIT>' ] [ '<STR_LIT>' ] = statistics . median ( self . sensor_buffer [ '<STR_LIT>' ] ) <EOL> sensor_data [ '<STR_LIT>' ] [ '<STR_LIT>' ] = statistics . mean ( self . sensor_buffer [ '<STR_LIT>' ] ) <EOL> sensor_data [ '<STR_LIT>' ] [ '<STR_LIT>' ] = statistics . mean ( self . sensor_buffer [ '<STR_LIT>' ] ) <EOL> sensor_data [ '<STR_LIT>' ] = Tools . now_int_epoch ( ) <EOL> Tools . log ( "<STR_LIT>" + str ( sensor_data ) ) <EOL> GLOBALS [ '<STR_LIT>' ] -= <NUM_LIT:0.5> <EOL> except Exception , e : <EOL> Tools . log ( '<STR_LIT>' % str ( e ) , <NUM_LIT:1> ) <EOL> return sensor_data <EOL> def clear_buffer ( self ) : <EOL> for key in self . sensor_buffer : <EOL> del self . sensor_buffer [ key ] [ : ] <EOL> return <EOL> def getAndClear ( self ) : <EOL> record = self . get_sensor_data ( ) <EOL> self . clear_buffer ( ) <EOL> Tools . log ( '<STR_LIT>' ) <EOL> return record ; </s>
