diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cd89388 --- /dev/null +++ b/.gitignore @@ -0,0 +1,41 @@ +*.py[cod] + +EGG-INFO +.exrc +test.* +Sav/* +Bak/* +*bak + +# C extensions +*.so + +# Packages +*.egg +*.egg-info +dist +build +eggs +parts +var +sdist +develop-eggs +.installed.cfg +lib +lib64 + +# Installer logs +pip-log.txt + +# Unit test / coverage reports +.coverage +.tox +nosetests.xml + +# Translations +*.mo + +# Mr Developer +.mr.developer.cfg +.project +.pydevproject diff --git a/RainEagle/EagleClass.py b/RainEagle/EagleClass.py index 1ab8663..639a2a2 100644 --- a/RainEagle/EagleClass.py +++ b/RainEagle/EagleClass.py @@ -3,9 +3,57 @@ import socket import sys import os import time +import xml.etree.ElementTree as ET + __all__ = ['Eagle'] +def et2d(et) : + """ Etree to Dict + + converts an ETree to a Dict Tree + lists are created for duplicate tag + + if there are multiple XML of the name name + an list array is used + attrib tags are converted to "tag_name" + "attrib_name" + + if an invalid arg is passed a empty dict is retrurned + + + arg: ETree Element obj + + returns: a dict obj + """ + d = dict() + if not isinstance(et, ET.Element) : + return d + children = list(et) + if et.attrib : + for k, v in list(et.items()) : + d[et.tag + "-" + k] = v + if children : + for child in children : + if child.tag in d : + if type(d[child.tag]) != list : + t = d[child.tag] + d[child.tag] = [t] + if list(child) or child.attrib : + if child.tag in d : + d[child.tag].append(et2d(child)) + else : + d[child.tag] = et2d(child) + else : + if child.tag in d : + d[child.tag].append(child.text) + else : + d[child.tag] = child.text + return d + + + +# +# Simple Base class for ISY Class class Eagle(object) : def __init__(self, **kwargs): @@ -14,7 +62,7 @@ class Eagle(object) : if self.debug : print self.__class__.__name__, __name__ self.addr = kwargs.get("addr", os.getenv('EAGLE_ADDR', None)) - self.port = kwargs.get("port", 5002) + self.port = kwargs.get("port", os.getenv('EAGLE_PORT', 5002)) self.soc = None @@ -78,24 +126,76 @@ class Eagle(object) : return replystr -# command as class funtions +# commands as class funtions def list_devices(self): comm_responce = self._send_comm("list_devices") - return comm_responce + # temp debug data + comm_responce = "" \ + " 0xd8d5b90000000xxx" \ + " 0x9ac4382dffa81xxx" \ + " 7e572b66c5b444xxx" \ + " 94227dca4e773xxx" \ + " 1.4.27 (5278)" \ + " 1.2.3" \ + " Rainforest Automation, I" \ + " RFA-Z109 EAGLE" \ + " 20130308PO020621" \ + "\n" + + etree = ET.fromstring('' + comm_responce + '' ) + rv = et2d(etree) + return rv # 3 def get_device_data(self, macid) : """ Send the GET_DEVICE_DATA command to get a data dump """ comm_responce = self._send_comm("get_device_data", MacId=macid) - return comm_responce + # temp debug data + comm_responce = "" \ + " 0xd8d5b90000000xxx" \ + " Rejoining" \ + " 0x001350030011bxxx" \ + " 0x7fffffffffffffff" \ + " 0x0000ffff" \ + " 24" \ + " 156" \ + "" \ + "" \ + " 0xd8d5b90000000xxx" \ + " 0x9ac4382dffa81xxx" \ + " 7e572b66c5b44xxx" \ + " 94227dca4e773xxx" \ + " 1.4.27 (5278)" \ + " 1.2.3" \ + " Rainforest Automation, I" \ + " RFA-Z109 EAGLE" \ + " 20130308PO020621" \ + "" \ + "" \ + " 0xd8d5b90000000xxx" \ + " 0x001350030011bxxx" \ + " 0x00000bf1" \ + " 0x195193e3" \ + " 0x00000001" \ + " 0x000003e8" \ + " 0x00000003" \ + " 0x0000000f" \ + " 0x0001" \ + "" + + etree = ET.fromstring('' + comm_responce + '' ) + rv = et2d(etree) + return rv # 10 def get_instantaneous_demand(self, macid, interval) : """ Send the GET_INSTANTANEOUS_DEMAND command to get the real time demand from the meter""" comm_responce = self._send_comm("get_instantaneous_demand", MacId=macid, Interval=interval) - return comm_responce + etree = ET.fromstring('' + comm_responce + '' ) + rv = et2d(etree) + return rv # 11 def get_demand_values(self, macid, interval, frequency=None ) : @@ -104,27 +204,35 @@ class Eagle(object) : if frequency : kwargs["Frequency"] = frequency comm_responce = self._send_comm("get_demand_values", **kwargs) - return comm_responce + etree = ET.fromstring('' + comm_responce + '' ) + rv = et2d(etree) + return rv # 12 def get_summation_values(self, macid, interval) : """ Send the GET_SUMMATION_VALUES command to get a series of net summation values """ comm_responce = self._send_comm("get_summation_values", MacId=macid, Interval=interval ) - return comm_responce + etree = ET.fromstring('' + comm_responce + '' ) + rv = et2d(etree) + return rv # 14 def set_fast_poll(self, macid, frequency, duration) : """ set the fast poll mode on the meter. """ comm_responce = self._send_comm("get_instantaneous_demand", MacId=macid, Frequency=frequency, Duration=duration) - return comm_responce + etree = ET.fromstring('' + comm_responce + '' ) + rv = et2d(etree) + return rv # 15 def get_fast_poll_status(self, macid) : """ get the current status of fast poll mode. """ comm_responce = self._send_comm("get_fast_poll_status", MacId=macid) - return comm_responce + etree = ET.fromstring('' + comm_responce + '' ) + rv = et2d(etree) + return rv # 17 @@ -136,7 +244,9 @@ class Eagle(object) : if frequency : kwargs["Frequency"] = frequency comm_responce = self._send_comm("get_fast_poll_status", **kwargs) - return comm_responce + etree = ET.fromstring('' + comm_responce + '' ) + rv = et2d(etree) + return rv # Do nothing # (syntax check) diff --git a/test.py b/test.py index a3dc0b2..fa8b870 100644 --- a/test.py +++ b/test.py @@ -1,9 +1,12 @@ import RainEagle +from pprint import pprint eg = RainEagle.Eagle( debug=1 ) -eg.list_devices() +r = eg.list_devices() +pprint(r) -eg.get_device_data("EE:24:01:05:50:23") +r = eg.get_device_data("EE:24:01:05:50:23") +pprint(r)