MdO82

Untitled

Apr 17th, 2016
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.15 KB | None | 0 0
  1. # encoding: utf-8
  2. '''
  3. pvoutput -- Upload van Domoticz naar pvoutput.org
  4.  
  5. Instructies:
  6.  
  7. Status informatie voor pvoutput kan ingesteld worden op 5, 10 of 15 minuten,
  8. roep dit programma aan op dat interval voor een juiste upload.
  9.  
  10. @author: Nicky Bulthuis
  11.  
  12. @copyright: 2014 Nicky Bulthuis. All rights reserved.
  13.  
  14. @license: BSD
  15.  
  16. @deffield updated: Updated
  17. '''
  18.  
  19. import sys
  20. import json
  21. import urllib2
  22. import re
  23. from decimal import Decimal
  24.  
  25. from argparse import ArgumentParser
  26.  
  27.  
  28. class Domoticz():
  29.  
  30. def __init__(self, url):
  31.  
  32. self.baseurl = url
  33.  
  34. def __execute__(self, url):
  35.  
  36. req = urllib2.Request(url)
  37. return urllib2.urlopen(req, timeout=5)
  38.  
  39. def get_device(self, xid):
  40. """
  41. Get the device information.
  42. """
  43. url = "%s/json.htm?type=devices&rid=%s" % (self.baseurl, xid)
  44. data = json.load(self.__execute__(url))
  45. return data
  46.  
  47.  
  48. def main(argv=None): # IGNORE:C0111
  49. '''Command line options.'''
  50.  
  51. parser = ArgumentParser(description='Upload van Domoticz naar pvoutput.org')
  52. parser.add_argument("-a", "--apikey", dest="apikey", help="PVOutput API Key", required=True)
  53. parser.add_argument("-s", "--sid", dest="sid", help="PVOutput System Id", required=True)
  54. parser.add_argument("-u", "--url", dest="url", help="URL naar Domoticz, eg: http://localhost:8080", default='http://localhost:8080')
  55. parser.add_argument("-e", "--e-device-id", dest="e_device_id", help="Domoticz Device Id voor het Elektriciteits verbruik", type=int, required=True)
  56. parser.add_argument("-p", "--pv-device-id", dest="pv_device_id", help="Domoticz Device Id voor het PV", type=int)
  57.  
  58. # Process arguments
  59. args = parser.parse_args()
  60.  
  61. e_in, e_uit, e_in_pwr, e_uit_pwr = get_e(args.url, args.e_device_id)
  62.  
  63. pv_in = 0
  64. pv_pwr = 0;
  65.  
  66. if args.pv_device_id:
  67. pv_in, pv_pwr = get_pv(args.url, args.pv_device_id)
  68.  
  69. v1 = pv_in
  70. v2 = pv_pwr
  71. v3 = e_in + pv_in - e_uit
  72. v4 = max(e_in_pwr + pv_pwr - abs(e_uit_pwr), 0)
  73.  
  74. code = upload_to_pvoutput(args.apikey, args.sid, v1, v2, v3, v4)
  75.  
  76. if code == 200:
  77. return 0
  78. else:
  79. return code
  80.  
  81. def floorTime(dt=None, roundTo=60):
  82. """
  83. Floor a datetime object to any time laps in seconds
  84. dt : datetime.datetime object, default now.
  85. """
  86. import datetime
  87.  
  88. if dt is None:
  89. dt = datetime.datetime.now()
  90.  
  91. dt_min = datetime.datetime(datetime.MINYEAR, 1, 1)
  92. dt_min = dt_min.replace(tzinfo=dt.tzinfo)
  93.  
  94. seconds = (dt - dt_min).seconds
  95. rounding = seconds // roundTo * roundTo
  96. return dt + datetime.timedelta(0, rounding - seconds, -dt.microsecond)
  97.  
  98. def upload_to_pvoutput(apikey, sid, v1, v2, v3, v4):
  99.  
  100. now = floorTime(roundTo=60*5)
  101. d = now.strftime('%Y%m%d')
  102. t = now.strftime('%H:%M')
  103.  
  104. url = 'http://pvoutput.org/service/r2/addstatus.jsp?d=%s&t=%s&v1=%s&v2=%s&v3=%s&v4=%s' % (d, t, v1, v2, v3, v4)
  105.  
  106. print url
  107. req = urllib2.Request(url)
  108. req.add_header('X-Pvoutput-Apikey', apikey)
  109. req.add_header('X-Pvoutput-SystemId', sid)
  110.  
  111. return urllib2.urlopen(req).getcode()
  112.  
  113.  
  114. def get_e(url, device_id):
  115. """
  116. Ophalen gegevens voor het elektriciteit.
  117. """
  118.  
  119. device_data = Domoticz(url).get_device(device_id)
  120. data = device_data['result'][0]
  121.  
  122. p = re.compile('^([0-9\.]+) .*$')
  123.  
  124. e_in = int(Decimal(p.match(data['CounterToday']).group(1)) * 1000)
  125. e_uit = int(Decimal(p.match(data['CounterDelivToday']).group(1)) * 1000)
  126.  
  127. e_in_pwr = int(p.match(data['Usage']).group(1))
  128. e_uit_pwr = int(p.match(data['UsageDeliv']).group(1))
  129.  
  130. return e_in, e_uit, e_in_pwr, e_uit_pwr
  131.  
  132.  
  133. def get_pv(url, device_id):
  134. """
  135. Ophalen gegevens voor PV.
  136. """
  137. device_data = Domoticz(url).get_device(device_id)
  138. data = device_data['result'][0]
  139.  
  140. p = re.compile('^([0-9\.]+) .*$')
  141.  
  142. pv = int(Decimal(p.match(data['CounterToday']).group(1)) * 1000)
  143. pv_pwr = int(p.match(data['Usage']).group(1))
  144.  
  145. return pv, pv_pwr
  146.  
  147.  
  148. if __name__ == "__main__":
  149. sys.exit(main())
Advertisement
Add Comment
Please, Sign In to add comment