diff --git a/src/Logger.py b/src/Logger.py index 9157c64..1dab788 100644 --- a/src/Logger.py +++ b/src/Logger.py @@ -7,7 +7,7 @@ import os, time LOG_FILENAME = "energenie.csv" -HEADINGS = 'timestamp,mfrid,prodid,sensorid,flags,switch,voltage,freq,reactive,real' +HEADINGS = 'timestamp,mfrid,prodid,sensorid,flags,switch,voltage,freq,reactive,real,apparent,current,temperature' log_file = None @@ -35,7 +35,7 @@ # set defaults for any data that doesn't appear in this message # but build flags so we know which ones this contains - flags = [0 for i in range(7)] + flags = [0 for i in range(8)] switch = None voltage = None freq = None @@ -43,6 +43,7 @@ real = None apparent = None current = None + temperature = None # capture any data that we want ##trace(msg) @@ -74,10 +75,13 @@ elif paramid == OpenThings.PARAM_CURRENT: flags[6] = 1 current = value + elif paramid == OpenThings.PARAM_TEMPERATURE: + flags[7] = 1 + temperature = value # generate a line of CSV flags = "".join([str(a) for a in flags]) - csv = "%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s" % (timestamp, mfrid, productid, sensorid, flags, switch, voltage, freq, reactive, real, apparent, current) + csv = "%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s" % (timestamp, mfrid, productid, sensorid, flags, switch, voltage, freq, reactive, real, apparent, current, temperature) log_file.write(csv + '\n') log_file.flush() ##trace(csv) # testing diff --git a/src/energenie/Messages.py b/src/energenie/Messages.py index 1ab2961..778dd69 100644 --- a/src/energenie/Messages.py +++ b/src/energenie/Messages.py @@ -2,8 +2,12 @@ # # pydict formatted message structures for OpenThings -import Devices -import OpenThings +try: # python 2 + import Devices + import OpenThings +except ImportError: + from . import Devices + from . import OpenThings SWITCH = { "header": { diff --git a/src/energenie/OpenThings.py b/src/energenie/OpenThings.py index a8516f6..eecf089 100644 --- a/src/energenie/OpenThings.py +++ b/src/energenie/OpenThings.py @@ -2,8 +2,11 @@ # # Implement OpenThings message encoding and decoding -import crypto import time +try: + import crypto # python 2 +except ImportError: + from . import crypto # python 3 class OpenThingsException(Exception): def __init__(self, value): @@ -192,7 +195,7 @@ param = payload[i] wr = ((param & 0x80) == 0x80) paramid = param & 0x7F - if param_info.has_key(paramid): + if paramid in param_info: paramname = (param_info[paramid])["n"] # name paramunit = (param_info[paramid])["u"] # unit else: @@ -254,7 +257,7 @@ payload.append(header["mfrid"]) payload.append(header["productid"]) - if not header.has_key("encryptPIP"): + if not ("encryptPIP" in header): if encrypt: warning("no encryptPIP in header, assuming 0x0100") encryptPIP = 0x0100 @@ -273,7 +276,7 @@ wr = rec["wr"] paramid = rec["paramid"] typeid = rec["typeid"] - if rec.has_key("length"): + if "length" in rec: length = rec["length"] else: length = None # auto detect @@ -290,7 +293,7 @@ # VALUE valueenc = [] # in case of no value - if rec.has_key("value"): + if "value" in rec: value = rec["value"] valueenc = Value.encode(value, typeid, length) if len(valueenc) > 15: @@ -579,8 +582,8 @@ mfrid = header["mfrid"] productid = header["productid"] sensorid = header["sensorid"] - if timestamp != None: - print("receive-time:%s" % time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(timestamp))) + if timestamp != None: + print("receive-time:%s" % time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(timestamp))) print("mfrid:%s prodid:%s sensorid:%s" % (hex(mfrid), hex(productid), hex(sensorid))) # RECORDS @@ -594,7 +597,7 @@ paramid = rec["paramid"] paramname = rec["paramname"] paramunit = rec["paramunit"] - if rec.has_key("value"): + if "value" in rec: value = rec["value"] else: value = None diff --git a/src/energenie/Registry.py b/src/energenie/Registry.py index f2316b0..c2ffae1 100644 --- a/src/energenie/Registry.py +++ b/src/energenie/Registry.py @@ -5,7 +5,10 @@ # NOTE: This is an initial, non persisted implementation only import time -import Devices +try: + import Devices # python 2 +except ImportError: + from . import Devices # python 3 directory = {} @@ -24,7 +27,7 @@ header = message["header"] sensorId = header["sensorid"] - if not directory.has_key(sensorId): + if not (sensorId in directory): # new device discovered desc = Devices.getDescription(header["mfrid"], header["productid"]) print("ADD device:%s %s" % (hex(sensorId), desc)) diff --git a/src/energenie/encoder.py b/src/energenie/encoder.py index 366a176..4cc0cec 100644 --- a/src/energenie/encoder.py +++ b/src/energenie/encoder.py @@ -156,7 +156,7 @@ shift = number-2 encoded = [] - for i in range(number/2): + for i in range(int(number/2)): bits = (data >> shift) & 0x03 #print(" shift %d bits %d" % (shift, bits)) encoded.append(ENCODER[bits])