RainEagle library plus script for polling data
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

136 lines
3.2 KiB

  1. #!/usr/local/bin/python2.7
  2. """
  3. A simple script get current meter values
  4. """
  5. __author__ = "Peter Shipley"
  6. __version__ = "0.1.7"
  7. # import RainEagle
  8. from RainEagle import Eagle, to_epoch_1970
  9. import time
  10. import os
  11. import argparse
  12. from pprint import pprint
  13. debug = 0
  14. def create_parser():
  15. parser = argparse.ArgumentParser(
  16. description="print power meter status")
  17. parser.add_argument("-a", "--address", dest="addr",
  18. default=os.getenv('EAGLE_ADDR', None),
  19. help="hostname or IP device")
  20. parser.add_argument("-p", "--port", dest="port", type=int,
  21. default=os.getenv('EAGLE_PORT', 5002),
  22. help="command socket port")
  23. parser.add_argument("-d", "--debug", dest="debug",
  24. default=debug, action="count",
  25. help="print debug info")
  26. parser.add_argument("-m", "--mac", dest="mac",
  27. help="Eagle radio mac addrress")
  28. parser.add_argument("-t", "--timeout", dest="timeout",
  29. help="Socket timeout")
  30. parser.add_argument("-v", '--version', action='version',
  31. version="%(prog)s {0}".format(__version__) )
  32. return parser
  33. def main() :
  34. parser = create_parser()
  35. args = parser.parse_args()
  36. eg = Eagle(**vars(args))
  37. # timeout=45,
  38. r = eg.get_device_data()
  39. print_instantdemand(r['InstantaneousDemand'])
  40. print
  41. print_currentsummation(r['CurrentSummation'])
  42. print
  43. exit(0)
  44. def twos_comp(val, bits=32):
  45. """compute the 2's compliment of int value val"""
  46. if( (val&(1<<(bits-1))) != 0 ):
  47. val = val - (1<<bits)
  48. return val
  49. def print_currentsummation(cs) :
  50. multiplier = int(cs['Multiplier'], 16)
  51. divisor = int(cs['Divisor'], 16)
  52. delivered = int(cs['SummationDelivered'], 16)
  53. received = int(cs['SummationReceived'], 16)
  54. if multiplier == 0 :
  55. multiplier = 1
  56. if divisor == 0 :
  57. divisor = 1
  58. reading_received = received * multiplier / float (divisor)
  59. reading_delivered = delivered * multiplier / float (divisor)
  60. if 'TimeStamp' in cs :
  61. time_stamp = to_epoch_1970(cs['TimeStamp'])
  62. print "{0:s} : ".format(time.asctime(time.localtime(time_stamp)))
  63. print "\tReceived = {0:{width}.3f} Kw".format(reading_received, width=10)
  64. print "\tDelivered = {0:{width}.3f} Kw".format(reading_delivered, width=10)
  65. print "\t\t{0:{width}.3f} Kw".format( (reading_delivered - reading_received), width=14)
  66. def print_instantdemand(idemand) :
  67. multiplier = int(idemand['Multiplier'], 16)
  68. divisor = int(idemand['Divisor'], 16)
  69. # demand = twos_comp(int(idemand['Demand'], 16))
  70. demand = int(idemand['Demand'], 16)
  71. if demand > 0x7FFFFFFF:
  72. demand -= 0x100000000
  73. if multiplier == 0 :
  74. multiplier = 1
  75. if divisor == 0 :
  76. divisor = 1
  77. reading = (demand * multiplier) / float (divisor)
  78. if 'TimeStamp' in idemand :
  79. time_stamp = to_epoch_1970(idemand['TimeStamp'])
  80. print "{0:s} : ".format(time.asctime(time.localtime(time_stamp)))
  81. print "\tDemand = {0:{width}.3f} Kw".format(reading, width=10)
  82. print "\tAmps = {0:{width}.3f}".format( ((reading * 1000) / 240), width=10)
  83. #
  84. if __name__ == "__main__":
  85. # import __main__
  86. # print(__main__.__file__)
  87. # print("syntax ok")
  88. main()
  89. exit(0)