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.
 
 

67 lines
2.5 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. interval = conf.get('interval', 10)
  27. tzinterval = conf.get('tzinterval', 24*60*60)
  28. tzs = s = time.time()
  29. while True:
  30. usage_data = raineagle.get_usage_data(macid)
  31. if False:
  32. print `usage_data`
  33. else:
  34. #{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'}
  35. fields = [ 'demand_timestamp', 'meter_status', 'demand',
  36. 'threshold_upper_demand', 'demand_units', 'summation_delivered',
  37. 'summation_received', 'summation_units', ]
  38. print_data('l', *(usage_data[x] for x in fields))
  39. e = time.time()
  40. if e >= tzs:
  41. tzs += tzinterval
  42. # {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'}
  43. tzinfo = raineagle.get_timezone(macid)
  44. tzoffset = int(tzinfo['timezone_utcTime']) - \
  45. int(tzinfo['timezone_localTime'])
  46. tzfields = [ 'timezone_tzName', 'timezone_localTime',
  47. 'timezone_utcTime', 'timezone_utcOffset' ]
  48. print_data('z', *(tzinfo[x] for x in tzfields))
  49. sys.stdout.flush()
  50. s += interval
  51. delay = s - e
  52. if delay <= 0:
  53. s = time.time()
  54. continue
  55. time.sleep(delay)
  56. #print `raineagle.get_time_source(macid)`