-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathdbus_paths.go
More file actions
89 lines (77 loc) · 2.63 KB
/
dbus_paths.go
File metadata and controls
89 lines (77 loc) · 2.63 KB
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
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"strings"
"github.com/godbus/dbus/v5"
log "github.com/sirupsen/logrus"
)
// objectpath is a string type whose methods are exported as D-Bus methods.
type objectpath string
// GetValue returns the current value for this path.
func (f objectpath) GetValue() (dbus.Variant, *dbus.Error) {
log.Debug("GetValue() called for ", f)
app := GetApp()
if app == nil {
return dbus.Variant{}, dbus.NewError("com.victronenergy.BusItem.Error", []interface{}{"Application not initialized"})
}
app.mu.RLock()
defer app.mu.RUnlock()
log.Debug("...returning ", app.values[0][f])
return app.values[0][f], nil
}
// GetText returns the text representation for this path.
func (f objectpath) GetText() (string, *dbus.Error) {
log.Debug("GetText() called for ", f)
app := GetApp()
if app == nil {
return "", dbus.NewError("com.victronenergy.BusItem.Error", []interface{}{"Application not initialized"})
}
app.mu.RLock()
defer app.mu.RUnlock()
log.Debug("...returning ", app.values[1][f])
return strings.Trim(app.values[1][f].String(), "\""), nil
}
// SetValue sets the value for this path.
func (f objectpath) SetValue(value dbus.Variant) (int32, *dbus.Error) {
log.Debug("SetValue() called for ", f, " with value ", value)
app := GetApp()
if app == nil {
return 0, dbus.NewError("com.victronenergy.BusItem.Error", []interface{}{"Application not initialized"})
}
app.mu.Lock()
defer app.mu.Unlock()
app.values[0][f] = value
return 0, nil
}
// GetItems returns all path values (called on the App at root "/").
func (a *App) GetItems() (map[string]map[string]dbus.Variant, *dbus.Error) {
log.Debug("GetItems() called on root")
a.mu.RLock()
defer a.mu.RUnlock()
items := make(map[string]map[string]dbus.Variant)
for path, valueVariant := range a.values[0] {
pathStr := string(path)
textVariant, ok := a.values[1][path]
if !ok {
textVariant = dbus.MakeVariant("")
}
items[pathStr] = map[string]dbus.Variant{
"Value": valueVariant,
"Text": textVariant,
}
}
return items, nil
}