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.
 
 

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