-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdbus_smappee.py
201 lines (164 loc) · 7.96 KB
/
dbus_smappee.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/python -u
import sys, os
import json
import logging
from itertools import groupby, count, izip_longest, izip
from argparse import ArgumentParser
sys.path.insert(1, os.path.join(os.path.dirname(__file__), 'ext', 'velib_python'))
from dbus.mainloop.glib import DBusGMainLoop
import dbus
import gobject
from vedbus import VeDbusService
from settingsdevice import SettingsDevice
from bridge import MqttGObjectBridge
VERSION = '0.1'
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
# We define these classes to avoid connection sharing to dbus. This is to allow
# more than one service to be held by a single python process.
class SystemBus(dbus.bus.BusConnection):
def __new__(cls):
return dbus.bus.BusConnection.__new__(cls, dbus.bus.BusConnection.TYPE_SYSTEM)
class SessionBus(dbus.bus.BusConnection):
def __new__(cls):
return dbus.bus.BusConnection.__new__(cls, dbus.bus.BusConnection.TYPE_SESSION)
def dbusconnection():
return SessionBus() if 'DBUS_SESSION_BUS_ADDRESS' in os.environ else SystemBus()
class Meter(object):
""" Represent a meter object on dbus. """
def __init__(self, name, host, base, instance, cts):
self.instance = instance
self.cts = cts
self.service = service = VeDbusService(
"{}.smappee_{:02d}".format(base, instance), bus=dbusconnection())
# Add objects required by ve-api
service.add_path('/Management/ProcessName', __file__)
service.add_path('/Management/ProcessVersion', VERSION)
service.add_path('/Management/Connection', host)
service.add_path('/DeviceInstance', instance)
service.add_path('/ProductId', 0xFFFF) # 0xB012 ?
service.add_path('/ProductName', "Smappee - {}".format(name))
service.add_path('/FirmwareVersion', None)
service.add_path('/Serial', None)
service.add_path('/Connected', 1)
_kwh = lambda p, v: (str(v) + 'KWh')
_a = lambda p, v: (str(v) + 'A')
_w = lambda p, v: (str(v) + 'W')
_v = lambda p, v: (str(v) + 'V')
service.add_path('/Ac/Energy/Forward', None, gettextcallback=_kwh)
service.add_path('/Ac/Energy/Reverse', None, gettextcallback=_kwh)
service.add_path('/Ac/L1/Current', None, gettextcallback=_a)
service.add_path('/Ac/L1/Energy/Forward', None, gettextcallback=_kwh)
service.add_path('/Ac/L1/Energy/Reverse', None, gettextcallback=_kwh)
service.add_path('/Ac/L1/Power', None, gettextcallback=_w)
service.add_path('/Ac/L1/Voltage', None, gettextcallback=_v)
service.add_path('/Ac/L2/Current', None, gettextcallback=_a)
service.add_path('/Ac/L2/Energy/Forward', None, gettextcallback=_kwh)
service.add_path('/Ac/L2/Energy/Reverse', None, gettextcallback=_kwh)
service.add_path('/Ac/L2/Power', None, gettextcallback=_w)
service.add_path('/Ac/L2/Voltage', None, gettextcallback=_v)
service.add_path('/Ac/L3/Current', None, gettextcallback=_a)
service.add_path('/Ac/L3/Energy/Forward', None, gettextcallback=_kwh)
service.add_path('/Ac/L3/Energy/Reverse', None, gettextcallback=_kwh)
service.add_path('/Ac/L3/Power', None, gettextcallback=_w)
service.add_path('/Ac/L3/Voltage', None, gettextcallback=_v)
service.add_path('/Ac/Power', None, gettextcallback=_w)
# Provide debug info about what cts make up what meter
service.add_path('/Debug/Cts', ','.join(str(c) for c in cts))
def set_path(self, path, value):
if self.service[path] != value:
self.service[path] = value
def update(self, voltages, powers):
totalpower = totalforward = totalreverse = 0
for phase, ct in izip(count(), self.cts):
# Fill in the values
d = powers[ct]
line = '/Ac/L{}'.format(phase+1)
self.set_path('{}/Current'.format(line), d['current'])
self.set_path('{}/Energy/Forward'.format(line), round(d['importEnergy']/3600000, 1))
self.set_path('{}/Energy/Reverse'.format(line), round(d['exportEnergy']/3600000, 1))
self.set_path('{}/Power'.format(line), d['power'])
self.set_path('{}/Voltage'.format(line), voltages.get(phase, None))
totalpower += d['power']
totalforward += d['importEnergy']
totalreverse += d['exportEnergy']
# Update the totals
self.set_path('/Ac/Power', totalpower)
self.set_path('/Ac/Energy/Forward', round(totalforward/3600000, 1))
self.set_path('/Ac/Energy/Reverse', round(totalreverse/3600000, 1))
def __repr__(self):
return self.__class__.__name__ + "(" + str(self.cts) + ")"
def __del__(self):
self.service.__del__()
class Bridge(MqttGObjectBridge):
def __init__(self, base, host, *args, **kwargs):
super(Bridge, self).__init__(host, *args, **kwargs)
self.base = base
self.host = host
self.meters = []
def _allocate_meters(self, name, channels, n, base2, instance_offset):
""" Allocates up to n meters from channels, attempting
to make three-phase meters. """
meters = []
phases = {i: [] for i in range(3)}
for phase, _channels in groupby(channels, lambda x: x['phase']):
phases[phase].extend(_channels)
spread = izip_longest(phases[0], phases[1], phases[2])
for c, phasedata in izip(count(), spread):
# stop when we have enough
if n is not None and c >= n: break
# Current sensors that makes up this meter
ids = [x['ctInput'] for x in phasedata]
meters.append(Meter(name, self.host,
'{}.{}'.format(self.base, base2), c+instance_offset, ids))
return meters
def _on_message(self, client, userdata, msg):
try:
data = json.loads(msg.payload)
except ValueError:
logger.warning('Malformed payload received')
return
# Channel config.
if msg.topic.endswith('/channelConfig'):
# Construct meter objects
channels = data['inputChannels']
consumption_channels = [c for c in channels \
if c['inputChannelType'] == 'CONSUMPTION']
production_channels = [c for c in channels \
if c['inputChannelType'] == 'PRODUCTION']
# Use DeviceInstance values from 50 up.
self.meters = self._allocate_meters('Consumption',
consumption_channels, 1, "grid", 50)
self.meters.extend(
self._allocate_meters('Production',
production_channels, None, "pvinverter", 51))
return
if msg.topic.endswith('/realtime'):
# Index the voltage by phase, and the powers by CT. Pass it
# to each meter so the meter can pick its values from the
# dictionary.
voltages = dict((d['phaseId'], d['voltage']) for d in data['voltages'])
powers = dict((d['ctInput'], d) for d in data['channelPowers'])
for meter in self.meters:
meter.update(voltages, powers)
# Update the firmware and serial on each meter
for meter in self.meters:
meter.set_path('/FirmwareVersion', data.get('firmwareVersion'))
meter.set_path('/Serial', data.get('serialNr'))
def _on_connect(self, client, userdata, di, rc):
self._client.subscribe('servicelocation/+/realtime', 0)
self._client.subscribe('servicelocation/+/channelConfig', 0)
def main():
parser = ArgumentParser(description=sys.argv[0])
parser.add_argument('--servicebase',
help='Base service name on dbus, default is com.victronenergy',
default='com.victronenergy.grid')
parser.add_argument('host', help='MQTT Host')
args = parser.parse_args()
DBusGMainLoop(set_as_default=True)
# MQTT connection
bridge = Bridge(args.servicebase, args.host)
mainloop = gobject.MainLoop()
mainloop.run()
if __name__ == "__main__":
main()