/
test.go
113 lines (94 loc) · 2.54 KB
/
test.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
105
106
107
108
109
110
111
112
113
package main
import (
"flag"
"fmt"
"sync"
"github.com/stamp/goADS"
"os"
"os/signal"
"syscall"
// "time"
log "github.com/cihub/seelog"
// "bytes"
// "encoding/hex"
// "encoding/binary"
)
var WaitGroup sync.WaitGroup
func main() {
defer log.Flush()
// Flags/*{{{*/
debug := flag.Bool("debug", false, "print debugging messages.")
ip := flag.String("ip", "172.16.21.10", "the address to the AMS router")
netid := flag.String("netid", "172.16.21.10.1.1", "AMS NetID of the target")
intport := flag.Uint("port", 801, "AMS Port of the target")
flag.Parse()
port := uint16(*intport)
fmt.Println(*debug, *ip, *netid, port) /*}}}*/
// Start the logger/*{{{*/
logger, err := log.LoggerFromConfigAsFile("logconfig.xml")
if err != nil {
panic(err)
}
log.ReplaceLogger(logger)
goADS.UseLogger(logger) /*}}}*/
// Startup the connection/*{{{*/
connection, e := goADS.NewConnection(*ip, *netid, port)
connection.Connect()
defer connection.Close() // Close the connection when we are done
if e != nil {
logger.Critical(e)
os.Exit(1)
} /*}}}*/
// Add a handler for Ctrl^C, soft shutdown/*{{{*/
go shutdownRoutine(connection) /*}}}*/
// Check what device are we connected to/*{{{*/
data, e := connection.ReadDeviceInfo()
if e != nil {
log.Critical(e)
os.Exit(1)
}
log.Infof("Successfully conncected to \"%s\" version %d.%d (build %d)", data.DeviceName, data.MajorVersion, data.MinorVersion, data.BuildVersion) /*}}}*/
symbols, _ := connection.UploadSymbolInfo()
//for _, segment := range symbols {
//segment.Walk()
//}
log.Warn("Count: ", len(symbols))
//pd,ok := symbols[".RECIPE"]
pd, ok := symbols[".PD"]
//pd,ok := symbols[".SYSTEMTASKINFOARR"]
if ok {
pd.AddDeviceNotification(func(symbol *goADS.ADSSymbol) {
val := connection.Value(".PD.TEST1BOOL")
if val == "True" {
connection.Set(".PD.TEST1WORD", "1")
} else {
connection.Set(".PD.TEST1WORD", "0")
}
symbol.Walk()
})
// pd.DebugWalk()
}
// Do some work/*{{{*/
/*for i := 0; i < 100; i++ {
WaitGroup.Add(1)
go func() {
_, e = connection.ReadDeviceInfo();
if e != nil {
log.Critical(e)
//connection.Close()
}
WaitGroup.Done()
}()
}*/ /*}}}*/
// Wait for all routines to finish/*{{{*/
WaitGroup.Wait()
connection.Wait()
log.Info("MAIN Done :)") /*}}}*/
}
func shutdownRoutine(conn *goADS.Connection) { /*{{{*/
sigchan := make(chan os.Signal, 2)
signal.Notify(sigchan, os.Interrupt)
signal.Notify(sigchan, syscall.SIGTERM)
<-sigchan
conn.Close()
} /*}}}*/