RainEagle library plus script for polling data
 
 

125 lines
2.8 KiB

  1. #!/usr/local/bin/python2.7
  2. """
  3. A simple script get current meter values
  4. """
  5. __author__ = "Peter Shipley"
  6. import sys
  7. sys.path.append('/usr/home/shipley/Projects/Eagle') # temp
  8. # import RainEagle
  9. from RainEagle import Eagle, to_epoch_1970
  10. import time
  11. from pprint import pprint
  12. import json
  13. last_delivered = 0
  14. debug = 0
  15. def main() :
  16. eg = Eagle( debug=debug , addr="10.1.1.39")
  17. # timeout=45,
  18. # print "\nlist_devices :"
  19. # r = eg.list_devices()
  20. # print "pprint 2"
  21. # pprint(r)
  22. print "\nget_device_data :"
  23. r = eg.get_device_data()
  24. print
  25. # pprint(r['InstantaneousDemand'])
  26. print_instantdemand( r['InstantaneousDemand'])
  27. print
  28. # pprint(r['CurrentSummation'])
  29. print_currentsummation(r['CurrentSummation'])
  30. print
  31. exit(0)
  32. def twos_comp(val, bits=32):
  33. """compute the 2's compliment of int value val"""
  34. if( (val&(1<<(bits-1))) != 0 ):
  35. val = val - (1<<bits)
  36. return val
  37. def print_currentsummation(cs) :
  38. multiplier=int(cs['Multiplier'], 16)
  39. divisor=int(cs['Divisor'], 16)
  40. delivered=int(cs['SummationDelivered'], 16)
  41. received=int(cs['SummationReceived'], 16)
  42. if multiplier == 0 :
  43. multiplier=1
  44. if divisor == 0 :
  45. divisor=1
  46. reading_received = received * multiplier / float (divisor )
  47. reading_delivered = delivered * multiplier / float (divisor )
  48. time_stamp = to_epoch_1970(cs['TimeStamp'])
  49. print time.asctime(time.localtime(time_stamp)), " : "
  50. print "\tReceived =", reading_received, "Kw"
  51. print "\tDelivered=", reading_delivered, "Kw"
  52. print "\t\t\t", (reading_delivered - reading_received)
  53. # print "{0}\t{1:.4f}\t{2:0.4f}\t{3:.4f}".format(
  54. # time.strftime("%Y-%m-%d %H:%M:%S", time_struct),
  55. # reading_received,
  56. # reading_delivered,
  57. # (reading_delivered - reading_received) )
  58. def print_instantdemand(idemand) :
  59. time_stamp = to_epoch_1970(idemand['TimeStamp'])
  60. multiplier=int(idemand['Multiplier'], 16)
  61. divisor=int(idemand['Divisor'], 16)
  62. # demand = twos_comp(int(idemand['Demand'], 16))
  63. demand=int(idemand['Demand'], 16)
  64. if demand > 0x7FFFFFFF:
  65. demand -= 0x100000000
  66. # print "Multiplier=", multiplier, "Divisor=", divisor, "Demand=", demand
  67. if multiplier == 0 :
  68. multiplier=1
  69. if divisor == 0 :
  70. divisor=1
  71. reading = (demand * multiplier) / float (divisor )
  72. print time.asctime(time.localtime(time_stamp)), " : "
  73. print "\tDemand=", reading, "Kw"
  74. print "\tAmps = {:.3f}".format( ((reading * 1000) / 240) )
  75. def print_reading(eg, rd) :
  76. for dat in rd['Reading'] :
  77. the_time = time.asctime(time.localtime( to_epoch_1970(dat['TimeStamp']) ) )
  78. print the_time, "Type=", dat['Type'], "Value=", dat['Value']
  79. #
  80. if __name__ == "__main__":
  81. # import __main__
  82. # print(__main__.__file__)
  83. # print("syntax ok")
  84. main()
  85. exit(0)