MeterDB Class

The MeterDb and SqliteMeterDB classes are designed as a helper for the simplest use case: read a meter and save the measurements. A SQL Server descendant class for Iron Python can also be found in the examples.

If you are using this library to write data to an existing program with an ORM such as SQLAlchemy, do not use these classes: extract the data and load to an appropriate object.

Most users of MeterDB will employ an existing subclass (like SqliteMeterDB). Overriding MeterDB is specifically for simple use cases, where overriding between one and five queries (create, insert, drop, and 2 index creates) is more approachable than setting up or learning an ORM.

class ekmmeters.MeterDB(connection_string)

Base class for single-table reads database abstraction.

setConnectString(connection_string)

Setter for connection string. :param connection_string: Connection string.

mapTypeToSql(fld_type='None', fld_len=0)

Translate FieldType to portable SQL Type. Override if needful. :param fld_type: FieldType in serial block. :type fld_type: int :param fld_len: Binary length in serial block

Returns:Portable SQL type and length where appropriate.
Return type:string
fillCreate(qry_str)

Return query portion below CREATE. :param qry_str: String as built.

Returns:Passed string with fields appended.
Return type:string
sqlCreate()

Reasonably portable SQL CREATE for defined fields. :returns: Portable as possible SQL Create for all-reads table.

Return type:string
sqlInsert(def_buf, raw_a, raw_b)

Reasonably portable SQL INSERT for from combined read buffer. :param def_buf: Database only serial block of all fields. :type def_buf: SerialBlock :param raw_a: Raw A read as hex string. :type raw_a: str :param raw_b: Raw B read (if exists, otherwise empty) as hex string.

Returns:SQL insert for passed read buffer
Return type:str
sqlIdxMeterTime()

Reasonably portable Meter_Address and Time_Stamp index SQL create. :returns: SQL CREATE INDEX statement.

Return type:str
sqlIdxMeter()

Reasonably portable Meter_Address index SQL create. :returns: SQL CREATE INDEX statement.

Return type:str
sqlDrop()

Reasonably portable drop of reads table. :returns: SQL DROP TABLE statement.

Return type:str
dbInsert(def_buf, raw_a, raw_b)

Call overridden dbExec() with built insert statement. :param def_buf: Block of read buffer fields to write. :type def_buf: SerialBlock :param raw_a: Hex string of raw A read. :type raw_a: str :param raw_b: Hex string of raw B read or empty.

dbCreate()

Call overridden dbExec() with built create statement.

dbDropReads()

Call overridden dbExec() with build drop statement.

dbExec(query_str)

Required override for MeterDB subclass, run a query. :param query_str: SQL Query to run.

class ekmmeters.SqliteMeterDB(connection_string='default.db')

MeterDB subclass for simple sqlite database

dbExec(query_str)

Required override of dbExec() from MeterDB(), run query. :param query_str: query to run

renderJsonReadsSince(timestamp, meter)

Simple since Time_Stamp query returned as JSON records.

Parameters:
  • timestamp (int) – Epoch time in seconds.
  • meter (str) – 12 character meter address to query
Returns:

JSON rendered read records.

Return type:

str

renderRawJsonReadsSince(timestamp, meter)

Simple Time_Stamp query returned as JSON, with raw hex string fields.

Parameters:
  • timestamp (int) – Epoch time in seconds.
  • meter (str) – 12 character meter address to query
Returns:

JSON rendered read records including raw hex fields.

Return type:

str