go run main.go > time_date.csv
Show help for optional arguments:
go run main.go -h
package main | |
import ( | |
"context" | |
"flag" | |
"io" | |
"log" | |
"net" | |
"os" | |
"time" | |
) | |
var hostname string | |
var connTimeout float64 | |
var samplePeriod float64 | |
func main() { | |
flag.StringVar(&hostname, "hostname", "10.163.103.187:23", "The hostname of the SRC PTC01 controller") | |
flag.Float64Var(&connTimeout, "connection-timeout", 5, "The timeout of the connection in seconds") | |
flag.Float64Var(&samplePeriod, "sample-period", 1, "The sample period in seconds") | |
flag.Parse() | |
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(connTimeout)*time.Second) | |
defer cancel() | |
var d net.Dialer | |
conn, err := d.DialContext(ctx, "tcp", hostname) | |
if err != nil { | |
log.Fatalf("failed to connect to %s: %s", hostname, err) | |
} | |
defer conn.Close() | |
go func() { | |
_, err := io.Copy(os.Stdout, conn) | |
if err != nil { | |
log.Fatalf("failed to receive data: %s", err) | |
} | |
}() | |
if err := requestHeader(conn); err != nil { | |
log.Fatalf("failed to request headers: %s", err) | |
} | |
for range time.Tick(time.Second * time.Duration(samplePeriod)) { | |
requestRecord(conn) | |
} | |
} | |
func requestHeader(conn net.Conn) error { | |
_, err := conn.Write([]byte("getOutput.names\r\n")) | |
return err | |
} | |
func requestRecord(conn net.Conn) error { | |
_, err := conn.Write([]byte("getOutputs\r\n")) | |
return err | |
} |