comparison velib_python/examples/vedbusservice_example.py @ 8:9c0435a617db

Import velib_python
author Daniel O'Connor <darius@dons.net.au>
date Sun, 05 Dec 2021 14:35:36 +1030
parents
children
comparison
equal deleted inserted replaced
5:982eeffe9d95 8:9c0435a617db
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3
4 from dbus.mainloop.glib import DBusGMainLoop
5 from gi.repository import GLib
6 import dbus
7 import dbus.service
8 import inspect
9 import pprint
10 import os
11 import sys
12
13 # our own packages
14 sys.path.insert(1, os.path.join(os.path.dirname(__file__), '../'))
15 from vedbus import VeDbusService
16
17 softwareVersion = '1.0'
18
19 def validate_new_value(path, newvalue):
20 # Max RPM setpoint = 1000
21 return newvalue <= 1000
22
23 def get_text_for_rpm(path, value):
24 return('%d rotations per minute' % value)
25
26 def main(argv):
27 global dbusObjects
28
29 print(__file__ + " starting up")
30
31 # Have a mainloop, so we can send/receive asynchronous calls to and from dbus
32 DBusGMainLoop(set_as_default=True)
33
34 # Put ourselves on to the dbus
35 dbusservice = VeDbusService('com.victronenergy.example')
36
37 # Most simple and short way to add an object with an initial value of 5.
38 dbusservice.add_path('/Position', value=5)
39
40 # Most advanced wayt to add a path
41 dbusservice.add_path('/RPM', value=100, description='RPM setpoint', writeable=True,
42 onchangecallback=validate_new_value, gettextcallback=get_text_for_rpm)
43
44 # You can access the paths as if the dbusservice is a dictionary
45 print('/Position value is %s' % dbusservice['/Position'])
46
47 # Same for changing it
48 dbusservice['/Position'] = 10
49
50 print('/Position value is now %s' % dbusservice['/Position'])
51
52 # To invalidate a value (see com.victronenergy.BusItem specs for definition of invalid), set to None
53 dbusservice['/Position'] = None
54
55 dbusservice.add_path('/String', 'this is a string')
56 dbusservice.add_path('/Int', 0)
57 dbusservice.add_path('/NegativeInt', -10)
58 dbusservice.add_path('/Float', 1.5)
59
60 print('try changing our RPM by executing the following command from a terminal\n')
61 print('dbus-send --print-reply --dest=com.victronenergy.example /RPM com.victronenergy.BusItem.SetValue int32:1200')
62 print('Reply will be <> 0 for values > 1000: not accepted. And reply will be 0 for values < 1000: accepted.')
63 mainloop = GLib.MainLoop()
64 mainloop.run()
65
66 main("")