Mercurial > ~darius > hgwebdir.cgi > epro
comparison velib_python/test/fixture_vedbus.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 import dbus | |
6 import dbus.service | |
7 import inspect | |
8 import platform | |
9 import pprint | |
10 import sys | |
11 import os | |
12 | |
13 # our own packages | |
14 sys.path.insert(1, os.path.join(os.path.dirname(__file__), '../')) | |
15 from gi.repository import GLib | |
16 from vedbus import VeDbusItemExport | |
17 | |
18 # Dictionary containing all objects exported to dbus | |
19 dbusObjects = {} | |
20 | |
21 def changerequest(path, newvalue): | |
22 if newvalue < 100: | |
23 return True | |
24 else: | |
25 return False | |
26 | |
27 def gettext(path, value): | |
28 return 'gettexted %s %s' % (path, value) | |
29 | |
30 def main(argv): | |
31 global dbusObjects | |
32 | |
33 # Have a mainloop, so we can send/receive asynchronous calls to and from dbus | |
34 DBusGMainLoop(set_as_default=True) | |
35 | |
36 # Connect to session bus whenever present, else use the system bus | |
37 dbusConn = dbus.SessionBus() if 'DBUS_SESSION_BUS_ADDRESS' in os.environ else dbus.SystemBus() | |
38 | |
39 # Register ourserves on the dbus as a service | |
40 name = dbus.service.BusName("com.victronenergy.dbusexample", dbusConn) | |
41 | |
42 # Create the management objects, as specified in the ccgx dbus-api document | |
43 | |
44 # Keep a reference in the global dictionary. Without this they would be removed by | |
45 # garbage collector again. | |
46 dbusObjects['string'] = VeDbusItemExport(dbusConn, '/String', 'this is a string') | |
47 dbusObjects['int'] = VeDbusItemExport(dbusConn, '/Int', 40000) | |
48 dbusObjects['negativeInt'] = VeDbusItemExport(dbusConn, '/NegativeInt', -10) | |
49 dbusObjects['float'] = VeDbusItemExport(dbusConn, '/Float', 1.5) | |
50 dbusObjects['invalid'] = VeDbusItemExport(dbusConn, '/Invalid', None) | |
51 dbusObjects['byte'] = VeDbusItemExport(dbusConn, '/Byte', dbus.Byte(84)) | |
52 dbusObjects['writeable'] = VeDbusItemExport(dbusConn, '/Writeable', 'original', writeable=True) | |
53 dbusObjects['not-writeable'] = VeDbusItemExport(dbusConn, '/NotWriteable', 'original', writeable=False) | |
54 | |
55 dbusObjects['not-writeable with cb'] = VeDbusItemExport(dbusConn, '/WriteableUpTo100', | |
56 'original', writeable=True, onchangecallback=changerequest) | |
57 | |
58 dbusObjects['gettextcallback'] = VeDbusItemExport(dbusConn, '/Gettextcallback', | |
59 '10', gettextcallback=gettext, writeable=True) | |
60 | |
61 mainloop = GLib.MainLoop() | |
62 print("up and running") | |
63 sys.stdout.flush() | |
64 | |
65 mainloop.run() | |
66 | |
67 main("") |