-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdbus-lambda.py
More file actions
executable file
·269 lines (229 loc) · 10.6 KB
/
dbus-lambda.py
File metadata and controls
executable file
·269 lines (229 loc) · 10.6 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Please note that any incorrect or careless usage of this module as
# well as errors in the implementation can damage your hardware!
# Therefore, the author does not provide any guarantee or warranty
# concerning to correctness, functionality or performance and does not
# accept any liability for damage caused by this module, examples or
# mentioned information.
#
# Thus, use it at your own risk!
# import normal packages
import logging
import platform
import logging
import sys
import os
import time
import configparser # for config/ini file
from enum import Enum
from math import log10
if sys.version_info.major == 2:
import gobject
else:
from gi.repository import GLib as gobject
sys.path.insert(1, os.path.join(os.path.dirname(__file__), '/opt/victronenergy/dbus-systemcalc-py/ext/velib_python'))
from vedbus import VeDbusService
from pymodbus.client.sync import ModbusTcpClient
from pymodbus.constants import Endian
from pymodbus.payload import BinaryPayloadDecoder
from pymodbus.exceptions import ModbusException
class DbusLAMBDAService:
def __init__(self, servicename, paths, productname='lambda', connection='LAMBDA Modbus Service'):
config = self._getConfig()
deviceinstance = int(config['DEFAULT']['Deviceinstance'])
self.host = str(config['DEFAULT']['Host'])
self.port = int(config['DEFAULT']['Port'])
self.acposition = int(config['DEFAULT']['Position'])
self.model = str(config['DEFAULT']['Model'])
self.timeout = int(config['DEFAULT']['Timeout'])
self._dbusservice = VeDbusService("{}.http_{:02d}".format(servicename, deviceinstance))
self._paths = paths
logging.debug("%s /DeviceInstance = %d" % (servicename, deviceinstance))
# Create the management objects, as specified in the ccgx dbus-api document
self._dbusservice.add_path('/Mgmt/ProcessName', __file__)
self._dbusservice.add_path('/Mgmt/ProcessVersion', 'Unknown version, running on Python ' + platform.python_version())
self._dbusservice.add_path('/Mgmt/Connection', connection)
# Create the mandatory objects
self._dbusservice.add_path('/DeviceInstance', deviceinstance)
self._dbusservice.add_path('/ProductId', 0xFFFF)
self._dbusservice.add_path('/ProductName', "LAMBDA " + self.model)
self._dbusservice.add_path('/CustomName', "LAMBDA " + self.model)
self._dbusservice.add_path('/FirmwareVersion', "0")
self._dbusservice.add_path('/Serial', "0")
self._dbusservice.add_path('/HardwareVersion', self.model)
self._dbusservice.add_path('/Connected', 1)
self._dbusservice.add_path('/UpdateIndex', 0)
self._dbusservice.add_path('/Position', self.acposition) # 0: ac out, 1: ac in
# add path values to dbus
for path, settings in self._paths.items():
self._dbusservice.add_path(path, settings['initial'], gettextcallback=settings['textformat'], writeable=True, onchangecallback=self._handlechangedvalue)
# last update
self._lastUpdate = 0
# add _update function 'timer'
gobject.timeout_add(self.timeout, self._update) # pause before the next request
# add _signOfLife 'timer' to get feedback in log every x minutes
gobject.timeout_add(self._getSignOfLifeInterval()*60*1000, self._signOfLife)
# open Modbus connection to heatpump
self._client = ModbusTcpClient(self.host, port=self.port)
self._client.connect()
logging.info("Modbus connected")
def __del__(self):
# close Modbus connection
self._client.close()
def _getConfig(self):
config = configparser.ConfigParser()
config.read("%s/config.ini" % (os.path.dirname(os.path.realpath(__file__))))
return config
def _getSignOfLifeInterval(self):
config = self._getConfig()
value = config['DEFAULT']['SignOfLifeLog']
if not value:
value = 0
return int(value)
def _handlechangedvalue(self, path, value):
logging.critical("Someone else updated %s to %s" % (path, value))
# TODO: handle changes
def getLAMBDAData(self, register):
if register == "state": # UINT16, works
addr = 1003
format = "H"
count = 1
factor = 1
comment = "Operating State"
unit = ""
elif register == "temp": # INT16, works
addr = 1004
format = "h"
count = 1
factor = 0.01
comment = "Flow Temperature"
unit = "°C"
elif register == "ttemp": # INT16, doesn't work
addr = 1016
format = "h"
count = 1
factor = 0.1
comment = "Request Flow Temperature"
unit = "°C"
elif register == "power": # INT16, works
addr = 103
format = "h"
count = 1
factor = 1
comment = "Power Consumption"
unit = "W"
elif register == "energy": # INT32, works
addr = 1020
format = "i"
count = 2
factor = 0.001
comment = "Total Energy"
unit = "kWh"
try:
rr = self._client.read_holding_registers(address=addr, count=count, slave=1)
except ModbusException as exc:
logging.error(f"Modbus exception: {exc!s}")
payload = BinaryPayloadDecoder.fromRegisters(rr.registers, byteorder=Endian.Big, wordorder=Endian.Big)
if register == "state": # UINT16, works
value = payload.decode_16bit_uint()
elif register == "temp": # INT16, works
value = payload.decode_16bit_int()
elif register == "ttemp": # INT16, doesn't work
value = payload.decode_16bit_int()
elif register == "power": # INT16, works
value = payload.decode_16bit_int()
elif register == "energy": # INT32, works
value = payload.decode_32bit_int()
value = value * factor
if factor < 1:
value = round(value, int(log10(factor) * -1))
logging.debug(f"{comment} = {value} {unit}")
return value
def _signOfLife(self):
logging.info("--- Start: sign of life ---")
logging.info("Last _update() call: %s" % (self._lastUpdate))
logging.info("Last '/Ac/Power': %s" % (self._dbusservice['/Ac/Power']))
logging.info("--- End: sign of life ---")
return True
def _update(self):
try:
self._dbusservice['/State'] = self.getLAMBDAData("state")
self._dbusservice['/Temperature'] = self.getLAMBDAData("temp")
self._dbusservice['/TargetTemperature'] = self.getLAMBDAData("ttemp")
self._dbusservice['/Ac/Power'] = self.getLAMBDAData("power")
self._dbusservice['/Ac/Energy/Forward'] = self.getLAMBDAData("energy")
# logging
logging.debug("Operating State (/State): %s" % (self._dbusservice['/State']))
logging.debug("Flow Temperature (/Temperature): %s" % (self._dbusservice['/Temperature']))
logging.debug("Request Flow Temperature (/TargetTemperature): %s" % (self._dbusservice['/TargetTemperature']))
logging.debug("Power Consumption (/Ac/Power): %s" % (self._dbusservice['/Ac/Power']))
logging.debug("Total Energy (/Ac/Energy/Forward): %s" % (self._dbusservice['/Ac/Energy/Forward']))
logging.debug("---")
# increment UpdateIndex - to show that new data is available
index = self._dbusservice['/UpdateIndex'] + 1 # increment index
if index > 255: # maximum value of the index
index = 0 # overflow from 255 to 0
self._dbusservice['/UpdateIndex'] = index
# update lastupdate vars
self._lastUpdate = time.time()
except Exception as e:
logging.critical('Error at %s', '_update', exc_info=e)
logging.critical(e)
# return true, otherwise add_timeout will be removed from GObject - see docs http://library.isr.ist.utl.pt/docs/pygtk2reference/gobject-functions.html#function-gobject--timeout-add
return True
def main():
# configure logging
logging.basicConfig( format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
level=logging.INFO,
handlers=[
logging.FileHandler("%s/current.log" % (os.path.dirname(os.path.realpath(__file__)))),
logging.StreamHandler()
]
)
try:
logging.info("Start")
from dbus.mainloop.glib import DBusGMainLoop
# Have a mainloop, so we can send/receive asynchronous calls to and from dbus
DBusGMainLoop(set_as_default=True)
# formatting
_kWh = lambda p, v: (str(round(v, 2)) + 'kWh')
_a = lambda p, v: (str(round(v, 1)) + 'A')
_w = lambda p, v: (str(round(v, 1)) + 'W')
_v = lambda p, v: (str(round(v, 1)) + 'V')
_degC = lambda p, v: (str(v) + '°C')
_s = lambda p, v: (str(v) + 's')
_n = lambda p, v: (str(v))
# start our main-service
hp_output = DbusLAMBDAService(
servicename='com.victronenergy.heatpump',
paths={
'/State': {'initial': 0, 'textformat': _n},
'/Temperature': {'initial': 0, 'textformat': _degC},
'/TargetTemperature': {'initial': 0, 'textformat': _degC},
'/Ac/Power': {'initial': 0, 'textformat': _w},
'/Ac/Energy/Forward': {'initial': 0, 'textformat': _kWh},
}
)
logging.info('Connected to dbus and switching over to gobject.MainLoop() (= event based)')
mainloop = gobject.MainLoop()
mainloop.run()
except Exception as e:
logging.critical('Error at %s', 'main', exc_info=e)
if __name__ == "__main__":
main()