From ef04000c56325d7288a82de1ba56bc59e8b96326 Mon Sep 17 00:00:00 2001 From: Peter Shipley Date: Sun, 9 Mar 2014 19:32:49 -0700 Subject: [PATCH] Funtional but Still a Work in progress --- RainEagle/EagleClass.py | 55 +++++++++++++------ RainEagle/__init__.py | 5 +- Tests/get_hist.py | 53 +++++++++++++++++- bin/get_meter_status.py | 116 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 210 insertions(+), 19 deletions(-) create mode 100644 bin/get_meter_status.py diff --git a/RainEagle/EagleClass.py b/RainEagle/EagleClass.py index 8b49cbc..b2c128c 100644 --- a/RainEagle/EagleClass.py +++ b/RainEagle/EagleClass.py @@ -24,9 +24,19 @@ api_arg_format = { } -__all__ = ['Eagle', 'to_unix_time'] +__all__ = ['Eagle', 'to_epoch_1970, to_epoch_2000'] -def to_unix_time(t) : +def to_epoch_2000(t) : + """ converts time stored as + to unix's epoch of 1970 + offset in seconds from "Jan 1 00:00:00 2000" + """ + if isinstance(t, time.struct_time ) : + t = time.mktime(t) + return t - 946684800 + + +def to_epoch_1970(t) : """ converts time stored as offset in seconds from "Jan 1 00:00:00 2000" to unix's epoch of 1970 @@ -79,18 +89,18 @@ def _et2d(et) : d[child.tag] = child.text return d - def _tohex(n, width=10) : - """ convert arg to string with hex representation if possible""" - if isinstance(n, str) : - if n.isdigit() : - return "{:#{width}x}".format(int(n), width=width) - else : - return n - if isinstance(n, (int, long) ) : - return "{:#{width}x}".format(n, width=width) - if isinstance(n, float) : +def _tohex(n, width=10) : + """ convert arg to string with hex representation if possible""" + if isinstance(n, str) : + if n.isdigit() : return "{:#{width}x}".format(int(n), width=width) - return n + else : + return n + if isinstance(n, (int, long) ) : + return "{:#{width}x}".format(n, width=width) + if isinstance(n, float) : + return "{:#{width}x}".format(int(n), width=width) + return n @@ -298,18 +308,31 @@ class Eagle(object) : # http commands as class functions def get_setting_data(self) : + """ + get settings + """ comm_responce = self._send_http_comm("get_setting_data") return comm_responce def get_device_config(self) : + """ + get configs + """ comm_responce = self._send_http_comm("get_device_config") return comm_responce def get_timezone(self) : + """ + get current timezone configuration + """ comm_responce = self._send_http_comm("get_timezone") return comm_responce def get_time_source(self, macid=None) : + """ + get time source for device + retrrns value "meter" or "nternet" + """ comm_responce = self._send_http_comm("get_time_source") return comm_responce @@ -456,7 +479,7 @@ class Eagle(object) : # Support functions def _connect(self) : - self.soc = socket.create_connection( (self.addr, self.port), 10) + self.soc = socket.create_connection( (self.addr, self.port), self.timeout) def _disconnect(self): try : @@ -509,8 +532,8 @@ class Eagle(object) : try: self._connect() - if cmd == "get_history_data" : - self.soc.settimeout(45) + # if cmd == "get_history_data" : + # self.soc.settimeout(45) self.soc.sendall(commstr) if self.debug : print "commstr : \n", commstr diff --git a/RainEagle/__init__.py b/RainEagle/__init__.py index 34337b7..2fa823b 100644 --- a/RainEagle/__init__.py +++ b/RainEagle/__init__.py @@ -14,10 +14,11 @@ __copyright__ = "Copyright (C) 2014 Peter Shipley" __license__ = "BSD" -from EagleClass import Eagle +import EagleClass +from EagleClass import Eagle, to_epoch_1970, to_epoch_2000 #from RainEagle.EagleClass import Eagle -__all__ = ['Eagle'] +__all__ = ['Eagle', 'to_epoch_1970', 'to_epoch_2000'] diff --git a/Tests/get_hist.py b/Tests/get_hist.py index a664293..e2a57a6 100755 --- a/Tests/get_hist.py +++ b/Tests/get_hist.py @@ -8,6 +8,7 @@ based on TalkToEagle.py import socket import sys import time +import xml.etree.ElementTree as ET my_macid = "0xd8d5b90000001296" @@ -16,6 +17,43 @@ my_macid = "0xd8d5b90000001296" # Enter your Eagle's IP below Eagle_IP = "10.1.1.39" +def print_summ(cs) : + # global last_delivered + # time_stamp = eg.to_epoch_1970(cs['TimeStamp']) + + _delivered = cs.find('SummationDelivered').text + _received = cs.find('SummationReceived').text + _multiplier = cs.find('Multiplier').text + _divisor = cs.find('Divisor').text + + # print "Multiplier=", _multiplier, "Divisor=", _divisor, "Delivered=", _delivered, "Received=", _received + + multiplier=int(_multiplier, 16) + divisor=int(_divisor, 16) + delivered=int(_delivered, 16) + received=int(_received, 16) + time_stamp = 946684800 + int(cs.find('TimeStamp').text, 16) + + # print "Multiplier=", multiplier, "Divisor=", divisor, "Delivered=", delivered, "Received=", received, "TimeStamp", time_stamp + + if multiplier == 0 : + multiplier=1 + + if divisor == 0 : + divisor=1 + + reading_received = received * multiplier / float (divisor ) + reading_delivered = delivered * multiplier / float (divisor ) + #reading_delta = (reading_delivered - last_delivered) + #last_delivered = reading_delivered + + print time.asctime(time.localtime(time_stamp)), " : ", reading_received, "\t", reading_delivered + + +def print_reading(eg, rd) : + for dat in rd['Reading'] : + time_stamp = time.asctime(time.localtime( to_epoch_1970(dat['TimeStamp']) ) ) + ## list_devices s = socket.create_connection( (Eagle_IP, 5002), 10) @@ -62,13 +100,26 @@ time.sleep(1) print "Eagle response: \n\r" +j=0 +buf_list = [] while 1: buf = s.recv(1000) if not buf: break - sys.stdout.write(buf) + buf_list.append(buf) + #sys.stdout.write(buf) + j = j + 1 + +result_xml = ''.join(buf_list) +print result_xml +etree = ET.fromstring(result_xml) +for cs in etree.iter('CurrentSummation'): + print_summ(cs) + +print "j =", j s.close() +#main() exit(0) diff --git a/bin/get_meter_status.py b/bin/get_meter_status.py new file mode 100644 index 0000000..43259e6 --- /dev/null +++ b/bin/get_meter_status.py @@ -0,0 +1,116 @@ +#!/usr/local/bin/python2.7 +""" + A simple script get current meter values +""" + +__author__ = "Peter Shipley" + +import sys +sys.path.append('/usr/home/shipley/Projects/Eagle') # temp + + +# import RainEagle +from RainEagle import Eagle, to_epoch_1970 +import time +from pprint import pprint +import json + +last_delivered = 0 +debug = 0 + + +def main() : + eg = Eagle( debug=debug , addr="10.1.1.39") + # timeout=45, + + # print "\nlist_devices :" + # r = eg.list_devices() + # print "pprint 2" + # pprint(r) + + + + print "\nget_device_data :" + r = eg.get_device_data() + print + + # pprint(r['InstantaneousDemand']) + print_instantdemand( r['InstantaneousDemand']) + print + + # pprint(r['CurrentSummation']) + print_currentsummation(r['CurrentSummation']) + print + + exit(0) + + +def print_currentsummation(cs) : + + multiplier=int(cs['Multiplier'], 16) + divisor=int(cs['Divisor'], 16) + delivered=int(cs['SummationDelivered'], 16) + received=int(cs['SummationReceived'], 16) + + if multiplier == 0 : + multiplier=1 + + if divisor == 0 : + divisor=1 + + reading_received = received * multiplier / float (divisor ) + reading_delivered = delivered * multiplier / float (divisor ) + + time_stamp = to_epoch_1970(cs['TimeStamp']) + + print time.asctime(time.localtime(time_stamp)), " : " + print "\tReceived=", reading_received, "Kw" + print "\tDelivered=", reading_delivered, "Kw" + print "\t\t", (reading_delivered - reading_received) + + +# print "{0}\t{1:.4f}\t{2:0.4f}\t{3:.4f}".format( +# time.strftime("%Y-%m-%d %H:%M:%S", time_struct), +# reading_received, +# reading_delivered, +# (reading_delivered - reading_received) ) + + +def print_instantdemand(idemand) : + + time_stamp = to_epoch_1970(idemand['TimeStamp']) + + multiplier=int(idemand['Multiplier'], 16) + divisor=int(idemand['Divisor'], 16) + demand=int(idemand['Demand'], 16) + + # print "Multiplier=", multiplier, "Divisor=", divisor, "Demand=", demand + + if multiplier == 0 : + multiplier=1 + + if divisor == 0 : + divisor=1 + + reading = demand * multiplier / float (divisor ) + + print time.asctime(time.localtime(time_stamp)), " : " + print "\tDemand=", reading, "Kw" + print "\tAmps={:.3f}".format( ((reading * 1000) / 240) ) + + + +def print_reading(eg, rd) : + for dat in rd['Reading'] : + the_time = time.asctime(time.localtime( to_epoch_1970(dat['TimeStamp']) ) ) + print the_time, "Type=", dat['Type'], "Value=", dat['Value'] + +# +if __name__ == "__main__": + # import __main__ + # print(__main__.__file__) + # print("syntax ok") + main() + exit(0) + +