RainEagle library plus script for polling data
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 

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