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.
 
 

139 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, unknown = parser.parse_known_args()
  36. # print "Args = ", args, vars(args)
  37. # print "unknown = ", unknown
  38. eg = Eagle(**vars(args))
  39. # timeout=45,
  40. r = eg.get_device_data()
  41. print_instantdemand(r['InstantaneousDemand'])
  42. print
  43. print_currentsummation(r['CurrentSummation'])
  44. print
  45. exit(0)
  46. def twos_comp(val, bits=32):
  47. """compute the 2's compliment of int value val"""
  48. if( (val&(1<<(bits-1))) != 0 ):
  49. val = val - (1<<bits)
  50. return val
  51. def print_currentsummation(cs) :
  52. multiplier = int(cs['Multiplier'], 16)
  53. divisor = int(cs['Divisor'], 16)
  54. delivered = int(cs['SummationDelivered'], 16)
  55. received = int(cs['SummationReceived'], 16)
  56. if multiplier == 0 :
  57. multiplier = 1
  58. if divisor == 0 :
  59. divisor = 1
  60. reading_received = received * multiplier / float (divisor)
  61. reading_delivered = delivered * multiplier / float (divisor)
  62. if 'TimeStamp' in cs :
  63. time_stamp = to_epoch_1970(cs['TimeStamp'])
  64. print "{0:s} : ".format(time.asctime(time.localtime(time_stamp)))
  65. print "\tReceived = {0:10.3f} Kw".format(reading_received)
  66. print "\tDelivered = {0:10.3f} Kw".format(reading_delivered)
  67. print "\tMeter = {0:10.3f} Kw".format( (reading_delivered - reading_received))
  68. def print_instantdemand(idemand) :
  69. multiplier = int(idemand['Multiplier'], 16)
  70. divisor = int(idemand['Divisor'], 16)
  71. # demand = twos_comp(int(idemand['Demand'], 16))
  72. demand = int(idemand['Demand'], 16)
  73. if demand > 0x7FFFFFFF:
  74. demand -= 0x100000000
  75. if multiplier == 0 :
  76. multiplier = 1
  77. if divisor == 0 :
  78. divisor = 1
  79. reading = (demand * multiplier) / float (divisor)
  80. if 'TimeStamp' in idemand :
  81. time_stamp = to_epoch_1970(idemand['TimeStamp'])
  82. print "{0:s} : ".format(time.asctime(time.localtime(time_stamp)))
  83. print "\tDemand = {0:10.3f} Kw".format(reading)
  84. print "\tAmps = {0:10.3f}".format( ((reading * 1000) / 240))
  85. #
  86. if __name__ == "__main__":
  87. # import __main__
  88. # print(__main__.__file__)
  89. # print("syntax ok")
  90. main()
  91. exit(0)