# Path: python/tHome/util/Data.py
# class Data:
#    def __init__( self, dict=None, **kwargs ):
#       if dict:
#          self.__dict__.update( dict )
#       if kwargs:
#          self.__dict__.update( kwargs )
#       
#    #--------------------------------------------------------------------------
#    def keys( self ):
#       return self.__dict__.keys()
#    
#    #--------------------------------------------------------------------------
#    def update( self, rhs ):
#       return self.__dict__.update( rhs.__dict__ )
#    
#    #--------------------------------------------------------------------------
#    def __setitem__( self, key, value ):
#       self.__dict__[key] = value
#       
#    #--------------------------------------------------------------------------
#    def __getitem__( self, key ):
#       return self.__dict__[key]
#    
#    #--------------------------------------------------------------------------
#    def __contains__( self, key ):
#       return key in self.__dict__
#    
#    #--------------------------------------------------------------------------
#    def __str__( self ):
#       out = StringIO.StringIO()
#       self._formatValue( self, out, 3 )
#       return out.getvalue()
# 
#    #--------------------------------------------------------------------------
#    def __repr__( self ):
#       return self.__str__()
#       
#    #--------------------------------------------------------------------------
#    def _formatValue( self, value, out, indent ):
#       if isinstance( value, Data ):
#          out.write( "%s(\n" % self.__class__.__name__ )
#          for k, v in sorted( value.__dict__.iteritems() ):
#             if k[0] == "_":
#                continue
#             
#             out.write( "%*s%s" % ( indent, '', k ) )
#             out.write( " = " )
#             self._formatValue( v, out, indent+3 )
#             out.write( ",\n" )
# 
#          out.write( "%*s)" % ( indent, '' ) )
#          
#       elif isinstance( value, dict ):
#          out.write( "{\n" )
#          for k, v in sorted( value.iteritems() ):
#             if k[0] == "_":
#                continue
#             
#             out.write( "%*s" % ( indent, '' ) )
#             self._formatValue( k, out, 0 )
#             out.write( " : " )
#             self._formatValue( v, out, indent+3 )
#             out.write( ",\n" )
# 
#          out.write( "%*s}" % ( indent, '' ) )
# 
#       elif isinstance( value, list ):
#          out.write( "[\n" )
#          for i in value:
#             out.write( "%*s" % ( indent, '' ) )
#             self._formatValue( i, out, indent+3 )
#             out.write( ",\n" )
# 
#          out.write( "%*s]" % ( indent, '' ) )
#             
#       elif isinstance( value, str ):
#          out.write( "'%s'" % ( value ) )
#          
#       else:
#          out.write( "%s" % ( value ) )


import StringIO
import time
from ..util import Data
batteryMap = {
   "normal" : 1.0,
   "low" : 0.1,
   }

#===========================================================================
def decode( text, sensorMap ):
   """Decode a sensor post from the Acurite bridge.

   Input is a line of text sent by the bridge to the Acurite server.
   Return valeue is a tHome.util.Data object (dict) with the parsed
   values.
   """
   # Skip lines that aren't the sensor information.
   idx = text.find( "id=" )
   if idx == -1:
      return

   text = text[idx:]

   # Split the input into fields.
   elems = text.split( "&" )

   # Get the key/value pairs for each element.
   items = {}
   for e in elems:
      k, v = e.split( "=" )
      items[k] = v

   # Create an empty data object (dict) to store the results.

# Path: python/tHome/util/Data.py
# class Data:
#    def __init__( self, dict=None, **kwargs ):
#       if dict:
#          self.__dict__.update( dict )
#       if kwargs:
#          self.__dict__.update( kwargs )
#       
#    #--------------------------------------------------------------------------
#    def keys( self ):
#       return self.__dict__.keys()
#    
#    #--------------------------------------------------------------------------
#    def update( self, rhs ):
#       return self.__dict__.update( rhs.__dict__ )
#    
#    #--------------------------------------------------------------------------
#    def __setitem__( self, key, value ):
#       self.__dict__[key] = value
#       
#    #--------------------------------------------------------------------------
#    def __getitem__( self, key ):
#       return self.__dict__[key]
#    
#    #--------------------------------------------------------------------------
#    def __contains__( self, key ):
#       return key in self.__dict__
#    
#    #--------------------------------------------------------------------------
#    def __str__( self ):
#       out = StringIO.StringIO()
#       self._formatValue( self, out, 3 )
#       return out.getvalue()
# 
#    #--------------------------------------------------------------------------
#    def __repr__( self ):
#       return self.__str__()
#       
#    #--------------------------------------------------------------------------
#    def _formatValue( self, value, out, indent ):
#       if isinstance( value, Data ):
#          out.write( "%s(\n" % self.__class__.__name__ )
#          for k, v in sorted( value.__dict__.iteritems() ):
#             if k[0] == "_":
#                continue
#             
#             out.write( "%*s%s" % ( indent, '', k ) )
#             out.write( " = " )
#             self._formatValue( v, out, indent+3 )
#             out.write( ",\n" )
# 
#          out.write( "%*s)" % ( indent, '' ) )
#          
#       elif isinstance( value, dict ):
#          out.write( "{\n" )
#          for k, v in sorted( value.iteritems() ):
#             if k[0] == "_":
#                continue
#             
#             out.write( "%*s" % ( indent, '' ) )
#             self._formatValue( k, out, 0 )
#             out.write( " : " )
#             self._formatValue( v, out, indent+3 )
#             out.write( ",\n" )
# 
#          out.write( "%*s}" % ( indent, '' ) )
# 
#       elif isinstance( value, list ):
#          out.write( "[\n" )
#          for i in value:
#             out.write( "%*s" % ( indent, '' ) )
#             self._formatValue( i, out, indent+3 )
#             out.write( ",\n" )
# 
#          out.write( "%*s]" % ( indent, '' ) )
#             
#       elif isinstance( value, str ):
#          out.write( "'%s'" % ( value ) )
#          
#       else:
#          out.write( "%s" % ( value ) )


# Path: python/tHome/acurite/decode.py
import StringIO
import time
from ..util import Data

batteryMap = {
   "normal" : 1.0,
   "low" : 0.1,
   }

#===========================================================================
def decode( text, sensorMap ):
   """Decode a sensor post from the Acurite bridge.

   Input is a line of text sent by the bridge to the Acurite server.
   Return valeue is a tHome.util.Data object (dict) with the parsed
   values.
   """
   # Skip lines that aren't the sensor information.
   idx = text.find( "id=" )
   if idx == -1:
      return

   text = text[idx:]

   # Split the input into fields.
   elems = text.split( "&" )

   # Get the key/value pairs for each element.
   items = {}
   for e in elems:
      k, v = e.split( "=" )
      items[k] = v

   # Create an empty data object (dict) to store the results.

   data = Data()