forked from victronenergy/dbus_generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_utils.py
83 lines (69 loc) · 1.83 KB
/
gen_utils.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
import sys
import os
import dbus
from vedbus import VeDbusService
from version import softwareversion
dummy = {'code': None, 'whenToLog': 'configChange', 'accessLevel': None}
class BaseEnum(object):
@classmethod
def lookup(klass, v):
return klass._lookup[v]
def enum(**kw):
c = type('Enum', (BaseEnum,), kw)
c._lookup = { x.lower(): y for x, y in kw.items() }
return c
class Errors(object):
NONE, REMOTEDISABLED, REMOTEINFAULT = range(3)
@staticmethod
def get_description(value):
description = [
'No error',
'Remote control disabled',
'Remote in fault condition']
d = ''
try:
d = description[value]
except IndexError:
pass
return d
class States(object):
STOPPED = 0
RUNNING = 1
ERROR = 10
@staticmethod
def get_description(value):
description = [
'Stopped',
'Running',
'Error']
d = ''
try:
d = description[value]
except IndexError:
pass
return d
class SettingsPrefix(object):
def __init__(self, settings, prefix):
self._settings = settings
self._prefix = prefix
def removeprefix(self, setting):
return setting.replace(self._prefix, "")
def __getitem__(self, setting):
return self._settings[setting + self._prefix]
def __setitem__(self, setting, value):
self._settings[setting + self._prefix] = value
def create_dbus_service(instance):
# Use a private bus, so we can have multiple services
bus = dbus.Bus.get_session(private=True) if 'DBUS_SESSION_BUS_ADDRESS' in os.environ else dbus.Bus.get_system(private=True)
dbusservice = VeDbusService("com.victronenergy.generator.startstop{}".format(instance), bus=bus)
dbusservice.add_mandatory_paths(
processname=sys.argv[0],
processversion=softwareversion,
connection='generator',
deviceinstance=instance,
productid=None,
productname=None,
firmwareversion=None,
hardwareversion=None,
connected=1)
return dbusservice