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.
 
 

75 lines
2.7 KiB

  1. import json
  2. import RainEagle
  3. import sys
  4. import time
  5. def print_data(*args):
  6. print '\t'.join(x.encode('utf8').encode('string-escape') for x in args)
  7. if __name__ == '__main__':
  8. with open('eagle.config') as fp:
  9. conf = json.load(fp)
  10. raineagle = RainEagle.Eagle( debug=0 , addr=conf['addr'],
  11. username = conf['username'],
  12. password = conf['password']
  13. )
  14. try:
  15. macid = conf['macid']
  16. except KeyError:
  17. ret_data = raineagle.list_devices()
  18. #print 'list devices:', `ret_data`
  19. macid = ret_data['DeviceInfo']['DeviceMacId']
  20. #print "device MacID = ", macid
  21. print >>sys.stderr, 'macid not specified, detected:', `macid`
  22. #macid = '0xd8d5b90000001346'
  23. #print `raineagle.get_instantaneous_demand(macid)`
  24. #print `raineagle.get_summation_values(macid, interval='day')`
  25. #print `raineagle.get_fast_poll_status(macid)`
  26. fields = [ 'demand_timestamp', 'meter_status', 'demand',
  27. 'threshold_upper_demand', 'demand_units', 'summation_delivered',
  28. 'summation_received', 'summation_units', ]
  29. tzfields = [ 'timezone_tzName', 'timezone_localTime',
  30. 'timezone_utcTime', 'timezone_utcOffset' ]
  31. interval = conf.get('interval', 10)
  32. tzinterval = conf.get('tzinterval', 24*60*60)
  33. tzs = s = time.time()
  34. while True:
  35. usage_data = raineagle.get_usage_data(macid)
  36. if False:
  37. print `usage_data`
  38. else:
  39. #{u'message_queue': u'active', u'threshold_lower_demand': u'-2.000000', u'fast_poll_frequency': u'0x00', u'summation_delivered': u'65374.021', u'message_read': u'Y', u'price': u'0.2700', u'demand_timestamp': u'1571842464', u'fast_poll_endtime': u'0x00000000', u'meter_status': u'Connected', u'message_confirm_required': u'N', u'message_confirmed': u'N', u'demand_units': u'kW', u'summation_units': u'kWh', u'demand': u'0.9980', u'price_units': u'840', u'message_timestamp': u'946684800', u'price_label': u'Set by User', u'threshold_upper_demand': u'9.155000', u'message_id': u'0', u'summation_received': u'0.000'}
  40. try:
  41. print_data('l', *(usage_data[x] for x in fields))
  42. except KeyError:
  43. print_data('e', 'usage', `usage_data`)
  44. e = time.time()
  45. if e >= tzs:
  46. tzs += tzinterval
  47. # {u'timezone_tzName': u'GMT+7', u'timezone_status': u'success', u'timezone_localTime': u'1571844539', u'timezone_olsonName': u'Etc/GMT+7', u'timezone_utcTime': u'1571869739', u'timezone_utcOffset': u'-0700'}
  48. tzinfo = raineagle.get_timezone(macid)
  49. tzoffset = int(tzinfo['timezone_utcTime']) - \
  50. int(tzinfo['timezone_localTime'])
  51. try:
  52. print_data('z', *(tzinfo[x] for x in tzfields))
  53. except KeyError:
  54. print_data('e', 'tz', `usage_data`)
  55. sys.stdout.flush()
  56. s += interval
  57. delay = s - e
  58. if delay <= 0:
  59. s = time.time()
  60. continue
  61. time.sleep(delay)
  62. #print `raineagle.get_time_source(macid)`