-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
104 lines (96 loc) · 1.89 KB
/
main.go
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
package main
import (
"fmt"
"github.com/huskar-t/opcda"
"github.com/huskar-t/opcda/com"
)
func main() {
com.Initialize()
defer com.Uninitialize()
host := "localhost"
progID := "Matrikon.OPC.Simulation.1"
server, err := opcda.Connect(progID, host)
if err != nil {
panic(err)
}
browser, err := server.CreateBrowser()
if err != nil {
panic(err)
}
browser.MoveToRoot()
root := &Tree{"root", nil, []*Tree{}, []Leaf{}}
buildTree(browser, root)
PrettyPrint(root)
}
type Tree struct {
Name string
Parent *Tree
Branches []*Tree
Leaves []Leaf
}
type Leaf struct {
Name string
Tag string
}
func buildTree(browser *opcda.OPCBrowser, branch *Tree) {
err := browser.ShowLeafs(false)
if err != nil {
panic(err)
}
count := browser.GetCount()
for i := 0; i < count; i++ {
item, err := browser.Item(i)
if err != nil {
panic(err)
}
itemID, err := browser.GetItemID(item)
if err != nil {
panic(err)
}
l := Leaf{Name: item, Tag: itemID}
branch.Leaves = append(branch.Leaves, l)
}
err = browser.ShowBranches()
if err != nil {
panic(err)
}
count = browser.GetCount()
for i := 0; i < count; i++ {
nextName, err := browser.Item(i)
if err != nil {
panic(err)
}
err = browser.MoveDown(nextName)
if err != nil {
panic(err)
}
nextBranch := &Tree{nextName, branch, []*Tree{}, []Leaf{}}
branch.Branches = append(branch.Branches, nextBranch)
buildTree(browser, nextBranch)
err = browser.MoveUp()
if err != nil {
panic(err)
}
err = browser.ShowBranches()
if err != nil {
panic(err)
}
}
}
func PrettyPrint(tree *Tree) {
fmt.Println(tree.Name)
printSubtree(tree, 1)
}
func printSubtree(tree *Tree, level int) {
space := ""
for i := 0; i < level; i++ {
space += " "
}
for _, l := range tree.Leaves {
fmt.Println(space, "-", l.Tag)
}
for _, b := range tree.Branches {
fmt.Println(space, "+", b.Name)
printSubtree(b, level+1)
}
}