comparison velib_python/examples/vedbusitem_import_examples.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 # This file has some tests, to do type checking of vedbus.py
5 # This file makes it easy to compare the values put on the dbus through
6 # Python (vedbus.VeDbusItemExport) with items exported in C (the mk2dbus process)
7
8 # Note that this file requires vedbusitemexport_examples to be running.
9
10 import dbus
11 import pprint
12 import os
13 import sys
14 from dbus.mainloop.glib import DBusGMainLoop
15
16 # our own packages
17 sys.path.insert(1, os.path.join(os.path.dirname(__file__), '../'))
18 from vedbus import VeDbusItemExport, VeDbusItemImport
19
20 DBusGMainLoop(set_as_default=True)
21
22 # Connect to the sessionbus. Note that on ccgx we use systembus instead.
23 dbusConn = dbus.SessionBus() if 'DBUS_SESSION_BUS_ADDRESS' in os.environ else dbus.SystemBus()
24
25
26 # dictionary containing the different items
27 dbusObjects = {}
28
29 # check if the vbus.ttyO1 exists (it normally does on a ccgx, and for linux a pc, there is
30 # some emulator.
31 hasVEBus = 'com.victronenergy.vebus.ttyO1' in dbusConn.list_names()
32
33 dbusObjects['PyString'] = VeDbusItemImport(dbusConn, 'com.victronenergy.example', '/String')
34 if hasVEBus: dbusObjects['C_string'] = VeDbusItemImport(dbusConn, 'com.victronenergy.vebus.ttyO1', '/Mgmt/ProcessName')
35
36 dbusObjects['PyFloat'] = VeDbusItemImport(dbusConn, 'com.victronenergy.example', '/Float')
37 if hasVEBus: dbusObjects['C_float'] = VeDbusItemImport(dbusConn, 'com.victronenergy.vebus.ttyO1', '/Dc/V')
38
39 dbusObjects['PyInt'] = VeDbusItemImport(dbusConn, 'com.victronenergy.example', '/Int')
40 if hasVEBus: dbusObjects['C_int'] = VeDbusItemImport(dbusConn, 'com.victronenergy.vebus.ttyO1', '/State')
41
42 dbusObjects['PyNegativeInt'] = VeDbusItemImport(dbusConn, 'com.victronenergy.example', '/NegativeInt')
43 if hasVEBus: dbusObjects['C_negativeInt'] = VeDbusItemImport(dbusConn, 'com.victronenergy.vebus.ttyO1', '/Dc/I')
44
45 # print the results
46 print('----')
47 for key, o in dbusObjects.items():
48 print(key + ' at ' + o.serviceName + o.path)
49 pprint.pprint(dbusObjects[key])
50 print('pprint veBusItem.get_value(): ')
51 pprint.pprint(dbusObjects[key].get_value())
52 print('pprint veBusItem.get_text(): ')
53 pprint.pprint(dbusObjects[key].get_text())
54 print('----')