-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathDbusSettingsResources
executable file
·211 lines (174 loc) · 6.59 KB
/
DbusSettingsResources
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
#!/bin/bash
# DbusSettingsResources for SetupHelper
#
# contains a functions and variables necessary to access dbus Settings parameters
# it should be sourced by scripts setting, creating and removing dbus settings
#
# dbus Settings is not operational during system boot when some setup scripts may
# need to make settings changes
# These functions check to see if the settings system is operational and defer
# the set/create/remove activity so the calling script may continue
# dbus Settings funcitons
# These functions encapsulate an interface to dbus Settings
# NOTE: dbus Settings resources are not always active when it is necessary for
# scripts to make changes or create/remove settings
# it is up to the caller to insure dbus Settings resources are active before callling
# these functions
# a dbus exeption error will be logged if settings are not active yet
# updateDbusStringSetting
# updateDbusIntSetting
# updateDbusRealSetting
# updates a dbus setting parameter with a new value
#
# if the setting does not exist, it is created
# but max and min values are not set and the default is "", 0 or 0.0 depending on data type
# if these are needed use the dbus command directly
# this can also be faster if lots of settings must be created at the same time
#
# other data types may exist and would need their own function
#
# $1 is the path to the setting starting with /Settings
# $2 is the new value
#
# if the setting does not yet exist, it is created, then updated to the new value
updateDbusStringSetting ()
{
# don't do any work if install has already failed
if $installFailed; then
return
fi
dbus-send --system --print-reply=literal --dest=com.victronenergy.settings "$1"\
com.victronenergy.BusItem.GetValue &> /dev/null
if (( $? != 0 )); then
logMessage "creating dbus Setting $1"
dbus -y com.victronenergy.settings / AddSettings "%[ {\"path\":\"$1\", \"default\":\"\"} ]" &> /dev/null
fi
dbus -y com.victronenergy.settings "$1" SetValue -- "$2" &> /dev/null
}
updateDbusIntSetting ()
{
# don't do any work if install has already failed
if $installFailed; then
return
fi
dbus-send --system --print-reply=literal --dest=com.victronenergy.settings "$1"\
com.victronenergy.BusItem.GetValue &> /dev/null
if (( $? != 0 )); then
logMessage "creating dbus Setting $1"
dbus -y com.victronenergy.settings / AddSettings "%[ {\"path\":\"$1\", \"default\":0} ]" &> /dev/null
fi
dbus -y com.victronenergy.settings "$1" SetValue -- "$2" &> /dev/null
}
updateDbusRealSetting ()
{
# don't do any work if install has already failed
if $installFailed; then
return
fi
dbus-send --system --print-reply=literal --dest=com.victronenergy.settings "$1"\
com.victronenergy.BusItem.GetValue &> /dev/null
if (( $? != 0 )); then
logMessage "creating dbus Setting $1"
dbus -y com.victronenergy.settings / AddSettings "%[ {\"path\":\"$1\", \"default\":0.0} ]" &> /dev/null
fi
dbus -y com.victronenergy.settings "$1" SetValue -- "$2" &> /dev/null
}
# addAllDbusSettings adds settings from DbusSettingsList in the package directory
# the format of each line is:
# {"path":"/Settings/GuiMods/ShortenTankNames", "default":1, "min":0, "max":1}
# min and max are optional
addAllDbusSettings ()
{
local settings
if [ -f "$scriptDir/DbusSettingsList" ]; then
logMessage "updating dbus Settings"
while read -r line || [[ -n "$line" ]]; do
settings+="$line, "
done < "$scriptDir/DbusSettingsList"
dbus -y com.victronenergy.settings / AddSettings "%[ $settings ]" &> /dev/null
fi
}
# same as above but removes them
# typically settings are retained when removing a package so
# the developer must make this call specifically in the setup script's UNINSTALL section
# if they wish to remove the settings
removeAllDbusSettings ()
{
local settings
if [ -f "$scriptDir/DbusSettingsList" ]; then
logMessage "removing dbus Settings"
while read -r line || [[ -n "$line" ]]; do
settings+=$( echo $line | awk -F[:,] '{print $2, ","}' )
done < "$scriptDir/DbusSettingsList"
dbus -y com.victronenergy.settings / RemoveSettings "%[ $settings ]"
fi
}
# removeDbusSettings removes the setting from dbus Settings
#
# all parameters are each a quoted path to the setting to be removed
# e.g., removeDbusSettings "/Settings/foo" "/Settings/bar"
# (including all settings in one dbus call is much faster)
removeDbusSettings ()
{
logMessage "removing dbus Settings $@"
local settings=$(echo "$@" | sed -e s_^_\"_ -e s_\$_\"_ -e s_\ _'", "'_g)
dbus -y com.victronenergy.settings / RemoveSettings "%[ $settings ]" &> /dev/null
}
# setSetting updates the dbus setting parameter
# the setting must already exist or the update will fail
# (the setting can not be created without knowing the data type(s))
#
# $1 is the new value
# $2 is the setting path
setSetting ()
{
# don't do any work if install has already failed
if $installFailed; then
return
fi
dbus -y com.victronenergy.settings $2 SetValue $1 &> /dev/null
}
# move a setting from setup options or from previous dbus Setting
# $1 is the setup options path
# $2 is the old dbus path (has priority over setup option)
# $3 is the new dbus path
# dbus paths start with /Settings
# if specified, the setup option file must include a value
# that value has priority over the old dbus parameter
#
# setup options can either contain a value or be a flag file
# for flag files, the file will be empty and the state of the option
# depends on the presence of the file (true) or absense of the file (false)
#
# Note: this function does NOT create or remove any old option or Setting
# use other functions or commands to do so
moveSetting ()
{
# don't do any work if install has already failed
if $installFailed; then
return
fi
local setupOption="$1"
local oldDbusPath=$2
local newDbusPath=$3
if [ ! -z "$oldDbusPath" ]; then
oldSetting=$(dbus-send --system --print-reply=literal --dest=com.victronenergy.settings\
$oldDbusPath com.victronenergy.BusItem.GetValue 2> /dev/null | awk '{print $3}')
elif [ ! -z $setupOption ]; then
if [ -f "$setupOption" ]; then
oldSetting=$(cat "$setupOption")
# flag file - old setting is true (1)
if [ -z $oldSetting ]; then
oldSetting=1
fi
# file did not exist - assume a false value for a flag file
else
oldSetting=0
fi
else
oldSetting=""
fi
if [ ! -z $oldSetting ] && [ ! -z "$newDbusPath" ]; then
dbus -y com.victronenergy.settings $newDbusPath SetValue $oldSetting &> /dev/null
fi
}